#include<iostream>
#include<string>
using namespace std;
int main(){
	int n;
	string s;
	cin>>n;
	for(int u=0;u<n;u++){
		cin>>s;
		bool a=true,b=true;
		if(!(s[0]=='>'&&s[1]=='\''&&s[s.size()-1]=='~'&&s[s.size()/2]=='#'&&s.size()%2==0))a=false;
		if(!(s[0]=='>'&&s[1]=='^'&&s[s.size()-2]=='~'&&s[s.size()-1]=='~'&&s.size()%2==0))b=false;
		if(a){for(int i=2;i<s.size()-1;i++){if(i!=s.size()/2&&s[i]!='=')a=false;}}
		if(b){for(int i=2;i<s.size()-2;i++){if((i%2==0&&s[i]!='Q')||(i%2==1&&s[i]!='='))b=false;}}
		if(a)cout<<"A"<<endl;if(b)cout<<"B"<<endl;if(!a&&!b)cout<<"NA"<<endl;
	}
	return 0 ;
}