#include<iostream>

using namespace std;

string in;

int isA(){
  if(in[0] != '>') return 0;
  if(in[1] != '\'') return 0;
  if(!(in[in.length()-1] == '~' && in[in.length()-2] == '=')) return 0;
  int data[2] = {0,0};
  int status = 0;
  for(int i = 2; i < in.length()-1; i++){
    if(in[i] == '#') status = 1;
    if(in[i] == '=') data[status]++;
  }
  if(data[0] == data[1]) return 1;
  return 0;
}

int isB(){
  if(in[0] != '>') return 0;
  if(in[1] != '^') return 0;
  if(!(in[in.length()-1] == '~' && in[in.length()-2] == '~')) return 0;
  for(int i = 2; i < in.length()-2; i+=2){
    if(!(in[i] == 'Q' && in[i+1] == '=')) return 0;
  }
  return 1;
}


int main(){
  int num;
  cin >> num;
  for(int i = 0; i < num; i++){
    cin >> in;
    if(isA()) cout << "A" << endl;
    else if(isB()) cout << "B" << endl;
    else cout << "NA" << endl;
  }
  return 0;
}