#include <iostream>
#include <string>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int main()
{
	int n;
	cin >> n;
	
	rep(i,n)
	{
		string s;
		cin >> s;

		if(s[0] == '>' && s[1] == '\'' && s[2] == '=')
		{
			int i = 2;
			int cnt = 0;
			while(s[i] == '=')
			{
				i++;
				cnt++;
			}

			if(s[i] == '#' && i < s.size())
			{
				i++;
				while(s[i] == '=')
				{
					if(i == s.size()-1) break;
					i++;
					cnt--;
				}
			}

			if(cnt == 0 && s[i] == '~') cout << "A" << endl;
			else cout << "NA" << endl;
		}
		else if(s[0] == '>' && s[1] == '^' && s[2] == 'Q')
		{
			int i = 2;
			int cnt = 0;
			bool end = false;
			while(i+1 < s.size())
			{
				if(s[i] == 'Q' && s[i+1] == '=')
				{
					i += 2;
				}
				else if(s[i] == '~' && s[i+1] == '~')
				{
					end = true;
					cout << "B" << endl;
					i += 2;
				}
				else
				{
					break;
				}
			}

			if (!end) cout << "NA" << endl;
		}
		else
		{
			cout << "NA" << endl;
		}
	}
	return 0;
}