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

bool isA( string& s )
{
	int ptr = 0;
	if( s.size() < 6 || s[ 0 ] != '>' || s[ 1 ] != '\'' || s[ s.size() - 1 ] != '~' ){
		return false;
	}
	ptr = 2;
	while( ptr < s.size() - 1 && s[ ptr ] == '=' ){
		++ptr;
	}
	if( s[ ptr ] != '#' ){
		return false;
	}
	int halfLength = ptr - 2;
	if( s.size() != 4 + halfLength * 2 ){
		return false;
	}
	++ptr;
	for( int i = 0; i < halfLength; ++i, ++ptr ){
		if( s[ ptr ] != '=' ){
			return false;
		}
	}
	return true;
}

bool isB( string& s )
{
	int ptr = 0;
	if( s.size() < 6 || s[ 0 ] != '>' || s[ 1 ] != '^' || s[ s.size() - 1 ] != '~' || s[ s.size() - 2 ] != '~' ){
		return false;
	}
	int restLength = s.size() - 4;
	if( restLength % 2 > 0 ){
		return false;
	}
	ptr = 2;
	while( ptr < restLength + 2 ){
		if( s[ ptr ] != 'Q' || s[ ptr + 1 ] != '=' ){
			return false;
		}
		ptr += 2;
	}
	return true;
}

int main()
{
	int n;

	while( cin >> n ){
		for( int i = 0; i < n; ++i ){
			string snake;
			cin >> snake;

			string result;
			if( isA( snake ) )      result = "A";
			else if( isB( snake ) ) result = "B";
			else                    result = "NA";

			cout << result << endl;
		}
	}

	return 0;
}