#include <iostream>
#include <string>

using namespace std;

int main(void){

  string s;
  int n;
  cin >> n;

  while(n--){

    cin >> s;
    if( s[1] == '\'' ){
      int cnt1 = 0;
      int i;
      for( i = 2 ; s[i] == '=' ; i++ ){
	cnt1++;
      }
      if( s[i] != '#' ){
	cout << "NA" << endl;
	goto skip;
      }
      else i++;
      int cnt2 = 0;
      for( ; s[i] == '=' ; i++ ){
	cnt2++;
      }
      if( cnt1 == cnt2 && s[i] == '~' )
	cout << "A" << endl;
      else cout << "NA" << endl;
    }
    else if( s[1] == '^' ){
      int x = 2;
      while( s[x] == 'Q' && s[x + 1] == '=' ) x += 2;
      if(s[x] == '~' && s[x + 1] == '~' && x + 2 == s.size() ){
	cout << "B" << endl;
      }
    }
    else cout << "NA" << endl;
  skip:;
  }
  return 0;
}