#include<iostream>
#include<string>
using namespace std;
string s;
int main(){
	int n,t,now;
	cin>>n;
	while(n--){
		cin>>s;
		if(s.size()>=6&&s.substr(0,3)==">'="){
			t=0;
			while(s[3+(t++)]=='=');
			if(s[3+t-1]!='#')
				cout<<"NA"<<endl;
			else
			if(4+2*t!=s.size()||s.substr(3+t,t)!=s.substr(2,t)){

				cout<<"NA"<<endl;
			}
			else
			if(s[3+2*t]=='~')
				cout<<"A"<<endl;
		}
		else if(s.size()>=6&&s.substr(0,4)==">^Q="){
			now=4;
			int pre=0;
			while(1){
				if(s.size()-now<2){
					pre=1;
					break;
				}
				if(s.substr(now,2)!="Q=")
					break;
				now+=2;
			}
			if(pre==1)
			cout<<"NA"<<endl;
			else
			if(s.size()-now==2&&s.substr(now,2)=="~~"){
				cout<<"B"<<endl;
			}
			else {cout<<"NA"<<endl;

			}
		}
		else cout<<"NA"<<endl;
	}
	return 0;
}