#include <iostream>
#include <string>
using namespace std;

bool isA(const string& str, int indx) {
	if(str[indx++] != '>')
		return false;
	if(str[indx++] != '\'')
		return false;

	int count0 = 0;
	for(; indx < str.size(); indx++) {
		if(str[indx] == '=')
			count0++;
		else
			break;
	}
	if(indx >= str.size())
		return false;
	if(str[indx++] != '#' || count0 == 0)
		return false;
	int count1 = 0;
	for(; indx < str.size(); indx++) {
		if(str[indx] == '=')
			count1++;
		else
			break;
	}

	if(count0 == count1 && indx + 1 == str.size() && str[indx] == '~')
		return true;
	return false;
}

bool isB(const string & str, int indx) {
	if(str[indx++] != '>')
		return false;
	if(str[indx++] != '^')
		return false;

	int count = 0;
	for(; indx + 1 < str.size(); indx += 2) {
		if(string(str.begin() + indx, str.begin() + indx + 2) == "Q=")
			count++;
		else
			break;
	}
	if(count > 0 && string(str.begin() + indx, str.begin() + indx + 2) == "~~" && indx + 2 == str.size())
		return true;
	return false;
}

int main() {
	string str;
	int n;

	cin >> n;
	for(int t = 0; t < n; t++) {
		int type = 0;
		cin >> str;
		for(int i = 0; i < str.size(); i++) {
			if(str[i] == '>') {
				if(isA(str, i)) {
					type = 1;
					break;
				}
				else if(isB(str, i)) {
					type = 2;
					break;
				}
			}
		}

		switch(type) {
			case 0:
				cout << "NA" << endl;
				break;
			case 1:
				cout << "A" << endl;
				break;
			case 2:
				cout << "B" << endl;
				break;
		}
	}
}