#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>

using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 10000000
#define rep(i,j) for(int i=0;i<(j);++i)
#define reps(i,j,k) for(int i=j;i<k;++i)
typedef long long ll;
typedef unsigned long long ull;
char snake[256]={0};
int check(int len){
	if(len % 2 == 1)return INF;
	if(snake[0] == '>'){
		if(snake[1] == 39){
			if(snake[len-1] != '~')return INF;
			int cnt = 0;
			int x = 0;
			rep(i,len){
				if(snake[i] == '#'){
					cnt++;
					x = i;
				}
			}
			if(cnt != 1)return INF;
			if(snake[2] != '=')return INF;
			reps(i,3,x){
				if(snake[i] != '=')return INF;
			}
			reps(i,x+1,len-1){
				if(snake[i] != '=')return INF; 
			}
			return (x-2 == len-x-2) ? 1 : INF; 
		}
		else if(snake[1] == '^'){
			if(snake[len-1] != '~' || snake[len-2] != '~')return INF;
			if(snake[2] != 'Q' || snake[3] != '=')return INF;
			for(int i = 4; i < len-2; i += 2){
				if(snake[i] != 'Q' || snake[i+1] != '=')return INF;
			}
			return 0;
		}
	}
	return INF;
}
int main(){
	int n;
	scanf("%d",&n);
	rep(q,n){
		memset(snake,0,sizeof(snake));
		scanf("%s",snake);
		int len = strlen(snake);
		int res = check(len);
		(res == 1) ? puts("A") : (res == 0) ? puts("B") : puts("NA");
	}
	return 0;
}