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

bool isA(const string& s){
	if( s.size() >= 1 && s[0] != '>' ) return false;
	if( s.size() >= 2 && s[1] != '\'' ) return false;
	
	bool a_flag = true;
	bool equal_flag = false;
	int cnt = 0;
	int j;
	for(int i=1 ; i < s.size() ; i++ ){
		if( equal_flag == false ){
			if( s[i] == '=' ){
				equal_flag = true;
				for(j=i ; j < s.size() && s[j] == '=' ; j++ ){
					cnt++;
				}
				if( j >= s.size() ){
					a_flag = false;
					break;
				}else if( s[j] != '#' ){
					a_flag = false;
					break;
				}else{
					j++;
				}
			}
		}else{
			for( ; j < s.size() && s[j] == '=' ; j++ )
				cnt--;
		}
	}
	if( cnt != 0 ) return false;
	if( a_flag == false ) return false;
	if( equal_flag == false ) return false;
	if( s[s.size()-1] != '~' ) return false;
	return true;
}

bool isB(const string& s){
	if( s.size() >= 1 && s[0] != '>' ) return false;
	if( s.size() >= 1 && s[1] != '^' ) return false;
	
	bool q_equal_flag = false;
	for(int i=3 ; i < s.size() ; i++ ){
		if( s[i-1] == 'Q' && s[i] == '=' ){
			q_equal_flag = true;
		}
	}
	
	if( s[s.size()-2] != '~' || s[s.size()-1] != '~' ){
		return false;
	}
	if( q_equal_flag == false ) return false;
	return true;
}

int main(){
	int n;
	cin >> n;
	for( ; n-- ; ){
		string s;
		cin >> s;
		
		if( isA( s ) ){
			cout << "A" << endl;
		}else if( isB( s ) ){
			cout << "B" << endl;
		}else{
			cout << "NA" << endl;
		}
	}
}