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

string makeSnakeA( int a )
{
    string s = ">\'";
    for( int i = 0; i < a; i++ )
        s += "=";
    s += "#";
    for( int i = 0; i < a; i++ )
        s += "=";
    s += "~";
    return s;
}

string makeSnakeB( int a )
{
    string s = ">^";
    for( int i = 0; i < a; i++ )
        s += "Q=";
    s += "~~";
    return s;
}

int main()
{
    int n;
    string str, s[200], t[200];
    for( int i = 0; i < 200; i++ )
    {
        s[i] = makeSnakeA( i + 1 );
        t[i] = makeSnakeB( i + 1 );
    }

    cin >> n;

    for( int i = 0; i < n; i++ )
    {
        cin >> str;

        int size = str.size();

        if( size >= 6 && s[(size-6)/2] == str )
            cout << "A" << endl;
        else if( size >= 6 && t[(size-6)/2] == str )
            cout << "B" << endl;
        else
            cout << "NA" << endl; 
    }

    return 0;
}
