class BlackjackDealer
  def initialize(hand:, deck:)
    @hand = hand
    @deck = deck
  end

  def get_score
    score = 0
    eleven_aces = 0

    @hand.each do |card|
      score += calc_card_value(card: card)
      eleven_aces += 1 if card == 'A'
    end

    return 'blackjack' if score == 21

    while keep_drawing?(score: score, eleven_aces: eleven_aces)
      card = @deck.shift
      score += calc_card_value(card: card)
      eleven_aces += 1 if card == 'A'

      if score > 21 && eleven_aces > 0
        score -= 10
        eleven_aces -= 1
      end
    end

    return 'bust' if score > 21
    score
  end

  private

  def keep_drawing?(score:, eleven_aces:)
    score <= 16 || (score == 17 && eleven_aces >= 1)
  end

  def calc_card_value(card:)
    cards_values = {'A' => 11, 'T' => 10, 'J' => 10, 'Q' => 10, 'K' => 10}
    cards_values.fetch(card) {card.to_i}
  end
end

rounds = gets.to_i

rounds.times do
  hand = gets.split
  deck = gets.split
  dealer = BlackjackDealer.new(hand: hand, deck: deck)
  puts dealer.get_score
end