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

int main()
{
	int i, count;
	string s, name;
	getline(cin, s);
	while (getline(cin, s)) {
		if (s[1]=='\'') {
			name = "A";
			if (s[2]!='=') name = "NA";
			else {
				count = 0;
				for (i = 2; s[i]=='='; i++) count++;
				if (s[i]!='#') name = "NA";
				else {
					for (; count > 0; count--)
						if (s[++i] != '=') {
							name = "NA";
							break;
						}
					if (s[++i]!='~'||i!=s.size()-1) name = "NA";
				}
			}
		} else if (s[1]=='^') {
			name = "B";
			for (i = 2; i < s.size()-2; i += 2) {
				if (s.size()&1) {
					if (s[i+2]!='~'||s[i+3]!='~') name="NA";
					break;
				}
				if (s[i]!='Q'||s[i+1]!='=') {
					if (s[i+2]!='~'||s[i+3]!='~') name="NA";
					break;
				}
			}
		}
		cout<<name<<endl;
	}
	return 0;
}