#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)
bool A(string s){
  if(s[0] != '>' ||
     s[1] != '\'' ||
     s[s.size() - 1] != '~')
    return false;
  if(s.size()%2==1)return false;
  if(s[s.size()/2] != '#')return false;
  s[s.size()/2] = '=';
  loop(i,2,s.size() - 1){
    if(s[i] != '=')return false;
  }
  //cout<<s<<endl;
  return true;
}
bool B(string s){
  if(s[0] != '>' ||
     s[1] != '^' ||
     s[s.size() - 2] != '~' ||
     s[s.size() - 1] != '~' )
    return false;
  loop(i,2,s.size()-2){
    if(i%2==0){
      if(s[i] != 'Q')return false;
    }else{
      if(s[i] != '=')return false;
      
    }
  }
  //cout<<s<<endl;
  return true;
  
}
int main(void){
  string str;
  int n;
  cin>>n;
  while(n--){
    cin>>str;
    if(A(str))cout<<"A"<<endl;
    else if(B(str))cout<<"B"<<endl;
    else cout<<"NA"<<endl;
  }
}