#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

bool isa(string s){
    if(s.substr(0,2) != ">'") return false;
    int x = 2;
    int c = 0;
    while(s[x+c] == '='){
	c++;
    }
    if(s[x+c] != '#') return false;
    x = x + c + 1;
    for(int i=0;i<c;i++){
	if(s[x+i] != '=') return false;
    }
    x += c;
    if(s[x] != '~') return false;
    return true;
}

bool isb(string s){
    if(s[0] != '>') return false;
    if(s[1] != '^') return false;
    int x = 2;
    int c = 0;
    while(s[x+c] == 'Q' && s[x+1+c] == '='){
	c += 2;
    }
    x += c;
    if(s[x] != '~') return false;
    if(s[x+1] != '~') return false;
    return true;
}

int main(){
    string s = "";
    int n;
    cin >> n;
    REP(i,n){
	cin >> s;
	if(s.size() < 6){
	    cout << "NA" << endl;
	    continue;
	}
	if(isa(s)) cout << "A"<<endl;
	else if(isb(s)) cout << "B" << endl;
	else cout << "NA" << endl;
    }
    return 0;
}