#include <bits/stdc++.h>

using namespace std;

#define int long long
#define FR first
#define SC second
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for(int i = (int)(f); i < (int)(n); i++)
#define each(a, b) for(auto& a : b)

typedef pair<int, int> P;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int n; cin >> n;
  while(n--) {
    string s; cin >> s;
    if(s.size() < 4) {
      puts("NA");
      continue;
    }
    int kind = 0;
    int sz = s.size();
    if(s[0] == '>' && s[1] == '\'' && s[sz-1] == '~') kind = 1;
    else if(s[0] == '>' && s[1] == '^' && s[sz-1] == '~' && s[sz-2] == '~' && sz%2 == 0) kind = 2;
    else kind = 3;
    if(kind == 1) {
      int cnt = 0, cmp = 0;
      for(int i = 2; i < sz-1 && kind != 3; i++) {
	if(s[i] != '=' && s[i] != '#') kind = 3;
	else {
	  if(s[i] == '=') cnt++;
	  else if(s[i] == '#' && !cmp) cmp = cnt, cnt = 0;
	  else kind = 3;
	}
      }
      if(cnt != cmp) kind = 3;
    } else if(kind == 2) {
      for(int i = 2; i < sz-2 && kind != 3; i+=2) {
	if(s.substr(i, 2) != "Q=") kind = 3;
      }
    }
    if(kind == 1) puts("A");
    else if(kind == 2) puts("B");
    else if(kind == 3) puts("NA");
  }
  
  return 0;
}