#include <iostream>
#include <algorithm>
using namespace std;
string judgeSnakeType(string s){
  if(s.find( ">'") == 0 && s.find("~") == s.size()-1)
      {
	int mark = 0;
	int cnt = 0;
	for( int i = 2 ; i < s.size() ; i++){
	  if( mark == 0 && s[i] == '='){
	    mark = 1;
	    cnt++;
	  }
	  else if( mark == 1 && s[i] == '='){
	    cnt++;
	  }
	  else if( s[i] == '#' ){
	    if( mark == 0 ) return "NA";
	    if( mark == 1 ) mark = 2;
	  }
	  else if( mark == 2 && s[i] == '=' ){
	    mark = 3;
	    cnt--;
	  }
	  else if( mark == 3 && s[i] == '=' ){
	    cnt--;
	  }
	}
	if( mark != 3 || cnt != 0 ) return "NA";
	return "A";
      }
  else if( s.find( ">^") == 0 && s.find("~~") == s.size()-2)
      {
	bool isMark = false;
	for( int i = 2 ; i < s.size()-2; i++){
	  if( !((i % 2 == 0 && s[i] == 'Q')
	      ||
		( i % 2 == 1 && s[i] == '=')))
	    {
	      return "NA";
	    }
	  else{
	    isMark = true;
	  }
	}
	if( !isMark ) return "NA";
	return "B";
      }
    else
      {
	return "NA";
      }
}
void solve(string snake){
  cout << judgeSnakeType( snake ) << endl;
}
int main(){
  int d;
  cin >> d;
  string snake;
  for(int i = 0 ; i < d; i++ )
    {
      cin >> snake;
      solve(snake);
    }
  return 0;
}