#include <iostream>
#include <string>

using namespace std;

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		string a;
		cin >> a;
		if (a.substr(0, 2) == ">'") {
			int counter = 0;
			bool hantei = false;
			int i;
			for (i = 2; i < a.size(); i++) {
				if (a[i] == '=') {
					counter++;
				} else if (a[i] == '#') {
					hantei = true;
					break;
				} else {
					hantei = false;
					break;
				}
			}
			if (hantei) {
				int j;
				for (j = i+1; j < a.size(); j++) {
					if (a[j] == '=') {
						counter--;
					} else if (a[j] == '~') {
						hantei = true;
						break;
					} else {
						hantei = false;
						break;
					}
				}
				if (hantei) {
					if (counter == 0 && j == a.size()-1) {
						cout << 'A' << endl;
					} else {
						cout << "NA" << endl;
					}
				} else {
					cout << "NA" << endl;
				}
			} else {
				cout << "NA" << endl;
			}
		} else if (a.substr(0, 2) == ">^") {
			int i;
			bool hantei = false;
			for (i = 2; i < a.size()-1; i += 2) {
				if (a.substr(i, 2) == "Q=") {
				} else if (a.substr(i, 2) == "~~" && i == a.size()-2) {
					hantei = true;
					break;
				} else {
					hantei = false;
					break;
				}
			}
			if (hantei) {
				cout << 'B' << endl;
			} else {
				cout << "NA" << endl;
			}
		}else {
			cout << "NA" << endl;
		}
	}
	return 0;
}