#include <bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	cin >> n;

	while(n--)
	{
		string str;
		string kp;
		string set;
		int flag=0;
		int count=0;
		int i=0;

		cin >> str;
		for(i=0;i<2;i++)kp+=str[i]++;
		
		if(kp==">'")
		{
			while(str[i]!='#')
			{
				if(str[i]=='=')count++;
				else flag=-1;

				if(flag==-1)break;
				i++;	
			}
			i++;
			while(str[i]!='~')
			{
				if(str[i]=='=')count--;
				else flag=-1;

				if(flag==-1)break;
				i++;
			}
			if(flag!=-1 && !count)flag=1;

			count=0;
			for(int j=i;j<str.length();j++)
			{
				if(str[i]!='~')flag=-1;
				else count++;

				if(flag==-1)break;
			}
			if(count!=1)flag=-1;
		}
		else if(kp==">^")
		{
			while(i<str.length()-2)
			{
				count=2;
				while(count--)
				{
					set+=str[i];
					i++;
				}
				if(set=="Q=")flag=2;
				else flag=-1;

				set.erase(set.begin(),set.end());
				if(flag==-1)break;
			}
			for(i=str.length()-2;i<str.length();i++)set+=str[i];
			if(set=="~~" && flag==2)flag=2;
			else flag=-1;
		}

		if(flag==1) cout << "A" << endl;
		else if(flag==2)cout << "B" << endl;
		else cout << "NA" << endl;
	}
	
	return 0;
}