import java.util.*; // Solution to "Poker" // Sat Oct 17 19:52:51 CDT 2015 // Author: Greg Hamerly (hamerly@cs.baylor.edu) public class poker_hamerly { public static class Card implements Comparable<Card> { public Card(String card) { rank = ranks.get(card.substring(0, card.length() - 1)); suit = card.charAt(card.length() - 1); } public int rank; public char suit; public int compareTo(Card other) { return rank - other.rank; } } static Map<String, Integer> ranks = new HashMap<String, Integer>(); static { for (int r = 2; r <= 9; ++r) ranks.put("" + r, r); ranks.put("T", 10); ranks.put("J", 11); ranks.put("Q", 12); ranks.put("K", 13); ranks.put("A", 14); } public static boolean isFlush(Vector<Card> hand) { char s = hand.elementAt(0).suit; for (Card c : hand) if (s != c.suit) return false; return true; } public static boolean isStraight(Vector<Card> hand) { int r = hand.elementAt(0).rank - 1; for (Card c : hand) { if (r + 1 != c.rank) return false; r = c.rank; } return true; } // encode N integers as MAX - (14^(N-1) * ranks[0] + 14^(N-2) * ranks[1] + ... + 14 * ranks[N-2] + ranks[0) // where MAX = 14^N + 14^(N-1) + ... + 14^2 + 14 public static int encode(int ... ranks) { int e = 0, max = 0; for (int r : ranks) { e = e * 14 + r; max = 14 * (max + 1); } return max - e; } // give a numeric score to this hand; lower scores are higher-value hands public static int score(Vector<Card> hand) { assert(hand.size() == 5); Collections.sort(hand); int a = hand.elementAt(0).rank; int b = hand.elementAt(1).rank; int c = hand.elementAt(2).rank; int d = hand.elementAt(3).rank; int e = hand.elementAt(4).rank; boolean flush = isFlush(hand); boolean straight = isStraight(hand); // minrank represents the minimum rank of hands of this type int minrank = 0; // numEncodings[i] represents the number of ways there are to encode i cards int numEncodings[] = new int[6]; for (int i = 1; i < numEncodings.length; ++i) numEncodings[i] = 14 * (numEncodings[i-1] + 1); // ROYAL FLUSH or STRAIGHT FLUSH: encode(highest card) if (flush && straight) return minrank + encode(e); minrank += numEncodings[1]; // FOUR OF A KIND: encode(shared rank of the four) if (b == e || a == d) return minrank + encode(d); minrank += numEncodings[1]; // FULL HOUSE: encode(shared rank of the three) if ((a == b && c == e) || (a == c && d == e)) return minrank + encode(c); minrank += numEncodings[1]; // FLUSH: encode(all cards) if (flush) return minrank + encode(e, d, c, b, a); minrank += numEncodings[5]; // STRAIGHT: encode(high card) if (straight) return minrank + encode(e); minrank += numEncodings[1]; // THREE OF A KIND: encode(shared rank of the three) if (a == c || b == d || c == e) return minrank + encode(c); minrank += numEncodings[1]; // TWO PAIR: encode(higher pair rank, lower pair rank, kicker rank) if ((d == e || c == d) && (b == c || a == b)) { int kicker = (a != b) ? a : ((b != c) ? c : e); return minrank + encode(d, b, kicker); } minrank += numEncodings[3]; // ONE PAIR: encode(pair rank, high kicker, mid kicker, low kicker) if (d == e || c == d || b == c || a == b) { int kicker1 = c, kicker2 = b, kicker3 = a; int pair = d; if (c == d || b == c || a == b) kicker1 = e; if (b == c || a == b) { kicker2 = d; pair = b; } if (a == b) kicker3 = c; return minrank + encode(pair, kicker1, kicker2, kicker3); } minrank += numEncodings[4]; // NO SPECIALS: encode(all cards) return minrank + encode(e, d, c, b, a); } public static void main(String args[]) { Scanner input = new Scanner(System.in); int w = Integer.parseInt(input.nextLine()); for (int i = 0; i < w; ++i) { String a[] = input.nextLine().split(" "); String b[] = input.nextLine().split(" "); Vector<Card> handA = new Vector<Card>(); Vector<Card> handB = new Vector<Card>(); for (String c : a) handA.addElement(new Card(c)); for (String c : b) handB.addElement(new Card(c)); int scoreA = score(handA); int scoreB = score(handB); if (scoreA != scoreB) { String winningHand[] = (scoreA < scoreB) ? a : b; boolean first = true; for (String c : winningHand) { if (!first) System.out.print(" "); System.out.print(c); first = false; } System.out.println(); } else { System.out.println("Tie"); } } } }