Blackjack

No attempts yetTime limit1sMemory limit128 MB

Problem

You play blackjack against the dealer with a standard deck. The deck contains the cards 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, and ace; suits do not affect the game in any way. Cards 2 through 10 are worth their face value, jack, queen, and king are each worth 10, and each ace may count as either 1 or 11.

For a hand, its low value is the sum of all of its cards counting every ace as 1. Its high value is the largest sum that is at most 21, counting each ace as 1 or 11. (Two aces counted as 11 already reach 22, so at most one ace can be counted as 11.)

This is a simplified variant: there is no splitting, doubling, insurance, or natural blackjack bonus.

Your goal is to maximize your total profit (equivalently, to minimize your losses). A single hand is played as follows:

  • You place a bet of $x$ euros, where the minimum bet is $p$ and the maximum bet is $q$, so $p \le x \le q$.
  • You receive one card, then the dealer receives one card; then you receive a second card, and finally the dealer receives a second card.
  • While your low value is below 22, you may either Hit (take one more card from the deck) or Stand (take no more cards).
  • If your low value ever exceeds 21, you lose your bet and the hand ends immediately.
  • The dealer then plays: while the dealer's high value is below 17, the dealer takes another card.
  • If the dealer's low value exceeds 21 (the dealer busts), you receive $2x$ euros, for a profit of $x$.
  • If the dealer's high value is greater than your high value, you lose your bet.
  • If the dealer's high value equals your high value, your bet is returned (no gain, no loss).
  • If the dealer's high value is less than your high value, you receive $2x$ euros, for a profit of $x$.
  • After the hand, all used cards are discarded and never return to the deck.

If at any moment the deck is empty and either you choose to take a card or the dealer must take a card, the current hand is cancelled and your bet is returned.

The exact order of the remaining cards in the deck is known to you in advance. Cards are always dealt from the top of the deck in this fixed order, and you may play consecutive hands, stopping whenever you like (you may even stop before playing any hand). Determine the maximum total profit you can obtain by playing optimally.

Input

The first line contains an integer $n$, the number of test cases. Each test case is given as follows:

  • One line with three integers $c$, $p$, and $q$ ($0 \le c \le 10000$, $0 \le p \le q \le 100$): the number of cards left in the deck, the minimum bet, and the maximum bet.
  • The next $\lceil c/60 \rceil$ lines list the $c$ cards in order, using the characters 2-9, T, J, Q, K, A, where T denotes the 10 card. The first character of the first line is the card dealt first. Each of these lines contains exactly 60 characters, except possibly the last, which may contain fewer.

Output

For each test case, print a single line containing one integer: the maximum total profit obtainable by playing optimally.