#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;

bool f(string s) {
  int l = 0, r = 0, sz = s.size();
  while (r < sz && s[r] == '=') r++;
  if (r - l == 0) return false;
  if (r < sz && s[r] != '#') return false;
  if (s.substr(r + 1, r - l) != string(r - l, '=')) return false;
  r += 1 + r - l;
  if (r + 1 != sz || s[r] != '~') return false;
  return true;
}

bool g(string s) {
  int sz = s.size();
  if (sz % 2 == 1 || sz == 2) return false;
  for (int i = 0; i < sz; i += 2) {
    if (s.substr(i, 2) == "Q=") continue;
    if (s.substr(i, 2) == "~~" && i + 2 == sz) break;
    return false;
  }
  return true;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string S;
    cin >> S;
    if (S.substr(0, 2) == ">'") {
      cout << (f(S.substr(2)) ? "A" : "NA") << endl;
    } else if (S.substr(0, 2) == ">^") {
      cout << (g(S.substr(2)) ? "B" : "NA") << endl;
    } else {
      cout << "NA" << endl;
    }
  }

  return 0;
}
