// 2011/10/02 Tazoe

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

int main()
{
	int n;
	cin >> n;

	for(int i=0; i<n; i++){
		string str;
		cin >> str;

		if(str[0]!='>'){
			cout << "NA" << endl;
			continue;
		}

		if(str[1]=='\''){
			int N = 0;
			int I = 2;
			while(str[I]=='='){
				N++;
				I++;
			}
			if(N==0){
				cout << "NA" << endl;
				continue;
			}

			if(str[I]!='#'){
				cout << "NA" << endl;
				continue;
			}
			I++;

			int M = 0;
			while(str[I]=='='){
				M++;
				I++;
			}
			if(N!=M){
				cout << "NA" << endl;
				continue;
			}

			if(!(str[I]=='~'&&I+1==str.size())){
				cout << "NA" << endl;
				continue;
			}

			cout << "A" << endl;
		}
		else if(str[1]=='^'){
			int N = 0;
			int I = 2;
			while(str[I]=='Q'&&str[I+1]=='='){
				N++;
				I += 2;
			}
			if(N==0){
				cout << "NA" << endl;
				continue;
			}

			if(!(str[I]=='~'&&str[I+1]=='~'&&I+2==str.size())){
				cout << "NA" << endl;
				continue;
			}

			cout << "B" << endl;
		}
		else
			cout << "NA" << endl;
	}

	return 0;
}