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

int main() {
  int n;
  cin >> n;
  getchar();
  for(int i = 0; i < n; i++) {
    string snake;
    getline(cin, snake);

    string ans = "NA";
    if(snake[0] == '>' && snake[1] == '\'' && snake.length() >= 6) {
      int shape = 0;
      for(int i = 2; i < snake.length(); i++) {
	if(i+1 >= snake.length()) {
	  if(snake[i] == '~') ans = "A";
	} else {
	  if(snake[i] != '=' && snake[i] != '#') break;
	  if(snake[i] == '#') {
	    if(shape == 1) break;
	    else {
	      shape++;
	      if(i-2 != (double)(snake.length()-4)/2) break;
	    }
	  }
	}
      }
    } else if(snake[0] == '>' && snake[1] == '^' && snake.length()%2 == 0 && snake.length() >= 6) {
      for(int i = 3; i < snake.length(); i+=2) {
	if(i+2 >= snake.length()) {
	  if(snake[i-1] == '~' && snake[i] == '~') ans = "B";
	} else {
	  if(snake[i-1] != 'Q' || snake[i] != '=') break;
	}
      }
    }

    cout << ans << endl;
  }
  return 0;
}