#include "stdio.h"
#include "math.h"

#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

const int INF = 10000000;
using namespace std;
typedef long long  ll;

/** Problem0139 : Snakes **/
int main()
{
	int n;
	string s;
	cin >> n;
	rep(k, n) {
		cin >> s;
		if (s.length()<2) {
			cout << "NA" << endl;
			goto end;
		}
		else if (s[0]=='>' && s[1]=='\'') {
			int num1=0, num2=0, i;
			for(i=2; i<s.length()-1;i++) {
				if (s[i]=='=')
					num1++;
				else if (s[i]=='#')
					break;
				else {
					cout << "NA" << endl;
					goto end;
				}
			}
			for (; i<s.length()-1; i++) {
				if (s[i]=='=')
					num2++;
			}
			if (num1 == num2 && s[s.length()-1]=='~') {
				cout << "A" << endl;
			} else {
				cout << "NA" << endl;
				goto end;
			}
		}
		else if (s[0]=='>' && s[1]=='^') {
			bool f=true;
			for (int i=2; i<s.length()-2; i++) {
				if (i%2==0 && s[i]!='Q')
					f=false;
				else if (i%2==1 && s[i]!='=')
					f=false;
			}
			if (f && s[s.length()-2]=='~' && s[s.length()-1]=='~') {
				cout << "B" << endl;
			} else {
				cout << "NA" << endl;
				goto end;
			}
		}
	end:;
	}
}