#include <iostream>
#include <string>

using namespace std;

void jagdeA( const string& str )
{
	int count = 0;
	int i;


	for( i = 2;i < str.size();i++ ){
		if( str[i] == '=' ) count++;
		else if( count && str[i] == '#' ) break;
		else{
			cout << "NA" << endl;
			return;
		}
	}
	if( i == str.size() ){
		cout << "NA" << endl;
		return;
	}

	i++;
	for(; i < str.size(); i++ ){
		if( str[i] == '=' ) count--;
		else if( str[i] == '~' ) break;
		else{
			cout << "NA" << endl;
			return;
		}
	}

	if( count || i == str.size() ) {
		cout << "NA" << endl;
		return;
	}

	cout << "A" << endl;
	return;
}

void jagdeB( const string& str )
{
	int count = 0;
	int i;

	for( i = 2;i < str.size();i += 2 ){
		if( i + 1 >= str.size() ){
			cout << "NA" << endl;
			return;
		}
		if( str[i] == 'Q' && str[i + 1] == '=' ){
			count++;
		} else if( str[i] == '~' && str[i + 1] == '~' && count ){
			cout << "B" << endl;
			return;
		} else {
			cout << "NA" << endl;
			return;
		}
	}

	cout << "NA" << endl;
	return;
}



int main(int argc, char const* argv[])
{
	int n;
	string str;

	cin >> n;

	for( int i = 0;i < n;i++ ){
		cin >> str;
		if( str.size() < 6 || str[0] != '>' ){
			cout << "NA" << endl;
			continue;
		}

		switch( str[1] ){
			case '\'':
				jagdeA( str );
				break;
			case '^':
				jagdeB( str );
				break;
			default:
				cout << "NA" << endl;
				break;
		}
	}
	return 0;
}