Video Poker

Time limit1sMemory limit128 MB

Summary
Given a poker payout table and a five-card hand, compute the exact fraction expected value of the best discard strategy over all 32 subsets of held cards.
Level

Medium6 of 10

Topics
Brute force, Combinatorics, Simulation, Math
Solved
No attempts yet

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 00. 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 100100). Each test case is given as follows:

  • One line with nine integers xix_i (0≤xi≤10000 \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 nn (1≤n≤101 \le n \le 10): the number of starting hands.
  • nn 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≥1q \ge 1 and gcd⁡(p,q)=1\gcd(p, q) = 1; an integer value vv 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.

Examples3

  1. Example 1

    Input
    1
    1 2 3 4 5 10 25 100 250
    5
    Ah Ac Ad As 2s
    Ks Qs Js Ts 2h
    Ks Qs 2d 2h 3s
    2d 4h 5d 3c 9c
    2h 3h 6d 8h Tc
    
    Expected output
    25/1
    421/47
    1672/1081
    44/47
    117866/178365
    
  2. Example 2

    Input
    1
    1 2 3 4 5 10 25 100 250
    1
    Ts Js Qs Ks As
    
    Expected output
    250/1
    
  3. Example 3

    Input
    1
    1 2 3 4 5 10 25 100 250
    1
    Ah Kh Qh Jh 3c
    
    Expected output
    314/47