Problem D: Speed

Do you know Speed? It is one of popular card games, in which two players compete how quick they can move their cards to tables.

To play 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:

  1. 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 A and 2, 4 and 3 are neighbors. A and K are also neighbors in this game.
  2. He is also allowed to draw a card from the deck and put it on a vacant area of the tableau.
  3. 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.

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:

The strength among the cards is determined by their ranks, then by the suits. The ranks are strong in the following order: A > K > Q > J > X (10) > 9 > . . . > 3 > 2. The suits are strong in the following order: S (Spades) > H (Hearts) > D (Diamonds) > C (Cloves). In other words, SA is the strongest and C2 is the weakest.

The robots require the following amount of time to complete each action:

Cancelling an action always takes the constant time of 500ms, 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 A is given the deck “S3 S5 S8 S9 S2” and Robot B is given the deck “H7 H3 H4”, then the playing will be like the description below. Note that, in the description, “the table A” (resp. “the table B”) denotes the right table for Robot A (resp. Robot B).

Input

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 NA , which specifies the number of cards to be dealt as a deck to Robot A. The next line contains a card sequences of length NA . Then the number NB and card sequences of length NB for Robot 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 2 characters. The first character is one of ‘S’ (spades), ‘H’ (hearts), ‘D’ (diamonds) or ‘C’ (cloves) and specifies the suit. The second is one of ‘A’, ‘K’, ‘Q’, ‘J’, ‘X’ (for 10) or a digit between ‘9’ and ‘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.

Output

For each data set, output the result of a game in one line. Output “A wins.” if Robot A wins, or output “B wins.” if Robot B wins. No extra characters are allowed in the output.

Sample Input

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 for the Sample Input

A wins.
B wins.
B wins.
A wins.