#include <bits/stdc++.h>
using namespace std;
int main(void){
    int n;
    cin>>n;
    for (int i=0;i<n;i++) {
        string s;
        cin>>s;
        if (s.size()<6||s.size()%2!=0){
            cout<<"NA"<<endl;
        } else {
            if (s.at(0)=='>'&&s.at(1)=='\''&&s.at(s.size()-1)=='~') {
                string tmp=s.substr(2,s.size()-3);
                string front=tmp.substr(0,(tmp.size()-1)/2);
                string back=tmp.substr((tmp.size()+2)/2,(tmp.size()-1)/2);
                if (front==back) {
                    cout<<"A"<<endl;
                } else {
                    cout<<"NA"<<endl;
                }
            } else if (s.at(0)=='>'&&s.at(1)=='^'&&s.at(s.size()-1)=='~'&&s.at(s.size()-2)=='~') {
                string center=s.substr(2,s.size()-4);
                for (int j=0;j<center.size();j+=2) {
                    if (center.at(j)!='Q'||center.at(j+1)!='=') {
                        cout<<"NA"<<endl;
                        break;
                    }
                    if (j==center.size()-2) {
                        cout<<"B"<<endl;
                    }
                }
            } else {
                cout<<"NA"<<endl;
            }
        }
    }
}
