#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <map>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,b) FOR(i,0,b)
#define PB push_back
#define MP make_pair
int main(){
  int n;
  cin>>n;
  while(n--){
    int a=0,b=0,cnt[2]={0,0};
    string in;
    cin>>in;
    for(int i=0;i<in.size();i++){
      if(a==0&&in[i]=='>')a=1;
      if(a==1&&in[i]=='=')cnt[0]++;
      if(a==1&&in[i]=='#')a=2;
      if(a==2&&in[i]=='=')cnt[1]++;
      if(a==2&&in[i]=='~'&&i==in.size()-1)a=3;
    }
    for(int i=0;i<in.size();i+=2){
      if(b==0&&in[i]=='>'&&in[i+1]=='^')b=1;
      if(b==1&&in[i]=='Q'&&in[i+1]=='=')b=2;
      if(b==2&&in[i]=='~'&&in[i+1]=='~'&&i+1==in.size()-1)b=3;
    }
    if(a==3&&cnt[0]==cnt[1]){
      cout<<"A"<<endl;
    }else if(b==3){
      cout<<"B"<<endl;
    }else{
      cout<<"NA"<<endl;
    }
  }
}