#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;
int check( string S )
{
    //???????????¢>??£, ????°??????¢~??£ ????????????????????????NA
    if( S[ 0 ] != '>' || S[ S.size() - 1 ] != '~' )
    {
        return 0;
    }
    //A?¨?
    if( S[ 1 ] == '\'' )
    {
        if( S.substr( 2, S.size() - 3 ).find_first_not_of( "#=", 0 ) != string::npos ) return 0;
        string tmpB = S.substr( 2, S.find('#') - 2 );
        string tmpA = S.substr( S.find('#') + 1, S.size() - ( S.find('#') + 2 ) );
        return tmpB.size() == tmpA.size() ? 1 : 0;
    }
    //B?¨?
    if( S[ 1 ] == '^' && S[ S.size() - 2 ] == '~' )
    {
        if( S.substr( 2, S.size() - 4 ).find_first_not_of( "Q=", 0 ) != string::npos ) return 0;
        for( int i = 2; i < S.size() - 2; i += 2 )
        {
            if( S[ i ] != 'Q' && S[ i + i ] != '=' ) return 0;
        }
        return 2;
    }
}
int main()
{
    int N;
    cin >> N;
    
    vector<string> res{ "NA", "A", "B" };
    string S;
    while( cin >> S )
    {
        cout << res[ check( S ) ] << endl;
    }
    return 0;
}