#include <iostream>
#include <string>

using namespace std;

int main(int argc, char **argv)
{
	int n, m, s, t;
	string str, p;
	cin >> m;
	while(m--){
		cin >> str;
		n = str.size();
		
		p = (str[1] == '^')? "B" : "A";
		if(str[n-1] != '~'){
			cout << "NA" << endl;
			continue;
		}
		
		if(p == "A"){
			s = (n+1)/2;
			if(str.substr(2, s-1) != str.substr(s+1, s-1) && str[s] != '#'){
				cout << "NA" << endl;
				continue;
			}
		} else {
			if(str[n-2] != '~'){
				cout << "NA" << endl;
				continue;
			}
			for(int i = 2; i < n-2; i+=2){
				if(str.substr(i, 2) != "Q="){
					n = -1;
					cout << "NA" << endl;
					break;
				}
			}
			if(n == -1){
				continue;
			}
		}
		cout << p << endl;
	}
	return 0;
}