n = gets.to_i
n.times{
    result = "NA"
    snake = gets.chomp
    case snake
    when /^>'=+#=+~/
        left, right = snake.split("#")
        result = "A" if left.count("=") == right.count("=")
    when /^>\^(Q=)+~~$/
        result = "B"
    end
    puts result
}