#include <iostream>
#include <string>

using namespace std;
#define repeat(n, i) for (int i = 0; i < (n); i++) 

bool is_a(string& s) {
    if (s[0] != '>' || s[1] != '\'') return false;
    int eq_count = 0;
    int i;
    for (i = 2; s[i] == '='; i++) {
        eq_count++;
    }
    if (s[i++] != '#') return false;
    for (; s[i] == '='; i++) {
        eq_count--;
    }
    if (eq_count == 0 && s[i] == '~') return true;
    return false;
}

bool is_b(string& s) {
    if (s[0] != '>' || s[1] != '^') return false;
    int len = s.size();
    if (len % 2 == 1) return false;
    int i;
    for (i = 2; i < len-2; i+=2) {
        if (s[i] != 'Q' || s[i+1] != '=') return false;
    }
    if (s[i] != '~' || s[i+1] != '~') return false;
    return true;
}

int main() {
    int n; cin >> n;
    repeat(n, i) {
        string s; cin >> s;
        if (is_a(s)) {
            cout << "A" << endl;
            continue;
        } else if (is_b(s)) {
            cout << "B" << endl;
            continue;
        } else {
            cout << "NA" << endl;
        }
    }
    return 0;
}