#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
bool check_A( string &S )
{
    auto sz = S.size();
    //size ??? 6 ??\?????????????????? false
    if( sz < 6 )
    {
        return 0;
    }
    //?????????[>'=], ????°????[~]?????????????????´?????? false
    if( S[ 0 ] != '>' || S[ 1 ] != '\'' || S[ 2 ] != '=' || S[ sz - 1 ] != '~' )
    {
        return 0;
    }
    //??????[>']??¨????°?[~]???????????????
    string R = S.substr( 2, sz - 3 );
    sz = R.size();
    //R ???[#,=]??\???????????????????????????????????? false
    if( R.find_first_not_of( "#=", 0 ) != string::npos )
    {
        return 0;
    }
    if( sz % 2 == 0 )
    {
        return 0;
    }
    //R ?????????????????????????????£???[#]???????????\??????[#]????????£?????? false
    auto pos = R.find( "#" );
    if( R.find( "#", pos + 1 ) != string::npos )
    {
        return 0;
    }
    // S ???????????????[#]???????????£?????? false
    if( R[ sz / 2 ] != '#' )
    {
        return 0;
    }
    return 1;
}
bool check_B( string &S )
{
    auto sz = S.size();
    //size ??? 6 ??\?????????????????? false
    if( sz < 6 )
    {
        return 0;
    }
    //?????????[>^], ????°????[~~]?????????????????´?????? false
    if( S[ 0 ] != '>' || S[ 1 ] != '^' || S[ sz - 1 ] != '~' || S[ sz - 2 ] != '~' )
    {
        return 0;
    }
    string R = S.substr( 2, sz - 4 );
    sz = R.size();
    // S ???size????\???°?????£?????? false
    if( sz % 2 != 0 )
    {
        return 0;
    }
    for( int i = 0; i < sz; i += 2 )
    {
        //[Q=]?????£?¶???§????????£?????? false
        if( R[ i ] != 'Q' || R[ i + 1 ] != '=' )
        {
            return 0;
        }
    }
    return 1;
}
int main()
{
    int N;
    cin >> N;
    
    REP( i, N )
    {
        string S;
        cin >> S;
        
        string res{ "NA" };
        if( check_A( S ) )
        {
            res = "A";
        }
        else if( check_B( S ) )
        {
            res = "B";
        }
        cout << res << endl;
    }
    return 0;
}