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

int main(){
	int n;
	string s;
	cin >> n;
	for(int i = 0;i < n;i++){
		cin >> s;
		if(s.substr(0,2) == ">\'"){
			if(s[s.length() - 1] != '~') cout << "NA" << endl;
			else {
				int cnt = 0,pos = 2;
				bool flag = true;
				for(;;pos++){
					if(s[pos] == '#') break;
					if(s[pos] == '=') cnt++;
					else {
						flag = false;
						break;
					}
				}
				if(!flag || !cnt) cout << "NA" << endl;
				else{
					pos++;
					for(int j = 0;j < cnt;j++){
						if(s[pos + j] != '='){
							flag = false;
							break;
						}
					}
					if(s[pos + cnt] != '~') flag = false;
					if(!flag) cout << "NA" << endl;
					else cout << "A" << endl;
				}
			}
		}else if(s.substr(0,2) == ">^"){
			if(s.length() % 2 || s.length() <= 4) cout << "NA" << endl;
			else {
				bool flag = true;
				for(int i = 1;i < s.length() / 2;i++){
					if(i != s.length() / 2 - 1 && s.substr(i * 2,2) != "Q=") flag = false;
					if(i == s.length() / 2 - 1 && s.substr(i * 2,2) != "~~") flag = false;
				}
				if(flag) cout << "B" << endl;
				else cout << "NA" << endl;
			}
		}else cout << "NA" << endl;
	}
	return 0;
}