#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define fr first
#define sc second
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int,pii> pip; 
const int INF = (1<<25);
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int n, len;
string s;

int checkA() {
  int i=2, j=len-2;
  while(i<j) {
    if(s[i] != '=' || s[j] != '=') return 0;
    i++; j--;
  }

  if(s[i] == '#' && s[len-1] == '~') return 1;
  return 0;
}

int checkB() {
  if(s[len-1]!='~' || s[len-2]!='~') return 0;
  for(int i=2; i<len-2; i+=2) {
    if(s[i] != 'Q' || s[i+1] != '=') return 0;
  }

  return 2;
}

int solve() {
  if(len<6 || len%2 == 1) return 0;
  if(s[0] == '>' && s[1] == '\'') return checkA();
  if(s[0] == '>' && s[1] == '^')  return checkB();
  return 0;
}

int main() {

  cin>> n;
  while(n--) {
    cin>> s;
    len = s.size();
    int ans = solve();
    if(ans == 0) cout<< "NA";
    if(ans == 1) cout<< 'A';
    if(ans == 2) cout<< 'B';
    cout<< endl;
  }
}