//11
#include<string>
#include<iostream>

using namespace std;

int main(){
  int n;
  cin>>n;
  while(n--){
    string s;
    cin>>s;
    if(s.size()>=6&&s==">'"+string((s.size()-4)/2,'=')+'#'+string((s.size()-4)/2,'=')+'~'){
      cout<<'A'<<endl;
    }else{
      int r=(s.size()-4)/2;
      string b;
      while(r--){
	b+="Q=";
      }
      if(s.size()>=6&&s==">^"+b+"~~"){
	cout<<'B'<<endl;
      }else{
	cout<<"NA"<<endl;
      }
    }
  }
  return 0;
}