#include<iostream>
#include<string>
using namespace std;
string S;
int n;

string solve(string T) {
	int chain = 0, ok = 0;
	if (T.size() >= 3) {
		if (T[T.size() - 1] == '~') {
			if (T.substr(0, 2) == ">'" && T.size() >= 6) {
				for (int i = 2; i < T.size() - 1; i++) {
					if (T[i] == '#') {
						ok++;
					}
					else if (T[i] == '=') {
						if (ok == 0) { chain++; }
						else { chain--; }
					}
					else {
						goto NA;
					}
				}
				if (ok == 1 && chain == 0) { return "A"; }
			}

			if (T.substr(0, 2) == ">^" && T[T.size() - 2] == '~' && T.size() >= 6) {
				for (int i = 2; i < T.size() - 2; i += 2) {
					if (T.substr(i, 2) != "Q=") {
						goto E;
					}
				}
				return "B";
			E:;
			}
		}
	}
NA:;
	return "NA";
}

int main() {
	cin >> n;
	for (int h = 0; h < n; h++) {
		cin >> S;
		cout << solve(S) << endl;
	}
}