#include<iostream>
#include<string>
#include<algorithm>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int main(){
	string s;
	int n;
	cin>>n;
	rep(i,n){
		cin>>s;
		int size=s.size();
		string A="";
		string B="";
		rep(i,size){
			if(i==0){A+=">";B+=">";}
			else if(i==1){A+="'";B+="^";}
			else{
				if(i<size-1){
					if(i==(size-3)/2+2)A+="#";
					else A+="=";
				}else A+="~";
				if(i<size-2){
					if(i%2==0)B+="Q";else B+="=";
				}else{B+="~";}
			}
		}
		if(size%2==0){
			if(s==A)cout<<"A"<<endl;
			else if(s==B)cout<<"B"<<endl;
			else cout<<"NA"<<endl;
		}else cout<<"NA"<<endl;
	}
	return 0;
}