#include <bits/stdc++.h>
#include <regex>

using namespace std;

string Sample;

//const regex REA("^>'(=+)#\1~$"),   REB("^>\\^(Q=)+~~$");
const regex REA("^>'(=+)#(=+)~$"), REB("^>\\^(Q=)+~~$");

int main() {
	int n;
	smatch smStr;

	cin >> n;
	while (n--) {
		cin >> Sample;
		//cout << Sample << endl;
		if (regex_match(Sample, smStr, REA)) {
			if (smStr[1].length() == smStr[2].length()) {
				cout << "A" << endl;
			}
			else {
				cout << "NA" << endl;
			}
		}
		else if (regex_match(Sample, REB)) {
			cout << "B" << endl;
		}
		else {
			cout << "NA" << endl;
		}
	}

	return 0;
}

