#include<stack>
#include<cstdio>
#include<cstdlib>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

int f[128];

struct card{
	char rank,suit;
	int strength()const{ return 4*f[rank]+f[suit]; }
	bool neighbor(const card &c)const{
		if(rank=='A'){
			return c.rank=='2' || c.rank=='K';
		}
		else if(c.rank=='A'){
			return rank=='2' || rank=='K';
		}
		else{
			return abs(f[rank]-f[c.rank])==1;
		}
	}
};

card parse(const char *s){ return (card){s[1],s[0]}; }

struct action{
	int type,i; // 0: deck からカードを引く, 1: 右の table にカード i を置く, 2: 左の table にカード i を置く, 3: 位置 i にカードを戻す
	int t; // この動作を終える時刻
};

bool gameover(const stack<card> &S1,const stack<card> &S2,const bool *b1,const bool *b2,const card &last1,const card &last2){
	bool end1=S1.empty(),end2=S2.empty();
	rep(i,4){
		if(b1[i]) end1=false;
		if(b2[i]) end2=false;
	}

	if(end1 && end2){
		puts(last1.strength()>last2.strength()?"A wins.":"B wins.");
		return true;
	}
	if(end1){
		puts("A wins.");
		return true;
	}
	if(end2){
		puts("B wins.");
		return true;
	}
	return false;
}

void cold_move(stack<card> &S,bool *b,card *tab,card &R){
	if(!S.empty()) R=S.top(), S.pop();
	else{ // rightmost card
		rep(i,4) if(b[i]) { R=tab[i]; b[i]=false; break; }
	}
}

bool get_next_action(int t,const stack<card> &S,const bool *b,const card *tab,const card &R,const card &L,action &res){
	// type 0
	if(!S.empty()){
		rep(i,4) if(!b[i]) { res.type=0; res.t=t+3; return true; }
	}
	// type 1
	int i0=-1;
	rep(i,4) if(b[i] && R.neighbor(tab[i])) if(i0==-1 || tab[i].strength()<tab[i0].strength()) i0=i;
	if(i0!=-1){ res.type=1; res.t=t+5; res.i=i0; return true; }
	// type 2
	i0=-1;
	rep(i,4) if(b[i] && L.neighbor(tab[i])) if(i0==-1 || tab[i].strength()<tab[i0].strength()) i0=i;
	if(i0!=-1){ res.type=2; res.t=t+7; res.i=i0; return true; }
	// type 3 or wait
	return false;
}

void do_action(const action &a,int &t,stack<card> &S,bool *b,card *tab,card &R,card &L){
	t=a.t;
	if(a.type==0){
		b[a.i]=true;
		tab[a.i]=S.top();
		S.pop();
	}
	else if(a.type==1){
		R=tab[a.i];
		b[a.i]=false;
	}
	else if(a.type==2){
		L=tab[a.i];
		b[a.i]=false;
	}
}

