#include<iostream>
using namespace std;

//入力文字列
char str_input[110] = {NULL};

//----- 初期化関数 -----//
void f_syokika();

//----- '='の始端と終端の位置から割り出す -----//
int f_A(int);

//----- Bかどうかの関数 -----//
bool f_B();

//文字列
const char str_A[] = ">'=#~";
const char str_B[] = ">^Q=~~";

int main(void)
{
	//入力数
	int n = 0;

	cin >> n;

	for (int i = 0; i < n; i++)
	{
		//文字列初期化
		f_syokika();
		//文字列を読み込む
		cin >> str_input;
		//A , Bどちらかの可能性があるflag
		bool flag_A = false;
		bool flag_B = false;
		//=の個数を数える
		int ikoru = 0;
		//配列で使うカウント変数
		int count_h_A = 0;
		int count_h_B = 0;

		//NAflag
		bool flag_NA = true;

		int count = 0;

		//A , Bかどうか
		if (str_input[count++] == str_A[count_h_A])
		{
			count_h_A++;
			count_h_B++;
			//A
			if (str_input[1] == str_A[count_h_A])
			{
				count_h_A++;
				count++;
				while (str_input[count] != '\0')
				{
					if (str_input[count] != '=' && str_input[count] != '#' && str_input[count] != '~')
					{
						break;
					}
					if (str_input[count] == '#')
					{
						int kettei = 0;
						//戻り値が-1ならば違う
						if ((kettei = f_A(count)) == -1)
						{
							cout << "NA" << endl;
							break;
						}
						//次とその先次第
						if (str_input[kettei] == '~' && str_input[kettei + 1] == '\0')
						{
							cout << "A" << endl;
						}
						else
						{
							cout << "NA" << endl;
						}
					}

					count++;
				}
			}
			else if (str_input[1] == str_B[count_h_B])
			{
				count_h_B++;
				count++;

				if (f_B() == true)
				{
					cout << "B" << endl;
				}
				else
				{
					cout << "NA" << endl;
				}
			}
			else
			{
				cout << "NA" << endl;
			}
		}
		else
		{
			cout << "NA" << endl;
		}
	}

	return 0;
}



//*****----- 初期化関数 -----*****//

void f_syokika()
{
	for (int i = 0; i < 110; i++)
	{
		str_input[i] = '\0';
	}
}



//*****----- '='の始端と終端の位置から割り出す -----*****//

int f_A(int index)
{
	//後ろ・前に動かす
	int usiro = index - 1;
	int mae = index + 1;
	//後ろ、前カウント変数
	int count_usiro = 0;
	int count_mae = 0;

	while (true)
	{
		//インデックスの範囲内
		if (usiro < 0)
		{
			break;
		}
		//違う文字なら抜ける
		if (str_input[usiro] != '=')
		{
			break;
		}
		//抜けてない = '='である
		count_usiro++;
		usiro--;
	}

	//インデックスはOK
	while (str_input[mae] == '=')
	{
		mae++;
		count_mae++;
	}

	//同じ数且つ0でない
	if (count_usiro == count_mae && count_usiro != 0)
	{
		return mae;
	}
	return -1;
}



//*****----- Bかどうかの関数 -----*****//

bool f_B()
{
	//Qと＝のカウント変数
	int count_Q = 0;
	int count_ikoru = 0;
	
	int i = 2;
	while (str_input[i] == 'Q')
	{
		count_Q++;
		i += 2;
	}

	i = 3;
	while (str_input[i] == '=')
	{
		count_ikoru++;
		i += 2;
	}

	if (count_Q != count_ikoru)
	{
		return false;
	}
	if (str_input[i - 1] == '~' && str_input[i] == '~' && str_input[i + 1] == '\0')
	{
		return true;
	}
	return false;
}