#include<iostream>
#include<string>
using namespace std;
#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
string A(int s){			
  string res = ">\'";	 			
  rep(i,s/2-2)res += '=';
  res += '#';		
  rep(i,s/2-2)res += '=';
  res += "~";
  return res;
}
string B(int s){
  string res = ">^";
  rep(i,s/2-2)res+= "Q=";
  res+="~~";
 return res;
}
int main(void){
  string str;
  int n;
  cin>>n;
  while(n--){
    cin>>str;
    //cout<<"ori "<<str<<endl;
    B(str.size());
    if(A(str.size()) == str)cout<<"A"<<endl;
    else if(B(str.size()) == str)cout<<"B"<<endl;
    else cout<<"NA"<<endl;
  }
}