#include <iostream>

//CÓ¶ðÁ·Ö
int clear(std::string s, std::string f){
	int pos;
	while(pos = s.find(f), pos != std::string::npos)
		s.replace(pos,f.size(),"");
	if(s == "")return 1;
	return 0;
}

int main(){
	int n;
	std::cin >> n;
	while(n--){
		std::string sn, res = "";
		std::cin >> sn;

		//Type A
		//ªª>'CKöª~C©Â¨ ÌªªïÅ é
		if(sn.substr(0,2) == ">'" &&
			 sn[sn.size()-1] == '~' && (sn.size()-3) % 2 == 1){
			std::string st = sn.substr(2,sn.size()-3);
			//¨ Ì^ñª#
			if(st[st.size()/2] == '#'){
				st.replace(st.size()/2,1,"");
				
				//#ÈOª·×Ä=
				// int eq = 1;
				// for(int i=0;i<st.size();i++){
				// 	if(st[i] != '='){eq = 0;break;}
				// }
				// if(eq)std::cout << "A" << std::endl;
				if(st != "")
					if(clear(st,"="))res = "A\n";
			}
		}

		//Type B
		if(sn.substr(0,2) == ">^" &&
			 sn.substr(sn.size()-2,2) == "~~" &&
			 (sn.size()-4) % 2 == 0){
			std::string st = sn.substr(2,sn.size()-4);
			if(st != "")
				if(clear(st,"Q="))res = "B\n";
		}

		if(res == "")res = "NA\n";
		std::cout << res;
	}
}