Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		ans := "NA"
		scanner.Scan()
		s := scanner.Text()
		if s[:2]==">'" && s[len(s)-1:] == "~" {
			tmp := s[2:len(s)-1]
			t := strings.Split(tmp, "#")
			if len(t) == 2 && len(t[0]) > 0 {
				valid := true
				for _, v := range t[0] {
					if string(v) != "=" {
						valid = false
						break
					}
				}
				if valid && t[0] == t[1] {
					ans = "A"
				}
			}
		} else if s[:2] == ">^" && s[len(s)-2:] == "~~" {
			t := s[2:len(s)-2]
			if len(t) > 0 && len(t)%2 == 0 {
				valid := true
				for i := 0; i < len(t); i += 2 {
					if t[i:i+2] != "Q=" {
						valid = false
						break
					}
				}
				if valid {
					ans = "B"
				}
			}
 		}
		fmt.Println(ans)
	}
}

C++: #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>

using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 10000000
#define rep(i,j) for(int i=0;i<(j);++i)
#define reps(i,j,k) for(int i=j;i<k;++i)
typedef long long ll;
typedef unsigned long long ull;
char snake[256]={0};
int check(int len){
	if(len % 2 == 1)return INF;
	if(snake[0] == '>'){
		if(snake[1] == 39){
			if(snake[len-1] != '~')return INF;
			int cnt = 0;
			int x = 0;
			rep(i,len){
				if(snake[i] == '#'){
					cnt++;
					x = i;
				}
			}
			if(cnt != 1)return INF;
			if(snake[2] != '=')return INF;
			reps(i,3,x){
				if(snake[i] != '=')return INF;
			}
			reps(i,x+1,len-1){
				if(snake[i] != '=')return INF; 
			}
			return (x-2 == len-x-2) ? 1 : INF; 
		}
		else if(snake[1] == '^'){
			if(snake[len-1] != '~' || snake[len-2] != '~')return INF;
			if(snake[2] != 'Q' || snake[3] != '=')return INF;
			for(int i = 4; i < len-2; i += 2){
				if(snake[i] != 'Q' || snake[i+1] != '=')return INF;
			}
			return 0;
		}
	}
	return INF;
}
int main(){
	int n;
	scanf("%d",&n);
	rep(q,n){
		memset(snake,0,sizeof(snake));
		scanf("%s",snake);
		int len = strlen(snake);
		int res = check(len);
		(res == 1) ? puts("A") : (res == 0) ? puts("B") : puts("NA");
	}
	return 0;
}

C++: #include <iostream>

//CÓ¶ðÁ·Ö
int clear(std::string s, std::string f){
	int pos;
	while(pos = s.find(f), pos != std::string::npos)
		s.replace(pos,f.size(),"");
	if(s == "")return 1;
	return 0;
}

int main(){
	int n;
	std::cin >> n;
	while(n--){
		std::string sn, res = "";
		std::cin >> sn;

		//Type A
		//ªª>'CKöª~C©Â¨ ÌªªïÅ é
		if(sn.substr(0,2) == ">'" &&
			 sn[sn.size()-1] == '~' && (sn.size()-3) % 2 == 1){
			std::string st = sn.substr(2,sn.size()-3);
			//¨ Ì^ñª#
			if(st[st.size()/2] == '#'){
				st.replace(st.size()/2,1,"");
				
				//#ÈOª·×Ä=
				// int eq = 1;
				// for(int i=0;i<st.size();i++){
				// 	if(st[i] != '='){eq = 0;break;}
				// }
				// if(eq)std::cout << "A" << std::endl;
				if(st != "")
					if(clear(st,"="))res = "A\n";
			}
		}

		//Type B
		if(sn.substr(0,2) == ">^" &&
			 sn.substr(sn.size()-2,2) == "~~" &&
			 (sn.size()-4) % 2 == 0){
			std::string st = sn.substr(2,sn.size()-4);
			if(st != "")
				if(clear(st,"Q="))res = "B\n";
		}

		if(res == "")res = "NA\n";
		std::cout << res;
	}
}

C++: #include<iostream>
using namespace std;
int n;
string s;
main()
{
    cin>>n;
    for(;n--;)
    {
        cin>>s;
        string t="NA";
        if(s.substr(0,2)==">'")
        {
            int cnt=0;
            int i=2;
            while(i<s.size()&&s[i]=='=')i++,cnt++;
            if(s[i]=='#')
            {
                int j=i+1;
                while(j<s.size()&&s[j]=='=')j++,cnt--;
                if(cnt==0&&j+1==s.size()&&s.substr(j,1)=="~")t="A";
            }
        }
        else if(s.substr(0,2)==">^")
        {
            int i=2;
            while(i+2<s.size()&&s.substr(i,2)=="Q=")i+=2;
            if(i>2&&i+2==s.size()&&s.substr(i,2)=="~~")
            {
                t="B";
            }
        }
        cout<<t<<endl;
    }
}

C++: #include<iostream>
#include<string>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

bool asnake(string s){
	int n=s.size();
	if(n%2 || n<6)return false;
	string com=">\'";
	rep(i,(n-4)/2)com+='=';
	com+='#';
	rep(i,(n-4)/2)com+='=';
	com+='~';
	if(s==com)return true;
	else return false;
}

bool bsnake(string s){
	int n=s.size();
	if(n%2 || n<6)return false;
	string com=">^";
	rep(i,(n-4)/2)com+="Q=";
	com+="~~";
	if(s==com)return true;
	else return false;
}

int main(){
	int n;
	cin>>n;
	rep(p,n){
		string s;
		cin>>s;
		if(asnake(s))cout<<"A"<<endl;
		else if(bsnake(s))cout<<"B"<<endl;
		else cout<<"NA"<<endl;
	}
	return 0;
}

C++: #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;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
#include <sstream>
using namespace std;

#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
static const int INF=1<<24;

template <class T>
void initvv(vector<vector<T> > &v,int a,int b, const T  &t = T()){
	v.assign(a,vector<T>(b,t));
}

//v.erase(unique(v.begin(),v.end()),v.end());
int main(){
	int n;
	cin>>n;
	rep(i,n){
		string s;
		cin>>s;
		if(s.substr(0,2)==">'"&&s.substr(s.size()-1,1)=="~"){
			s=s.substr(2);
			s=s.substr(0,s.size()-1);
			bool f=true;
			if(s.size()==0) f=false;
			if(s.size()&&s[0]!='='){
				f=false;
			}
			while(s.size()>1){
				if(s[0]==s[s.size()-1]&&s[0]=='='){
					s=s.substr(1,s.size()-2);
				}
				else{
					f=false;
					break;
				}
			}
			if(f){
				if(s[0]=='#'){
					cout<<"A\n";
				}
				else cout<<"NA\n";
			}
			else{
				cout<<"NA\n";
			}
		}
		else if(s.substr(0,2)==">^"&&s.substr(s.size()-2,2)=="~~"){
			s=s.substr(2);
			s=s.substr(0,s.size()-2);
			bool f=true;
			if(s.size()<2) f=false;
			if(s.size()>=2&&s.substr(0,2)!="Q=") f=false;
			while(s.size()>=2){
				if(s.substr(0,2)=="Q="){
					s=s.substr(2);
				}
				else{
					f=false;
					break;
				}
			}
			if(f){
				cout<<"B\n";
			}
			else cout<<"NA\n";
		}
		else{
			cout<<"NA\n";
		}
	}
}