eolymp
bolt
Try our new interface for solving problems

Speed

Do you know \textit{Speed}? It is one of popular card games, in which two players compete how quick they can move their cards to tables. To play \textit{Speed}, two players sit face-to-face first. Each player has a deck and a tableau assigned for him, and between them are two tables to make a pile on, one in left and one in right. A tableau can afford up to only four cards. There are some simple rules to carry on the game: \begin{enumerate} \item A player is allowed to move a card from his own tableau onto a pile, only when the rank of the moved card is a neighbor of that of the card on top of the pile. For example \textbf{A} and \textbf{2}, \textbf{4} and \textbf{3} are neighbors. \textbf{A} and \textbf{K} are also neighbors in this game. \item He is also allowed to draw a card from the deck and put it on a vacant area of the tableau. \item If both players attempt to move cards on the same table at the same time, only the faster player can put a card on the table. The other player cannot move his card to the pile (as it is no longer a neighbor of the top card), and has to bring it back to his tableau. \end{enumerate} First each player draws four cards from his deck, and places them face on top on the tableau. In case he does not have enough cards to fill out the tableau, simply draw as many as possible. The game starts by drawing one more card from the deck and placing it on the tables on their right simultaneously. If the deck is already empty, he can move an arbitrary card on his tableau to the table. Then they continue performing their actions according to the rule described above until both of them come to a deadend, that is, have no way to move cards. Every time a deadend occurs, they start over from each drawing a card (or taking a card from his or her tableau) and placing on his or her right table, regardless of its face. The player who has run out of his card first is the winner of the game. Mr. James A. Games is so addicted in this simple game, and decided to develop robots that plays it. He has just completed designing the robots and their program, but is not sure if they work well, as the design is so complicated. So he asked you, a friend of his, to write a program that simulates the robots. The algorithm for the robots is designed as follows: \begin{itemize} \item A robot draws cards in the order they are dealt. \begin{itemize} \item Each robot is always given one or more cards. \item In the real game of \textit{Speed}, the players first classify cards by their colors to enable them to easily figure out which player put the card. But this step is skipped in this simulation. \item The game uses only one card set split into two. In other words, there appears at most one card with the same face in the two decks given to the robots. \end{itemize} \item As a preparation, each robot draws four cards, and puts them to the tableau from right to left. \begin{itemize} \item If there are not enough cards in its deck, draw all cards in the deck. \end{itemize} \item After this step has been completed on both robots, they synchronize to each other and start the game by putting the first cards onto the tables in the same moment. \begin{itemize} \item If there remains one or more cards in the deck, a robot draws the top one and puts it onto the right table. Otherwise, the robot takes the rightmost card from its tableau. \end{itemize} \item • Then two robots continue moving according to the basic rule of the game described above, until neither of them can move cards anymore. \begin{itemize} \item When a robot took a card from its tableau, it draws a card (if possible) from the deck to fill the vacant position after the card taken is put onto a table. \item It takes some amount of time to move cards. When a robot completes putting a card onto a table while another robot is moving to put a card onto the same table, the robot in motion has to give up the action immediately and returns the card to its original position. \item A robot can start moving to put a card on a pile at the same time when the neighbor is placed on the top of the pile. \item If two robots try to put cards onto the same table at the same moment, only the robot moving a card to the left can successfully put the card, due to the position settings. \item When a robot has multiple candidates in its tableau, it prefers the cards which can be placed on the right table to those which cannot. In case there still remain multiple choices, the robot prefers the weaker card. \end{itemize} \item When it comes to a deadend situation, the robots start over from each putting a card to the table, then continue moving again according to the algorithm above. \item When one of the robots has run out the cards, i.e., moved all dealt cards, the game ends. \begin{itemize} \item The robot which has run out the cards wins the game. \item When both robots run out the cards at the same moment, the robot which moved the stronger card in the last move wins. \end{itemize} \end{itemize} The strength among the cards is determined by their ranks, then by the suits. The ranks are strong in the following order: \textbf{A} > \textbf{K} > \textbf{Q} > \textbf{J} > \textbf{X (10)} > \textbf{9} > ··· > \textbf{3} > \textbf{2}. The suits are strong in the following order: \textbf{S} (Spades) > \textbf{H}(Hearts) > \textbf{D} (Diamonds) > \textbf{C} (Cloves). In other words, \textbf{SA} is the strongest and \textbf{C2} is the weakest. The robots require the following amount of time to complete each action: \begin{itemize} \item \textbf{300} milliseconds to draw a card to the tableau, \item \textbf{500} milliseconds to move a card to the right table, \item \textbf{700} milliseconds to move a card to the left table, and \item \textbf{500} milliseconds to return a card to its original position. \end{itemize} Cancelling an action always takes the constant time of \textbf{500} ms, regardless of the progress of the action being cancelled. This time is counted from the exact moment when the action is interrupted, not the beginning time of the action. You may assume that these robots are well-synchronized, i.e., there is no clock skew between them. For example, suppose Robot \textbf{A} is given the deck "\textbf{S3 S5 S8 S9 S2}" and Robot \textbf{B} is given the deck "\textbf{H7 H3 H4}", then the playing will be like the description below. Note that, in the description, "\textbf{the table A}" (resp. "\textbf{the table B}") denotes the right table for Robot \textbf{A} (resp. Robot \textbf{B}). \begin{itemize} \item Robot \textbf{A} draws four cards \textbf{S3}, \textbf{S5}, \textbf{S8}, and \textbf{S9} to its tableau from right to left. Robot B draws all the three cards\textbf{H7}, \textbf{H3}, and \textbf{H4}. \item Then the two robots synchronize for the game start. Let this moment be \textbf{0} ms. \item At the same moment, Robot \textbf{A} starts moving \textbf{S2} to the table \textbf{A} from the deck, and Robot \textbf{B} starts moving \textbf{H7}to the table \textbf{B} from the tableau. \item At \textbf{500} ms, the both robots complete their moving cards. Then Robot \textbf{A} starts moving \textbf{S3} to the table \textbf{A}1(which requires \textbf{500} ms), and Robot \textbf{B} starts moving \textbf{H3} also to the table \textbf{A} (which requires \textbf{700} ms). \item At \textbf{1000} ms, Robot \textbf{A} completes putting \textbf{S3} on the table \textbf{A}. Robot \textbf{B} is interrupted its move and starts returning \textbf{H3} to the tableau (which requires \textbf{500} ms). At the same time Robot \textbf{A} starts moving \textbf{S8} to the table\textbf{B} (which requires \textbf{700} ms). \item At \textbf{1500} ms, Robot \textbf{B} completes returning \textbf{H3} and starts moving \textbf{H4} to the table \textbf{A} (which requires \textbf{700} ms). \item At \textbf{1700} ms, Robot \textbf{A} completes putting \textbf{S8} and starts moving \textbf{S9} to the table \textbf{B}. \item At \textbf{2200} ms, Robot \textbf{B} completes putting \textbf{H4} and starts moving \textbf{H3} to the table \textbf{A}. \item At \textbf{2400} ms, Robot \textbf{A} completes putting \textbf{S9} and starts moving \textbf{S5} to the table \textbf{A}. \item At \textbf{2900} ms, The both robots are to complete putting the cards on the table \textbf{A}. Since Robot \textbf{B} is moving the card to the table left to it, Robot \textbf{B} completes putting \textbf{H3}. Robot \textbf{A} is interrupted. \item Now Robot \textbf{B} has finished moving all the dealt cards, so Robot \textbf{B} wins this game. \end{itemize} \InputFile The input consists of multiple data sets, each of which is described by four lines. The first line of each data set contains an integer \textbf{N_A}, which specifies the number of cards to be dealt as a deck to Robot \textbf{A}. The next line contains a card sequences of length \textbf{N_A}. Then the number \textbf{N_B} and card sequences of length \textbf{N_B} for Robot \textbf{B} follows, specified in the same manner. In a card sequence, card specifications are separated with one space character between them. Each card specification is a string of \textbf{2} characters. The first character is one of '\textbf{S}' (spades), '\textbf{H}' (hearts), '\textbf{D}' (diamonds) or '\textbf{C}' (cloves) and specifies the suit. The second is one of '\textbf{A}', '\textbf{K}', '\textbf{Q}', '\textbf{J}', '\textbf{X}' (for \textbf{10}) or a digit between '\textbf{9}' and '\textbf{2}', and specifies the rank. As this game is played with only one card set, there is no more than one card of the same face in each data set. The end of the input is indicated by a single line containing a zero. \OutputFile For each data set, output the result of a game in one line. Output "\textbf{A wins.}" if Robot \textbf{A} wins, or output "\textbf{B wins.}" if Robot \textbf{B} wins. No extra characters are allowed in the output.
Time limit 1 second
Memory limit 64 MiB
Input example #1
1
SA
1
C2
2
SA HA
2
C2 C3
5
S3 S5 S8 S9 S2
3
H7 H3 H4
10
H7 CJ C5 CA C6 S2 D8 DA S6 HK
10
C2 D6 D4 H5 DJ CX S8 S9 D3 D5
0
Output example #1
A wins.
B wins.
B wins.
A wins.
Source ACM-ICPC Japan Alumni Group Summer Camp 2007, Day 2, Tokyo, Japan, 2007-09-23