#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define rep(i,n) for (int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
int main(){
  int n;
  cin>>n;
  while(n--){
    string s;
    cin>>s;
    int a=0,b=0,k=-1;
    bool f=false;
    for(int i=0;i<s.size();i++)if(s[i]=='>')k=i;
    if(k!=-1){
      if(s[k+1]=='^'){
        f=false;
        for(int i=k;i<s.size()-1;i++){
          if(s[i]=='Q'&&s[i+1]=='=')f=true;
        }
        if(f&&s[s.size()-1]=='~'&&s[s.size()-2]=='~'){
          cout<<"B"<<endl;
          continue;
        }
      }
      int cnt=0;
      f=false;
      while(k<s.size()){
        if(s[k]=='='){
          while(s[k]=='='){
            cnt++;k++;
          }
          break;
        }
        k++;
      }
      if(s[k]=='#')f=true;
      while(k<s.size()){
        if(s[k]=='='){
          while(s[k]=='='){
            cnt--;k++;
          }
          break;
        }
        k++;
      }
      if(f&&cnt==0){
        cout<<"A"<<endl;
        continue;
      }
    }
    cout<<"NA"<<endl;
  }
}