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

int n;

int main()
{
    cin >> n;

    for (int i = 0; i < n; i++) {
        string s, t, w;

        cin >> s;
        t = "NA";

        int m = s.size();

        if (m > 4 && m % 2 == 0) {
            string u(m - 4, '=');
            u.insert(u.begin() + m / 2 - 2, '#');
            if (s == ">'" + u + '~')
                t = "A";

            for (int j = 0; j < (m / 2 - 2); j++)
                w += "Q=";

            if (s == ">^" + w + "~~")
                t = "B";            
        }

        cout << t << endl;
    }

    return 0;
}

