#include<iostream>
#include<string>
#include<sstream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		string line;
		cin>>line;
		string sol = "NA";
		if(line.find('#')!=string::npos){
			bool flag = false;
			string a = "";
			string b = "";
			for(int x=2;x<line.size()-1;++x){
				if(line[x] == '#'){
					flag = true;
					continue;
				}
				if(!flag){
					a += line[x];
				}else{
					b += line[x];
				}
			}
			if(line.size()> 3 && line[0]=='>' && line[1]=='\'' && line[line.size()-1]=='~' && a==b && a!="")sol = "A";
		}else{
			bool flag = true;
			for(int x=2;line.size()>4 && x<(line.size()-3);x+=2){
				if(line[x] !='Q' || line[x+1] != '=')flag = false;
			}
			if(line.size()>4 && line[0]=='>' && line[1]=='^' && line[line.size()-1]=='~' && line[line.size()-2]=='~' && flag)sol="B";	}
		cout<<sol<<endl;
	}
	return 0;
}