#include<iostream>
#include<string>
using namespace std;
int main(){
long n,f,i,j,k,m;
string s;
cin>>n;
for(i=0;i<n;i++){
cin>>s;
if(s.substr(0,2)==">'"){
if(s.substr(2,1)!="="){f=2;goto E;}
j=s.find_first_not_of('=',3);
m=j-2;
if(s.substr(j,1)!="#"){f=2;goto E;}
k=s.find_first_not_of('=',j+1);
if(k-j-1!=m){f=2;goto E;}
if(s.substr(k,1)!="~"){f=2;goto E;}
if(k+1!=s.size()){f=2;goto E;}
f=0;
}else if(s.substr(0,2)==">^"){
if(s.substr(2,2)!="Q="){f=2;goto E;}
for(j=4;;j+=2){
if(s.substr(j,2)!="Q=")break;
}
if(s.substr(j,2)!="~~")f=2;else f=1;
}else{
f=2;
}
E:
if(f==0)cout<<"A"<<endl;
else if(f==1)cout<<"B"<<endl;
else cout<<"NA"<<endl;
}
return 0;
}