#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)
//#define INF (1LL<<59)

typedef string::const_iterator State;

int main(){
    int n;
    cin>>n;
    while(n--){
        string s;
        cin>>s;
    
        State beg = s.begin();
        try{
            if(*beg!='>')throw -1;
            beg++;
            if(*beg=='\''){
                beg++;
                int eq_count=0;
                while(*beg=='='){
                    if(beg==s.end())throw -1;
                    eq_count++;
                    beg++;
                }
                if(*beg!='#')throw -1;
                beg++;
                int c=0;
                while(*beg=='='){
                    if(beg==s.end())throw -1;
                    c++;
                    beg++;
                }
                if(eq_count!=c)throw -1;
                if(*beg!='~')throw -1;
                beg++;
                if(beg!=s.end())throw -1;
                
                cout<<"A"<<endl;
            }
            else if(*beg=='^'){
                beg++;
                bool f=false;
                while(1){
                    if(f&&*beg!='Q')break;
                    f=true;
                    if(*beg!='Q')throw -1;
                    beg++;
                    if(*beg!='=')throw -1;
                    beg++;
                }
                if(*beg!='~')throw -1;
                beg++;
                if(*beg!='~')throw -1;
                beg++;
                if(beg!=s.end())throw -1;
                
                cout<<"B"<<endl;
            }else throw -1;
            
            
            
        }catch(int e){
            cout<<"NA"<<endl;
            continue;
        }
    }
}