# define _CRT_SECURE_NO_WARNINGS 1
# include <iostream>
# include <numeric>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iomanip>
# include <queue>
# include <sstream>
# include <climits>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <forward_list>
# include <map>
# include <stack>
using namespace std;

# define INF ((int)(1<<30))
# define REP(i,n) for(int i=0;i<(int)n;i++)
# define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
# define TORAD 2.0*M_PI/360.0
# define INT(x) int x;cin>>x;
# define ALL(x) (x).begin(),(x).end()
# define RALL(x) (x).rbegin(),(x).rend()
# define DEBUG(x) cout<<#x<<" :"<<x<<endl;
# define EPS 1e-12

template<class T> void debug(T a) { for (auto i : a)cout << i << endl; }

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = { 0,-1,1,0 }, dy[4] = { 1,0,0,-1 };

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		string a;
		cin >> a;
		bool ok = true;
		string ans;
		if (a[0] == '>'&&a[1] == '\'')ans = "A";
		else if (a[0] == '>'&&a[1] == '^')ans = "B";
		else ok = false;
		if (ans == "A")
		{
			int c = 0;
			for (int j = 2; j < a.size(); j++)
			{
				if (a[j] == '=')c++;
				else break;
			}
			if (c > a.size() / 2) {
				ok = false; goto end;
			}
			if (a[2 + c] != '#')
			{
				ok = false; goto end; 
			}
			int d = 0;
			for (int j = 2 + c +1; j < a.size(); j++)
			{
				if (a[j] == '=')d++;
				else break;
			}
			if (d != c)
			{
				ok = false; goto end;
			}
			int e = 0;
			for (int j = 2 + c + d + 1; j < a.size(); j++)
			{
				if (a[j] == '~')e++;
				else
				{
					ok = false;
					goto end;
				}
			}
			if (e != 1)ok = false;

		}
		if (ans == "B")
		{
			int j;
			for (j = 2; j < a.size() - 1; j += 2)
			{
				if (a[j] != 'Q' || a[j + 1] != '='){
					break;
				}
			}
			if (a[j] != '~' || a[j + 1] != '~')ok = false;
			if (j != a.size() - 2)ok = false;
		}
		end:
		if (ok)cout << ans << endl;
		else cout << "NA" << endl;

	}

}