int main(){
	const int list[]={77,2,3,4,5,6,7,8,9,10,11,12,13,3,2,1,0};
	rep(i,17) f["A23456789XJQKSHDC"[i]]=list[i];

	while(1){
		stack<card> S1,S2; // deck
		card L,R; // table の一番上のカード
		bool b1[4]={},b2[4]={}; // tableau の i 番目にカードがあるかどうか
		card tab1[4],tab2[4]; // tableau ( 手札 )

		// input
		char tmp[52][3];
		int n1; scanf("%d",&n1);
		rep(i,n1) scanf("%s",tmp[i]);
		for(int i=n1-1;i>=0;i--) S1.push(parse(tmp[i]));
		int n2; scanf("%d",&n2);
		rep(i,n2) scanf("%s",tmp[i]);
		for(int i=n2-1;i>=0;i--) S2.push(parse(tmp[i]));
		if(n1==0) break;

		// draw 4 cards
		rep(i,4){
			if(!S1.empty()){ card c=S1.top(); S1.pop(); b1[i]=true; tab1[i]=c; }
			if(!S2.empty()){ card c=S2.top(); S2.pop(); b2[i]=true; tab2[i]=c; }
		}

		// gao
		while(1){
			// cold
			cold_move(S1,b1,tab1,R);
			cold_move(S2,b2,tab2,L);
			if(gameover(S1,S2,b1,b2,R,L)) goto END;

			// jakajaka
			int t=0;
			action a1,a2;
			bool ok1=get_next_action(t,S1,b1,tab1,R,L,a1);
			bool ok2=get_next_action(t,S2,b2,tab2,L,R,a2);
			card last1,last2; // 最後に置いたカード
			while(ok1 || ok2){
				if(ok1 && (a1.type==1 || a1.type==2)) last1=tab1[a1.i];
				if(ok2 && (a2.type==1 || a2.type==2)) last2=tab2[a2.i];

				if(ok1 && ok2){
					if(a1.t<a2.t){
						do_action(a1,t,S1,b1,tab1,R,L);
						if(a1.type==1 && a2.type==2 || a1.type==2 && a2.type==1) a2.type=3, a2.t=t+5;
						ok1=get_next_action(t,S1,b1,tab1,R,L,a1);
					}
					else if(a2.t<a1.t){
						do_action(a2,t,S2,b2,tab2,L,R);
						if(a1.type==1 && a2.type==2 || a1.type==2 && a2.type==1) a1.type=3, a1.t=t+5;
						ok2=get_next_action(t,S2,b2,tab2,L,R,a2);
					}
					else{ // a1.t==a2.t
						if(a1.type==1 && a2.type==2){
							do_action(a2,t,S2,b2,tab2,L,R);
							a1.type=3;
							a1.t=t+5;
							ok2=get_next_action(t,S2,b2,tab2,L,R,a2);
						}
						else if(a1.type==2 && a2.type==1){
							do_action(a1,t,S1,b1,tab1,R,L);
							a2.type=3;
							a2.t=t+5;
							ok1=get_next_action(t,S1,b1,tab1,R,L,a1);
						}
						else{
							do_action(a1,t,S1,b1,tab1,R,L);
							do_action(a2,t,S2,b2,tab2,L,R);
							ok1=get_next_action(t,S1,b1,tab1,R,L,a1);
							ok2=get_next_action(t,S2,b2,tab2,L,R,a2);
						}
					}
				}
				else if(ok1){
					do_action(a1,t,S1,b1,tab1,R,L);
					ok1=get_next_action(t,S1,b1,tab1,R,L,a1);
					ok2=get_next_action(t,S2,b2,tab2,L,R,a2);
				}
				else if(ok2){
					do_action(a2,t,S2,b2,tab2,L,R);
					ok1=get_next_action(t,S1,b1,tab1,R,L,a1);
					ok2=get_next_action(t,S2,b2,tab2,L,R,a2);
				}
				if(gameover(S1,S2,b1,b2,last1,last2)) goto END;
			}
		}

		END:;
	}

	return 0;
}
### Prompt

Please create a solution in Cpp to the following problem:

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:

* A robot draws cards in the order they are dealt.
* Each robot is always given one or more cards.
* In the real game of 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.
* 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.
* As a preparation, each robot draws four cards, and puts them to the tableau from right to left.
* If there are not enough cards in its deck, draw all cards in the deck.
* 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.
* 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.
* Then two robots continue moving according to the basic rule of the game described above, until neither of them can move cards anymore.
* 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.
* 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.
* 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.
* 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.
* 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.
* 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.
* When one of the robots has run out the cards, i.e., moved all dealt cards, the game ends.
* The robot which has run out the cards wins the game.
* When both robots run out the cards at the same moment, the robot which moved the stronger card in the last move wins.



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:

* 300 milliseconds to draw a card to the tableau,
* 500 milliseconds to move a card to the right table,
* 700 milliseconds to move a card to the left table, and
* 500 milliseconds to return a card to its original position.



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).

