#include <iostream>
//#include <fstream>

#define NA cout << "NA" << endl; goto LABEL

using namespace std;

int main() {
	//ifstream fin("in.txt");

	int snakeCount;
	cin >> snakeCount;

	for(int loopCount = 0; loopCount < snakeCount; loopCount++) {
		char arr[202];
		cin >> arr;
		int i = 0;

		if(arr[i]!='>') {
			NA;
		}
		i++;

		if(arr[i]=='\'') {
			int equalCount = 0;
			bool mode = false;
			for(int j = i+1; j < 202; j++) {
				if(arr[j]=='=') {
					if(!mode) {
						equalCount++;
					}else {
						equalCount--;
					}
				}else if(arr[j]=='#') {
					if(!mode && equalCount>0) {
						mode = !mode;
					}else {
						NA;
					}
				}else if(arr[j]=='~') {
					if(arr[j+1]=='\0'&&equalCount==0&&mode) {
						cout << "A" << endl;
						goto LABEL;
					}else {
						NA;
					}
				}else {
					NA;
				}
			}
		}else if(arr[i]=='^') {
			int count = 0;
			for(int j = i+1; j < 202; j++) {
				if(arr[j]=='Q'&&arr[j+1]=='=') {
					j++;
					count++;
					continue;
				}else if(arr[j]=='~'&&arr[j+1]=='~'&&arr[j+2]=='\0'&&count>0) {
					cout<< "B" << endl;
					goto LABEL;
				}else {
					NA;
				}
			}
		}else {
			NA;
		}
		LABEL:;
	}

	return 0;
}