#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)
#define all(a) (a).begin(), (a).end()

const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

int main(){
	int n; cin >> n;
	rep(q, n){
		string s; cin >> s;
		bool p = true;
		if(s.substr(0, 2) == ">'"){
			s = s.substr(2);
			int c = 0;
			for(int i=0; i < s.size(); i++) if(s[i] == '='){c++;} else break;
			if(s[c] != '#')p = false;
			s = s.substr(c+1);
			if(c+1 != s.size() || s[s.size()-1] != '~')p = false;
			rep(i, c) if(s[i] != '=')p = false;
			cout << (p ? "A" : "NA") << endl;
		}else if(s.substr(0, 2) == ">^"){
			s = s.substr(2);
			while(s.substr(0, 2) == "Q=") s = s.substr(2);
			cout << (s == "~~" ? "B" : "NA") << endl;
		}else {
			cout << "NA" << endl;
		}
	}
}