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

int main(void){
	int n;
	cin >> n;
	for(int i=0;i<n;i++){
		string str;
		cin >> str;
		if(str.size()<2)cout << "NA" << endl;
		else if(str[0]=='>' && str[1]=='\''){
			int j=2;
			int l[2]={0,0};
			int ck=0;
			bool ch[2]={false,true};
			while(j<str.size()){
				if(str[j]=='=')l[ck]++;
				else if(str[j]=='#' && l[0]>0)ch[0]=true,ck=1;
				else if(str[j]=='~')break;
				else ch[1]=false;
				j++;
			}
			if(ch[0]==true && ch[1]==true && l[0]==l[1])cout << "A" << endl;
			else cout << "NA" << endl;
		}else if(str[0]=='>' && str[1]=='^'){
			if(str.size()%2==1 || str.size()<6)cout << "NA" << endl;
			else{
				bool ch=true;
				for(int j=2;j<str.size()-2;j+=2){
					if(str[j]!='Q' || str[j+1]!='=')ch=false;
				}
				if(str[str.size()-2]!='~' || str[str.size()-1]!='~')ch=false;
				if(ch==true)cout << "B" << endl;
				else cout << "NA" << endl;
			}
		}else cout << "NA" << endl;
	}
    return 0;
}