Domiyahtzee!

No attempts yetTime limit3sMemory limit256 MB

Problem

Yahtzee is a dice game where five dice score points for various combinations. Domiyahtzee uses the payoffs in the table below. The first two rows differ slightly from the official Yahtzee rules.

CombinationDescriptionPoints
3-of-a-Kindthree dice showing the same facesum of those three dice
4-of-a-Kindfour dice showing the same facesum of those four dice
Full Housea 3-of-a-Kind and a pair25
Small Straightfour consecutive values on any four dice30
Large Straightfive consecutive values40
Yahtzeefive dice showing the same face50 for the first one, 100 for each one after that

Domiyahtzee is played on a 5×55 \times 5 grid. A standard set of 21 dominoes pairs up the numbers 1 through 6 and holds (1,1), (1,2), ..., (1,6), (2,2), (2,3), ..., (6,6), one of each. The grid holds 12 of those 21 dominoes, and one remaining square holds a single value between 1 and 6, called the singleton. All 25 squares carry a number.

The grid has 12 lines: 5 rows, 5 columns, and the 2 long diagonals. One line is five dice. A line scores the single highest paying combination it contains, and 0 if it contains none. A line showing 6 5 5 6 6 holds a 3-of-a-Kind worth 18 and a Full House worth 25, so that line scores 25.

Yahtzee lines are counted together across the whole grid. If kk lines are Yahtzees with k1k \ge 1, one of them scores 50 and each of the other k1k - 1 scores 100. The score of a grid is the sum of the scores of the 12 lines.

You may raise the score with at most one replacement. Pick one domino on the grid, take it off, and put one of the 9 dominoes that are not on the grid into the two empty squares. The new domino uses the two squares the old one used, and you choose which of its halves goes in which square. The singleton never moves. You may also leave the grid alone.

Print the highest score you can reach this way.

Input

The first line has the number of grids nn (1n1001 \le n \le 100).

Each grid is given by 13 specifications. A specification is H a b, V a b, or S a, with 1a,b61 \le a, b \le 6. Of the 13 specifications of a grid, 12 are dominoes and exactly 1 is the singleton, and the 12 dominoes are all different. The specifications of one grid may be split across lines, so read the input as a stream of tokens.

Process the specifications in the order given. Scan the grid from top to bottom, and left to right inside a row, and place the specification on the first empty square you meet. H a b puts a on that square and b on the square to its right. V a b puts a on that square and b on the square below it. S a puts a on that square. The input always fills the grid completely.

Output

For each grid print one line Case i: s, where ii is the grid number starting at 1 and ss is the highest score reachable with at most one replacement.

Hint

A puzzle on the side. Which Domiyahtzee grid scores the most? The grid the setter found is worth 498 points. This question has no effect on judging.