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

int main(){
  int i,j,n,a;
  string str,ans;
  cin >> n;
  for(i=0;i<n;i++){  
    cin >> str;
    ans = "NA";

    if(str[0] == '>'){
      if(str[1] == '\''){
	a = 2;
	int c=0,c2=0;
	if(str[a] == '='){
	  while(str[a] == '='){
	    a++;
	    c++;
	  }
	  if(str[a] == '#'){
	    a++;
	    if(str[a] == '='){
	      while(str[a] == '='){
		c2++;
		a++;
	      }
	      if(str[a] == '~' && a + 1 == str.size() && c == c2) ans = "A";
	    }
	  }
	}
      }
      else if(str[1] == '^'){
	a = 2;
	if(str[a] == 'Q' && str[a+1] == '='){
	  while(str[a] == 'Q' && str[a+1] == '=') a += 2;
	  if(str[a] == '~' && str[a+1] == '~' && a + 2 == str.size()) ans = "B";
	}
      }
    }
    cout << ans << endl;
  }
  return 0;
}