#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;
typedef pair<int, string> PSI;

int n;
string s;
bool F;

int main(){
	cin >> n;
	REP(i, n){
		F = 0;
		cin >> s;
		if (s[0] == '>'&&s[s.size() - 1] == '~'){
			if (s[1] == '\''){
				int icnt = 0;
				bool f = 1;
				FOR(j, 2, s.size() - 1){
					if (s[j] == '='){
						if (f){
							icnt++;
						}
						else{
							icnt--;
						}
					}
					else if (s[j] == '#'){
						if (f)f = 0;
						else {
							F = 1;
							break;
						}
					}
					else{
						F = 1;
						break;
					}
				}
				if (F == 0 && icnt == 0 && s.size() >= 6){
					cout << "A" << endl;
				}
				else {
					cout << "NA" << endl;
				}
			}
			else if (s[1] == '^'&&s.size() % 2 == 0 && s.size() >= 6
				&& s[s.size() - 1] == '~'&&s[s.size() - 2] == '~'){
				FOR(j, 2, s.size() - 2){
					if (j % 2 == 0){
						if (s[j] != 'Q'){
							F = 1;
							break;
						}
					}
					else{
						if (s[j] != '='){
							F = 1;
							break;
						}
					}
				}
				if (F){
					cout << "NA" << endl;
				}
				else{
					cout << "B" << endl;
				}
			}
			else {
				cout << "NA" << endl;
			}
		}
		else{
			cout << "NA" << endl;
		}
	}
}