chkA ('>':'\'':s) = chkA_1 s
chkA s = False

chkA_1 ('=':s) =
  let n = 1 + (length . takeWhile (== '=')) s
  in
    chkA_2 n $ dropWhile (== '=') s
chkA_1 s = False
  
chkA_2 n ('#':s) = chkA_3 n s
chkA_2 n s = False

chkA_3 n ('=':s) =
  let m = 1 + (length . takeWhile (== '=')) s
  in
    if n == m
    then chkA_4 $ dropWhile (== '=') s
    else False
chkA_3 n s = False

chkA_4 ('~':[]) = True
chkA_4 s = False

chkB ('>':'^':s) = chkB_1 s
chkB s = False

chkB_1 ('Q':'=':s) = chkB_1 s
chkB_1 ('~':'~':[]) = True
chkB_1 s = False


ans s =
  let a = chkA s
      b = chkB s
  in
    if a
    then "A"
    else if b
         then "B"
         else "NA"

main = do
  _ <- getLine
  c <- getContents
  let i = lines c
      o = map ans i
  mapM_ putStrLn o