#include<iostream>
#include<string>

using namespace std;

int checkA(string);
int checkB(string);

int main(){
  int i, n;
  string snake;

  cin >> n;

  for(i=0; i<n; ++i){
    cin >> snake;

    if(checkA(snake) == 1)
      cout << "A" << endl;
    else if(checkB(snake) == 1)
      cout << "B" << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}

int checkA(string snake){
  int i, j, count1=0, count2=0;

  if(snake[0] != '>' || snake[1] != '\'' || snake[2] != '=') return 0;

  for(i=2; snake[i] == '='; ++i) ++count1;
  if(snake[count1 + 2] != '#') return 0;

  for(j=count1+3; snake[j] == '='; ++j) ++count2;
  if(count1 != count2 || j != snake.size()-1 || snake[snake.size()-1] != '~') return 0;

  return 1;
}

int checkB(string snake){
  int i;

  if(snake[0] != '>' || snake[1] != '^' || snake[2] != 'Q' || snake[3] != '=') return 0;

  for(i=2; i < snake.size()-2; i+=2){
    if(snake[i] != 'Q' || snake[i+1] != '=') return 0;
  }

  if(snake[snake.size()-2] != '~' || snake[snake.size()-1] != '~') return 0;

  return 1;
}