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

bool is_A(string s){
	int size = s.size();
	if(size < 6) return false;
	if(s[0] != '>' || s[1] != '\'') return false;
	if(s[size - 1] != '~') return false;
	if(s[2] != '=') return false;
	int count = 0;
	int i = 2;
	while(s[i] == '='){ count++; i++; }
	if(s[i++] != '#') return false;
	for(int j = 0; j < count; j++, i++){
		if(s[i] != '=') return false;
	}
	if(i == size - 1) return true;
	else return false;
}

bool is_B(string s){
	int size = s.size();
	if(size < 6) return false;
	if(s[0] != '>' || s[1] != '^') return false;
	if(s[size - 1] != '~' || s[size - 2] != '~') return false;
	int i = 2;
	while(i < size - 2){
		if(s[i] != 'Q' || s[i+1] != '=') return false;
		i += 2;
	}
	return true;
}

int main(){
	string s;
	int n;
	cin >> n;
	getline(cin, s);
	for(int i = 0; i < n; i++){
		getline(cin, s);
		if(is_A(s)) cout << "A";
		else if(is_B(s)) cout << "B";
		else cout << "NA";
		cout << endl;
	}
	return 0;
}