#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;

int a;
bool check(const string st,const int isa) {
	a = 0;
	if (isa) {
		if (st[a] != '>')return false;
		a++;
		if (st[a] != '\'')return false;
		a++;
		int num = 0;
		while (st[a] == '=') {
			num++;
			a++;
		}
		if (!num)return false;
		if (st[a] != '#')return false;
		a++;
		int num2 = 0;
		while (st[a] == '=') {
			num2++;
			a++;
		}
		if (num != num2)return false;
		if (st[a] != '~')return false;
		a++;
		if (st[a] != ' ')return false;
		return true;
	}
	else {
		if (st[a] != '>')return false;
		a++;
		if (st[a] != '^')return false;
		a++;
		int num = 0;
		while (1) {
			if (st[a] == 'Q') {
				num++;
				a++;
				if (st[a] != '=') {
					return false;
				}
				else {
					a++;
				}
			}
			else {
				break;
			}
		}
		if (!num)return false;
		
		
		if (st[a] != '~')return false;
		a++;
		if (st[a] != '~')return false;
		a++;
		if (st[a] != ' ')return false;
		return true;
	}
}

int main() {
	int N; cin >> N;
	while (N--) {
		string st; cin >> st;
		for (int i = 0; i < 100; ++i) {

			st.push_back(' ');
		}
		if (check(st, true)) {
			cout << "A" << endl;
		}
		else if (check(st, false)) {
			cout << "B" << endl;
		}
		else {
			cout << "NA" << endl;
		}
	}
	return 0;
}