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

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

string solve(string s){
	if(s[0] != '>')return "NA";
	if(s.size() % 2 != 0)return "NA";
	
	int i = 0, j;
	if(s[1] == '\''){
		for(i = 2;i < s.size();++i){
			if(s[i] == '#')break;
			else if(s[i] != '=')return "NA";
		}
		int l = i - 2; ++i;
		rep(j, l)if(s[i + j] != '=')return "NA";
		if(s[s.size() - 1] != '~')return "NA";
		if(i + l + 1 != s.size())return "NA";
		return "A";
	}else if(s[1] == '^'){
		for(i = 2;i < s.size();i+=2){
			if(s.substr(i, 2) != "Q="){
				if(i + 2 == s.size() && s.substr(i, 2) == "~~")break;
				else return "NA";
			}
		}
		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;
}