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

bool A(string body) {
  if(int a = body.find("#") != -1 && body.size() % 2 == 0 && body.substr(body.size()-1,1) == "~" && body.size() >= 4) {
    for(int i=0;i<a;i++) {
      if(body.substr(i,1) == body.substr(body.size()-2-i,1) && body.substr(i,1) == "=") {

        continue;
      }
      else return false;
    }
  }
  else return false;
  return true;
}

bool B(string body) {

  if(body.substr(body.size()-2,2) == "~~" && body.size() % 2 == 0 && body.size() >= 4) {
    for(unsigned int i=0;i<body.size()-2;i+=2) {
      if(body.substr(i,2) == "Q=") {
        continue;
      }
      else return false;
    }
  }
  else return false;
  return true;
}

int main() {
  int n;
  string str;
  scanf("%d\n",&n);
  for(int i=0;i<n;i++) {
    getline(cin,str);
    if(str.substr(0,2) == ">'") {
      if(A(str.substr(2))) cout << "A" << endl;
      else cout << "NA" << endl;
    }
    else if(str.substr(0,2) == ">^") {
      if(B(str.substr(2))) cout << "B" << endl;
      else cout << "NA" << endl;
    }
    else cout << "NA" << endl;
  }
}