#include <bits/stdc++.h>

using namespace std;

static inline int in(){ int x; scanf("%d", &x); return x; }
vector<string> split(const string& s, char delim)
{
  stringstream ss(s);
  string token;
  vector<string> res;
  while (getline(ss, token, delim)){
    res.push_back(token);
  }
  return res;
}

int main()
{
  int n = in();
  for (int i = 0; i < n; i++){
    string snake;
    cin >> snake;
    string head = snake.substr(0, 2);
    bool ok = true;
    if (head == ">'"){
      vector<string> ts = split(snake.substr(2), '#');
      if (ts.size() == 2 && ts[0] + '~' == ts[1]){
        puts("A");
      }
      else ok = false;
    }
    else if (head == ">^"){
      vector<string> ts = split(snake.substr(2), '=');
      for (int j = 0; j < ts.size() - 1; j++){
        if (ts[j] != "Q"){
          ok = false;
        }
      }
      if (ok) ok = ts[ts.size() - 1] == "~~";
      if (ok) puts("B");
    }
    else ok = false;
    if (!ok) puts("NA");
  }
  return 0;
}