#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int solve(string &s){
  int t = -1;
  if(s.length() < 3) return -1;
  if(s.length() >= 3 && s.substr(0, 2) == ">'" && s[(int)s.length() - 1] == '~') t = 0;
  if(s.length() >= 4 && s.substr(0, 2) == ">^" && s.substr((int)s.length() - 2) == "~~") t = 1;
  if(t == -1) return -1;
  if(t){
    for(int i = 2; i + 2 < s.length(); i += 2) if(s.substr(i, 2) != "Q=") return -1;
    return t;
  } else{
    int c1 = 0, c2 = 0, i = 2;
    for( ; i + 1 < s.length() && s[i] == '='; ++i) ++c1;
    if(s[i++] != '#') return -1;
    for( ; i + 1 < s.length() && s[i] == '='; ++i) ++c2;
    if(c1 != c2 || s[i] != '~' || i + 1 != (int)s.length()) return -1;
    return t;
  }
  return -1;
}

int main() {
  int T; cin >>T;
  REP(t, T){
    string s; cin >>s;
    int ans = solve(s);
    cout <<(ans == 0 ? "A" : (ans == 1 ? "B" : "NA")) <<endl;
  }
  return 0;
}