#include <bits/stdc++.h>
using namespace std;

bool isOutOfStr(int ind, string str){
  return ind >= str.length();
}

int main(void){
  int n;
  cin >> n;
  while(n--){
    string str; cin >> str;
    if(!isOutOfStr(1, str) &&str[0] == '>' && str[1] == '\''){
      int sharp_cnt = 0;
      int ind;
      ind = 2;

      if(!isOutOfStr(ind, str) && str[ind] != '='){
        cout << "NA" << endl;
        continue;
      }

      for(;;ind++){
        if(isOutOfStr(ind, str) || str[ind] != '='){
          break;
        }
        sharp_cnt++;
      }

      if(isOutOfStr(ind, str) || str[ind] != '#'){
        cout << "NA" << endl;
        continue;
      }
      ind++;

      bool flg = true;
      for(int i=0; i < sharp_cnt; i++, ind++){
        if(isOutOfStr(ind, str) || str[ind] != '='){
          cout << "NA" << endl;
          flg = false;
          break;
        }
      }
      if(!flg) continue;
      
      if(!isOutOfStr(ind, str) && str[ind] == '~')
        cout << "A" << endl;
      else{
        cout << "NA" << endl;
      }



    }else if(str[0] == '>' && str[1] == '^'){
      int ind;
      ind+=2;

      int flg = true;
      for(;;ind+=2){
        if(isOutOfStr(ind, str) || !(str[ind] == 'Q' && str[ind+1] == '=')){
          break;
        }
      }

      if(!isOutOfStr(ind, str) && !isOutOfStr(ind+1, str) && str[ind] == '~' && str[ind+1] == '~'){
        cout << "B" << endl;
      }else{
        cout << "NA" << endl;
      }
    }else{
      cout << "NA" << endl;
    }
  }
  return 0;
}