#include<iostream>
#include<regex>
#include<string>

int main()
{
	int n;
	std::cin >> n;
	std::cin.ignore();

	while( n-- )
	{
		std::string s;
		std::getline( std::cin, s );

		std::smatch m;
		if( std::regex_match( s, m, std::regex( ">\'(=+)#(=+)~" ) ) && m[1].length() == m[2].length() )
			std::cout << 'A' << std::endl;
		else if( std::regex_match( s, std::regex( ">^(Q=)+~~" ) ) )
			std::cout << 'B' << std::endl;
		else
			std::cout << "NA" << std::endl;
	}

	return 0;
}