require 'pp'

$stdin.gets.chomp.to_i.times do
  line = $stdin.gets.chomp
  ans = false

  if (m = line.match(/^>'(=+)#(=+)~$/)) then
    if m[1].length == m[2].length then
      puts 'A'
      ans = true
    end
  end

  if (m = line.match(/^>\^(.*?)~~$/)) then
    if (m[1].length > 0 && (m[1].unpack("a2"*(m[1].length/2))).all? {|x| x == "Q="}) then
      puts 'B'
      ans = true
    end
  end

  puts 'NA' if !ans
end