#include<iostream>
#include<string>

using namespace std;

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