#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int n;
string s;
int main(void){
  cin >> n;
  for(int k = 0; k < n; k++){
    cin >> s;
    bool f = true;
    if(s[0]== '>' && s[1]=='\''){
      int c=0;
      int i;
      for(i = 2; s[i]=='='; i++) c++;
      if(c && s[i]=='#'){
	for(i += 1; s[i]=='='; i++) c--;
	if(!c && (int)s.size() == i+1 && s[i]=='~'){
	  cout << 'A' << endl;
	  f = false;
	}
      }
    }
    else if(s[0]== '>' && s[1]=='^' && s[2]=='Q' && s[3]=='='){
      int i;
      for(i = 4; s[i]=='Q' && s[i+1]=='='; i+=2);
      if((int)s.size() == i+2 && s[i]=='~' && s[i+1]=='~'){
	  cout << 'B' << endl;
	  f = false;
      }
    }
    if(f){
      cout << "NA" << endl;
    }
  }
}