#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
int main(){
    int n; cin>>n;
    rep(hoge,n){
        string s;cin>>s;
        int state=0;
        if(s.size()<=5){cout<<"NA"<<endl; continue;}
        if(s[0]=='>'&&s[1]=='\'')state=1;
        else if(s[0]=='>'&&s[1]=='^')state=2;
        int cal=0,cnt=0; bool mybool=true,flag=true,key;
        if(state){
            rep(i,s.size()){
                if(i==0||i==1)continue;
                if(state==1){
                    if(s[i]=='#')mybool=false;
                    else if(mybool&&s[i]=='=')cnt++;
                    else if(mybool==false&&s[i]=='=')cnt--;
                    else if(s[i]=='~'&&i==s.size()-1)continue;
                    else flag=false;
                }else if(state==2){
                    if(!(s[s.size()-1]=='~'&&s[s.size()-2]=='~'))flag=false;
                    if(i>=s.size()-2)continue;
                    if((s[i]=='Q'&&s[i+1]=='=')){i++;}
                    else flag=false;
                }
            }
            if(state==1&&cnt==0&&flag)cout<<"A"<<endl;
            else if(state==2&&flag)cout<<"B"<<endl;
            else cout<<"NA"<<endl;
        }
    }
}