import scala.io.StdIn.{readInt,readLine}
  
object Main {

  def find(s:String) = {
    if(s.length<6) "NA"
    else if(s(0)=='>') {
      if(s(1) == ''') { // maybeA
        var isA = true
        val s2 = s.drop(2).dropRight(1)
        if(s2(s2.length/2) != '#') isA = false
        else {
          val s3 = s2.split("#")
          if(s3(0)!=s3(1) || s3(0).exists(_!='=')) isA = false
        }
        if(isA && s(s.length-1)=='~') "A" else "NA"
      } else if (s(1) == '^') { // maybeB
        var isB = true
        for(i<-2 until s.length-2 by 2) {
          if(s(i)!='Q' || s(i+1)!='=') {
            isB = false
          }
        }
        if(isB && s(s.length-2)=='~' && s(s.length-1)=='~') "B" else "NA"
      } else {
        "NA"
      }
    } else "NA"
  }

  def main(args:Array[String]) = {
    val n = readInt
    for(i<-1 to n) {
      val s = readLine.trim
      println(find(s))
    }
  }
}