#include <bits/stdc++.h>
using namespace std;

int main() {
  int T;
  cin >> T;
  while(T--) {
    string s;
    cin >> s;
    if(s[0]!='>'||s.size()<=3) cout << "NA" << endl;
    else if(s[1]=='\'') {
      if(s[s.size()-1]!='~'||s.size()%2) {
        cout << "NA" << endl;
        goto next;
      }
      s=s.substr(2,s.size()-3);
      for(int i=0; i<s.size()/2; i++) {
        if(s[i]!='='||s[i+s.size()/2+1]!='=') {
          cout << "NA" << endl;
          goto next;
        }
      }
      if(s.size()<3||s[s.size()/2]!='#') cout << "NA" << endl;
      else cout << "A" << endl;
    } else {
      if(s[s.size()-2]!='~'||s[s.size()-1]!='~'||s.size()%2) {
        cout << "NA" << endl;
        goto next;
      }
      s=s.substr(2,s.size()-4);
      for(int i=0; i<s.size(); i+=2) {
        if(s[i]!='Q'||s[i+1]!='=') {
          cout << "NA" << endl;
          goto next;
        }
      }
      if(s.size()>=2) cout << "B" << endl;
      else cout << "NA" << endl;
    }
  next:;
  }
  return 0;
}
