#include<iostream>
using namespace std;
int main(){

    int n;
    cin>>n;
    while(n--){

        string str;
        cin>>str;
        if(str[0]!='>')cout<<"NA"<<endl;
        else{
            if(str[1]=='\''&&str[2]=='='){
                int e=2;
                for(;str[e]=='=';e++);
                if(str[e]=='#'){
                    int f=e+1;
                    for(;str[f]=='=';f++);
                    if(e==f-e+1&&str[f]=='~'&&str[f+1]=='\0')cout<<"A"<<endl;
                    else cout<<"NA"<<endl;
                }
                else cout<<"NA"<<endl;
            }
            else if(str[1]=='^'&&str[2]=='Q'){
                int e,flg=0;
                for(e=2;str[e]=='Q';e+=2){
                    if(str[e+1]!='='){
                        cout<<"NA"<<endl;
                        flg=1;
                        break;
                    }
                }
                if(!flg){
                    if(str[e]=='~'&&str[e+1]=='~'&&str[e+2]=='\0')cout<<"B"<<endl;
                    else cout<<"NA"<<endl;
                }
            }
            else cout<<"NA"<<endl;
        }

    }

    return 0;
}