#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>


typedef long long ll;
const double Eps = 1e-6;

using namespace std;

int main()
{
	char buf[256];
	int n;
	scanf("%d\n", &n);
	while (n--)
	{
		memset(buf, '\0', sizeof(buf));
		cin.getline(buf, sizeof(buf));
		if (strlen(buf) < 3)
		{
			puts("NA");
			continue;
		}

		const char* p = buf+2;
		if (buf[0] == '>' && buf[1] == '\'')
		{
			int c[4];
			memset(c, 0, sizeof(c));
			int sharp, tilde;
			int i;
			for (i = 0; p[i] != '\0'; ++i)
			{
				if (p[i] == '=')
					++c[0];
				else if (p[i] == '#')
				{
					++c[1];
					sharp = i;
				}
				else if (p[i] == '~')
				{
					++c[2];
					tilde = i;
				}
				else
					++c[3];
			}
			if (c[0] > 0 && c[0] % 2 == 0 && c[1] == 1 && sharp == i/2-1
				&& c[2] == 1 && tilde == i-1)
				puts("A");
			else
				puts("NA");
		}
		else if (buf[0] == '>' && buf[1] == '^')
		{
			int i;
			for (i = 0; p[i] != '\0'; i += 2)
			{
				if (p[i] != 'Q' || p[i+1] != '=')
					break;
			}
			if (i >= 2 && strcmp(p+i, "~~") == 0)
				puts("B");
			else
				puts("NA");
		}
		else
			puts("NA");
	}

	return 0;
}