#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <map> 
#include <sstream>
#include <cstdio>
#include <complex>
#include <cstring>
using namespace std;
 
const int MAX= 10000100;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
#define ll long long int
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a)
int GCD(int a, int b) {if(!b) return a; return gcd(b, a%b);}
int lcm(int a, int b) {return a*b / gcd(a, b);}

bool A(string s){
  if(s.size()%2 != 0)return false;
  if(s[0] != '>')return false;
  if(s[1] != '\'')return false;
  if(s[s.size()-1] != '~')return false;
  if(s[s.size()/2] != '#')return false;
  else s[s.size()/2] = '=';
  loop(i,2,s.size()-1){
    if(s[i] != '=')return false;
  }
  return true;
}
bool B(string s){
  if(s[0] != '>')return false;
  if(s[1] != '^')return false;
  loop(i,2,s.size()-2){
    if(i%2){
      if(s[i] != '=')return false;
    }else{
      if(s[i] != 'Q')return false;
    }
  }
  if(s[s.size()-2] != '~')return false;
  if(s[s.size()-1] != '~')return false;
  return true;
}
int main(void){
  int n;
  cin>>n;
  rep(i,n){
    string s;
    cin>>s;
    if(A(s))cout<<"A";
    else if(B(s))cout<<"B";
    else cout<<"NA";
    cout<<endl;
  }
}