#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<complex>

#define f first
#define s second
#define mp make_pair

#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()

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef complex<double> P;

bool isA(const string &str){
  int n = str.size();
  int m = 0;
  int pos = 2;
  if(n == 0 || n==1) return false;
  if(str[0] != '>') return false;
  if(str[1] != '\'') return false;
  while(pos < n && str[pos] == '=') {m++; pos++;}
  if(pos == n || m == 0) return false;
  if(str[pos++] != '#') return false;
  while(pos < n && str[pos] == '=') {m--; pos++;}
  if(pos == n) return false;
  if(str[pos++] != '~') return false;
  if(m == 0 && pos == n) return true;
  return false;
}

bool isB(const string &str){
  int n = str.size();
  int pos = 2;
  int m = 0;
  if(n == 0 || n == 1) return false;
  if(str[0] != '>') return false;
  if(str[1] != '^') return false;
  while(pos+1 < n && str[pos] == 'Q' && str[pos+1] == '=') {m++; pos+=2;}
  if(m == 0) return false;
  if(pos+2 != n) return false;
  if(str[pos] == '~' && str[pos+1] == '~') return true;
  return false;
}

int main(){
  int n;
  string str;
  getline(cin,str); sscanf(str.c_str(),"%d",&n);
  while(n --> 0){
    getline(cin,str);
    if(isA(str)) puts("A");
    else if(isB(str)) puts("B");
    else puts("NA");
  }
  return 0;
}