#include<iostream>
#include<string>
using namespace std;

int main(){

  int in;

  cin >> in;

  for(int i = 0; i < in; i++){
    int status = 0;
    string snake;
    cin >> snake;
    if(snake[0] != '>') {
      cout << "NA" << endl;
      continue;
    }
    if(snake[1] == '^'){

      for(int j = 2; j < snake.size()-2; j+=2){
	if(snake[j] != 'Q' || snake[j+1] != '='){
	  cout << "NA" << endl;
	  status = 1;
	  break;
	}
      }
      if(snake[snake.size()-2] == '~' && snake[snake.size()-1] == '~' && status == 0){
	cout << "B" << endl;
      }
      continue;
    }else if(snake[1] == '\''){
      int head = 0, tale = 0, join;

      for(int j = 2; j < snake.size(); j++){
	if(snake[j] == '#'){
	  join = j;
	  break;
	}
	head++;
      }

      for(int j = join+1; j < snake.size()-1; j++){
	tale++;
      }

      if(head == tale && snake[snake.size()-1] == '~'){
	cout << "A" << endl;
	continue;
      }
    }
    cout << "NA" << endl;
  }

  return 0;
}