Magic Trick

No attempts yetTime limit1sMemory limit128 MB

Problem

A magician invented a new card trick and presented it at a prestigious magicians' conference, where it won the "Best Magic Award". The trick needs three people: the magician, a spectator, and an assistant.

The spectator shuffles a standard 52-card deck and picks 5 cards at random. These 5 cards are handed to the assistant, and the magician does not see them. The assistant looks at the cards and shows four of the five to the magician, one by one. After seeing those four cards, the magician names the hidden fifth card.

The trick works because, for any 5 cards, the assistant can always pick 4 of them and use the order in which they are shown to encode which card is hidden. The encoding relies on a fixed total order on the cards: first by suit, then by face value.

  • Suit order: $H < C < D < S$ (Hearts, Clubs, Diamonds, Spades).
  • Face-value order: $1 < 2 < \dots < 9 < T < J < Q < K$, where $T$, $J$, $Q$, $K$ mean Ten, Jack, Queen, and King. (The ace is written as $1$.)

Each card is written as two characters: its face value followed by its suit letter, for example QH (Queen of Hearts) or 1C (Ace of Clubs).

As an example, suppose the spectator chose JD, 8S, 7H, 8C, QH. The assistant's strategy is:

  • Find a suit $s$ that appears at least twice among the five cards (Hearts here). If several suits appear at least twice, take the one that is lowest in the suit order.
  • Among the cards of suit $s$, hide the card $x$ whose face value is at most six positions ahead of another same-suit card $y$ in the cyclic value order $1 < 2 < \dots < T < J < Q < K < 1 < 2 < \dots$. This is always possible because a suit has only thirteen values (here the assistant hides QH). If more than one card qualifies as $x$, pick the one with the smallest face value.
  • Show $y$ to the magician first. The magician then knows the hidden card's suit and knows that its face value is at most six positions ahead of $y$'s.
  • The three remaining cards encode the exact gap, a number from 1 to 6. Sorting them as $z_1 < z_2 < z_3$, the order in which they are shown means:
    • $z_1, z_2, z_3$ means 1
    • $z_1, z_3, z_2$ means 2
    • $z_2, z_1, z_3$ means 3
    • $z_2, z_3, z_1$ means 4
    • $z_3, z_1, z_2$ means 5
    • $z_3, z_2, z_1$ means 6

The magician adds this number to $y$'s face value, wrapping around after $K$ back to $1$, to obtain the hidden card's face value; the hidden card's suit is the same as $y$'s.

Given the four cards shown by the assistant, in the order shown (so the first card is $y$), write a program that determines the hidden card.

Input

The first line contains an integer $N$, the number of test cases ($1 \le N \le 10000$). Each of the next $N$ lines contains one test case: the four cards, separated by single spaces, in the order the assistant presented them (so the first card is $y$).

Output

For each test case, print one line containing the hidden card.