import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
public class Main{
static boolean gameOver;
static boolean firstTurn ;
while(true)
{
if(xx.charAt(0)=='#')
break;
Deque<String> nonD = new LinkedList<String>();
Deque<String> D = new LinkedList<String>();
boolean lastD = true;
for (int i = 0; i < 13; i++) {
if(lastD)
nonD.add(st.nextToken());
else
D.add(st.nextToken());
lastD = !lastD;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 13; j++)
{
if(lastD)
nonD.add(st.nextToken());
else
D.add(st.nextToken());
lastD = !lastD;
}
}
Deque<String> heap = new LinkedList<String>();
gameOver = false;
firstTurn = true;
boolean winner = true;
while(true)
{
if(firstTurn)
{
if(nonD.isEmpty() )
{
winner = false;
break;
}
if(gameOver)
{
if(nonD.isEmpty())
winner = false;
else
winner = true;
break;
}
String card
= nonD.
pollLast(); heap.add(card);
firstTurn = false;
switch(card.charAt(1))
{
case 'A':
firstTurn = !firstTurn;
simulate(D, nonD, heap, 4);
break;
case 'K':
firstTurn = !firstTurn;
simulate(D, nonD, heap, 3);
break;
case 'Q':
firstTurn = !firstTurn;
simulate(D, nonD, heap, 2);
break;
case 'J':
firstTurn = !firstTurn;
simulate(D, nonD, heap, 1);
break;
default:
break;
}
}
if(!firstTurn)
{
if(D.isEmpty() )
{
winner = true;
break;
}
if(gameOver)
{
if(nonD.isEmpty())
winner = false;
else
winner = true;
break;
}
firstTurn = true;
heap.add(card);
switch(card.charAt(1))
{
case 'A':
firstTurn = !firstTurn;
simulate(nonD, D, heap, 4);
break;
case 'K':
firstTurn = !firstTurn;
simulate(nonD, D, heap, 3);
break;
case 'Q':
firstTurn = !firstTurn;
simulate(nonD, D, heap, 2);
break;
case 'J':
firstTurn = !firstTurn;
simulate(nonD, D, heap, 1);
break;
default:
break;
}
}
}
if(!winner)
{
String ans
= (D.
size()<10)? " "+D.
size() : D.
size()+""; out.append("1 "+ans+"\n");
//out.flush();
}
else
{
String ans
= (nonD.
size()<10)? " "+nonD.
size() :nonD.
size()+""; out.append("2 "+ans+"\n");
//out.flush();
}
}
out.flush();
}
public static void simulate(Deque<String> first ,Deque<String> sec,Deque<String> heap,int counter)
{
while(counter-->0)
{
if(first.isEmpty())
{
gameOver = true;
return;
}
String card
= first.
pollLast(); heap.add(card);
switch (card.charAt(1)) {
case 'A':
firstTurn = !firstTurn;
simulate(sec, first, heap, 4);
return;
case 'K':
firstTurn = !firstTurn;
simulate(sec, first, heap, 3);
return;
case 'Q':
firstTurn = !firstTurn;
simulate(sec, first, heap, 2);
return;
case 'J':
firstTurn = !firstTurn;
simulate(sec, first, heap, 1);
return;
default:
break;
}
}
takeHeap(sec, heap);
}
public static void takeHeap(Deque<String>player,Deque<String>heap)
{
while(!heap.isEmpty())
{
player.offerFirst(heap.pollFirst());
}
}
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEudGV4dC5EZWNpbWFsRm9ybWF0OwppbXBvcnQgamF2YS51dGlsLio7CnB1YmxpYyBjbGFzcyBNYWluewoJc3RhdGljIGJvb2xlYW4gZ2FtZU92ZXI7CglzdGF0aWMgYm9vbGVhbiBmaXJzdFR1cm4gOwoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgdGhyb3dzIFRocm93YWJsZSB7CgkJQnVmZmVyZWRSZWFkZXIgYnIgPSBuZXcgQnVmZmVyZWRSZWFkZXIobmV3IElucHV0U3RyZWFtUmVhZGVyKFN5c3RlbS5pbikpOwoJCVByaW50V3JpdGVyIG91dCA9IG5ldyBQcmludFdyaXRlcihTeXN0ZW0ub3V0KTsKCQl3aGlsZSh0cnVlKQoJCXsKCQkJU3RyaW5nIHh4ID0gYnIucmVhZExpbmUoKTsKCQkJaWYoeHguY2hhckF0KDApPT0nIycpCgkJCQlicmVhazsKCQkJRGVxdWU8U3RyaW5nPiBub25EID0gbmV3IExpbmtlZExpc3Q8U3RyaW5nPigpOwoJCQlEZXF1ZTxTdHJpbmc+IEQgPSBuZXcgTGlua2VkTGlzdDxT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