#include <iostream>
#include <string>
using namespace std;

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