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

int main()
{
  int n,f,i,m;
  string s;
  cin>>n;
  while(n-->0){
    cin>>s;
    f=0;
    if(s[1]=='\''){
      m=s.find('#');
      if(m!=string::npos){
	for(i=1;s[m-i]==s[m+i];)i++;
	if(i>1 && m-i==1 && m+i==s.size()-1 && s[m+i]=='~')f=1;
      }
    }else if(s[1]=='^'){
      for(i=2;s[i]=='Q'&&s[i+1]=='=';)i+=2;
      if(i>2 && i==s.size()-2 && s[i]=='~' && s[i+1]=='~')f=2;
    }
    if(f)puts(f==1?"A":"B");
    else puts("NA");
  }
}