#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
#include <sstream>
using namespace std;

#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
static const int INF=1<<24;

template <class T>
void initvv(vector<vector<T> > &v,int a,int b, const T  &t = T()){
	v.assign(a,vector<T>(b,t));
}

//v.erase(unique(v.begin(),v.end()),v.end());
int main(){
	int n;
	cin>>n;
	rep(i,n){
		string s;
		cin>>s;
		if(s.substr(0,2)==">'"&&s.substr(s.size()-1,1)=="~"){
			s=s.substr(2);
			s=s.substr(0,s.size()-1);
			bool f=true;
			if(s.size()==0) f=false;
			if(s.size()&&s[0]!='='){
				f=false;
			}
			while(s.size()>1){
				if(s[0]==s[s.size()-1]&&s[0]=='='){
					s=s.substr(1,s.size()-2);
				}
				else{
					f=false;
					break;
				}
			}
			if(f){
				if(s[0]=='#'){
					cout<<"A\n";
				}
				else cout<<"NA\n";
			}
			else{
				cout<<"NA\n";
			}
		}
		else if(s.substr(0,2)==">^"&&s.substr(s.size()-2,2)=="~~"){
			s=s.substr(2);
			s=s.substr(0,s.size()-2);
			bool f=true;
			if(s.size()<2) f=false;
			if(s.size()>=2&&s.substr(0,2)!="Q=") f=false;
			while(s.size()>=2){
				if(s.substr(0,2)=="Q="){
					s=s.substr(2);
				}
				else{
					f=false;
					break;
				}
			}
			if(f){
				cout<<"B\n";
			}
			else cout<<"NA\n";
		}
		else{
			cout<<"NA\n";
		}
	}
}