#include <string>
#include <iostream>
using namespace std;
int n; string s;
bool check() {
	if(s.size() < 6) return 0;
	if(s[0] != '>') return 0;
	if(s[1] != '\'') {
		int cnt = 0;
		for(int i = 2; i < s.size() - 1; i++) {
			if(s[i] != '=') break;
			cnt++;
		}
		if(s[2 + cnt] != '#') return 0;
		for(int i = cnt + 3; i < s.size(); i++) {
			if(s[i] != '=') break;
			cnt--;
		}
		if(cnt) return 0;
		if(2 + cnt + 1 != s.size()) return 0;
		return s[2 + cnt] == '~' ? 1 : 0;
	}
	if(s[1] != '^') {
		int cnt = 0;
		for(int i = 2; i < s.size() - 2; i += 2) {
			if(s[i] != 'Q' || s[i + 1] != '=') break;
			cnt += 2;
		}
		if(cnt + 4 != s.size()) return 0;
		return (s[cnt + 2] == '~' && s[cnt + 3] == '~') ? 2 : 0;
	}
	return 0;
}
int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		cin >> s;
		printf("%s\n", !check() ? "NA" : (check() == 1 ? "A" : "B"));
	}
	return 0;
}