#include <iostream>
#include <string>
using namespace std;

int stateA,stateB;
int anum;

void inA(char arg){
	//cout << stateA << endl;
	if(stateA == 0){
		if(arg == '>'){
			stateA = 1;
		}
	}else if(stateA == 1){
		if(arg == '\''){
			stateA = 2;
		}else{
			stateA = -1;
		}
	}else if(stateA == 2){
		if(arg == '='){
			stateA = 3;
			anum++;
		}else{
			stateA = -1;
		}
	}else if(stateA == 3){
		if(arg == '='){
			anum++;
		}else if(arg == '#'){
			stateA = 4;
		}else{
			stateA = -1;
		}
	}else if(stateA == 4){
		if(arg == '='){
			anum--;
		}else{
			stateA = -1;
		}
		if(anum==0){
			stateA = 5;
		}
	}else if(stateA == 5){
		if(arg == '~'){
			stateA = 6;
		}else{
			stateA = -1;
		}
	}else if(stateA == 6){
		stateA = -1;
	}
}

void inB(char arg){
	if(stateB == 0){
		if(arg == '>'){
			stateB = 1;
		}else{
			stateB = -1;
		}
	}else if(stateB == 1){
		if(arg == '^'){
			stateB = 2;
		}else{
			stateB = -1;
		}
	}else if(stateB == 2){
		if(arg == 'Q'){
			stateB = 3;
		}else{
			stateB = -1;
		}
	}else if(stateB == 3){
		if(arg == '='){
			stateB = 4;
		}else{
			stateB = -1;
		}
	}else if(stateB == 4){
		if(arg == 'Q'){
			stateB = 5;
		}else if(arg == '~'){
			stateB = 6;
		}else{
			stateB = -1;
		}
	}else if(stateB == 5){
		if(arg == '='){
			stateB = 4;
		}else{
			stateB = -1;
		}
	}else if(stateB == 6){
		if(arg == '~'){
			stateB = 7;
		}else{
			stateB = -1;
		}
	}else if(stateB == 7){
		stateB = -1;
	}
}

main(){
	int n;
	string str;
	
	cin >> n;
	cin.ignore();
	while(n && getline(cin,str)){
		stateA=0;
		stateB=0;
		anum=0;
		bool A=false, B=false;
		
		int l = str.size();
		for(int i=0;i<l;i++){
			inA(str[i]);
			inB(str[i]);
		}
		if(stateA == 6){
			cout << "A" << endl;
		}else if(stateB == 7){
			cout << "B" << endl;
		}else{
			cout << "NA" << endl;
		}
		
		n--;
	}
}