#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main() {
	int n,i,j,s;
	string c1,c2,c3;
	cin >> n;
	for (i=0;i<n;i++) {
		cin >> c1;
		c2=c3=c1;
		s=c1.size();
		if (s<6 || s % 2==1) { cout << "NA" << endl; continue; }
		for (j=0;j<s;j++) c2[j]=c3[j]='=';
        c2[0]=c3[0]='>'; c2[s-1]=c3[s-1]=c3[s-2]='~';
		c2[1]='\''; c2[s/2]='#'; c3[1]='^';
        for (j=2;j<s-2;j+=2) c3[j]='Q';
		if (c1==c2) cout << 'A'; else if (c1==c3) cout << 'B'; else cout << "NA";
		cout << endl;
	}
	return 0;
}