var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var Arr=(input.replace(/\n$/,"")).split("\n");
var n=Arr.shift();
for(var i=0;i<n;i++){
   var str=Arr[i];
   var strA=str.replace(/\>\'(\=+)\#(\=+)\~/,function(s,s1,s2){
      if(s1==s2)s=s.replace(/\=+/g,"=");
      return s;
   });
   var strB=str.replace(/\>\^(Q=)+~~/,function(s){
      return ">^Q=~~";
   });
   if(strA==">'=#=~"){console.log("A");continue;}
   if(strB==">^Q=~~"){console.log("B");continue;}
   console.log("NA");
}