#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--)
	{
		cin.getline(buf, sizeof(buf));
		string s(buf);
		bool ok = false;
		for (int i = 1; i < 100; ++i)
		{
			string a = ">'";
			for (int j = 0; j < i; ++j)
				a += "=";
			a += "#";
			for (int j = 0; j < i; ++j)
				a += "=";
			a += "~";
			if (s == a)
			{
				puts("A");
				ok = true;
				break;
			}

			string b = ">^";
			for (int j = 0; j < i; ++j)
				b += "Q=";
			b += "~~";
			if (s == b)
			{
				puts("B");
				ok = true;
				break;
			}
		}
		if (!ok)
			puts("NA");
	}

	return 0;
}