Know When to Hold Them

Time limit1sMemory limit128 MB

Problem

In poker, each player holds 5 playing cards. At the end of the final round every player reveals their cards, and the player with the most valuable hand wins. Your task is to read the poker hand of each player, rank the hands by their relative worth, and print them from most to least valuable.

Poker is played with a deck of 52 cards divided evenly into 4 suits: hearts (♥), spades (♠), diamonds (♦), and clubs (♣). Each suit has 13 cards, and each card has a rank that determines its worth. From highest to lowest these ranks are: ace (high), king, queen, jack, 10, 9, 8, 7, 6, 5, 4, 3, 2, ace (low). Although each suit has only one ace and it usually ranks as the highest card, in some situations the ace is treated as the lowest card. The suit itself does not affect a card's worth; it is only used to decide whether a hand is a flush or a straight flush (see below).

Every poker hand falls into exactly one of nine categories based on the combination of cards it contains. A higher category always beats a lower category, regardless of the individual card ranks. When two hands share the same category, the individual card ranks break the tie, and each category has its own tie-breaking rule. The order of the cards within a hand does not affect its category. The categories are listed below from most to least valuable.

  1. Straight Flush — All five cards share the same suit and have consecutive ranks. Between two straight flushes, the one with the higher top card wins. The low-ace rule applies here: a hand of 5, 4, 3, 2, ace counts the ace as the lowest card (so the 5 is the highest). Example: A♥ K♥ Q♥ J♥ 10♥ beats K♠ Q♠ J♠ 10♠ 9♠, and 6♦ 5♦ 4♦ 3♦ 2♦ beats 5♣ 4♣ 3♣ 2♣ A♣.
  2. Four of a Kind — Four cards of the same rank plus a fifth card of a different rank. The hand with the higher-ranked four wins; if the fours are equal, the higher fifth card wins. Example: 8♣ 8♥ 8♠ 8♦ Q♣ beats 2♣ 2♥ 2♠ 2♦ A♦, and 4♣ 4♥ 4♠ 4♦ A♥ beats 4♣ 4♥ 4♠ 4♦ J♥.
  3. Full House — Three cards of one rank plus two cards of another rank. The hand with the higher-ranked three wins; if the threes are equal, the higher-ranked pair wins. Example: 4♦ 4♥ 4♣ Q♠ Q♣ beats 3♣ 3♠ 3♥ K♦ K♥, and Q♥ Q♦ Q♠ J♦ J♣ beats Q♥ Q♦ Q♠ 10♥ 10♠.
  4. Flush — All five cards share the same suit but are not a straight. The hand with the higher top card wins; if equal, compare the next-highest card, and so on. Example: Q♦ 8♦ K♦ J♦ 2♦ beats J♠ 10♠ 9♠ 2♠ 8♠, and Q♥ J♥ 9♥ 8♥ 3♥ beats Q♣ J♣ 7♣ 3♣ 2♣.
  5. Straight — All five cards have consecutive ranks but are not all the same suit. The hand with the higher top card wins. The low-ace rule applies: a hand of 5, 4, 3, 2, ace counts the ace as the lowest card. Example: Q♠ J♣ 10♠ 9♥ 8♦ beats 7♥ 6♦ 5♥ 4♠ 3♦, and A♣ K♠ Q♣ J♣ 10♥ beats 5♦ 4♥ 3♣ 2♠ A♥.
  6. Three of a Kind — Three cards of the same rank plus two unmatched cards (kickers) whose ranks differ from each other and from the three. The hand with the higher-ranked three wins; if equal, compare the higher kicker, then the other kicker. Example: 7♥ 7♣ 7♠ J♦ 10♠ beats 4♥ 4♠ 4♦ 7♣ Q♦, and 8♠ 8♥ 8♦ 3♠ 6♦ beats 8♣ 8♦ 8♠ 3♦ 5♥.
  7. Two Pair — Two cards of one rank, two cards of another rank, and a fifth card (kicker) matching neither pair. The hand with the higher pair wins; if the higher pairs tie, compare the lower pairs; if both pairs tie, the higher kicker wins. Example: K♥ K♣ 3♦ 3♣ 2♠ beats J♦ J♥ Q♠ Q♣ K♠, and 8♠ 8♣ 5♦ 5♣ 3♥ beats 8♥ 8♦ 4♠ 4♦ 3♣.
  8. One Pair — One pair of equal-ranked cards plus three kickers whose ranks match neither the pair nor each other. The hand with the higher pair wins; if the pairs tie, compare the highest kicker, then the second, then the third. Example: 4♥ 4♦ 8♥ 10♦ J♠ beats 3♥ 3♠ Q♣ 8♥ 2♦, and 9♠ 9♦ 10♥ J♥ 4♣ beats 9♥ 9♣ J♠ 7♥ 8♦.
  9. High Card — A hand that fits none of the above: every card has a different rank, the cards are not all the same suit, and they are not in sequence. The hand with the higher top card wins; if equal, compare the next-highest, and so on. Example: 8♣ K♠ 3♥ J♦ 2♥ beats 10♥ J♠ 8♣ 7♥ 6♦, and A♠ 3♦ 4♥ Q♥ J♣ beats K♠ 8♣ Q♥ 10♦ 9♥.

Input

The first line contains a single integer N (1 ≤ N ≤ 100), the number of data sets. Each data set begins with a line containing an integer M (1 ≤ M ≤ 100), the number of poker hands in the data set. The next M lines each describe one hand: five cards separated by spaces, where each card is a two-character string.

  • The first character is the card's rank: A, K, Q, J for ace, king, queen, and jack; 0 (the digit zero) for a 10; and 2 through 9 for the matching numeric rank.
  • The second character is the card's suit: H, S, D, C for hearts, spades, diamonds, and clubs.

Within a single hand the same card (same rank and suit) never appears more than once.

Output

For each data set, first print the heading Data Set #k, where k is 1 for the first data set, 2 for the second, and so on. After the heading, print the hands in that data set in order from most to least valuable, one per line. The cards within each hand must be printed in the same order in which they were read. No two hands in a data set have the same worth, so the sorted order is always unique.