#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main(void) {
  int n, len, flag=0, cnt1=0, cnt2=0;
  char snake[10000][200];
  cin >> n;
  for(int i = 0 ; i < n ; i++) {
    scanf("%s", snake[i]);
    len = strlen(snake[i]);
    if(snake[i][0] == '>' && snake[i][1] == '\'' && snake[i][2] == '=') {
      for(int j = 3 ; j < len - 2 ; j++) {
	if(snake[i][j] == '=' && flag == 0) cnt1++;
	if(snake[i][j] == '=' && flag == 1) cnt2++;
	if(snake[i][j] == '#') flag = 1;
	//cout << cnt1 << ' ' << cnt2 << endl;
      }
      if(snake[i][len-1] == '~' && cnt1 == cnt2) cout << 'A' << endl;
      else cout << "NA" << endl;
      flag = 0;
      cnt1 = 0; cnt2 = 0;
    }
    
    else if(snake[i][0] == '>' && snake[i][1] == '^' && snake[i][2] == 'Q' && snake[i][3] == '=') {
      if(snake[i][len-2] == '~' && snake[i][len-1] == '~') cout << 'B' << endl;
    }
    else cout << "NA" << endl;
  }
  return 0;
}