#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;
}


bool checkA( string s, int k, bool f0, bool f1, bool f2 ) {
    int n = s.size();
    if ( k == n - 1 ) {
        if ( s[k] != '~' ) return false;
        return f0 && f1 && f2;
    }
    if ( k == 0 ) {
        if ( n < 2 ) return false;
        if ( s[k] != '>' || s[k+1] != '\'' ) return false;
        return checkA( s, k + 2, f0, f1, f2 );
    }
    if ( ! f0 ) {
        if ( s[k] != '=' ) return false;
        return checkA( s, k + 1, true, f1, f2 );
    }
    if ( ! f1 ) {
        if ( s[k] == '#' ) {
            return checkA( s, k + 1, f0, true, f2 );
        } else if ( s[k] == '=' ) {
            return checkA( s, k + 1, f0, f1, f2 );
        }
        return false;
    }
    if ( ! f2 ) {
        if ( s[k] != '=' ) return false;
        return checkA( s, k + 1, f0, f1, true );
    }
    return s[k] == '=' && checkA( s, k + 1, f0, f1, f2 );
}

bool checkB( string s, int k ) {
    int n = s.size();
    if ( k == 0 ) {
        if ( n < 2 ) return false;
        if ( s[k] != '>' || s[k+1] != '^' ) return false;
        return checkB( s, k + 2 );
    }
    if ( s[k] == 'Q' ) {
        if ( k + 1 >= n ) return false;
        if ( s[k+1] != '=' ) return false;
        return checkB( s, k + 2 );
    }
    if ( k + 2 == n && s[k] == '~' ) {
        return s[k+1] == '~';
    }
    return false;
}

string solve( string s ) {
    if ( checkA( s, 0, false, false, false ) ) return "A";
    if ( checkB( s, 0 ) ) 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;
}