#include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
  int n;
  cin>>n;
  while(n--){
  string s;
  cin>>s;
  if(s[0]=='>'){
    if(s[1]=='\''){
      int sum=0;
      if(s.size()<6)goto L;
      r(i,s.size())if(s[i]=='=')sum++;
      if(s.size()%2==0&&s[s.size()/2]=='#'&&sum==s.size()-4&&s[s.size()-1]=='~')
        cout<<'A'<<endl;
      else goto L;
    }
    else if(s[1]=='^'){
      int c=2;
      if(s.size()%2||s.size()<6)goto L;
      while(c<s.size()-3){
        if(s[c]!='Q')goto L;
        c++;
        if(s[c]!='=')goto L;
        c++;
      }
      if(s[c]=='~'&&s[c+1]=='~')cout<<'B'<<endl;
      else goto L;
    }
    else goto L;
  }
  else goto L;
  if(0)L:cout<<"NA"<<endl;
  }
}