#include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int i,j;
  int n;
  vector<string> s;
  cin>>n;
  for(i=0;i<n;i++){
    string str;
    cin>>str;
    s.push_back(str);
  }
  for(i=0;i<n;i++){
    if(s[i].find(">'")==0){
      if(s[i].find("~")==s[i].length()-1){
	if(s[i].find("#")!=-1){
	  if(s[i].find("#")-2==s[i].length()-s[i].find("#")-2){
	    int len;
	    string str;
	    str=s[i].substr(2,s[i].find("#")-2);  
	    len=str.length();
	    if(len>0){
	      for(j=0;j<len;j++){
		if(str[j]!='=')
		  break;
	      }
	      if(j==len){
		str=s[i].substr(s[i].find("#")+1,s[i].find("#")-2);
		len=str.length();
		for(j=0;j<len;j++){
		  if(str[j]!='=')
		    break;
		}
		if(j==len){
		  cout<<"A"<<endl;
		  continue;
		}
	      }
	    }
	  }
	}
      }
    }else if(s[i].find(">^")==0){
      if(s[i].find("~~")==s[i].length()-2){
	int len;
	string str;
	str=s[i].substr(2,s[i].length()-4);
	len=str.length();
	if(len>1&&len%2==0){
	  for(j=0;j<len;j+=2){
	    if(str[j]!='Q')
	      break;
	  }
	  if(j==len){
	    for(j=1;j<len;j+=2){
	      if(str[j]!='=')
		break;
	    }
	    if(j==len+1){
	      cout<<"B"<<endl;
	      continue;
	    }
	  }
	}
      }
    }
    cout<<"NA"<<endl;
  }
  return 0;
}