#include <bits/stdc++.h>

using namespace std;

int main() {
  
  int Tc; cin >> Tc;
  
  while(Tc--) {
    string S; cin >> S;
    
  LA:
    {
      string s = S;
      
      if(s.size() < 6) goto BAD;
      if(s.substr(0, 3) != ">'=") goto LB;
      if(s[s.size()-1] != '~') goto BAD;
      
      s = s.substr(2);
      s = s.substr(0,s.size()-1);
      
      int pos = -1;
      int const size = s.size();
      int cnt = 0;
      for(int i=0; i<size; i++) {
	if(s[i] == '=') cnt++;
	else if(s[i] == '#') { pos = i; break; }
	else goto BAD;
      }
      if(pos == -1) goto BAD;
      if(2*cnt+1 != s.size()) goto BAD;
      
      for(int i=cnt+1; i<2*cnt+1; i++) {
	if(s[i] != '=') goto BAD;
      }
      
      cout << "A" << endl; goto EXIT;
    }
    
  LB:
    {
      string s = S;
      
      if(s.substr(0, 2) != ">^") goto BAD;
      if(s.substr(s.size()-2) != "~~") goto BAD;
      
      s = s.substr(2);
      s = s.substr(0, s.size()-2);
      
      if(s.size() % 2 || s.empty()) goto BAD;
      
      int const size = s.size();
      for(int i=0; i<size; i+=2) {
	if(!(s[i] == 'Q' && s[i+1] == '=')) goto BAD;
      }
      
      cout << "B" << endl; goto EXIT;
    }
    
  BAD:;
    cout << "NA" << endl;
    
  EXIT:;
  }
  
  return 0;
}