#!/usr/bin/env ruby

n = gets.strip.to_i

n.times do
  snake = gets.strip
  if snake =~ /^>'(=+)#(=+)~$/ and $1.length == $2.length
    puts 'A'
  elsif snake =~ /^>\^(?:Q=)+~~$/
    puts 'B'
  else
    puts 'NA'
  end
end