#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int main() {
	int n; scanf("%d", &n);
	rep(i, n) {
		string s; cin >> s;
		if (s.size() < 4 || s.size() % 2 == 1) {
			puts("NA");
			continue;
		}
		if (s[0] == '>'&&s[1] == '\''&&s[s.size() - 1] == '~') {
			int cnt = 0;
			bool flag = false;
			bool ok = false;
			for (int i = 2; i < s.size() - 1; i++) {
				if (s[i] != '='&&s[i] != '#') { puts("NA"); goto g; }
				if (flag&&s[i] == '#') { puts("NA"); goto g; }
				if (s[i] == '#')flag = true;
				else {
					ok = true;
					if (flag)cnt--;
					else cnt++;
				}
			}
			if (!ok) { puts("NA"); goto g; }
			puts(cnt ? "NA" : "A");
		}
		else if (s[0] == '>'&&s[1] == '^'&&s[s.size() - 2] == '~'&&s[s.size() - 1] == '~') {
			if (s.size() == 4) { puts("NA"); goto g; }
			for (int i = 2; i < s.size() - 2; i += 2) {
				if (s[i] != 'Q' || s[i + 1] != '=') {
					puts("NA"); goto g;
				}
			}
			puts("B");
		}
		else puts("NA");
	g:;
	}
}