#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)

int main() {
	int N;
	cin >> N;
	REP(n, N) {
		string str;
		int type = 0, pos = 0;
		cin >> str;
		
		if (str[pos] == '>') {
			pos++;
			if (str[pos] == '\'') {
				pos++;
				int cnt = 0;
				while (pos < str.size() && str[pos] == '=') { cnt++; pos++; }
				
				if (cnt != 0 && str[pos] == '#') {
					pos++;
					while (pos < str.size() && str[pos] == '=') { cnt--; pos++; }
					if (cnt == 0 && str[pos] == '~') type = 1;
				}
				
			}
			else if (str[pos] == '^') {
				bool f = false;
				pos++;
				while (pos + 1 < str.size()) {
					if (str[pos] == 'Q' && str[pos + 1] == '=') { pos += 2; f = true; }
					else break;
				}
				if (f && pos + 2 == str.size() && str[pos] == '~' && str[pos + 1] == '~')
					type = 2;
			}
		}
		
		
		switch (type) {
		case 0: cout << "NA" << endl; break;
		case 1: cout << "A" << endl; break;
		case 2: cout << "B" << endl; break;
		}
	}
	return 0;
}