#include <iostream>
#include <string>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)

int main() {
	string s;
	int n;
	cin >> n;
	rep(i,n) {
		cin >> s;
		int len = s.length();
		bool fa = 0, fb = 0;
		if(len > 5 && len % 2 == 0) {
			fa = 1;
			string ns;
			ns += s.substr(2,len/2-2);
			ns += s.substr(len/2+1,len/2-2);
			rep(i,ns.length()) {
				if(ns[i] != '=') fa = 0;
			}
			if(s[0] != '>' || s[1] != '\'' || s[len/2] != '#' || s[len-1] != '~') {
				fa = 0;
			}
			fb = 1;
			rep2(i,1,(len-2)/2) {
				if(s[2*i] != 'Q' || s[2*i+1] != '=') fb = 0;
			}
			if(s[0] != '>' || s[1] != '^' || s[len-2] != '~' || s[len-1] != '~') {
				fb = 0;
			}
		}
		if(fa) cout << "A" << endl;
		else if(fb) cout << "B" << endl;
		else cout << "NA" << endl;
	}
	return 0;
}