#include <iostream>
#include <string>
using namespace std;

bool isa(string s) {
    int pos = 2;
    while(s[pos] == '=') pos++;
    if(pos == 2 || s[pos] != '#') return false;

    int cnt = pos++ - 2;
    for(int i=0; i<cnt; ++i) {
        if(s[pos+i] != '=') return false;
    }
    pos += cnt;
    if(pos == s.length()-1 && s[pos] == '~') return true;
    return false;
}

bool isb(string s) {
    int pos = 2;
    for(; pos < s.length()-2; pos += 2)
        if(s[pos] != 'Q' || s[pos+1] != '=') return false;

    if(s[s.length()-1] == s[s.length()-2] && s[s.length()-1] == '~') return true;
    return false;
}

int main() {
    int n;
    cin>>n;

    string s;
    for(int i=0; i<n; ++i) {
        cin>>s;
        if(s[1] == '\'') {
            if(isa(s)) {
                cout<<"A"<<endl;
            }else{
                cout<<"NA"<<endl;
            }
        } else {
            if(isb(s)) {
                cout<<"B"<<endl;
            }else{
                cout<<"NA"<<endl;
            }
        }
    }
}