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

  def get_score
    @hand.each do |card|
      @score += calc_card_value(card: card)
      @aces += 1 if card == 'A'
      adjust_score
    end

    return 'blackjack' if @score == 21

    while keep_drawing? && @deck.any?
      card = @deck.shift
      @score += calc_card_value(card: card)
      @aces += 1 if card == 'A'
      adjust_score
    end

    return 'bust' if @score > 21
    @score
  end

  private

  def keep_drawing?
    @score <= 16 || (@score == 17 && @aces >= 1)
  end

  def adjust_score
    while @score > 21 && @aces > 0
      @score -= 10
      @aces -= 1
    end
  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