#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int main() {
	int n=in();
	string snake;
	while(n--){
		cin>>snake;
		int i;
		bool a=true,b=true;
		if(snake.size()<6||snake.size()%2!=0){
			a=false;
			b=false;
		}
		for(i=0;i<snake.size();i++){
			if(i==0){
				if(snake[i]!='>'){
					a=false;
					b=false;
				}
			}
			if(i==1){
				if(snake[i]!='\'')a=false;
				if(snake[i]!='^')b=false;
			}
			if(i*2==snake.size()){
				if(snake[i]!='#')a=false;
			}
			if(i==snake.size()-1){
				if(snake[i]!='~'){
					a=false;
					b=false;
				}
			}
			if(i==snake.size()-2){
				if(snake[i]!='~')b=false;
			}
			if(i!=0&&i!=1&&i*2!=snake.size()&&i!=snake.size()-1){
				if(snake[i]!='=')a=false;
			}
			if(i%2==0&&i!=0&&i!=snake.size()-2){
				if(snake[i]!='Q')b=false;
			}
			if(i%2==1&&i!=1&&i!=snake.size()-1){
				if(snake[i]!='=')b=false;
			}
		}
		if(a){
			cout<<"A\n";
		}else if(b){
			cout<<"B\n";
		}else{
			cout<<"NA\n";
		}
	}
	return 0;
}