Magic Trick

Interview

Time limit1sMemory limit128 MB

Summary
Given four cards in the order shown, decode the hidden fifth card using the suit of the first card and the permutation of the last three.
Level

Easy3 of 10

Topics
Implementation, Sorting, Math, Brute force
Solved
No attempts yet

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<SH < C < D < S (Hearts, Clubs, Diamonds, Spades).
  • Face-value order: 1<2<⋯<9<T<J<Q<K1 < 2 < \dots < 9 < T < J < Q < K, where TT, JJ, QQ, KK mean Ten, Jack, Queen, and King. (The ace is written as 11.)

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 ss 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 ss, hide the card xx whose face value is at most six positions ahead of another same-suit card yy in the cyclic value order 1<2<⋯<T<J<Q<K<1<2<…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 xx, pick the one with the smallest face value.
  • Show yy 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 yy's.
  • The three remaining cards encode the exact gap, a number from 1 to 6. Sorting them as z1<z2<z3z_1 < z_2 < z_3, the order in which they are shown means:
    • z1,z2,z3z_1, z_2, z_3 means 1
    • z1,z3,z2z_1, z_3, z_2 means 2
    • z2,z1,z3z_2, z_1, z_3 means 3
    • z2,z3,z1z_2, z_3, z_1 means 4
    • z3,z1,z2z_3, z_1, z_2 means 5
    • z3,z2,z1z_3, z_2, z_1 means 6

The magician adds this number to yy's face value, wrapping around after KK back to 11, to obtain the hidden card's face value; the hidden card's suit is the same as yy's.

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

Input

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

Output

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

Examples1

  1. Example 1

    Input
    2
    7H 8S 8C JD
    TC 2D 1S 5H
    
    Expected output
    QH
    1C