Poker

Compare two five-card poker hands by standard rankings and kicker tiebreakers and print the winning hand or Tie.

Medium5ImplementationSortingNo attempts yetTime limit1sMemory limit256 MB

Problem

Poker is a family of gambling card games that involve betting and individual play. The winner is determined by the ranks and combinations of the players' cards, some of which stay hidden until the end of the game. In this problem you compare two five card hands and decide which one wins.

Each card has a suit (Club, Diamond, Heart, Spade) and a rank. The ranks, from lowest to highest, are ace (low), 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, ace (high). Only one ace exists within each suit, and it usually ranks as the highest card, but under some circumstances the ace is treated as the lowest card. The suit itself has no bearing on the worth of a card and is only used to determine which category a hand belongs to.

Poker hands are made of combinations of 2 to 5 cards in the hand. Cards that are not used in the combination are called kicker cards and can be used to break ties if needed.

The ranking of poker hands, best to worst, is as follows.

A Royal Flush is a hand that includes a 10, Jack, Queen, King, and Ace all of the same suit. If both hands have a Royal Flush, the hands tie.

A Straight Flush is a hand where the ranks of the cards are sequential and they all share the same suit. For example, a hand that has the 5, 6, 7, 8, and 9 of Clubs is a Straight Flush. An Ace can be either low (it comes before a 2) or high (it comes after a King). When comparing Straight Flushes, the straight with the highest ranked card wins. If the hands have the same highest ranked card, the hands tie.

A Four of a Kind is a hand that contains four cards of the same rank. For example, a hand that has the 2 of Clubs, 2 of Hearts, 2 of Diamonds, 2 of Spades, and any other card is a Four of a Kind. When comparing Four of a Kinds, the hand with the set of four cards with the highest rank wins.

A Full House is a hand that contains three cards of one rank and two cards of another rank. For example, a hand that has the 2 of Clubs, 2 of Hearts, 2 of Diamonds, 3 of Clubs, and 3 of Hearts is a Full House of 2s over 3s. When comparing Full Houses, the hand with the set of three cards with the highest rank wins.

A Flush is a hand of five cards that all share the same suit. For example, a hand that has the Ace of Spades, 3 of Spades, 7 of Spades, 10 of Spades, and King of Spades is a Flush. When comparing Flushes, the hand with the highest ranked card wins. If both hands have the same high card, compare the next highest card and repeat until a winner is found. If both hands have the same five ranks, the hands tie.

A Straight is a hand where the ranks of the cards are sequential. For example, a hand that has the 5 of Clubs, 6 of Hearts, 7 of Spades, 8 of Diamonds, and 9 of Clubs is a Straight. Like a Straight Flush, an Ace can be either high or low. When comparing Straights, the straight with the highest ranked card wins. If the hands have the same highest ranked card, the hands tie.

A Three of a Kind is a hand that has three cards of the same rank. For example, a hand that has the 2 of Clubs, 2 of Hearts, 2 of Diamonds, 3 of Clubs, and 4 of Clubs is a Three of a Kind of 2s. When comparing Three of a Kinds, the hand with the highest ranking set wins.

A Two Pair is a hand that has two sets of two cards of the same rank. For example, a hand that has the 2 of Clubs, 2 of Hearts, 3 of Clubs, 3 of Hearts, and the 4 of Clubs is a Two Pair. When comparing Two Pairs, the hand that has the highest ranking set wins. If both hands have the same highest ranking set, the hand with the second highest ranking set wins. If those sets have the same rank, the hand with the highest ranking kicker card wins. If the kicker cards have the same rank, the hands tie.

A One Pair is a hand that has one set of two cards of the same rank. For example, a hand that has the 2 of Clubs, 2 of Hearts, 3 of Clubs, 4 of Clubs, and 5 of Clubs is a One Pair. When comparing One Pairs, the hand with the highest ranking set wins. If both hands have the same highest ranking set, the hand with the highest kicker card wins. If both hands have the same high kicker card, compare the next highest kicker card and repeat until a winner is found. If both hands have the same kicker cards, the hands tie.

If neither hand has a special combination, the hand with the card with the highest rank wins. If both hands have the same high card, compare the next highest card and repeat until a winner is found. If both hands have the same five cards, ignoring suits, the hands tie.

Input

The first line contains an integer WW, the number of rounds to score. (1W2001 \le W \le 200)

After the first line there are WW pairs of lines, and each line describes one poker hand. A line holds five rank and suit combinations separated by spaces. Single digit ranks (2 through 9) are written as that number. Ten, Jack, Queen, King, and Ace are each written as the capitalized first letter of the word, so they are T, J, Q, K, and A. The suit is also written as the capitalized first letter of its name, so Club is C, Diamond is D, Heart is H, and Spade is S. For example, 2C 4D 8H TD KS is the hand that has the 2 of Clubs, 4 of Diamonds, 8 of Hearts, 10 of Diamonds, and King of Spades. Within each round there are no duplicate cards.

Output

For each pair of hands, print one line containing the hand that wins the round, written exactly as it was given in the input. If the hands tie, print Tie instead.