checkA :: String -> Bool
checkA ('>':'\'':sls) = sLength /= 0 && tilda srest sLength
  where
    (sLength, srest) = sharp sls
    sharp :: String -> (Int, String)
    sharp ('#':sr) = (0, sr)
    sharp ('=':sr) = (i + 1, s)
      where
        (i, s) = sharp sr
    sharp _ = (0, [])
    tilda :: String -> Int -> Bool
    tilda ['~'] 0 = True
    tilda ('=':sr) i = tilda sr (i - 1)
    tilda _ _ = False
checkA _ = False

checkB :: String -> Bool
checkB ('>':'^':'Q':sr) = f ('Q':sr) == "~~"
  where
    f :: String -> String
    f ('Q':'=':s) = f s
    f s = s
checkB _ = False

distinction :: String -> String
distinction s
  |checkA s = "A"
  |checkB s = "B"
  |otherwise = "NA"

main :: IO ()
main = do
  arg <- fmap lines getContents
  mapM_ (putStrLn . distinction) (tail arg)