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