#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

#define rep(i, n)for((i) = 0;(i) < (n);++(i))

string solve(string s){
	int i = 0, j;
	string tmp;
	rep(i, 100){
		tmp += "=";
		if(s == ">'" + tmp + "#" + tmp + "~~")return "A";
	}
	tmp.clear();
	rep(i, 100){
		tmp += "Q=";
		if(s == ">^" + tmp + "~~")return "B";
	}
	return "NA";
}

int main(void){
	int i, j, n;
	string s;
	scanf("%d", &n);
	for(;n--;){
		cin >> s;
		cout << solve(s) << endl;
	}
	
	return 0;
}