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

using namespace std;

int main(){
	int n;
	while(cin>>n,n){
		for(int x=0;x<n;++x){
			string line;
			cin>>line;
			string sol = "NA";
			int j = line.find('#');
			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(a==b)sol = "A";
//				cout<<a<<":"<<b<<endl;
			}else{
				bool flag = true;
				for(int x=2;x<line.size()-3;){
					if(line[x] !='Q' || line[x+1] != '=')flag = false;
//					cout<<line[x]<<":"<<line[x+1]<<endl;
					x++;
					x++;
				}
				if(flag)sol = "B";
			}
			cout<<sol<<endl;
		}
	}
	return 0;
}