#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#define INF 999999
using namespace std;
int main(){
    int N,j;
    cin>>N;
    while(N--){
        int runcnt=0,j;
        char S[128000]={0};
        cin>>S;
            if(S[0]=='>'&&S[1]==39){
                int K=0;
                for(j=2;;j++){
                    if(S[j]=='='){
                        runcnt++;
                        K=1;
                    }
                    else if(S[j]=='#'){
                        break;
                    }
                    else {
                        cout<<"NA"<<endl;
                        goto end;
                    }
                }
                for(int i = j + 1;;i++){
                    if(S[i] == '='){
                        runcnt--;
                    }
                    else if(runcnt == 0 && S[i] == '~' && K == 1){
                        cout<<"A"<<endl;
                        goto end;
                    }
                    else{
                        cout<<"NA"<<endl;
                        goto end;
                    }
                }
            }
            else if(S[0] == '>' && S[1] == '^'){
                int F = 0;
                for(j = 2;;){
                    if(S[j] == 'Q' && S[j + 1] == '='){
                        j += 2;
                        F = 1;
                    }
                    else if(S[j] == '~' && S[j+1] == '~' && F == 1){
                        cout<<"B"<<endl;
                        goto end;
                    }
                    else{
                        cout<<"NA"<<endl;
                        goto end;
                    }
                }
            }
            else cout<<"NA"<<endl;
            end:;
    }
    return 0;
}