#include<bits/stdc++.h>
using namespace std;

int main(){
  string str;
  int n; cin >> n;
  while(n--){
    cin >> str;
    int idx = 2;
    int cnt = 0;
    bool flg = true;
    if(str[0] == '>' && str[1] == 39){
      bool used = false;
      while(idx < str.size() && str[idx] == '=') {
	used = true;
	cnt++;
	idx++;
      }
      if(str[idx] == '#'){
	idx++;
	while(idx < str.size() && str[idx] == '=') {
	  cnt--;
	  idx++;
	}
      }
      if(cnt == 0 && used) {
	cout << "A" << endl;
	flg = false;
      }
    } else if(str[0] == '>' && str[1] == '^'){
      while(idx+1 < str.size() && str[idx] == 'Q' && str[idx+1] == '='){
	idx += 2;
	cnt++;
      }
      if(cnt > 0 && str[idx] == '~' && str[idx+1] == '~') {
	cout << "B" << endl;
	flg = false;
      }
    }
    if(flg) cout << "NA" << endl;
  }
}