Welcome to ZOJ
 Contests Information Problems Runs Statistics Ranklist Clarification
98 - The 2010 ACM-ICPC Asia Chengdu Regional Contest - K
Snooker Referee

Time Limit: 3 Seconds      Memory Limit: 65536 KB

Snooker is a cue sport that is played on a large baize-covered table with pockets in each of the four corners and in the middle of each of the long side cushions. It is played using a cue and snooker balls: one white cue ball, 15 red balls worth one point each, and six balls of different colors: yellow (2 points), green (3), brown (4), blue (5), pink (6) and black (7). A player (or team) wins a frame (individual game) of snooker by scoring more points than the opponent(s), using the cue ball to pot the red and colored balls.

In this problem, your job is the referee of snooker. You should score both players, ask the correct player to play next, as well as place some of the balls back to the table if necessary. The rules of snooker needed for this problem are following. (We ignore some fouls about incorrectly hitting the cue ball here. We assume that the cue ball is never snookered after a foul, so free ball will never occur. We also assume that both players will make their best attempts to hit the ball on, so you do not need to declare a miss when they do not hit the ball on first.)

At the beginning of each frame the balls are set up by the referee as illustrated above. This will be followed by a break-off shot, the white cue ball can be placed anywhere inside the D (it is called in-hand, which also happens when the cue ball is potted).

Players take turns in visiting the table. A break is the number of points scored by a player in one single visit to the table. A player's turn and break end when he fails to pot a ball, when he does something against the rules of the game, which is called a foul, or when a frame has ended.

The ball or balls that can be hit first by the white are called the ball(s) "on" for that particular stroke. The ball(s) "on" differ from shot to shot: a red ball, if potted, must be followed by a color, and so on until a break ends; if a red is not potted, any red ball remains the ball "on". Only a ball or balls "on" may be potted legally by a player. If a ball not "on" is potted, this is a foul.

The game of snooker generally consists of two phases. The first phase is the situation in which there are still red balls on the table. In the first phase, at the beginning of a player's turn, the balls "on" are all remaining red balls. The player must therefore attempt to first hit and pot one or more red balls. For every red ball potted, the player will receive 1 point. When a red has been potted, it will stay off the table and the player can continue his break. If no red has been potted or a foul has been made, the other player will come into play.

In case one or more red balls have been potted, the player can continue his break. This time one of the six colors (yellow, green, brown, blue, pink and black) is the ball "on". Only one of these can be the ball "on" and the rules of the game state that a player must nominate his desired color to the referee, although it is often clear which ball the striker is playing and it is not necessary to nominate.

When the nominated color is potted, the player will be awarded the correct number of points (yellow, 2; green, 3; brown, 4; blue, 5; pink, 6; black, 7). The color is then taken out of the pocket by the referee and placed on its original spot.

Because only one of the colors is the ball "on", it is a foul to first hit multiple colors at the same time, or pot more than one color.

If a player fails to pot a ball "on", it being a red or nominated color, the other player will come into play and the balls "on" are always the reds, as long as there are still reds on the table.

The alternation between red balls and colors ends when all reds have been potted and a color is potted after the last red, or a failed attempt to do so is made. Then the second phase begins. All six colors have to be potted in ascending order of their points value (yellow, green, brown, blue, pink, black). Each becomes the ball "on" in that order. During this phase, when potted, the colors stay down and are not replaced on the table, unless a foul is made when potting the color, in which case the color is respotted.

When only the black is left, the first score or foul ends the frame, and the player who has scored most points has won it. However, if the score is tied after that, the black is respotted, the players draw lots for choice of playing, the next player plays from in-hand, and the next score or foul ends the frame.

When a foul is made during a shot, the player's turn is ended and he will receive no points for the foul shot. The other player will receive penalty points. Colors illegally potted are respotted (while reds are not), and if the cue ball is potted, the next player will play from in-hand. Fouls concerned in this problem are:

• failing to hit any other ball with the cue ball
• first hitting a ball "not-on" with the cue ball
• potting a ball "not-on"
• potting the white (in-off)

Penalty points are at least 4 points and at most 7 points. The number of penalty points is the value of the ball "on", or any of the "foul" balls, whichever is highest. When more than one foul is made, the penalty is not the added total - only the most highly valued foul is counted. As players usually do not nominate a color explicitly when hitting the colors, please be tolerant and assume that he always nominate the ball with the lowest score when it cannot be deduced from the ball first hit (i.e. when the cue ball does not hit any ball or hit a red first).

