#include <iostream>
#include <string>
using namespace std;

string vbString(int Number,string Character){
string res="";
for(int i=0;i<Number;i++) res+=Character;
return res;
}
string checksnake(string s){
string t;
for(int i=1;i<49;i++){
t=">\'"+vbString(i,"=")+"#"+vbString(i,"=")+"~";
if(t==s) return "A";
}
for(int i=1;i<49;i++){
t=">^"+vbString(i,"Q=")+"~~";
if(t==s) return "B";
}
return "NA";
}
int main(){
string s;
int n,i=0;
cin >> n;
for(i=0;i<n;i++){
cin >> s;
cout << checksnake(s) << endl;
}
return 0;
}