#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<functional>
#include<map>
using namespace std;

int main() {
	int a;
	cin >> a;
	for (int b = 0; b < a; b++) {
		string c; cin >> c;
		if ((c.length() & 1)==0&&c.length()>=6) {
			if (c[0] == '>'&&c[1] == '^'&&c[c.length() - 1] == '~'&&c[c.length() - 2] == '~') {
				bool e = true;
				for (int d = 2; d < c.length() - 2; d += 2) {
					if (c[d] != 'Q' || c[d + 1] != '=')e = false;
				}
				if (e) {
					puts("B");
					continue;
				}
			}
			if (c[0] == '>'&&c[1] == '\''&&c[c.length() - 1] == '~') {
				bool k = true;
				for (int j = 2; j < c.length() - 1; j++) {
					if (j == c.length() / 2) {
						if (c[j] != '#')k = false;
					}
					else {
						if (c[j] != '=')k = false;
					}
				}
				if (k) { puts("A"); continue; }
			}
		}
		puts("NA");
	}
}