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


bool isA(string &s){
	if(s.size()<6)return false;
	if(s[0]!='>'||s[1]!='\'')return false;
	int i=2;
	while(s[i]=='=')i++;
	int cnt=i-2;
	if(cnt==0||s[i]!='#')return false;
	for(i++;i<s.size()&&s[i]=='=';i++)cnt--;
	if(cnt||i==s.size()||s[i]!='~')return false;
	return i+1==s.size();
}
bool isB(string &s){
	if(s.size()<6)return false;
	if(s[0]!='>'||s[1]!='^')return false;
	int i=2;
	for(;i<s.size()&&s[i]=='Q';){
		if(i+1<s.size()&&s[i+1]=='=')i+=2;
		else break;
	}
	if(i==2)return false;
	if(i+2==s.size()&&s[i]=='~'&&s[i+1]=='~')return true;
	return false;
}


int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		string s;
		cin>>s;
		if(isA(s))cout<<"A\n";
		else if(isB(s))cout<<"B\n";
		else  cout<<"NA\n";
	}
	return 0;
}