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

  int n;
  int flg=0;
  cin>> n;
  while(n--) {
    string s;
    int cnt=0;
    flg=0;
    cin>> s;
    if(s[0] == '>') {
      if(s[1] == '\'') {
	int equal[2]={0}; // 0:左 1:右
	int now=0; 
	for(int i=2; i<s.size(); i++) {
	  if(s[i] == '=') {
	    equal[now]++;
	  }
	  else if(s[i] == '#') {
	    now=1;
	  }
	}
	if(equal[0] == equal[1]) {
	  flg=1;
	}
      }

      else if(s[1] == '^') {
	for(int i=2; i<s.size()-2; i+=2) {
	  if(s[i] != 'Q' && s[i] != '=') {
	    break;
	  }
	  cnt=i;
	}
	if(cnt == (s.size()-4) && s[s.size()-2] == '~' &&  s[s.size()-1] == '~') {
	  flg=2;
	}
      }
    }

    if(flg == 0) {
      cout<< "NA"<< endl;
    }
    else if(flg == 1) {
      cout<< 'A'<< endl;
    }
    else {
      cout<< 'B'<< endl;
    }
  }
}