#include <iostream>

int main()
{
	std::string sn;	getline(std::cin, sn);
	int n = std::stoi(sn);
	for (int i=0; i<n; i++) {
		std::string snake; 
		getline(std::cin, snake);
		int state = 0;
		int equal_count = 0;
		for (int k=0; k<snake.length(); k++) {
			char c = snake[k];
			switch (state) {
			case 0:
				if (c == '>') state = 1;
				else state = 9;
				break;
			case 1:
				if (c == '\'') state = 10;
				else if (c == '^') state = 11;
				else state = 9;
				break;
			case 2:
				if (c == '=') equal_count++;
				else if (c == '#') state = 3;
				else state = 9;
				break;
			case 3:
				if (c == '=') {
					equal_count--;
					if (equal_count < 0) state = 9;
				}
				else if (equal_count == 0 && c == '~') state = 4;
				else state = 9;
				break;
			case 4:
				state = 9;
				break;
			case 5:
				if (c == 'Q') state = 6;
				else if (c == '~') state = 7;
				else state = 9;
				break;
			case 6:
				if (c == '=') state = 5;
				else state = 9;
				break;
			case 7:
				if (c == '~') state = 8;
				else state = 9;
				break;
			case 8:
				state = 9;
				break;
			case 10:
				if (c == '=') {
					equal_count++; state = 2;
				}
				else state = 9;
				break;
			case 11:
				if (c == 'Q') state = 12;
				else state = 9;
				break;
			case 12:
				if (c == '=') state = 5;
				else state = 9;
				break;
			}
		}
		if (state == 4) std::cout << "A" << std::endl;
		else if (state == 8) std::cout << "B" << std::endl;
		else std::cout << "NA" << std::endl;
	}
	
	return 0;
}


