#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair <INT, INT> II;
typedef vector <II> VII;

template<class T> ostream& operator << ( ostream& os, vector<T> v ) {
    for ( typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i ) {
        os << *it_i << ", ";
    }
    return os;
}


typedef set <string> SS;

bool checkA( string s ) {
    SS S;
    for ( int i = 1; i <= 100; ++ i ) {
        S.insert( ">'" + string( i, '=' ) + "#" + string( i, '=' ) + "~" );
    }
    return S.count( s ) > 0;
}

bool checkB( string s ) {
    SS S;
    for ( int i = 1; i <= 100; ++ i ) {
        VS con( i, "Q=" );
        S.insert( ">^" + accumulate( con.begin(), con.end(), string() ) + "~~" );
    }
    return S.count( s ) > 0;
}

string solve( string s ) {
    if ( checkA( s ) ) return "A";
    if ( checkB( s ) ) return "B";
    return "NA";
}

int main() {
    int n;
    cin >> n;
    for ( int i = 0; i < n; ++ i ) {
        string s;
        cin >> s;
        cout << solve( s ) << endl;
    }
    return 0;
}