#include <bits/stdc++.h>
using namespace std;
#define L 2000;
int main(){
int n,k=1,tag;
tag=0;
while(scanf("%d",&n)!=EOF){
getchar();
if(n==0){
break;
}
tag=1;
for(int i=0;i<n;i++){
char str[2001]={0};
int x[2]={0},y[2]={0},z[2]={0};
gets(str);
if(tag){
cout<<"Case #"<<k++<<":"<<endl;
tag=0;
}
for(int v=2;v<strlen(str);v+=6){
if(str[v]=='X'){
x[0]+=str[v-2]-'0';
x[1]=str[v+2]-'0';
}else if(str[v]=='Y'){
y[0]+=str[v-2]-'0';
y[1]=str[v+2]-'0';
}else if(str[v]=='Z'){
z[0]+=str[v-2]-'0';
z[1]=str[v+2]-'0';
}
}
//求导
if(x[1]>0){
x[0]*=x[1];
x[1]--;
}
if(y[1]>0){
y[0]*=y[1];
y[1]--;
}
if(z[1]>0){
z[0]*=z[1];
z[1]--;
}
if(x[0]>0){
if(x[1]>0){
cout<<x[0]<<" * X ^ "<<x[1];
}else if(x[1]==0){
cout<<x[0];
}
if(y[0]>0){
cout<<" + ";
}else {
if(z[0]>0){
cout<<" + ";
}
}
}
if(y[0]>0){
if(y[1]>0){
cout<<y[0]<<" * Y ^ "<<y[1];
}else if(y[1]==0){
cout<<y[0];
}
if(z[0]>0){
cout<<" + ";
}
}
if(z[0]>0){
if(z[1]>0){
cout<<z[0]<<" * Z ^ "<<z[1];
}else if(z[1]==0){
cout<<z[0];
}
}
cout<<endl;
}
}
return 0;
}