#include <bits/stdc++.h>
using namespace std;

int main() {
    int n; cin >> n;
    
    for (int i=0; i<n; i++) {
        string S; cin >> S;
        
        if (S.size()<4) {
            cout << "NA" << endl;
            continue;
        }
        
        if (S.substr(0, 2)==">'" && S[S.size()-1]=='~') {
            int mark = -1;
            for (int j=2; j<S.size()-1; j++) {
                if (S[j]=='#') mark = j;
            }
            bool flag = true;
            for (int j=2; j<S.size()-1; j++) {
                if (j==mark) continue;
                if (S[j]!='=') flag = false;
            }
            if (flag && mark!=-1 && mark-2==S.size()-2-mark && mark-2>0) cout << 'A' << endl;
            else cout << "NA" << endl;
        }
        else if (S[0]=='>' && S[1]=='^' && S[S.size()-2]=='~' && S[S.size()-1]=='~') {
            int idx = 2, cnt = 0;
            while (true) {
                if (S[idx]=='Q' && S[idx+1]=='=') {
                    cnt++;
                    idx += 2;
                }
                else break;
            }
            if (cnt>0 && idx==S.size()-2) cout << 'B' << endl;
            else cout << "NA" << endl;
        }
        else cout << "NA" << endl;
    }
}
