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

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

  cin >> n;
  for(int i=0;i<n;i++){
    cin >> snake;
    len = snake.size();
    int ans = -1;
    if(snake[0] == '>' && snake[1] == '\''){
      int pos = 2;
      int turn = 0;
      int flag = 0;
      int count = 0;
      do{
	if(turn && snake[pos] == '=')count--;
	else if(snake[pos] == '=') count++;
	else if(snake[pos] == '#')turn = 1;
	else flag = 0;
	if(pos == 2 && count > 0)flag = 1;
	pos++;
      }while(pos < len-1);
      if(count == 0 && flag && snake[pos] == '~')ans = 0;
    }else if(snake[0] == '>' && snake[1] == '^'){
      int pos = 2;
      int flag = 1;
      do{
	if(snake[pos] != 'Q' || snake[pos+1] != '='){
	  flag = 0;
	  break;
	}
	pos += 2;
      }while(pos < len-2);
      if(flag && snake[pos] == '~' && snake[pos+1] == '~')ans = 1;
    }
    if(ans == 0)cout << 'A' << endl;
    else if(ans == 1)cout << 'B' << endl;
    else cout << "NA" << endl;
  }

  return 0;
}