Woburn ECOO 1997

The Tower of Babylon

INPUT FILE: babylon.in
OUTPUT FILE: babylon.out

Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of the tale have been forgotten. So now, in line with the educational nature of this contest, we will tell you the whole story:

The Babylonians had n types of blocks, and an unlimited supply of blocks of each of these types. Each block was a rectangular prism with a speicifed width, height and depth. A block could be reoriented so that any two of its three dimensions determined the base and the other its height. They wanted to construct the tallest tower possible by stacking blocks. The ploblem was that. in building a tower, one block could only be placed on top of another block as long as the two base dimensions of the upper block were strictly smaller than the corresponding base dimensions of the lower block. This meant, for example, that blocks oriented to have equal-sized bases couldn't be stacked.

Your job is to write a program that determines the height opf the tallesttower the Babyloniasn can build with a given set of blocks.

INPUT
The input file will contain one or more test cases.
The first line of each test case contains an integer n, representing the number of different blocks in the following data set. The maximum value of n is 30. Each of the next n lines contains the dimensions of a particular block the Babylonians can use.
Input is terminated by a value of zero for n.

OUTPUT
For each test case, print one line containing the case number and the height of the tallest possible tower in the format shown below.

Sample Input File

1
10 20 30
2
6 8 10
5 5 5
7
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27
0

Output for Sample Input

Case 1: Maximum height = 40
Case 2: Maximum height = 21
Case 3: Maximum height = 28
Case 4: Maximum height = 342
Downloader failed! Response object 006~ASP 0159~Buffering Off~Buffering must be on.