#include<iostream>
#include<string>

using namespace std;

int main()
{
	int n;
	cin >> n;
	for(int D = 0; D < n; ++D){
		string s;
		cin>>s;
		
		if(s.compare(0,2,">'")== 0){
			int bef_eq = 0;
			int aft_eq = 0;
			int shp = 0;
			int pos_shp = 0;
			int pos_tld = 0;

			for(unsigned int i = 2; i < s.length(); ++i ){
				if( s[i] == '=' )
					if( shp == 0 )
						++bef_eq;
					else
						++aft_eq;
				else if( s[i] == '#' ){
					shp++;
				}else if( s[i] == '~' ){
					pos_tld = i;
					break;
				}else{
					shp = 2;
					break;
				}
			}
			if( bef_eq && aft_eq && pos_tld && bef_eq == aft_eq &&
				shp == 1 && pos_tld == s.length() - 1 )
				cout << "A\n";
			else
				cout << "NA\n";
		}else if(s.compare(0,2,">^")==0){
			int cnt_qeq = 0;
			int pos_tltl = 0;
			bool invalid = false;
			
			if( s.length() <= 2 || s.length() % 2 == 1 )
				invalid = true;
			
			for(unsigned int i = 2; i < s.length() && !invalid; i+=2){
				if( s.compare(i,2,"Q=") == 0 )
					++cnt_qeq;
				else if( s.compare(i,2,"~~") == 0 ){
					pos_tltl = i;
					break;
				}else
					invalid = true;
			}
			if( cnt_qeq == 0 || pos_tltl != s.length() - 2 ){
				invalid = true;
			}
			if( invalid )
				cout << "NA\n";
			else
				cout << "B\n";
		}else{
			cout <<"NA\n";
		}
	}
	return 0;
}