#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;

int Judge(string &s){
	if(s[0] != '>') return -1;
	if(s.size() < 2) return -1;
	if(s[1] == '\''){
		if(s[s.size()-1] != '~') return -1;
		int len = sz(s) - 3;
		if(len < 3 || len % 2 == 0) return -1;
		FOR(i,(len-1)/2){
			if(s[2+i] != '=' || s[2+len/2+1+i] != '=') return -1;
		}
		if(s[2+len/2] != '#') return -1;
		return 0;
	} else if(s[1] == '^'){
		if(s[s.size()-1] != '~' || s[s.size()-2] != '~')return -1;
		int len = sz(s) - 4;
		if(len < 2 || len % 2 != 0) return -1;
		FOR(i,len/2){
			if(s[2+2*i] != 'Q' || s[2+2*i+1] != '=') return -1;
		}
			return 1;
	} else {
		return -1;
	}
}

int main(){
	int n; cin>>n;
	string out[] = {"NA","A","B"};
	FOR(i,n){
		string s; cin>>s;
		int a = Judge(s);
		cout << out[a+1] << endl;
	}
	return 0;
}