If a player commits a foul, and his opponent considers that the position left is unattractive, he may request that the offender play again from the resulting position.

Input

The input file contains multiple test cases. The first line of the input file is a single integer T (T ≤ 200), the number of test cases.

For each test case (frame), the first line contains the names of the two players separated by a whitespace. The first player will take the break-off shot. Each name is made up of no more than 20 English letters, and the two names are different.

After that, the input mainly consists of lines that describe a stroke each (with two exceptions stated later). A stroke is described by the color of the ball first hit by the cue ball (or "None" if the cue ball does not hit any ball), followed by zero or more colors of the balls potted, all separated by whitespaces. For example, a line "Red Red White Red" means the cue ball first hit a red ball, and 2 reds are potted as well as the cue ball itself; and a line "None" means the cue ball does not hit any ball thus no ball is potted. You can assume that all strokes are legal according to the balls remain on the table, and the cue ball will not hit two or more ball first simultaneously.

A line "Play again" may appear if and only if the last stroke is a foul. It means the other player request that the offender play again from the resulting position.

If a score or foul occurs when only the black is left, and the score is tied after that, a line with either player's name will follow. That means the player will play next as a result of the lot.

The case end when the frame ends. There is a blank line before every test case.

Output

For each frame, print a line in the format "Frame K" first, where K is the index of this case starts from 1. Then use the output to indicate the referee's behaviors:

• When a frame begins, print a line in the format "PlayerName's turn, in-hand", where PlayerName is the name of the player who take the break-off shot.

• After each stroke, print a line "Foul!" first if it is a foul. Then print a line with current score in the format "ScoreA : ScoreB", where ScoreA is the score of the player who take the break-off shot, and ScoreB is the other player's score.

After that, if the frame continue fairly (i.e. not only black is left before the stroke, or it is not a score or foul when only black is left), and some ball(s) should be respotted. Print a line with the word "Respot" following by the color(s) of the ball(s), all separated by whitespace. If more than one ball should be respotted, print their colors in ascending order of their values. Do not print anything if no ball needed to be respotted.

At last (when the frame continue fairly and any necessary ball has been respotted), if the last player's break ends, print a line in the format "PlayerName's turn" to ask the other player to play next, where PlayerName is the next player's name. If the next player should play from in-hand, print "PlayerName's turn, in-hand" instead.

• After a foul, if the other player request that the offender play again, just print a line "PlayerName's turn" or "PlayerName's turn, in-hand" according to whether the cue ball is in-hand, where PlayerName is the offender's name. Note that the requester is actually requesting in his turn, after you asked him to play next.

• If a score or foul occurs when only the black is left, and the score is tied after that, print a line "Tie" after the score. Then print two lines "Respot Black" and "PlayerName's turn, in-hand" to respot the black and play from in-hand, where PlayerName is the next player's name (determined by the lot).

• When the frame ends, print a line "PlayerName wins" after the score, where PlayerName is the winner's name.

Print a blank line between every two successive cases.

Sample Input

```1

Zero Maxbreak
Red White
Red Red
Black Black
Red Red
Black Black
Red Red
Black Black
Red Red
Black Black
Red Red
Black Black
Red Red
Black Black
Red Red
Black Black
Red Red
Black Black
Red Red
Black Black
Red Red
Black Black
Red Red
Black Black
Red Red
Black Black
Red Red
Black Black
Red Red
Black Black
Red Red
Black Black
Yellow Yellow
Green Green
Brown Brown
Blue Blue
Pink Pink
Black Black
```

Sample Output

```Frame 1
Zero's turn, in-hand
Foul!
0 : 4
Maxbreak's turn, in-hand
0 : 5
0 : 12
Respot Black
0 : 13
0 : 20
Respot Black
0 : 21
0 : 28
Respot Black
0 : 29
0 : 36
Respot Black
0 : 37
0 : 44
Respot Black
0 : 45
0 : 52
Respot Black
0 : 53
0 : 60
Respot Black
0 : 61
0 : 68
Respot Black
0 : 69
0 : 76
Respot Black
0 : 77
0 : 84
Respot Black
0 : 85
0 : 92
Respot Black
0 : 93
0 : 100
Respot Black
0 : 101
0 : 108
Respot Black
0 : 109
0 : 116
Respot Black
0 : 117
0 : 124
Respot Black
0 : 126
0 : 129
0 : 133
0 : 138
0 : 144
0 : 151
Maxbreak wins
```

Reference:

Author: XIAO, Dong
Source: The 2010 ACM-ICPC Asia Chengdu Regional Contest
Submit    Status