require 'pp'

$stdin.gets.chomp.to_i.times do
  line = $stdin.gets.chomp
  puts a = if (line =~ /^>'(=+)#(=+)~$/ and $1.length == $2.length) then
    'A'
  elsif (line =~ /^>\^(:?Q=)+~~$/) then
    'B'
  else
    'NA'
  end
end