Video Poker

Time limit1sMemory limit128 MB

Problem

Video poker is a slot-machine variant of poker based on five-card draw. The player is dealt a hand of five cards drawn at random from a standard 52-card deck. The player may then discard any number of these cards (from 0 to 5, inclusive) and replace each discarded card with a new card drawn at random from the 47 cards that remain in the deck. The resulting five-card hand is evaluated and rewarded according to a fixed payout table. A common payout table is:

handpayout
one pair1
two pair2
three of a kind3
straight4
flush5
full house10
four of a kind25
straight flush100
royal flush250

Any hand that does not match a row of the table (a plain high-card hand) pays $0$. Knowing the payout table, for a given starting hand you decide which cards to discard so as to maximize the expected reward. Your task is to compute this maximal expected reward.

Standard poker hand rankings apply. A card is written as a two-character token Xs, where X is the rank (2-9, T, J, Q, K, A) and s is the suit (c, d, h, s). An ace ranks high, except that it may also be the lowest card of the straight A 2 3 4 5. A royal flush is the straight flush T J Q K A in one suit.

Input

The first line contains one positive integer: the number of test cases (at most $100$). Each test case is given as follows:

  • One line with nine integers $x_i$ ($0 \le x_i \le 1000$), the payouts in increasing order for one pair, two pair, three of a kind, straight, flush, full house, four of a kind, straight flush, and royal flush.
  • One line with one integer $n$ ($1 \le n \le 10$): the number of starting hands.
  • $n$ lines, each describing one starting hand as five space-separated card tokens.

Output

For each starting hand, print on its own line the maximal expected reward as an exact reduced fraction p/q, where $q \ge 1$ and $\gcd(p, q) = 1$; an integer value $v$ is printed as v/1.

The expected reward is always rational: once the set of kept cards is fixed, the reward is the sum of the rewards over all equally likely draws divided by the number of such draws, so the maximum over all discard choices has an exact value that is reported as the fraction p/q.