#include <bits/stdc++.h>

using namespace std;

static inline int in(){ int x; scanf("%d", &x); return x; }

int main()
{
  int n = in();
  int am[12][256] = {};
  am[1]['>'] = 2;
  am[2]['\''] = 3;
  am[3]['='] = 4;
  am[4]['='] = 4;
  am[4]['#'] = 5;
  am[5]['='] = 5;
  am[5]['~'] = 6;
  am[2]['^'] = 7;
  am[7]['Q'] = 8;
  am[8]['='] = 9;
  am[9]['Q'] = 8;
  am[9]['~'] = 10;
  am[10]['~'] = 11;
  const int typeA = 6;
  const int typeB = 11;

  for (int i = 0; i < n; i++){
    string snake;
    cin >> snake;
    int aid = 1;
    int c4, c5;
    c4 = c5 = 0;
    for (int j = 0; j < snake.size(); j++){
      aid = am[aid][snake[j]];
      if (aid == 4) c4++;
      if (aid == 5) c5++;
    }
    if (aid == typeA && c4 == c5 - 1){
      puts("A");
    }
    else if (aid == typeB){
      puts("B");
    }
    else {
      puts("NA");
    }
  }
  return 0;
}