#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  int n;
  cin >> n;
  while(n--) {
    string s;
    cin >> s;
    int f=0;
    if (s.substr(0,2) == ">'") {
      int i=2, j1=0,j2=0;
      for (;i<s.length() && s[i]=='='; i++) j1++;
      if (i<s.length() && s[i++] == '#') {
        for (;i<s.length() && s[i]=='='; i++) j2++;
        if (j1==j2 && i<s.length() && s[i] == '~') {
          cout << "A";
          f++;
        }  
      }
    } else if (s.substr(0,2) == ">^") {
      int i = 2;
      for (;i<s.length()-1 && s.substr(i,2) == "Q="; i+=2) ;
      if (s.substr(i,2) == "~~") {
        cout << "B";
        f++;
      }
    }
    if (f==0)
      cout << "NA";
    cout << endl;
  }
}