#include <iostream>
#include <string>
using namespace std;
int main()
{
  int n,i;
  string s,t;
  cin >> n;
  while(n--){
    cin >> s;
    t = s.substr(0,2);
    s = s.substr(2);
    if(t == ">'"){
      t = s.substr(s.length()-1);
      s = s.substr(0,s.length()-1);
      if(t == "~" && (s.length() % 2) == 1){
	for(i=0;i<s.length();i++){
	  if(i != s.length() / 2 && s[i] != '=' ||
	     i == s.length() / 2 && s[i] != '#') break;
	}
	if(i==s.length()) cout << "A" << endl;
	else cout << "NA" << endl;
      }else cout << "NA" << endl;
    }else if(t == ">^"){
      t = s.substr(s.length()-2);
      s = s.substr(0,s.length()-2);
      if(t == "~~" && (s.length() % 2) == 0){
	for(i=0;i<s.length();i++){
	  if((i % 2) == 0 && s[i] != 'Q' || (i % 2) == 1 && s[i] != '=') break;
	}
	if(i==s.length()) cout << "B" << endl;
	else cout << "NA" << endl;
      }else cout << "NA" << endl;
    }else cout << "NA" << endl;
  }
  return 0;
}