
#include<iostream>
#include<string>
using namespace std;
main(){
  int f,i,m;
  string s;
  for(cin>>f;cin>>s;f?puts(f-2?"A":"B"):puts("NA")){
    f=0;
    if(s[0]+s[1]==101){
      if((m=s.find(35))!=string::npos)
	for(i=1;s[m-i]==s[m+i];)
	  if(m-++i==1 && m+i==s.size()-1 && s[m+i]=='~')f=1;
    }else if(s[0]+s[1]==156){
      for(i=2;s[i]=='Q'&&s[i+1]=='=';)
	if(++++i==s.size()-2 && s[i]+s[i+1]==252)f=2;
    }
  }
}