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:
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.
The first line contains an integer $n$, the number of test cases. Each test case is given as follows:
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.For each test case, print a single line containing one integer: the maximum total profit obtainable by playing optimally.