#include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n;
string s;

int main(){
	cin>>n;
	while(cin>>s){
		if(s.size()<6||s[0]!='>'){cout<<"NA"<<endl;continue;}
		int cur=2,l=0,l2=0,f=0,f2=0;
		if(s[1]=='\''){
			while(cur<s.size()){
				if(f2){f2=0;break;}
				else if(s[cur]=='='){
					if(!f)l++;
					else l2++;
				}
				else if(s[cur]=='#'){
					if(!l||f)break;
					else f=1;
				}
				else if(s[cur]=='~'){
					if(l&&l==l2){f2=1;}
					else break;
				}
				else break;
				cur++;
			}
		}
		else if(s[1]=='^'){
			while(cur<s.size()){
				if(f2){f2=0;break;}
				if(s[cur]=='Q'){
					if(!f)f=1;
					else break;
				}
				else if(s[cur]=='='){
					if(f==1)f=0;
					else break;
				}
				else if(s[cur]=='~'){
					if(!f)f=2;
					else if(f==2)f2=2;
					else break;
				}
				else break;
				cur++;
			}
		}
		if(f2==1)cout<<"A"<<endl;
		else if(f2==2)cout<<"B"<<endl;
		else cout<<"NA"<<endl;
	}
}