

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#define int long long
#define rep(i, n) for (int i=0; i<n; i++)
#define REP(n) rep(i, n)
using namespace std;

int n;
string snake, head, tail, body;

signed main()
{
    cin >> n;

    REP(n)
    {   
        cin >> snake;

        if (snake.length() < 6)
        {   
            cout << "NA" << endl;
            continue;
        }   

        head = snake.substr(0, 2); 
        if (head == ">'")
        {   
            tail = snake.substr(snake.length() - 1, 1); 
            if (tail != "~")
            {   
                cout << "NA" << endl;
                continue;
            }   
            body = string((snake.length() - 4) / 2, '=') + "#" + string((snake.length() - 4) / 2, '=');
            if (snake.substr(2, snake.length() - 3) == body)
            {   
                cout << "A" << endl;
                continue;
            } else {
                cout << "NA" << endl;
                continue;
            }   
        } else if (head == ">^") {
            tail = snake.substr(snake.length() - 2, 2); 
            if (tail != "~~")
            {   
                cout << "NA" << endl;
                continue;
            }   
            body = ""; 
            int cnt = (snake.length() - 4) / 2;
            while (cnt--) body += "Q=";
            if (snake.substr(2, snake.length() - 4) == body)
            {
                cout << "B" << endl;
                continue;
            } else {
                cout << "NA" << endl;
                continue;
            }
        } else {
            cout << "NA" << endl;
            continue;
        }
    }

    return 0;
}