#include <iostream>
#include <algorithm>
using namespace std;
string judgeSnakeType(string s){
  if(s.find( ">'") == 0 && s.find("~") == s.size()-1)
    {
      s.erase(s.size()-1,1); s.erase(0,2);
      int cnt = 0;
      while( s.size() > 0 && s[0] == '=' ){
	if(s[0] == '='){
	  s.erase(0,1);
	  cnt++;
	}
      }
      if( s[0] != '#' )return "NA";
      s.erase(0,1);
      while( s.size() > 0 && s[0] == '=' ){
	if(s[0] == '='){
	  s.erase(0,1);
	  cnt--;
	}
      }
      if(s.size() == 0 && cnt == 0) return "A";
      else return "NA";
    }
  else if( s.find( ">^") == 0 && s.find("~~") == s.size()-2)
    {
      s.erase(s.size()-2,2); s.erase(0,2);
      while( s.size() > 0 && s.size() % 2 == 0){
	if(s[0] == 'Q' && s[1] == '='){
	  s.erase(0,2);
	}
	else {
	  return "NA";
	}
      }
      return s == "" ? "B" : "NA";
    }
  else
    {
      return "NA";
    }
  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;
}