#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>

using namespace std;

typedef long long ll;
typedef pair<double,int> P;

static const double eps = 10e-9;

bool isA(string str){
	if(str.substr(0,2) != ">'") return false;
	string rear = str.substr(2,str.size()-2);
	
	int eq = count(rear.begin(),rear.end(),'=');
	int shrp = count(rear.begin(),rear.end(),'#');
	int til = count(rear.begin(),rear.end(),'~');

	if(shrp != 1 || (eq + shrp+til) != rear.size() || til != 1) return false;

	int lhs=0;
	int rhs=0;
	bool flag=true;
	for(int i=0;i<rear.size()-1;i++){
		if(rear[i]=='#'){
			flag = false;
			continue;
		}
		flag ? lhs++ : rhs++;
	}

	return lhs == rhs && lhs != 0;
}

bool isB(string str){
	if(str.substr(0,2) != ">^") return false;
	string rear = str.substr(2,str.size()-2);

	bool isok=false;
	int i;
	for(i=2;i<rear.size();){
		if(rear.substr(i,2) == "Q="){
			isok=true;
			i+=2;
		}
		else break;
	}

	return (isok && rear.substr(i,2) == "~~") && (i+2 == rear.size());
}

int main(){
	int n;
	while(~scanf("%d",&n)){
		for(int i=0;i<n;i++){
			string str;
			cin >> str;

			if(isA(str)) cout << "A" << endl;
			else if(isB(str)) cout << "B" << endl;
			else cout << "NA" << endl;
		}
	}
	
}