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

void printNA() {
    cout << "NA" << endl;
}

void judgeB(string s) {
    if (s.substr(s.size() - 2, 2) != "~~") {
        printNA();
        return;
    }
    for (int i = 2; i < s.size() - 3; i += 2) {
        if (s.substr(i, 2) != "Q=") {
            printNA();
            return;
        }
    }
    cout << "B" << endl;
}

void judgeA(string s) {
    int len = 0;
    int i;
    for (i = 2; s[i] == '='; i++) {
        len++;
    }
    if (len == 0 || s.size() < i || s[i] != '#') {
        printNA();
        return;
    }
    if (s.size() < 2 + 2 * len) {
        printNA();
        return;
    }
    for (i++ ; i <= 2 + 2 * len; i++) {
        if (s[i] != '=') {
            printNA();
            return;
        }
    }
    if (s.size() == i || s[i] != '~') {
        printNA();
    } else {
        cout << "A" << endl;
    }
}

int main() {
    int n;
    cin >> n;
    for (int c = 0; c < n; c++) {
        string s;
        cin >> s;
        if (s.substr(0, 2) == ">'") {
            judgeA(s);
        } else if (s.substr(0, 2) == ">^") {
            judgeB(s);
        } else {
            cout << "NA" << endl;
        }
    }
    return 0;
}