* Robot A draws four cards S3, S5, S8, and S9 to its tableau from right to left. Robot B draws all the three cards H7, H3, and H4.
* Then the two robots synchronize for the game start. Let this moment be 0ms.
* At the same moment, Robot A starts moving S2 to the table A from the deck, and Robot B starts moving H7 to the table B from the tableau.
* At 500ms, the both robots complete their moving cards. Then Robot A starts moving S3 to the table A 1(which requires 500ms), and Robot B starts moving H3 also to the table A (which requires 700ms).
* At 1000ms, Robot A completes putting S3 on the table A. Robot B is interrupted its move and starts returning H3 to the tableau (which requires 500ms). At the same time Robot A starts moving S8 to the table B (which requires 700ms).
* At 1500ms, Robot B completes returning H3 and starts moving H4 to the table A (which requires 700ms).
* At 1700ms, Robot A completes putting S8 and starts moving S9 to the table B.
* At 2200ms, Robot B completes putting H4 and starts moving H3 to the table A.
* At 2400ms, Robot A completes putting S9 and starts moving S5 to the table A.
* At 2900ms, The both robots are to complete putting the cards on the table A. Since Robot B is moving the card to the table left to it, Robot B completes putting H3. Robot A is interrupted.
* Now Robot B has finished moving all the dealt cards, so Robot B wins this game.



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.

Example

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

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

### Response

