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

int n;
string s;

bool checkA(string s) {
	if (s.length() < 6) return false;
	if (s[0] != '>') return false;
	if (s[1] != '\'') return false;
	
	int i;
	for (i = 2; i < s.length() && s[i] == '='; i++);
	if (i == s.length() || s[i] != '#') return false;
	int cnt = i - 2;
	for (i = i + 1; i < s.length() && s[i] == '='; i++, cnt--);
	if (cnt != 0) return false;
	if (s.length() != i + 1) return false;
	if (s[i] != '~') return false;
	return true;
}

bool checkB(string s) {
	if (s.length() < 6) return false;
	if (s[0] != '>') return false;
	if (s[1] != '^') return false;
	int i;
	for (i = 2; i < s.length(); i += 2) {
		if (i + 1 >= s.length()) return false;
		if (s[i] != 'Q') break;
		if (s[i + 1] != '=') return false;
	}
	if (s.length() != i + 2) return false;
	if (s[i] != '~' || s[i + 1] != '~') return false;
	return true;
}

int main() {
	cin >> n;
	while (n--) {
		cin >> s;
		if (checkA(s)) cout << "A" << endl;
		else if (checkB(s)) cout << "B" << endl;
		else cout << "NA" << endl;
	}
	return 0;
}