#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<unordered_map>
#include<map>
using namespace std;

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