#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define fr first
#define sc second
#define mp make_pair
#define foreach(c, it) for(typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)

typedef pair< int, int > iP;
typedef pair< iP, int > iiP;

int main(){
  
  int N;
  cin >> N;
  while(N--) {
    string s; cin >> s;
    if(s[0] != '>') {
      cout << "NA" << endl;
      continue;
    }
    int select = 0;
    if(s[1] == '\'') {
      int cnt = 0;
      if(!(s.size() % 2)) {
        for(int i = 2; i < s.size() / 2; i++) {
          if(s[i] != '=') {
            select = -1;
            break;
          }
          cnt++;
        }
        if(~select && !cnt) {
          cout << "NA" << endl;
          continue;
        }
        for(int i = s.size() / 2 + 1; i < s.size() - 1; i++) {
          if(s[i] != '=') {
            select = -1;
            break;
          }
          cnt--;
        }
      } else select = -1;
      if(!cnt && ~select && s[s.size() / 2] == '#' && s[s.size() - 1] == '~')
        cout << "A" << endl;
      else cout << "NA" << endl; 
    } else if(s[1] == '^') {
      if(!(s.size() % 2)) {
        for(int i = 2; i < s.size() - 2; i++) {
          if((!(i % 2) && s[i] != 'Q') || (i % 2 && s[i] != '=')) {
            select = -1;
            break;
          }
        }
        if(s.size() >= 6 && ~select && s[s.size() - 1] == s[s.size() - 2] && s[s.size() - 1] == '~') cout << "B" << endl;
        else cout << "NA" << endl;
      } else cout << "NA" << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}