#include<iostream>
#include<string>
using namespace std;
int main(){
  int n,size;
  string s,a,b;
  for(cin>>n;n>0;n--){
    cin>>s;
    size = s.size();
    if(size%2){cout<<"NA"<<endl;continue}
    a=">'";
    b=">^";
    for(int i=0;i<size/2-2;i++){a=a+"=";b=b+"Q=";}
    a+="#";
    for(int i=0;i<size/2-2;i++){a=a+"=";}
    a+="~";
    b+="~~";
    if (s==a)cout<<"A"<<endl;
    else if (s==b)cout<<"B"<<endl;
    else cout<<"NA"<<endl;
  }
  return 0;
}