#include <iostream>
using namespace std;

typedef string::iterator S;
string ss;

string snakeA(S it){
    int count = 0;
    for(;it!=ss.end()&&*it=='=';it++,count++);
    if(it==ss.end()||*it!='#') return "NA";
    it++;
    for(;it!=ss.end()&&*it=='=';it++,count--);
    if(it==ss.end()||*it!='~') return "NA";
    it++;
    if(*it||count) return "NA";
    return "A";
}

string snakeB(S it){
    while(true){
        if(it==ss.end()||*it++!='Q') return "NA";
        if(it==ss.end()||*it++!='=') return "NA";
        if(it==ss.end()) return "NA";
        if(*it=='~') break;
    }
    if(*++it=='~'&&!*++it) return "B";
    return "NA";
}

string snake(string ss){
    S it = ss.begin();
    if( *it++ != '>' ) return "NA";
    if( *it == '\'' ) return snakeA(++it);
    else if( *it == '^' ) return snakeB(++it);
    else return "NA";
}

int main(){
    int n;
    for(cin>>n,cin.ignore();n--;){
        getline(cin, ss);
        cout<<snake(ss)<<endl;
    }
    return 0;
}