#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int n;
vector <int> v;
void f(long long **a, long long i, long long j ){
if(a[j][i]>0){
v.push_back(a[j][i]);
if((a[j][i]<=n)&&(i<=n)&&(j<=n)){
f(a,i,a[j][i]);
f(a,a[j][i],j);
}
}
}
string answer(long long x, long long si, long long sj, long long el){
long long z1=el,z2=el;
if(sj!=si){
if(si!=x){
while((v[v.size()-z1]<si)||(v[v.size()-z1]>=x))z1++;
}
if(sj!=(x+1)){
while((v[v.size()-z2]<(x+1))||(v[v.size()-z2]>=sj))z2++;
}
}
return si==sj? "A"+to_string(si):"("+answer(v[v.size()-z1],si,x,z1+1)+" x "+answer(v[v.size()-z2],x+1,sj,z2+1)+")";
}
int main() {
long long i,j,useless,number=1,**dp;
cin>>n;
while(n){
n++;
dp = new long long* [n];
for(i=0;i<n;i++)dp[i]=new long long [n];
for(i=0;i<n;i++){
for(j=0;j<n;j++){
dp[i][j]=0;
}
}
cin>>dp[0][0];
for(i=1;i<n;i++){
cin>>dp[i][i];
if(i<(n-1))cin>>useless;
}
i=2;
for(i=2;i<n;i++){
for(j=0;j<=(i-2);j++){
dp[i][j]=2e10;
}
}
long long t;
for(i=2;i<n;i++){
for(j=0;j<(n-i);j++){
long long k=j+i;
for(t=1;(j+t)<(n-1);t++){
if(dp[k][j]>(dp[k][j+t]+dp[j+t][j]+dp[k][k]*dp[j][j]*dp[j+t][j+t])){
dp[k][j]=dp[k][j+t]+dp[j+t][j]+dp[k][k]*dp[j][j]*dp[j+t][j+t];
dp[j][k]=j+t;
}
}
}
}
f(dp,n-1,0);
reverse(v.begin(),v.end());
cout<<"Case "<<number<<": "<<answer(v[v.size()-1],1,n-1,1)<<'\n';
number++;
delete [] dp;
v.clear();
cin>>n;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8YWxnb3JpdGhtPgp1c2luZy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