#include <iostream>
#include <string>

using namespace std;

int main(){
  int n;
  cin >> n;

  for(int q=0;q<n;q++) {
    string str;
    cin >> str;
    if(str[0] != '>') {
      cout <<"NA"<<endl; 
      continue;
    }  else if(str[1] != '\''&& str[1] != '^') {
      cout << "NA" << endl;
      continue;
    }
    int flg = 0,sum;
    if(str[1] == '\'') {
      int t = 0;
      sum=0;
      for(int i=2;i<str.size();i++) {
	if(str[i] == '=') t++;
	else if(str[i] == '#' && sum==0 && i!=2) sum = t , t = 0;
	else break;
	if(str[str.size()-1] == '~' && i == str.size() -2 && sum==t ) flg = 1;
	
      }
    }

      if(str[1] == '^' && flg == 0) {
	flg = 0;
	for(int i=2;i<str.size() ;i++) {
	  if(i%2 == 0 && str[i] != 'Q') break;
	  if(i%2 == 1 && str[i] !='=')  break;

	  if(str[str.size()-2] == '~' && str[str.size()-1] =='~' && i == str.size()-3&& i%2==1 ){
	    flg = 2;
	    break;
	  }
	}
      }

      if(flg == 0) cout << "NA" << endl;
      else if(flg == 1)cout <<"A" << endl;
      else if(flg == 2) cout << "B"<<endl;
  }     


  return 0;
}