#include <iostream>
using namespace std;

int main(){
	int n;	cin >> n;
	while(n--){
		int i,eq1,eq2;
		string ret = "NA";
		
		string s; cin >> s;
		if(s.substr(0,3) == ">'="){
			i = 2;
			
			eq1 = 0;
			while(s[i] == '=' && i < s.length() )i++,eq1++;
			if(s[i] != '#')goto label;
			
			i++; eq2 = 0;
			while(s[i] == '=' && i < s.length() )i++,eq2++;
			
			if(i == s.length()-1 && s[i] == '~' && eq1 == eq2)
				ret = "A"; 
		}else if(s.substr(0,4) == ">^Q="){
			for(i=2;i+1<s.length()-2;i+=2){
				if(s[i] == 'Q' && s[i+1] == '='){}
				else goto label;
			}
			if(s[s.length()-1] == '~' && s[s.length()-2] == '~' && s[s.length()-3] == '=' && s[s.length()-4] == 'Q')
				ret = "B";
			
		}
		label:;
		cout << ret << endl;
		
		
	}
}