#include <iostream>
using namespace std;

/*s[i]ツつゥツづァツ督ッツつカツ閉カツ篠堋つェツ連ツ堕アツつオツづつ「ツづゥツ青板づーツ陛板つキ*/
int seqlen(string &s,int i){
	char c = s[i];
	int ret=0;
	while(s[i++]==c)ret++;
	return ret;
}

int main(){
	int n;
	int flag = 0;
	string s;
	cin >> n;
	while(n--){
		flag = 0;
		cin >> s;
		/*A*/
		if(s[0]=='>' && s[1]==39){
			int x=2;
			x += seqlen(s,x);
			if(s[x]!='#')flag = 1;
			x ++ ;
			if(seqlen(s,2) != seqlen(s,x))flag = 1;
			x += seqlen(s,x);
			if(s[x]!='~')flag = 1;
			if(!flag)flag='A';
		}
		else if(s[0]=='>' && s[1]=='^'){
			int pointa = 0;
			for(int i=2;i<s.length()-2;i++){
				if(i%2){
					if(s[i] != '=')flag = 1;
					else pointa++;
				}else{
					if(s[i] != 'Q')flag = 1;
					else pointa++;
				}
			}
			if(s[s.length()-2] != '~' || s[s.length()-1] != '~')flag = 1;
			if(!flag && pointa>=2)flag='B';
			else flag = 1;
		}
		else {flag = 1;}
		
		if(flag != 1)cout << (char)flag << endl;
		else cout << "NA" << endl;
	}
}