#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <istream>
#include <sstream>
#include <iomanip>
#include <iterator>
#include <climits>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef vector<int> VI;
typedef vector< VI > VVI;

typedef long long LL;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define X first
#define Y second

bool check1(string s) {
	int n = s.size();

	if (n < 6 || s[0] != '>' || s[1] != '\'' || s[n - 1] != '~') {
		return false;
	}

	string sub = s.substr(2, n - 3);
	n = sub.size();

	if (!(n % 2) || sub[n / 2] != '#') {
		return false;
	}

	for (int i = 0; i < n / 2; i++) {
		if (sub[i] != '=' || sub[n - i - 1] != '=') {
			return false;
		}
	}

	return true;
}

bool check2(string s) {
	int n = s.size();

	if (n < 6 || s[0] != '>' || s[1] != '^' || s[n - 1] != '~' || s[n - 2] != '~') {
		return false;
	}

	string sub = s.substr(2, n - 4);
	n = sub.size();

	for (int i = 0; i < n / 2; i++) {
		if (sub[i * 2] != 'Q' || sub[i * 2 + 1] != '=') {
			return false;
		}
	}

	return true;
}

int main(void) {
	int n;
	cin >> n;

	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;

		if (check1(s)) {
			cout << "A" << endl;
		}else if (check2(s)) {
			cout << "B" << endl;
		}else {
			cout << "NA" << endl;
		}
	}

	return 0;
}