#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define f first
#define s second

int main(){
  string hebi;
  int n;
  cin>>n;
  while(n--){
    cin>>hebi;
    bool a=true,b=true;
    if(hebi.size()<6 || hebi.size()%2){
      a=false;
      b=false;
    }
    rep(i,hebi.size()){
      if(i==0 && hebi[i]=='>')continue;
      if(i==1 && hebi[i]=='\'')continue;
      if(i==hebi.size()-1 && hebi[i]=='~')continue;
      else if(i>=2 && i<hebi.size()-1 && i!=hebi.size()/2 && hebi[i]=='=')continue;
      else if(i==hebi.size()/2 && hebi[i]=='#')continue;
      a=false;
    }

    rep(i,hebi.size()){
      if(i==0 && hebi[i]=='>')continue;
      if(i==1 && hebi[i]=='^')continue;
      if(i==hebi.size()-1 && hebi[i]=='~')continue;
      if(i==hebi.size()-2 && hebi[i]=='~')continue;
      else if(i%2==0 && hebi[i]=='Q')continue;
      else if(i%2==1 && hebi[i]=='=')continue;
      b=false;
    }

    if(a)puts("A");
    else if(b)puts("B");
    else puts("NA");
  }
}