#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int checksnake(string, int);

int main(void) {
  int n, len;
  char snake[200];
  int type;
  cin >> n;
  for(int i = 0 ; i < n ; i++) {
    scanf("%s", snake);
    len = strlen(snake);
    type = checksnake(snake, len);
    if(type == 1) cout << 'A' << endl;
    else if(type == 2) cout << 'B' << endl;
    else if(type == 3) cout << "NA" << endl;
    for(int a = 0 ; a < 200 ; a++) {
      snake[a] = 0;
    }
    len = 0;
  }
  return 0;
}

int checksnake(string snake, int len) {
  int flag1=0, flag2=0, flag3=0;
  int true_flag=0, true_flag2=0, true_flag3=0;
  int cnt1=0, cnt2=0;
  
  if(true_flag == 0) {
    for(int i = 0 ; i < len - 1 ; i++) {
      if(snake[i] != '>' && snake[i] != '#' && snake[i] != '=' && snake[i] != '\'') { true_flag2 = 1; break; }
      if(snake[i] == '>' && snake[i+1] == '\'') flag2 = 1;
      if(snake[i] == '=' && flag1 == 0 && flag2 == 1) {cnt1++; flag3 = 1; }
      if(snake[i] == '=' && flag1 == 1 && flag2 == 1) {cnt2++; flag3 = 1; }
      if(snake[i] == '#' && flag2 == 1 && flag3 == 1) flag1 = 1;
      //cout << cnt1 << ' ' << cnt2 << endl;
    }
    if(snake[len-1] == '~' && cnt1 == cnt2 && flag2 == 1 && flag3 == 1 && true_flag2 == 0) {
      return 1;
      true_flag = 1;
    }
  }
  
  if(true_flag == 0) {
    for(int i = 0 ; i < len - 2 ; i++) {
      if(snake[i] != '>' && snake[i] != '^' && snake[i] != '=' && snake[i] != 'Q') { true_flag3 = 1; break; }
      if(snake[i] == '>' && snake[i+1] == '^') flag3 = 1;
      if(snake[i] == 'Q' && snake[i+1] == '=' && flag3 == 1) flag1 = 1;
    }
    if(snake[len-2] == '~' && snake[len-1] == '~'  && flag1 == 1 && flag3 == 1 && true_flag3 == 0) {
      return 2;
      true_flag = 1;
    }
  }

  if(true_flag == 0 || true_flag2 == 1 || true_flag3 == 1) {
    return 3;
  }
}