#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;


int main(){

	int n,length,second_length;
	char snake[201];

	scanf("%d",&n);

	for(int i=0; i < n; i++){
		scanf("%s",snake);

		if(snake[1] == '\0'){
			printf("NA\n");
			continue;
		}

		if(snake[0] == '>' && snake[1] == '\''){
			if(snake[2] != '='){
				printf("NA\n");
			}else{
				for(length = 0;snake[length+2] != '\0' && snake[length+2] == '='; length++);

				if(snake[length+2] != '#'){
					printf("NA\n");
				}else{
					for(second_length = 0; snake[length+3+second_length] != '\0' &&
								snake[length+3+second_length] == '=';second_length++);

					if(length != second_length || snake[length+3+second_length] != '~'){
						printf("NA\n");
					}else{
						if(snake[length+4+second_length] == '\0'){
							printf("A\n");
						}else{
							printf("NA\n");
						}
					}
				}
			}
		}else if(snake[0] == '>' && snake[1] == '^'){
			if(snake[2] != 'Q' || snake[3] != '='){
				printf("NA\n");
			}else{
				bool breakFLG = false;
				for(length=0;snake[length+2] != '\0' && (snake[length+2] == 'Q' || snake[length+2] == '=');length++){
					if(length > 1){
						if(snake[length+2] == 'Q' && snake[length+2-1] != '='){
							breakFLG = true;
							break;
						}
						if(snake[length+2] == '=' && snake[length+2-1] != 'Q'){
							breakFLG = true;
							break;
						}
					}
				}
				if(breakFLG)printf("NA\n");
				else{
					if(snake[length+2-1] == 'Q'|| snake[length+2] != '~'){
						printf("NA\n");
					}else{
						if(snake[length+3] != '~'){
							printf("NA\n");
						}else{
							if(snake[length+4] == '\0'){
								printf("B\n");
							}else{
								printf("NA\n");
							}
						}
					}
				}
			}
		}else{
			printf("NA\n");
		}


	}

    return 0;
}