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

string repeat(int n, string s) {
    string res = "";
    for (int i=0; i<n; ++i)
        res += s;
    return res;
}

int main() {
    int N; cin >> N;
    for (int i=0; i<N; ++i) {
        string snake; cin >> snake;

        int len = snake.size();
        if (len <= 4) {
            cout << "NA" << endl; continue;
        }

        string body = repeat(len/2 - 2, "=");
        string A = ">'" + body + "#" + body + "~";
        if (snake == A) {
            cout << "A" << endl; continue;
        }

        body = repeat(len/2 - 2, "Q=");
        string B = ">^" + body + "~~";
        if (snake == B) {
            cout << "B" << endl; continue;
        }

        cout << "NA" << endl;
    }
    return 0;
}