#include <iostream>
#include <string>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
int main()
{
    int n; cin >> n;
    while (n--) {
        string s; cin >> s;
        string head = s.substr(0, 2);
        bool ok = true;
        if (head == ">'") {
            int c = 0;
            REP(i, 2, s.size()) { if (s[i] != '=') break; c++; }
            if (s[c+2] != '#') ok = false;
            else if (s.substr(c+3, c) != string(c, '=')) ok = false;
            else if (s.size() != c*2 + 4) ok = false;
            else if (s[s.size()-1] != '~') ok = false;
            puts(ok ? "A" : "NA");
        }
        else if (head == ">^") {
            for (int i = 2; i < s.size()-2; i+=2)
                if (s.substr(i, 2) != "Q=") { ok = false; break; }
            if (ok && s.substr(s.size()-2) != "~~") ok = false;
            puts(ok ? "B" : "NA");
        }
        else puts("NA");
    }
    return 0;
}