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

char buf[1024];
int M;
bool checkA(){
  if( buf[0] != '>' ) return false;
  if( buf[1] != '\'') return false;
  int cnt1=0,cnt2=0;
  bool f = false;
  int i;
  for(i=2;i<M;i++){
    if(!f){
      if( buf[i] == '=' ) cnt1++;
      else if( buf[i] == '#' ) f = true;
      else return false;
    } else {
      if( buf[i] == '=' ) cnt2++;
      else break;
    }
  }
  if( buf[i] != '~' ) return false;
  i++;
  if( i == M && cnt1 == cnt2 && cnt1>0 && f ) return true;
  return false;
}

bool checkB(){
  if( buf[0] != '>' ) return false;
  if( buf[1] != '^' ) return false;
  int i;
  bool f = false;
  for(i=2;i<M-2;i+=2){
    if( buf[i] != 'Q' ) return false;
    if( buf[i+1] != '=' ) return false;
    f = true;
  }
  if( f && buf[i] == '~' && buf[i+1] == '~' && i + 2 == M ) return true;
  return false;
}

int main(){
  int n; cin >> n;
  for(int i=0;i<n;i++){
    cin >> buf; M = strlen(buf);
    if( checkA() ) cout << "A" << endl;
    else if( checkB() ) cout << "B" << endl;
    else cout << "NA" << endl;
  }
}