def is_a?(s)
  l = s.size
  n = (l-4)/2
  return false if l.odd?
  return true if s.last == '~' && s[2,n] == s[-(n+1),n] && s[2,n].all?{|x| x=='='}
  return false
end

def is_b?(s)
  l = s.size
  return false unless s[-2,2].all?{|x| x=='~'}
  s[2..-3].each_slice(2) do |q,e|
    return false if q != 'Q' || e != '='
  end
  return true
end

gets.to_i.times do
  snake = gets.chomp.chars
  if snake.first != '>'
    puts "NA"
    next
  end
  a_or_b = snake[1]
  case a_or_b
  when "'"
    if is_a?(snake)
      puts "A"
      next
    end
  when '^'
    if is_b?(snake)
      puts "B"
      next
    end
  end

  puts "NA"
end