We are given multiple test cases. For each test case, we have the dealer's initial two cards (given as two characters, space separated). Then we have the top eight cards in the pile (after all players finish their plays) as eight characters, space separated.
We need to simulate the dealer's play according to the rules, using cards from the pile (presumably the dealer will draw cards from this pile in order when they need to hit). The rules: The dealer first checks for blackjack (a two-card hand consisting of an Ace and a ten-point card: T, J, Q, K, or maybe 10? In input representation, 'T' stands for ten, J for jack, Q for queen, K for king, and A for ace). So if the dealer's initial two cards form a blackjack, then we output "blackjack" (and the dealer does not draw any more cards). Otherwise, we need to apply the dealer's drawing rules: He continues to hit until he must stand. The rules:
- Hits if the score of the hand is 16 or less.
- Also hits if the score of the hand is 17 and one of aces is counted as 11.
- Stands otherwise.
The score calculation: Aces count as 11 if that does not cause bust, else as 1. However the rule mentions "one of aces is counted as 11" when score is 17. That is: if the current hand has a "soft 17" meaning that the hand's total is 17 but includes an ace counted as 11 (so there is some flexibility), then the dealer must hit. If the hand is "hard 17" (i.e., no ace counted as 11, or sum of all cards with aces counted as 1 gives 17), then stand.
We must simulate correctly. The dealer uses the top eight cards from the pile in order when hitting. The problem states: "the dealer never needs to hit more than eight times as long as he or she plays according to the rule described above." So we won't run out.
We need to output "bust" if the dealer's hand exceeds 21 after any hit (or maybe after the whole play?). Actually, if the dealer busts, we output "bust". If the dealer gets blackjack initially, we output "blackjack". Otherwise, we output the final score (the total after standing, which will be between 17 and 21 inclusive if not bust). Actually could be less than 17? No, because dealer hits until at least 17 (or soft 17 triggers hit). So final score will be >= 17 unless bust? Actually if dealer hits and busts, we output bust. If dealer stands, the hand is not bust and total is >= 17 (hard 17 or greater, or soft 18+ maybe?). But could be 17 if hard; if soft 17 would hit; so after stand total will be >= 17, and either hard 17 or 18-21, or possibly >21 bust.
Thus for each test case, we simulate.
Implementation details:
We parse each test case. The first line: two card characters (maybe separated by space). The second line: eight card characters separated by spaces.
We'll need to compute the value of a card. For numeric cards '2'..'9', value is the integer (2-9). For 'T', 'J', 'Q', 'K', value is 10. For 'A', it's special.
We simulate a hand: list of cards. At any point, we need to compute the best possible total <= 21, using aces as 11 or 1. But the dealer's rules mention "score of the hand" when deciding to hit or stand. How is score defined? Typically in blackjack, the score is the best total that does not exceed 21. If all aces counted as 11 exceed 21, then you adjust some aces to 1. So the dealer's score is the highest total <= 21 (with aces as 11 unless bust). That's standard. The rule says: "An ace counts as 11 or 1 such a way the score gets closer to but does not exceed 21." So indeed we compute the maximal total <= 21. That is the "score". For the hit condition: "Hits if the score of the hand is 16 or less. Also hits if the score of the hand is 17 and one of aces is counted as 11." So we need to compute score and also know if the hand is "soft" (i.e., an ace is counted as 11 in that maximal total). Actually "one of aces is counted as 11" means that the current score is based on counting at least one ace as 11. That is, if the hand is "soft". So we need to determine if the best total uses an ace as 11. If the best total is 17 but it's soft (i.e., there is an ace counted as 11), then hit. Otherwise stand.
One nuance: When the hand has multiple aces, we might count some as 11, some as 1. The condition "one of aces is counted as 11" is equivalent to the hand being soft. Because if any ace is counted as 11 to achieve the best total, then it's soft. Actually if there are multiple aces, it's possible that best total counts exactly one ace as 11 and the rest as 1, or maybe two aces as 11? But if you have two aces, the max total without bust could be: one as 11 and one as 1 gives 12; both as 11 gives 22 bust; so you only count one as 11. So effectively soft means there's at least one ace that can be counted as 11 while total <=21. So the condition is: score <=16 -> hit; score >=18 -> stand; score == 17: hit if soft else stand.
Now we need to simulate: Initially hand has two cards. If it's blackjack, output "blackjack" and stop. Otherwise, then evaluate if the dealer should hit or stand according to the rules. If stand, then output the score. If hit, then draw the next card from the pile (the eight cards are given in order), add to hand, recalc, and repeat. Continue until stand or bust.
If after drawing a card, the hand's best total > 21? Actually the score is defined as the best total not exceeding 21. If it's impossible to have a total <=21 (i.e., even counting all aces as 1, total >21), then the hand is bust. In that case, we output "bust". The rule: "A hand of more than 21 points is called bust, which makes a player automatically lose." So we need to detect bust. In simulation, after each draw, compute the best total. If best total is defined (i.e., there exists a sum <=21), we get a score. But if even the minimal sum (counting all aces as 1) exceeds 21, then it's bust. For example, hand: 10, 5, 7 -> minimal sum = 22, bust. So we can compute: sum with aces as 1, and count of aces. The maximal total without bust is computed by trying to add 10 for each ace (since 11 vs 1 difference is 10) if that doesn't exceed 21. So we can compute: total = sum with aces as 1 + 10 * (number of aces) provided that total <=21; else we reduce number of aces counted as 11 until total <=21. But easier: compute the sum where we count aces as 11 as many as possible without bust. However for bust detection, we can compute minimal sum: all aces as 1. If minimal sum >21, then bust. Otherwise we have a score which is <=21, possibly soft.
Let's define a function to compute score and soft indicator. Input: list of cards (we just need counts of values? but we can compute total sum with aces counted as 1, and number of aces. Then let score = sum_ones + 10 * aces, if that <=21; else we need to reduce: For each ace we subtract 10 until <=21, so score = sum_ones + 10 * k where k is the max integer such that sum_ones + 10k <=21. Since each ace can contribute 10 extra beyond the base 1. So k = min(aces, floor((21 - sum_ones)/10)) if sum_ones <=21? But if sum_ones >21, then bust. So we check: if sum_ones > 21 -> bust. Else compute possible extra = min(aces, (21 - sum_ones)//10). Then score = sum_ones + 10extra. Then soft = (extra > 0) because at least one ace counted as 11.
But careful: Suppose sum_ones = 12, aces = 1. Then (21-12)//10 = 0, so extra=0, score=12, soft=False. That's correct.
Suppose sum_ones = 6, aces=2. (21-6)//10 = 1, extra = min(2,1)=1, score=16, soft=True because one ace counted as 11? Actually both aces counted as 1 base sum = 6? Wait base sum: each ace counted as 1 gives 2? Let's do properly: sum_ones = sum of non-ace cards (2-10, T,J,Q,K count 10) + number_of_aces (since each ace as 1). That's the minimal total. Then we can add 10 for each ace that we upgrade to 11, provided total doesn't exceed 21. So if we have two aces, sum_ones = non_ace_sum + 2. Then extra = min(2, (21 - sum_ones)//10). Example: non_ace_sum = 4 (like a 4), aces=2: sum_ones = 4+2=6, (21-6)//10=1, extra=1, score=6+10=16, soft=True because we used one ace as 11, the other as 1. So yes.
Alternatively, we could compute by iterating.
Now, decision: if bust -> dealer would bust and we stop and output bust. If not bust, then check: if score <=16 -> hit; if score ==17 and soft -> hit; else stand.
But we also must handle the initial blackjack check: A blackjack is exactly two cards: one Ace and one ten-point card (T, J, Q, K, or maybe also 10? But 10 is represented as T). So we check if hand size 2 and the set contains 'A' and one of {'T','J','Q','K'} (or numeric 10? No, numeric 10 is not a card; cards are 2-9, T for ten, J, Q, K). So it's Ace + Ten-valued card. Note: It could be Ace + 10? That's ten-valued, but there is no '10' card, it's 'T'. So yes. Also order doesn't matter. So we check if 'A' in hand and other card in { 'T','J','Q','K' } or if both cards are such? But blackjack requires exactly Ace and ten-point card. So if both are Ace and ten? Actually two Aces is not blackjack; Ace + Ace is not blackjack. So we need exactly one Ace and one card worth 10. So if hand[0]=='A' and hand[1] in ten_cards OR hand[1]=='A' and hand[0] in ten_cards.
If blackjack, we output "blackjack". That's it.
Otherwise we proceed.
We need to draw from the pile. The pile has exactly eight cards. We'll use an index and draw sequentially when needed. According to the statement, the dealer never needs to hit more than eight times, so we are safe.
Now, let's test on given examples:
Example input:
4
5 4
9 2 8 3 7 4 6 5
A J
K Q J T 9 8 7 6
T 4
7 J A 6 Q T K 7
2 2
2 3 4 K 2 3 4 K
Output:
18
blackjack
21
bust
Let's simulate.
Case 1: initial 5 and 4. Score = 9 (<=16) so hit. Use first pile card: 9 -> hand 5,4,9 => total = 18 (hard). Score=18, stand. Output 18. matches.
Case 2: initial A and J. That's blackjack, output blackjack.
Case 3: initial T and 4. Score = 10+4=14 (hard) => hit. First pile: 7 -> hand T,4,7 => total = 10+4+7=21 (hard). Score=21 => stand? Actually rules: score=21 > 17? Actually 21 is >16, and not soft 17, so stand. Output 21. Matches.
Wait after hitting, we have 21, but we didn't check for blackjack again because already two cards? Not relevant.
Case 4: initial 2 and 2. Score=4 <=16, hit. Draw 2 from pile: first card 2. Now hand: 2,2,2 => total=6, <=16, hit. Draw 3: hand: 2,2,2,3 => total=9? Actually sum=2+2+2+3=9. Hit. Draw 4: hand ... sum=13. Hit. Draw K: (value 10) sum=23. But need to compute with aces? None. Minimal sum = 2+2+2+3+4+10 = 23 >21 => bust. So output bust. matches.
Thus logic seems correct.
Edge Cases:
-
Soft 17: e.g., Ace and 6 => sum_ones = 1+6=7, aces=1, extra = min(1, (21-7)//10 =1) so score=17, soft=True => hit.
-
Ace and 5 and Ace? For multiple aces: A, A, 5 => sum_ones = 1+1+5=7, aces=2, extra = min(2, (21-7)//10=1) => score=17, soft=True? Actually extra=1, score=17, soft=True (since at least one ace counted as 11). But should dealer hit on soft 17? Yes. So we would hit.
-
After drawing more cards, aces might become hard if no longer counted as 11. For example, starting with Ace and 6 (soft 17) -> hit, draw a 5. Now hand: A,6,5. sum_ones = 1+6+5=12, aces=1, extra = min(1, (21-12)//10=0) => score=12, soft=False, then must hit again because score <=16? Actually score=12 <=16, hit. So okay.
We must implement careful: After each draw, we recompute score and soft. Then check bust (if minimal sum >21). If bust, output bust. If not bust, then apply rules: if score <=16 or (score == 17 and soft) then hit again; else stand, output score.
Note: When score==17 and soft, we hit. When score==17 and hard, stand.
Potential pitfalls: In the rule "Also hits if the score of the hand is 17 and one of aces is counted as 11." So we need to know if any ace is counted as 11 in the current best score. That's exactly soft flag.
Now, we have to be mindful of the blackjack check: It is only at the very beginning, before any hits. Even if after hitting the dealer ends up with two cards that would be blackjack? Not considered blackjack because it's not the initial deal.
Now, regarding input format: The first line of input: N. Then for each case: a line with two card characters separated by space. Then a line with eight card characters separated by space. There may be extra whitespace? Usually just as described.
We'll read lines and split.
Implementation steps in code:
- Read integer N.
- For each test case:
- Read line for initial hand; split into tokens (two tokens). Possibly there could be trailing spaces, but split handles.
- cards = [token1, token2]
- Read line for pile; split into tokens (should be 8 tokens). Convert to list.
- Define mapping from card to value for non-ace: if card in '23456789' -> int(card). For 'T','J','Q','K' -> 10. For 'A' -> we'll treat specially: for sum_ones we add 1, and count aces.
- First, check blackjack: if (cards[0]=='A' and cards[1] in ten_cards) or (cards[1]=='A' and cards[0] in ten_cards): print "blackjack" and continue to next case.
- Else:
- Create hand list = cards.
- Set index for pile (p = 0).
- While True:
- Compute sum_ones = sum of values (with aces counted as 1) and count_aces.
We'll loop over hand: for each card: if card == 'A': sum_ones += 1; count_aces += 1. else: add value.
- If sum_ones > 21: then bust. Print "bust" and break.
- Compute extra = min(count_aces, (21 - sum_ones)//10)
- score = sum_ones + 10*extra
- soft = extra > 0
- Now evaluate:
if score <= 16 or (score == 17 and soft):
need to hit
if p >= 8: but problem guarantees not more than 8 hits needed, so p will be <8.
hand.append(pile[p])
p += 1
continue loop
else:
stand
print(score)
break
But note: When we compute score, if sum_ones <=21, we get a score between sum_ones and sum_ones+10*count_aces but <=21. However, is it possible that even though sum_ones <=21, there is no combination that yields a score exactly that value? The method of adding 10 per ace until just under 21 is correct because aces are independent. For example, sum_ones=6, aces=2: the possible totals: 6, 16, 26 (bust). So 16 is max <=21. Our method: extra = min(2, (21-6)//10 =1) => score=16. Good.
But we must consider that the dealer's "score" is defined as the best total (maximum <=21). So that matches.
Now, after hitting, we need to recompute sum_ones and count_aces again. This is fine.
Potential nuance: In the condition for hitting on soft 17, it says "Also hits if the score of the hand is 17 and one of aces is counted as 11." That means if the best total is 17 and it includes an ace counted as 11. Our soft flag indicates exactly that. So correct.
Now, test again with an example: A,6 (soft 17) -> hit. Suppose draw A: hand A,6,A. sum_ones = 1+6+1=8, aces=2, (21-8)//10=1, extra=1, score=18, soft=True? Actually score=8+10=18, soft because extra>0. So now score=18 > 17, stand. So output 18. That seems correct.
Another: A,5,A? Actually that's soft 17? Let's compute: A,5,A: sum_ones=1+5+1=7, aces=2, extra = min(2,(21-7)//10=1)=1, score=17, soft=True, still hit. So if we have A,5,A we have soft 17, hit. Good.
Now, is there any scenario where the dealer could have score=17 but soft=False yet there is an ace in hand? Yes, if ace is counted as 1 because if it were counted as 11 it would bust. For example: A,6,10 (21? Actually A,6,10: sum_ones=1+6+10=17, aces=1, (21-17)//10=0, extra=0, score=17, soft=False. So it's hard 17, stand. That's correct per rule: if the hand is 17 and not soft, stand.
Now, let's test with a tricky scenario: initial hand: A, A (two aces). That is not blackjack. Score: sum_ones=2, aces=2, (21-2)//10=1, extra=1, score=12, soft=True? Actually score=12 (with one ace as 11, other as 1). Since score <=16, hit. So dealer will draw. That's typical.
Now, we need to consider that the blackjack check should be before any hitting. So done.
Now, final output per test case as specified.
Now, complexity trivial.
Potential pitfalls: Input reading: after reading N, we need to read lines. Possibly there might be blank lines. But the problem statement likely uses exactly given format. We can read using sys.stdin.read().split() to get all tokens, but careful with order. Since each test case has exactly 10 tokens (2 initial + 8 pile), we could just read all tokens and parse. But easier: read line by line. We'll use input() in Python.
But when using input(), we must be careful that after reading N, we need to strip newline. I'll do something like:
import sys
def main():
import sys
data = sys.stdin.read().strip().split()
# First token N
# Then each test case: 2 cards + 8 cards = 10 cards
# So total tokens = 1 + N*10
it = iter(data)
N = int(next(it))
out_lines = []
for _ in range(N):
hand = [next(it), next(it)]
pile = [next(it) for __ in range(8)]
# process...
print("\n".join(out_lines))
That avoids dealing with lines and possible blank lines. The input says characters delimited by a single space, so tokens will be separated by spaces and newlines; using split is fine.
Now implement processing function.
Pseudo:
ten_cards = set(['T','J','Q','K'])
def is_blackjack(c1, c2):
return (c1 == 'A' and c2 in ten_cards) or (c2 == 'A' and c1 in ten_cards)
def card_value(c):
if c.isdigit(): return int(c)
elif c in ten_cards: return 10
elif c == 'A': return 1 # but we'll count separately; but we can treat as 1 for sum_ones
else: # shouldn't happen
return 0
But careful: cards '2'..'9' are digits; 'T','J','Q','K' are not digits. So we can test:
if c in 'TJQK': return 10
elif c == 'A': return 1 (or handle separately)
else: return int(c)
Now compute:
def evaluate(hand):
sum_ones = 0
aces = 0
for card in hand:
if card == 'A':
sum_ones += 1
aces += 1
elif card in 'TJQK':
sum_ones += 10
else:
sum_ones += int(card)
if sum_ones > 21:
return 'bust', None, None # indicates bust
extra = min(aces, (21 - sum_ones)//10)
score = sum_ones + 10*extra
soft = extra > 0
return 'ok', score, soft
But we need to break out on bust.
In main loop:
if is_blackjack(hand[0], hand[1]):
out_lines.append("blackjack")
continue
p = 0
while True:
status, score, soft = evaluate(hand)
if status == 'bust':
out_lines.append("bust")
break
# else
if score <= 16 or (score == 17 and soft):
# hit
hand.append(pile[p])
p += 1
else:
out_lines.append(str(score))
break
Note: The dealer may bust after drawing a card, and we output "bust". That matches expected.
Now test with examples.
Let's simulate with code mentally.
Example 1: hand = ['5','4']; is_blackjack? No. Evaluate: sum_ones=5+4=9, aces=0, extra=0, score=9, soft=False. score<=16 -> hit. pile[0]='9'. hand becomes ['5','4','9']. Evaluate: sum_ones=5+4+9=18, aces=0, score=18, soft=False. Now score>16, not soft 17 -> stand. Output 18.
Example 2: hand ['A','J']; is_blackjack? Yes (A and J). Output blackjack.
Example 3: hand ['T','4'] -> sum_ones=10+4=14, no aces -> score 14 <=16 -> hit. pile[0]='7' -> hand ['T','4','7'] sum_ones=21 -> score=21 -> stand, output 21.
Example 4: hand ['2','2'] sum_ones=4 -> hit. pile[0]='2' -> hand ['2','2','2'] sum_ones=6 -> hit. pile[1]='3' -> hand add '3' sum_ones=9 -> hit. pile[2]='4' -> sum_ones=13 -> hit. pile[3]='K' (10) -> sum_ones=23 >21 -> bust. Output bust.
Works.
Now consider a test for soft 17: Suppose hand ['A','6'] (soft 17). is_blackjack? No (6 not ten). Evaluate: sum_ones=1+6=7, aces=1, (21-7)//10=1, extra=1, score=17, soft=True. Since score==17 and soft -> hit. We need to draw from pile. Suppose pile[0]='2' -> hand ['A','6','2'] -> sum_ones=1+6+2=9, aces=1, (21-9)//10=1, extra=1, score=19? Wait 9+10=19, soft=True. Now score 19 > 16 -> stand, output 19. Good.
Now consider a scenario where after hit, still soft 17: e.g., hand ['A','6','A']? Let's compute: sum_ones=1+6+1=8, aces=2, (21-8)//10=1, extra=1, score=18, soft=True? Actually 8+10=18, so not 17. But if we have hand like ['A','5','A']? sum_ones=1+5+1=7, aces=2, extra=1, score=17, soft=True -> still hit. So dealer would hit again. That's correct.
Now, one nuance: In the rule, "Hits if the score of the hand is 16 or less. Also hits if the score of the hand is 17 and one of aces is counted as 11." So if score is less than or equal 16, hit regardless of soft/hard. So our condition is correct.
Now, about initial two cards: Could there be a scenario where initial hand is blackjack but the dealer also has to check something else? According to rules: "In case the dealer gets a blackjack, the dealer wins and the game ends immediately." So we output blackjack and stop. So correct.
Now, we need to ensure we output exactly as required: "blackjack" all lowercase, "bust" all lowercase, and score as integer with no extra spaces.
Now, about the pile: It contains eight cards. But what if the dealer stands before using all eight? That's fine. The problem statement says the top eight cards are given; we just use as needed.
Now, confirm: The dealer's initial hand is also from the deck? But not relevant. We are just given those cards.
Now, think about the case where the initial hand is bust? That cannot happen with two cards: maximum two cards: two aces sum_ones = 2 (<=21), or two tens =20, etc. So no bust initially.
Now, we can implement.
Now let's consider one more tricky case: Hand with multiple aces and drawing cards may cause bust detection correctly.
E.g., hand: ['A','A','A'] (three aces). sum_ones=3, aces=3, (21-3)//10=1, extra=1, score=13, soft=True. So hit. Draw a ten: hand ['A','A','A','T'] sum_ones=1+1+1+10=13, aces=3, (21-13)//10=0, extra=0, score=13, soft=False. (Actually base sum =13? Let's compute: 1+1+1+10=13, aces=3? Wait, after adding ten, we have three aces counted as 1 each, ten=10 => sum_ones=13. Then extra = min(3, (21-13)//10=0) =0, so score=13, soft=False. So still <=16, hit. Draw another ten: sum_ones=23 (1+1+1+10+10) = 23 >21 -> bust. So output bust.
That seems correct.
Now, we must ensure we treat 'T' as ten. In card_value, we should handle 'T' as 10.
Edge: In the input, 'T' stands for ten. It is not a digit, so we must not try int('T').
Now, what about reading input: Using sys.stdin.read().split() will split on whitespace. The first token is N, then the cards. It's safe.
Now, test with more cases:
Case: hand: A K (blackjack). Output blackjack.
Case: hand: A T (also blackjack). Output blackjack.
Now, think about the scenario: If initial hand is not blackjack, but we need to check blackjack after some hits? No.
Now, code.
But there is a nuance: The problem statement says: "In case the dealer gets a blackjack, the dealer wins and the game ends immediately." So if initial two cards form blackjack, we output blackjack. However, there is also a scenario: what if the dealer has blackjack but also according to rules would hit? No, because game ends immediately, so we do not hit.
Thus our approach is correct.
Now, finalize.
Let's also ensure we handle case where pile cards might be used fully? Problem says never more than 8 hits needed, but if by some reason dealer needs more than 8 hits (shouldn't happen), but we would run out of pile; we could error. But we trust input.
Now, code:
def evaluate_hand(hand):
sum_ones = 0
aces = 0
for card in hand:
if card == 'A':
sum_ones += 1
aces += 1
elif card in 'TJQK':
sum_ones += 10
else:
sum_ones += int(card)
if sum_ones > 21:
return None, None, True # bust
extra = min(aces, (21 - sum_ones) // 10)
score = sum_ones + 10 * extra
soft = extra > 0
return score, soft, False # not bust
Then loop.
Potential issue: In the case of sum_ones <=21 but after adding 10s we may get exactly 21, but that's fine.
Now, one subtlety: The rule "Hits if the score of the hand is 16 or less." So if score is 16 and it's soft? The rule doesn't differentiate; hit anyway. So our condition covers that.
Now, what if score is 17 and soft? hit.
Now, what if score is 17 and hard? stand.
Now, what if score is 18 or more? stand.
Now, check: if score is 17 and soft, we hit; but what if after hitting the hand becomes hard 17? We'll recalc.
Now, confirm with standard blackjack dealer rules: Typically dealer hits on soft 17. Yes.
Now, implement.
Now, produce final answer.
But we must also consider that the dealer's initial hand might be considered blackjack only if it's exactly two cards with one Ace and one ten-valued card. However, there is nuance: if the dealer uses multiple decks, blackjack can be with Ace and any ten-valued card. So our check is correct.
Now, test with example given: good.
Let's also test with a custom case:
Input:
1
A 6
2 3 4 5 6 7 8 9
We have initial soft 17, so hit. Draw 2 -> hand A,6,2: sum_ones=1+6+2=9, aces=1, extra=1 -> score=19? Wait 9+10=19, soft=True -> stand with 19. So output 19. Should be correct.
If we had drawn 5: then hand A,6,5 sum_ones=12, aces=1, extra=0 -> score=12 <=16 -> hit again. Draw 3 -> sum_ones=15, aces=1, extra= min(1,(21-15)//10=0)=0 -> score=15 <=16 -> hit. Draw 4 -> sum_ones=19, aces=1, extra=0 -> score=19 -> stand. So output 19.
Now, what about a case where after drawing, we get exactly 21 with soft? For instance, hand A,5 (soft 16? Actually A+5 is soft 16? Let's compute: sum_ones=6, aces=1, extra=1 -> score=16 soft True. Because 6+10=16. So score=16 <=16 -> hit. Draw 5: hand A,5,5 -> sum_ones=1+5+5=11, aces=1, extra=1 -> score=21, soft=True? Actually 11+10=21, soft=True. Score=21 >16 -> stand, output 21. That's fine.
Now, about output for bust: the word "bust" all lowercase.
Now, confirm that we output each result on separate line.
Now, we should also consider that maybe the dealer could have blackjack after drawing? That is not a rule; only initial two-card blackjack counts. So we don't check later.
Now, final code.
Let's also consider reading input with potential blank lines: using sys.stdin.read().split() is robust.
Now, do we need to convert to uppercase? Input is uppercase letters, so fine.
Now, write code.
Time complexity: O(N * (hand size up to 10)) trivial.
Now, answer.
But before finalizing, let's double-check the rule for "score" definition. The statement: "The score is the total points of the cards in the hand. Cards from 2 to 10 are worth their face value. Face cards (jack, queen and king) are worth 10 points. An ace counts as 11 or 1 such a way the score gets closer to but does not exceed 21." So that's standard. So our evaluation is correct.
Now, consider a scenario: hand: A, A, 9. sum_ones = 1+1+9 = 11, aces=2, (21-11)//10=1, extra=1, score=21, soft=True? Actually score=11+10=21, and extra>0, so soft. According to rules, score=21 >16, stand. That's fine.
Now, consider a scenario: hand: A, A, 8. sum_ones=10, aces=2, (21-10)//10=1, extra=1, score=20, stand. Good.
Now, one nuance: In some blackjack variants, the dealer's "soft 17" rule may be defined as "if the dealer's hand totals 17 with an ace counted as 11" (i.e., the hand is soft 17) then the dealer hits. But sometimes they also count multiple aces. But our method with soft flag works.
Now, let's also test a scenario: hand: A, 6, A (soft 17? Actually sum_ones=8, aces=2, extra=1 => score=18? Wait compute: base sum = 1+6+1=8, extra= min(2, (21-8)//10=1) =1, score=18. So not 17. So dealer would stand. Good.
Now, one more: hand: A, 5, A (soft 17) => base sum=7, extra=1 => score=17, soft=True -> hit.
Now, all good.
Now, corner: after hitting, we might get a hand where sum_ones <=21 but after adding possible 10s we get a score that is <=21 but still soft? That's fine.
Now, note: In the evaluation, we compute extra as min(aces, (21 - sum_ones) // 10). But what if sum_ones is exactly 21? Then (21-21)//10 = 0, extra=0, score=21, soft=False. But if there is an ace, the ace would be counted as 1, because counting it as 11 would bust. So it's hard 21. That's correct. So no soft flag.
But is there any scenario where a hand with ace could be considered soft even when total is 21? Example: A, T -> blackjack (soft? Actually blackjack is an Ace and a ten, total 21. That's considered soft? Usually it's called a "natural" but it's also soft in the sense that ace is counted as 11. However, in the dealer's rules, blackjack ends the game, so we don't evaluate further. For other hands, like A,6,4: sum_ones=1+6+4=11, aces=1, extra=1 => score=21, soft=True? Actually 11+10=21, so extra>0 -> soft True. So there are soft 21 hands. According to the rules, if the score is 21, dealer stands regardless of softness, because the condition "hits if score is 16 or less; also hits if score is 17 and soft". Score 21 is not <=16, and not 17, so stand. So it's fine.
Now, verify that our condition uses "score <= 16 or (score == 17 and soft)". So for score 21, it's false, so stand.
Now, implement.
Potential optimization: Not needed.
Now, test with all sample input.
We'll write the code.
One final thought: The input may have N=0? Problem says positive integer, so at least 1.
Now, code.
But there is one more nuance: The eight cards are the top eight cards in the pile after all players finish their plays. The dealer uses them when hitting. But what about the initial dealer hand? The initial two cards are also from the deck but they are not part of these eight. So we only use the eight when needed.
Now, think about order: We draw cards from the pile in the order given (the first card listed is the topmost). So we need to use them sequentially.
Now, implement.
Potential bug: In reading tokens, after reading N, the tokens might be arranged as N, then for each case: two cards, then eight cards. There are no extra lines. So reading all tokens and iterating works.
But caution: The input might have multiple spaces or newlines, but split handles.
Now, we also might need to handle the case where there are extra blank lines at the end; reading all tokens and ignoring blanks is fine.
Now, coding.
We'll define ten_cards = set(['T', 'J', 'Q', 'K']).
Now, function to check blackjack.
Now, function to evaluate.
Now, loop.
Now, add output lines.
Now, final code.
I'll write in Python 3.
Now, test sample:
Input:
4
5 4
9 2 8 3 7 4 6 5
A J
K Q J T 9 8 7 6
T 4
7 J A 6 Q T K 7
2 2
2 3 4 K 2 3 4 K
Our program should output:
18
blackjack
21
bust
Let's simulate with code.
Case 1: hand = ['5','4']; blackjack? No. Evaluate: sum_ones=9, score=9 -> hit. pile[0]='9' -> hand ['5','4','9'] -> eval sum_ones=18 -> score=18 -> not hit -> output 18.
Case 2: hand ['A','J']; blackjack true -> output blackjack.
Case 3: hand ['T','4'] -> sum_ones=14 -> hit. pile[0]='7' -> hand ['T','4','7'] sum_ones=21 -> score=21 -> output 21.
Case 4: hand ['2','2'] -> sum_ones=4 -> hit. pile[0]='2' -> hand ['2','2','2'] sum_ones=6 -> hit. pile[1]='3' -> hand ['2','2','2','3'] sum_ones=9 -> hit. pile[2]='4' -> sum_ones=13 -> hit. pile[3]='K' -> sum_ones=23 >21 -> bust. Output bust.
Works.
Now, I think we are done.
But I want to consider a test where the dealer gets a blackjack but also we have pile? The output should be blackjack, ignoring pile. So fine.
Now, ensure we break out of loop for blackjack.
Now, one more: In the rules: "In case the dealer gets a blackjack, the dealer wins and the game ends immediately." So we output blackjack. No further drawing.
Now, final code.
I'll also add if name == "main": main().
Now, we should produce the final answer. ```python
import sys
TEN_CARDS = set('TJQK')
def is_blackjack(c1, c2):
"""Return True if the two cards form a blackjack (A + ten‑value)."""
return (c1 == 'A' and c2 in TEN_CARDS) or (c2 == 'A' and c1 in TEN_CARDS)
def evaluate(hand):
"""
Compute the dealer's current score and whether the hand is soft.
If the hand is bust, return (None, None, True).
Otherwise return (score, is_soft, False).
"""
sum_ones = 0 # aces counted as 1
aces = 0
for card in hand:
if card == 'A':
sum_ones += 1
aces += 1
elif card in TEN_CARDS:
sum_ones += 10
else: # digit 2..9
sum_ones += int(card)
# Bust if even with all aces as 1 we exceed 21
if sum_ones > 21:
return None, None, True
# How many aces can be promoted to 11 without busting?
extra = min(aces, (21 - sum_ones) // 10)
score = sum_ones + 10 * extra
soft = extra > 0
return score, soft, False
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
N = int(next(it))
out_lines = []
for _ in range(N):
# dealer's initial two cards
c1 = next(it)
c2 = next(it)
# next eight cards from the pile
pile = [next(it) for _ in range(8)]
if is_blackjack(c1, c2):
out_lines.append("blackjack")
continue
hand = [c1, c2]
idx = 0 # index in pile
while True:
score, soft, bust = evaluate(hand)
if bust:
out_lines.append("bust")
break
# dealer's decision
if score <= 16 or (score == 17 and soft):
# hit
hand.append(pile[idx])
idx += 1
else:
# stand
out_lines.append(str(score))
break
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()