#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <stdio.h>
#include <ctype.h>
#include <string>
#include <iostream>
#include <vector>
#include <stack>
#include <fstream>
#include <sstream>
#include <queue>
#include <exception>
#include <cmath>
#include <numeric>
#include <map>
#include <algorithm>
#include <bitset>
#include <set>
#include <functional>

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)

using namespace std;
typedef long long int lint;

void AOJ0139()
{
	string snake;
	cin >> snake;
	if (snake[0] != '>') {
		cout << "NA" << endl;
		return;
	}
	//A
	if (snake[1] == '\'') {
		if (snake[2] != '=') {
			cout << "NA" << endl;
			return;
		}
		int eq_count = 0;
		int pos = 2;
		while (snake[pos] == '=') {
			eq_count++;
			pos++;
		}
		if (snake[pos] != '#') {
			cout << "NA" << endl;
			return;
		}
		pos++;
		FOR(i, pos, pos + eq_count) {
			if (snake[i] != '=') {
				cout << "NA" << endl;
				return;
			}
		}
		pos += eq_count;
		if (snake.substr(pos) != "~") {
			cout << "NA" << endl;
			return;
		}
		cout << "A" << endl;
		return;
	}
	//B
	if (snake[1] == '^') {
		int Q_count = 0;
		int pos = 2;
		if (snake.substr(pos, 2) != "Q=") {
			cout << "NA" << endl;
			return;
		}
		while (snake.substr(pos, 2) == "Q=") {
			pos += 2;
		}
		if (snake.substr(pos) != "~~") {
			cout << "NA" << endl;
			return;
		}
		cout << "B" << endl;
		return;
	}
	cout << "NA" << endl;
	return;
}

int main()
{
	int n;
	cin >> n;
	REP(i, n) AOJ0139();

	return 0;
}