import java.util.Scanner ;
public class Main {
private static class Matrix {
private int width;
private int height;
public Matrix( int width, int height) {
this .width = width;
this .height = height;
}
}
private static long multiple( long [ ] [ ] dp, Matrix[ ] sizes, int [ ] [ ] bestCut, int l, int r ) {
if ( dp
[ l
] [ r
] == Long .
MAX_VALUE ) { for ( int i = l; i < r; i++ ) {
long cur = multiple( dp, sizes, bestCut, l, i) + multiple( dp, sizes, bestCut, i + 1 , r) + sizes[ l] .width * sizes[ i] .height * sizes[ r] .height ;
if ( cur < dp[ l] [ r] ) {
dp[ l] [ r] = cur;
bestCut[ l] [ r] = i;
}
}
}
return dp[ l] [ r] ;
}
private static void print( int l, int r, int [ ] [ ] bestCut) {
if ( l == r) {
System .
out .
print ( "A" + ( l
+ 1 ) ) ; }
else {
print( l, bestCut[ l] [ r] , bestCut) ;
print( bestCut[ l] [ r] + 1 , r, bestCut) ;
}
}
public static void main
( String [ ] args
) { Scanner sc
= new Scanner
( System .
in ) ; for ( int c = 1 , n = sc.nextInt ( ) ; n != 0 ; n = sc.nextInt ( ) , c++ ) {
long [ ] [ ] dp = new long [ n] [ n] ;
for ( int i = 0 ; i < n; i++ ) {
for ( int j = 0 ; j < n; j++ ) {
dp
[ i
] [ j
] = Long .
MAX_VALUE ; }
}
Matrix[ ] sizes = new Matrix[ n] ;
int [ ] [ ] bestCut = new int [ n] [ n] ;
for ( int i = 0 ; i < n; i++ ) {
for ( int j = 0 ; j < n; j++ ) {
}
}
for ( int i = 0 ; i < n; i++ ) {
int width = sc.nextInt ( ) ;
int height = sc.nextInt ( ) ;
sizes[ i] = new Matrix( width, height) ;
}
for ( int i = 0 ; i < n; i++ ) {
dp[ i] [ i] = 0 ;
}
multiple( dp, sizes, bestCut, 0 , n- 1 ) ;
System .
out .
print ( "Case " + c
+ ": " ) ; print( 0 , n - 1 , bestCut) ;
}
}
}
aW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwoKcHVibGljIGNsYXNzIE1haW4gewogICAgcHJpdmF0ZSBzdGF0aWMgY2xhc3MgTWF0cml4IHsKICAgICAgICBwcml2YXRlIGludCB3aWR0aDsKICAgICAgICBwcml2YXRlIGludCBoZWlnaHQ7CgogICAgICAgIHB1YmxpYyBNYXRyaXgoaW50IHdpZHRoLCBpbnQgaGVpZ2h0KSB7CiAgICAgICAgICAgIHRoaXMud2lkdGggPSB3aWR0aDsKICAgICAgICAgICAgdGhpcy5oZWlnaHQgPSBoZWlnaHQ7CiAgICAgICAgfQogICAgfQoKICAgIHByaXZhdGUgc3RhdGljIGxvbmcgbXVsdGlwbGUobG9uZ1tdW10gZHAsIE1hdHJpeFtdIHNpemVzLCBpbnRbXVtdIGJlc3RDdXQsIGludCBsLCBpbnQgciApIHsKICAgICAgICBpZiAoZHBbbF1bcl0gPT0gTG9uZy5NQVhfVkFMVUUpIHsKICAgICAgICAgICAgZm9yIChpbnQgaSA9IGw7IGkgPCByOyBpKyspIHsKICAgICAgICAgICAgbG9uZyBjdXIgPSBtdWx0aXBsZShkcCwgc2l6ZXMsIGJlc3RDdXQsIGwsIGkpICsgbXVsdGlwbGUoZHAsIHNpemVzLCBiZXN0Q3V0LCBpICsgMSwgcikgKyBzaXplc1tsXS53aWR0aCAqIHNpemVzW2ldLmhlaWdodCAqIHNpemVzW3JdLmhlaWdodDsKICAgICAgICAgICAgICAgIGlmIChjdXIgPCBkcFtsXVtyXSkgewogICAgICAgICAgICAgICAgICAgIGRwW2xdW3JdID0gY3VyOwogICAgICAgICAgICAgICAgICAgIGJlc3RDdXRbbF1bcl0gPSBpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBkcFtsXVtyXTsKICAgIH0KCiAgICBwcml2YXRlIHN0YXRpYyB2b2lkIHByaW50KGludCBsLCBpbnQgciwgaW50W11bXSBiZXN0Q3V0KSB7CiAgICAgICAgaWYgKGwgPT0gcikgewogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50KCJBIiArIChsICsgMSkpOwogICAgICAgIH0KICAgICAgICBlbHNlIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludCgiKCIpOwogICAgICAgICAgICBwcmludChsLCBiZXN0Q3V0W2xdW3JdLCBiZXN0Q3V0KTsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludCgiIHggIik7CiAgICAgICAgICAgIHByaW50KGJlc3RDdXRbbF1bcl0gKyAxLCByLCBiZXN0Q3V0KTsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludCgiKSIpOwogICAgICAgIH0KICAgIH0KICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBTY2FubmVyIHNjID0gbmV3IFNjYW5uZXIoU3lzdGVtLmluKTsKICAgICAgICBmb3IgKGludCBjID0gMSwgbiA9IHNjLm5leHRJbnQoKTsgbiAhPSAwOyBuID0gc2MubmV4dEludCgpLCBjKyspIHsKICAgICAgICAgICAgbG9uZ1tdW10gZHAgPSBuZXcgbG9uZ1tuXVtuXTsKICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgbjsgaisrKSB7CiAgICAgICAgICAgICAgICAgICAgZHBbaV1bal0gPSBMb25nLk1BWF9WQUxVRTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBNYXRyaXhbXSBzaXplcyA9IG5ldyBNYXRyaXhbbl07CiAgICAgICAgICAgIGludFtdW10gYmVzdEN1dCA9IG5ldyBpbnRbbl1bbl07CiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG47IGorKykgewogICAgICAgICAgICAgICAgICAgIGJlc3RDdXRbaV1bal0gPSBJbnRlZ2VyLk1BWF9WQUxVRTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICAgICAgaW50IHdpZHRoID0gc2MubmV4dEludCgpOwogICAgICAgICAgICAgICAgaW50IGhlaWdodCA9IHNjLm5leHRJbnQoKTsKICAgICAgICAgICAgICAgIHNpemVzW2ldID0gbmV3IE1hdHJpeCh3aWR0aCwgaGVpZ2h0KTsKICAgICAgICAgICAgfQogICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICAgICAgZHBbaV1baV0gPSAwOwogICAgICAgICAgICB9CiAgICAgICAgICAgIG11bHRpcGxlKGRwLCBzaXplcywgYmVzdEN1dCwgMCwgbi0xKTsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludCgiQ2FzZSAiICsgYyArICI6ICIpOwogICAgICAgICAgICBwcmludCgwLCBuIC0gMSwgYmVzdEN1dCk7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigpOwogICAgICAgIH0KICAgIH0KfQ==