#include <iostream>
#include <string>
using namespace std;

int main(){
  
  int n;
  cin >> n;
  while(n--){
    bool judge = false;
    string s; cin >> s;
    if(s[0] == '>'){
      if(s[1] == '\'' && s[s.size()-1] == '~'){
	if(s[s.size() / 2] == '#' && !(s.size() & 1) && s.size() >= 6)  cout << "A" << endl;
	else judge = true;
      }
      else if(s[1] == '^' && s[s.size()-1] == s[s.size()-2] && s[s.size()-1] == '~'){
	for(int i = 2; i < s.size()-2; i++){
	  if((!(i & 1) && s[i] != 'Q') || (i & 1 && s[i] != '=') || s.size() < 6) judge = true;
	}
	if(!judge) cout << "B" << endl;
      }
      else judge = true;
    }
    else judge = true;
    if(judge) cout << "NA" << endl;
  }

}