```cpp
#include<stack>
#include<cstdio>
#include<cstdlib>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

int f[128];

struct card{
	char rank,suit;
	int strength()const{ return 4*f[rank]+f[suit]; }
	bool neighbor(const card &c)const{
		if(rank=='A'){
			return c.rank=='2' || c.rank=='K';
		}
		else if(c.rank=='A'){
			return rank=='2' || rank=='K';
		}
		else{
			return abs(f[rank]-f[c.rank])==1;
		}
	}
};

card parse(const char *s){ return (card){s[1],s[0]}; }

struct action{
	int type,i; // 0: deck からカードを引く, 1: 右の table にカード i を置く, 2: 左の table にカード i を置く, 3: 位置 i にカードを戻す
	int t; // この動作を終える時刻
};

bool gameover(const stack<card> &S1,const stack<card> &S2,const bool *b1,const bool *b2,const card &last1,const card &last2){
	bool end1=S1.empty(),end2=S2.empty();
	rep(i,4){
		if(b1[i]) end1=false;
		if(b2[i]) end2=false;
	}

	if(end1 && end2){
		puts(last1.strength()>last2.strength()?"A wins.":"B wins.");
		return true;
	}
	if(end1){
		puts("A wins.");
		return true;
	}
	if(end2){
		puts("B wins.");
		return true;
	}
	return false;
}

void cold_move(stack<card> &S,bool *b,card *tab,card &R){
	if(!S.empty()) R=S.top(), S.pop();
	else{ // rightmost card
		rep(i,4) if(b[i]) { R=tab[i]; b[i]=false; break; }
	}
}

bool get_next_action(int t,const stack<card> &S,const bool *b,const card *tab,const card &R,const card &L,action &res){
	// type 0
	if(!S.empty()){
		rep(i,4) if(!b[i]) { res.type=0; res.t=t+3; return true; }
	}
	// type 1
	int i0=-1;
	rep(i,4) if(b[i] && R.neighbor(tab[i])) if(i0==-1 || tab[i].strength()<tab[i0].strength()) i0=i;
	if(i0!=-1){ res.type=1; res.t=t+5; res.i=i0; return true; }
	// type 2
	i0=-1;
	rep(i,4) if(b[i] && L.neighbor(tab[i])) if(i0==-1 || tab[i].strength()<tab[i0].strength()) i0=i;
	if(i0!=-1){ res.type=2; res.t=t+7; res.i=i0; return true; }
	// type 3 or wait
	return false;
}

void do_action(const action &a,int &t,stack<card> &S,bool *b,card *tab,card &R,card &L){
	t=a.t;
	if(a.type==0){
		b[a.i]=true;
		tab[a.i]=S.top();
		S.pop();
	}
	else if(a.type==1){
		R=tab[a.i];
		b[a.i]=false;
	}
	else if(a.type==2){
		L=tab[a.i];
		b[a.i]=false;
	}
}

int main(){
	const int list[]={77,2,3,4,5,6,7,8,9,10,11,12,13,3,2,1,0};
	rep(i,17) f["A23456789XJQKSHDC"[i]]=list[i];

	while(1){
		stack<card> S1,S2; // deck
		card L,R; // table の一番上のカード
		bool b1[4]={},b2[4]={}; // tableau の i 番目にカードがあるかどうか
		card tab1[4],tab2[4]; // tableau ( 手札 )

		// input
		char tmp[52][3];
		int n1; scanf("%d",&n1);
		rep(i,n1) scanf("%s",tmp[i]);
		for(int i=n1-1;i>=0;i--) S1.push(parse(tmp[i]));
		int n2; scanf("%d",&n2);
		rep(i,n2) scanf("%s",tmp[i]);
		for(int i=n2-1;i>=0;i--) S2.push(parse(tmp[i]));
		if(n1==0) break;

		// draw 4 cards
		rep(i,4){
			if(!S1.empty()){ card c=S1.top(); S1.pop(); b1[i]=true; tab1[i]=c; }
			if(!S2.empty()){ card c=S2.top(); S2.pop(); b2[i]=true; tab2[i]=c; }
		}

		// gao
		while(1){
			// cold
			cold_move(S1,b1,tab1,R);
			cold_move(S2,b2,tab2,L);
			if(gameover(S1,S2,b1,b2,R,L)) goto END;

			// jakajaka
			int t=0;
			action a1,a2;
			bool ok1=get_next_action(t,S1,b1,tab1,R,L,a1);
			bool ok2=get_next_action(t,S2,b2,tab2,L,R,a2);
			card last1,last2; // 最後に置いたカード
			while(ok1 || ok2){
				if(ok1 && (a1.type==1 || a1.type==2)) last1=tab1[a1.i];
				if(ok2 && (a2.type==1 || a2.type==2)) last2=tab2[a2.i];

				if(ok1 && ok2){
					if(a1.t<a2.t){
						do_action(a1,t,S1,b1,tab1,R,L);
						if(a1.type==1 && a2.type==2 || a1.type==2 && a2.type==1) a2.type=3, a2.t=t+5;
						ok1=get_next_action(t,S1,b1,tab1,R,L,a1);
					}
					else if(a2.t<a1.t){
						do_action(a2,t,S2,b2,tab2,L,R);
						if(a1.type==1 && a2.type==2 || a1.type==2 && a2.type==1) a1.type=3, a1.t=t+5;
						ok2=get_next_action(t,S2,b2,tab2,L,R,a2);
					}
					else{ // a1.t==a2.t
						if(a1.type==1 && a2.type==2){
							do_action(a2,t,S2,b2,tab2,L,R);
							a1.type=3;
							a1.t=t+5;
							ok2=get_next_action(t,S2,b2,tab2,L,R,a2);
						}
						else if(a1.type==2 && a2.type==1){
							do_action(a1,t,S1,b1,tab1,R,L);
							a2.type=3;
							a2.t=t+5;
							ok1=get_next_action(t,S1,b1,tab1,R,L,a1);
						}
						else{
							do_action(a1,t,S1,b1,tab1,R,L);
							do_action(a2,t,S2,b2,tab2,L,R);
							ok1=get_next_action(t,S1,b1,tab1,R,L,a1);
							ok2=get_next_action(t,S2,b2,tab2,L,R,a2);
						}
					}
				}
				else if(ok1){
					do_action(a1,t,S1,b1,tab1,R,L);
					ok1=get_next_action(t,S1,b1,tab1,R,L,a1);
					ok2=get_next_action(t,S2,b2,tab2,L,R,a2);
				}
				else if(ok2){
					do_action(a2,t,S2,b2,tab2,L,R);
					ok1=get_next_action(t,S1,b1,tab1,R,L,a1);
					ok2=get_next_action(t,S2,b2,tab2,L,R,a2);
				}
				if(gameover(S1,S2,b1,b2,last1,last2)) goto END;
			}
		}

		END:;
	}

	return 0;
}
```