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

int isfa(string);
int isfb(string);

int main(){
  int n,fa,fb;
  string str;
  cin >> n;

  for(int i = 0;i < n;i++){
    cin >> str;
    if(str.size()>5){
      fa = isfa(str);
      fb = isfb(str);
    }else fa=fb=0;

    if(fa == 1)
       cout << "A" << endl;
    else if(fb == 1)
      cout << "B" << endl;
    else 
      cout << "NA" << endl;
  }
  return 0;
}

int isfa(string n){
  int pos,c1,c2;
  c1=c2=0;
  pos=n.size()-1;
  if(n[0]!='>')return 0;
  if(n[1]!='\'')return 0;
  if(n[pos]!='~')return 0;
  pos--;
  while(n[pos]=='='){
    c1++;
    pos--;
  }
  if(n[pos]!='#')return 0;
  pos--;
  while(n[pos]=='='){
    c2++;
    pos--;
  }
  if(c1!=c2)return 0;
  if(pos!=1)return 0;

  return 1;
}

int isfb(string n){
  int pos;
  pos = n.size()-1;
  if(n[pos]!='~'||n[pos-1]!='~')
    return 0;
  if(n[0]!='>'||n[1]!='^')return 0;
  pos -= 2;
  while(n[pos]=='='&&n[pos-1]=='Q')pos-=2;
  if(pos!=1)return 0;
  return 1;
}



  