#include<iostream>
#include<string>
using namespace std;
bool check(string s,int n) {
	if (s.size() <= n)return true;
	return false;
}
void solve() {
	string s; cin >> s;
	int n = 0;
	if (s[n] == '>') {
		n++;
		if (check(s, n));
		else if (s[n] == '\'') {
			n++;
			int cnt = 0;
			while (s[n] == '=' && !check(s, n)) { cnt++; n++; }
			if (s[n] == '#') {
				n++;
				if (check(s, n))goto exit;
				else if (s[n] != '=')goto exit;
				while (s[n] == '=' && !check(s, n)) { cnt--; n++; }
				if (cnt == 0) {
					if (s[n] == '~') {
						n++;
						if (n == s.size()) {
							cout << "A\n";
							return;
						}
					}
				}
			}
		}
		else if (s[n] == '^') {
			n++;
			if (check(s, n))goto exit;
			bool b = false;
			while (true) {
				if (check(s, n + 1))goto exit;
				else {
					if (s[n] == 'Q'&&s[n + 1] == '=') { n += 2; b = true; }
					else if (s[n] == '~'&&s[n + 1] == '~') {
						if (b) {
							cout << "B\n";
							return;
						}
						else goto exit;
					}
					else goto exit;
				}
			}
			n++;
			if (check(s, n))goto exit;
			if (s[n] == '~') {
				n++;
				if (check(s, n))goto exit;
				if (s[n] == '~') {
					n++;
					if (n == s.size()) {
						cout << "B\n";
						return;
					}
				}
			}
		}
	}
exit:;
	cout << "NA\n";
	return;
}
int main() {
	int n; cin >> n;
	for (int i = 0; i < n; i++)solve();
}