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

int main() {
    int n;
    cin >> n;
    string s;
    for (int i=0; i<n; ++i) {
        cin >> s;
        int m = s.size();
        string res = "NA";
        if (s.size() < 6 || s.size() % 2 == 1) {
            // pass
        } else if (s.substr(0,2) == ">'" && s[m-1] == '~') {
            res = "A";
            if (s[m/2] != '#') {
                res = "NA";
            }
            for (int j=2; j<=m-2; ++j) {
                if (j == m/2) continue;
                if (s[j] != '=') {
                    res = "NA";
                    break;
                }
            }
        } else if (s.substr(0,2) == ">^" && s.substr(m-2,2) == "~~") {
            res = "B";
            for (int j=2; j<=m-4; j+=2) {
                if (s[j] != 'Q' || s[j+1] != '=') {
                    res = "NA";
                    break;
                }
            }
        }
        cout << res << endl;
    }
    return 0;
}