#include <iostream>
#include <cstring>

using namespace std;

int main(int argc, char **argv)
{
	int n, f, m;
	char a[101], w;
	cin >> m;
	
	while(m--){
		f = 0;
		scanf("%s", a);
		w = (a[1] == '^')? 'B' : 'A';
		n = strlen(a);
		if(w == 'A'){
			if(a[n-1] == '~'){
				for(int i = 2; i < n-i; i++){
					if(a[i] != a[n-i]){
						f = 1;
						break;
					}
				}
			} else {
				f = 1;
			}
		} else {
			if(a[n-1] == '~' && a[n-2] == '~'){
				for(int i = 2; i < n-2; i+=2){
					if(a[i] != 'Q' && a[i+1] != '='){
						f = 1;
						break;
					}
				}
			} else {
				f = 1;
			}
		}
		
		if(f == 1){
			cout << "NA" << endl;
		} else {
			cout << w << endl;
		}
	}
	
	return 0;
}