#include <iostream>
#include <string>

using namespace std;

void solve()
{
	int n;
	cin >> n;
	while(n--)
	{
		string str;
		cin >> str;
		if(str[0] == '>' && str[1] == '\'' && str[str.size() - 1] == '~')
		{
			int count[2] = {0, 0};
			int pos = 0;
			for(int i = 2; i < str.size(); ++i)
			{
				if(str[i] == '=')
				{
					count[pos]++;
				}
				else if(str[i] == '#')
				{
					pos++;
				}
			}
			if(count[0] > 0 && count[0] == count[1])
			{
				cout << "A" << endl;
			}
			else 
			{
				cout << "NA" << endl;
			}
		}
		else if(str[0] == '>' && str[1] == '^' && str[str.size() - 2] == '~' && str[str.size() - 1] == '~')
		{
			for(int i = 2; i < str.size() - 1; ++i)
			{
				if(str[i] == 'Q' && str[i + 1] == '=')
				{
					cout << "B" << endl;
					goto NEXT;
				}
			}
			cout << "NA" << endl;
		}
		else
		{
			cout << "NA" << endl;
		}
NEXT:
		;
	}
}

int main()
{
	solve();
	return(0);
}