#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( ( S.find( ">'=" ) != 0 && S.find( "~" ) != sz - 1 ) || sz < 6 )
    {
        return 0;
    }
    //??????[>']??¨????°?[~]???????????????
    S = S.substr( 2, sz - 3 );
    sz = S.size();
    //??°?????? S ???[#,=]??\???????????????????????????????????? false
    if( S.find_first_not_of( "#=", 0 ) != string::npos )
    {
        return 0;
    }
    //??°?????? S ????????? ????????????????????£???[#]???????????\??????[#]????????£?????? false
    auto pos = S.find( "#" );
    if( S.find( "#", pos + 1 ) != string::npos )
    {
        return 0;
    }
    // S ???????????????[#]???????????£??????, ????????? S ???size?????¶??°?????£?????? false
    if( S[ sz / 2 ] != '#' || sz % 2 == 0 )
    {
        return 0;
    }
    return 1;
}
bool check_B( string &S )
{
    auto sz = S.size();
    //?????????[>^Q=], ????°????[~~]???????????????, ????????? size ??? 6 ??\?????????????????? false
    if( ( S.find( ">^Q=" ) != 0 && S.find( "~~" ) != sz - 2 ) || sz < 6 )
    {
        return 0;
    }
    S = S.substr( 2, sz - 4 );
    sz = S.size();
    // S ???size????\???°?????£?????? false
    if( sz % 2 != 0 )
    {
        return 0;
    }
    for( int i = 0; i < sz; i += 2 )
    {
        //[Q=]?????£?¶???§????????£?????? false
        if( S[ i ] != 'Q' && S[ 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";
        }
        if( check_B( S ) )
        {
            res = "B";
        }
        cout << res << endl;
    }
    return 0;
}