#include<iostream>
#include<string>
using namespace std;
int main() {
	int n;
	cin >> n;
	for (int z = 0; z < n; z++) {
		string a;
		cin >> a;
		string b = a.substr(0, 2),c=a.substr(a.length()-2,2);
		if (b == ">'") {
			if (c[1] == '~') {
				int d[2] = { 0,0 } , e = 0;
				bool f = false;
				for (int i = 2; i < a.length() - 1; i++) {
					if (a[i] == '=') {
						while (a[i] == '=') {
							i++;
							d[e]++;
							if (i == a.length())
								break;
						}
						e++;
					}
					if (a[i] == '#'&&e==1) {
						f = true;
					}
				}
				if (f == true && d[0] == d[1]) {
					cout << "A" << endl;
					goto stop;
				}
			}
		}
		if (b == ">^") {
			if (c == "~~") {
				for (int i = 2; i < a.length() - 2; i++) {
					string d = a.substr(i, 2);
					if (d == "Q=") {
						cout << "B" << endl;
						goto stop;
					}
				}
			}
		}
		cout << "NA" << endl;
	stop:;
	}
}