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