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

int main(){
  int n,j,fa,fb,acount,acount2,i;
  string data;
  
  cin >> n;
  
  getline(cin,data);
  for(j = 0; j < n; j++){
    fa = 0;
    fb = 0;
    acount = acount2 = 0;
    getline(cin,data);
    if(data.length() < 6)
      data = "OOHAZURE";
    if(data[0] == '>' && data[1] == '\''){
      fa = 1;
      i = 2;
      while(data[i] == '='){
	acount++;
	i++;
      }
      if(data[i] != '#')
	fa = 0;
      i++;
      while(data[i] == '='){
	acount2++;
	i++;
      }
      if(i != data.length()-1)
	fa = 0; 
      if(acount != acount2)
	fa = 0;
      if(data[data.length()-1] != '~'){
	fa = 0;
      }
    }
    if(data[0] == '>' && data[1] == '^'){
      fb = 1;
      for(i = 2; i < data.length()-3; i = i+2){
	if(data[i] != 'Q' || data[i+1] != '=')
	  fb = 0;
      }
      if(i != data.length()-2)
	fb = 0;
      if(data[data.length()-1] != '~' || data[data.length()-2] != '~')
	fb = 0;
    }
    if(fa == 1)
      cout << "A" << endl; 
    else if(fb == 1)
      cout << "B" << endl;
    else 
      cout << "NA" << endl;
  }
  return 0;
}