#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
	bool b;
	int n, i, j, t, c[2];
	char s[201];
	cin >> n;
	for (i = 0; i < n; i++)
	{
		b = true;
		cin >> s;
		if (s[0] == '>')
		{
			if (s[1] == '\'')
			{
				for (j = 2, t = c[0] = c[1] = 0; j < strlen(s)-1; j++)
				{
					if (s[j] == '=')
						c[t]++;
					else if (t == 0 && s[j] == '#')
						t++;
					else
					{
						b = false;
						break;
					}
				}
				if (b && c[0] > 0 && c[0] == c[1] && s[strlen(s)-1] == '~')
					cout << "A" << endl;
				else
					cout << "NA" << endl;
				continue;
			}
			else if (s[1] == '^')
			{
				for (j = 2; j < strlen(s)-2; j+=2)
				{
					if (s[j] != 'Q' || s[j+1] != '=')
					{
						b = false;
						break;
					}
				}
				if (b && s[strlen(s)-2] == '~' && s[strlen(s)-1] == '~')
					cout << "B" << endl;
				else
					b = false;
				continue;
			}
			else
				b = false;
		}
		else
			b = false;
		if (!b)
			cout << "NA" << endl;
	}
}