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

int main(){
	int n,a,c,f;
	string str = "\0";
	cin >> n;
	for(int i = 0;i < n;++i){
		cin >> str;
		c = f = 0;
		a = 2;
		if(str[0] == '>'){
			if(str[1] == '\''){
				while(str[a] == '='){
					++a;
					++c;
				}
				if(str[a] != '#' || c == 0) goto end;
				++a;
				while(str[a] == '='){
					++a;
					++f;
				}
				if(str[a] == '~' && c == f) cout << "A" << endl;
				else goto end;
			}
			else if(str[1] == '^'){
				while(str[a] == 'Q' && str[a+1] == '='){
					a += 2;
				}
				if(str[a] == '~' && str[a+1] == '~') cout << "B" << endl;
				else goto end;
			}
			else goto end;
		}
		else {
			end:
			cout << "NA" << endl;
		}
	}
	return 0;
}