#include <iostream>
using namespace std;

int main(){
  char buf[210];
  int bi,n,eqc,ti;

  cin >> n;

  for (int i=0;i<n;i++){
    bi=0;
    cin >> buf;
    if (buf[bi++]!='>') {
      cout << "NA" << endl;
      continue;
    }
    if (buf[bi]=='\''){
      bi++;
      eqc=0;
      if (buf[bi]!='='){
	cout << "NA" << endl;
	continue;
      }
      while (buf[bi+eqc]=='=')eqc++;
      if (buf[bi+eqc]!='#'){
	cout << "NA" << endl;
	continue;
      }
      bi += eqc+1;
      for (ti=0;ti<eqc;ti++)
	if (buf[bi+ti]!='=')break;
      if (ti <eqc){
	cout << "NA" << endl;
	continue;
      }
      bi+=ti;
      if (buf[bi]=='~'&&buf[bi+1]=='\0')cout << "A" << endl;
      else cout << "NA" << endl;
    }else if(buf[bi]=='^'){
      bi++;
      //cout <<"dbg "<< buf[bi] << buf[bi+1] << endl;
      if(buf[bi]!='Q'||buf[bi+1]!='='){
	cout << "NA" << endl;
	continue;
      }
      bi+=2;
      while (buf[bi]=='Q'&&buf[bi+1]=='=')bi +=2;
      //cout <<"dbg "<< buf[bi] << buf[bi+1] << endl;
      if (buf[bi]=='~'&&buf[bi+1]=='~'){
	if (buf[bi+2]=='\0')cout << "B" << endl;
	else cout << "NA" << endl;
      }else cout << "NA" << endl;
    }else{
      cout << "NA" << endl;
      continue;
    }
  }
  return 0;
}