#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    int n;
    cin >> n;

    while(--n >= 0){
        string s;
        cin >> s;
        int n = s.size();
        if(n < 6 || n % 2 == 1){
            cout << "NA" << endl;
            continue;
        }

        string sa(n, '=');
        sa[0] = '>';
        sa[1] = '\'';
        sa[n/2] = '#';
        sa[n-1] = '~';
        if(s == sa){
            cout << 'A' << endl;
            continue;
        }

        string sb(n, '=');
        sb[0] = '>';
        sb[1] = '^';
        for(int i=2; i<n-2; i+=2)
            sb[i] = 'Q';
        sb[n-2] = '~';
        sb[n-1] = '~';
        if(s == sb)
            cout << 'B' << endl;
        else
            cout << "NA" << endl;
    }

    return 0;
}