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

string kaga(string s,int num){
	string san;
	for(int i=0;i<num;i++)san+=s;
	return san;
}

void check(string s){
	for(int i=1;i<99;i++){
		string a,b;
		a=">'"+kaga("=",i)+'#'+kaga("=",i)+'~';
		b=">^"+kaga("Q=",i)+"~~";
		if(a==s){
			cout<<"A"<<endl;
			goto end;
		}
		if(b==s){
			cout<<"B"<<endl;
			goto end;
		}
	}
	cout<<"NA"<<endl;
	end:;
}

int main(){
	int n;
	string s;
	for(cin>>n;n>0;n--){
		cin>>s;
		check(s);
	}
	return 0;
}
