#include <bits/stdc++.h>
using namespace std;

bool checkA(char* s){
	if(s[0] != '>' || s[1] != '\'' || s[2] != '='){
		return false;
	}
	int slen = 0;
	while(s[slen+2] == '='){
		slen++;
	}
	if(s[slen+2] != '#'){
		return false;
	}
	for(int i = slen+3; i < slen*2+3;i++){
		if(s[i] != '='){
			return false;
		}
	}
	return (s[slen*2+3] == '~' && s[slen*2+4] == '\0');
}

bool checkB(char* s){
	if(s[0] != '>' || s[1] != '^' || s[2] != 'Q' || s[3] != '='){
		return false;
	}
	int slen = 0;
	while(s[slen*2+2] == 'Q' && s[slen*2+3] == '='){
		slen++;
	}
	return (s[slen*2+2] == '~' && s[slen*2+3] == '~' && s[slen*2+4] == '\0');
}

int main(){
	int n;
	char S[1024];

	scanf("%d",&n);
	for(int step = 0; step < n; step++){
		scanf("%s",S);
		if(checkA(S)){
			printf("A\n");
		}
		else if(checkB(S)){
			printf("B\n");
		}
		else{
			printf("NA\n");
		}
	}

	return 0;
}


