fh=STDIN
regB=Regexp.new(/^>\^(Q=)+~~$/)
n=fh.gets.chomp.to_i
0.upto(n-1){|i|
  s=fh.gets.chomp
  if s=~ /^>'(=+)#/ and s=~ Regexp.new(/^>'#{$1}##{$1}~/) then
    puts "A"
  elsif s=~ regB then
    puts "B"
  else
    puts "NA"
  end
}