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.
| Combination | Description | Points |
|---|---|---|
| 3-of-a-Kind | three dice showing the same face | sum of those three dice |
| 4-of-a-Kind | four dice showing the same face | sum of those four dice |
| Full House | a 3-of-a-Kind and a pair | 25 |
| Small Straight | four consecutive values on any four dice | 30 |
| Large Straight | five consecutive values | 40 |
| Yahtzee | five dice showing the same face | 50 for the first one, 100 for each one after that |
Domiyahtzee is played on a 5×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 k lines are Yahtzees with k≥1, one of them scores 50 and each of the other k−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.
The first line has the number of grids n (1≤n≤100).
Each grid is given by 13 specifications. A specification is H a b, V a b, or S a, with 1≤a,b≤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.
For each grid print one line Case i: s, where i is the grid number starting at 1 and s is the highest score reachable with at most one replacement.
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.