#include<stdio.h>
#include<string.h>

int main(void)
{
	char str[]="特に意味のない文章";
	int cut_i,cut_j;
	int len,kaisu;
	int hantei;

	scanf("%d",&kaisu);

	for(cut_i=0;cut_i<kaisu;cut_i++){
		scanf("%s",str);
		len = strlen(str);
		hantei = 0;

		if((len < 5 )|| (str[0] != '>') || (len % 2 != 0)){
			printf("NA\n");
			continue;
		}
		
		len--;		

		if((str[1] == '\'') && (str[len] == '~') ){
			cut_j = 2;
			while(str[cut_j] == '='){
				hantei++;
				cut_j++;
			}

			if(str[cut_j] != '#'){
				printf("NA\n");
			}
			else{
				cut_j++;

				while(str[cut_j] == '='){
					hantei--;
					cut_j++;
				}

				if( hantei == 0){
					printf("A\n");
				}
				else {
					printf("NA\n");
				}
			}
		}

		else if((str[1] == '^') &&(str[len-1] == str[len]) && (str[len] == '~')){
			for(cut_j =2;cut_j<len-2;cut_j+=2){
				if((str[cut_j] != 'Q') || (str[cut_j+1] != '=')){
					hantei = -1;
				}
			}
			printf("B\n");
		}
		else {
			printf("NA\n");
			continue;
		}			
	}	
	return 0;
}