#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

const int INF = 10000000;
using namespace std;
typedef long long  ll;

/** Problem0139 : Snakes **/
int main()
{
	int n;
	string s;
	cin >> n;
	rep(k, n) {
		cin >> s;
		REP(i, 1, 100) {
			string a=">'", b=">^";

			rep(j, i) {
				a+='=';
				b+="Q=";
			}
			a+='#';
			rep(j, i)
				a+='=';
			a+='~';
			b+="~~";
			if (s == a) {
				cout << "A" << endl;
				goto end;
			} else if (s == b) {
				cout << "B" << endl;
				goto end;
			}
		}
		cout << "NA" << endl;
	end:;
	}
	return 0;
}