import Control.Monad  -- replicateM

main = do 
  n  <- readLn
  ss <- replicateM n getLine
  putStr . unlines . map snakeType $ ss

snakeType s
  | isTypeA s = "A"
  | isTypeB s = "B"
  | otherwise = "NA"

isTypeA s =
  s == ">'" ++ eqs ++ "#" ++ eqs ++ "~" && length eqs >= 1
  where eqs = takeWhile (== '=') (drop 2 s)

isTypeB s =
  s == ">^" ++ qs ++ "~~"
  where qs = concat $ replicate ((length s - 4) `div` 2) "Q="