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

int get_type(char *s) {
  int l = strlen(s);
  int c, d, p;
  if (s <= 0) return 2;
  if (s[0] != '>') return 2;
  if (s[1] == '\'') {
    c = d = 0;
    while (c+2<l && s[c+2]=='=') ++c;
    if (!c || c+2>=l || s[c+2]!='#') return 2;
    while (d+c+3<l && s[d+c+3]=='=') ++d;
    if (c!=d || d+c+4!=l || s[d+c+3]!='~') return 2;
    return 0;
  } else if (s[1] == '^') {
    c = 0;
    while (2*c+3<l && s[2*c+2]=='Q' && s[2*c+3]=='=') ++c;
    if (!c || 2*c+4!=l || s[2*c+2]!='~' || s[2*c+3]!='~') return 2;
    return 1;
  }
  return 2;
}

int main() {
  int n, t;
  char snake[201];
  const char *type[] = {"A", "B", "NA"};
  cin >> n;
  for (int i=0; i<n; i++) {
    cin >> snake;
    cout << type[get_type(snake)] << endl;
  }
  return 0;
}