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<string>
#include<algorithm>
#include<iostream>
using namespace std;
int main(){
    int N;
    cin>>N;
    while(N--){
        string str;
        cin>>str;
        bool Aflag,Bflag;
        Aflag=Bflag=true;
        if(str.length()<2){
            cout<<"NA"<<endl;
            continue;
        }
        if(str.substr(0,2)==">'")Bflag=false;
        else if(str.substr(0,2)==">^")Aflag=false;
        else Aflag=Bflag=false;

        if(Aflag){

            int p=str.find("#");
            if(p==str.npos){
                Aflag=false;
                break;
            }
            int cnt=0;
            for(int i=2;i<p;i++){
                if(str[i]=='='&&str[p+i-1]=='=')cnt++;
                else{
                    Aflag=false;
                    break;
                }
            }
            if(cnt==0)Aflag=false;
            
            if(str[cnt*2+3]=='~'&&str.length()==cnt*2+4);
            else Aflag=false;

        }

        if(Bflag){
            int p=2;
            while(str.substr(p,2)=="Q=")p+=2;
            if(p==2){
                Bflag=false;
            }
            if(str.substr(p,2)=="~~"&&str.length()==p+2);
            else Bflag=false;
        }
        if(Aflag)cout<<"A"<<endl;
        else if(Bflag)cout<<"B"<<endl;
        else cout<<"NA"<<endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
bool check_A( string &S )
{
    auto sz = S.size();
    //size ??? 6 ??\?????????????????? false
    if( sz < 6 )
    {
        return 0;
    }
    //?????????[>'=], ????°????[~]???????????????
    if( !( S[0] == '>' && S[1] == '\'' && S[2] == '=' && S[sz - 1] == '~' ) )
    {
        return 0;
    }
    //??????[>']??¨????°?[~]???????????????
    string R = S.substr( 2, sz - 3 );
    sz = R.size();
    //R ???[#,=]??\???????????????????????????????????? false
    if( R.find_first_not_of( "#=", 0 ) != string::npos )
    {
        return 0;
    }
    if( sz % 2 == 0 )
    {
        return 0;
    }
    //R ?????????????????????????????£???[#]???????????\??????[#]????????£?????? false
    auto pos = R.find( "#" );
    if( R.find( "#", pos + 1 ) != string::npos )
    {
        return 0;
    }
    // S ???????????????[#]???????????£?????? false
    if( R[ sz / 2 ] != '#' )
    {
        return 0;
    }
    return 1;
}
bool check_B( string &S )
{
    auto sz = S.size();
    //size ??? 6 ??\?????????????????? false
    if( sz < 6 )
    {
        return 0;
    }
    //?????????[>^Q=], ????°????[~~]???????????????, ????????? 
    if( !( S[0] == '>' && S[1] == '^' && S[sz - 1] == '~' && S[sz-2] == '~' ) )
    {
        return 0;
    }
    string R = S.substr( 2, sz - 4 );
    sz = R.size();
    // S ???size????\???°?????£?????? false
    if( sz % 2 != 0 )
    {
        return 0;
    }
    for( int i = 0; i < sz; i += 2 )
    {
        //[Q=]?????£?¶???§????????£?????? false
        if( R[ i ] != 'Q' && R[ i + 1 ] != '=' )
        {
            return 0;
        }
    }
    return 1;
}
int main()
{
    int N;
    cin >> N;
    
    REP( i, N )
    {
        string S;
        cin >> S;
        
        string res{ "NA" };
        if( check_A( S ) )
        {
            res = "A";
        }
        if( check_B( S ) )
        {
            res = "B";
        }
        cout << res << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#define pb(in,tmp) in.push_back(tmp)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) loop(i,0,b)
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
using namespace std;
int main(){
  int n;
  cin>>n;
  rep(i,n){
    string ans;
    string s;
	cin>>s;
	if(s[0]!='>'){cout<<"NA"<<endl;continue;}
	if(s[1]=='^'){
	  ans='B';
	  int h=2;
	  while(h<s.size()-2){
	    if(s[h++]!='Q'||s[h++]!='='){
		  ans="NA";
		  break;
		}
	  }
      if(s[h++]!='~'||s[h++]!='~')ans="NA";
	  if(s[h]!='\0')ans="NA";
	}else if(s[1]=='\''){
	  ans='A';
	  int h1=0,h2=0;
	  int j=1;
	  while(s[++j]=='=')h1++;
	  if(s[j]!='#')ans="NA";
	  while(s[++j]=='=')h2++;
	  if(s[j]!='~')ans="NA";
	  if(s[++j]!='\0')ans="NA";
	  if(h1!=h2)ans="NA";
	}
	cout<<ans<<endl;
  }
}

C++: #include <iostream>
#include <string>

using namespace std;

void solve()
{
	int n;
	cin >> n;
	while(n--)
	{
		string str;
		cin >> str;
		if(str[0] == '>' && str[1] == '\'' && str[str.size() - 1] == '~')
		{
			int count[2] = {0, 0};
			int pos = 0;
			for(int i = 2; i < str.size(); ++i)
			{
				if(str[i] == '=')
				{
					count[pos]++;
				}
				else if(str[i] == '#')
				{
					pos++;
				}
			}
			if(count[0] > 0 && count[0] == count[1])
			{
				cout << "A" << endl;
			}
			else 
			{
				cout << "NA" << endl;
			}
		}
		else if(str[0] == '>' && str[1] == '^' && str[str.size() - 2] == '~' && str[str.size() - 1] == '~')
		{
			for(int i = 2; i < str.size() - 1; ++i)
			{
				if(str[i] == 'Q' && str[i + 1] == '=')
				{
					cout << "B" << endl;
					goto NEXT;
				}
			}
			cout << "NA" << endl;
		}
		else
		{
			cout << "NA" << endl;
		}
NEXT:
		;
	}
}

int main()
{
	solve();
	return(0);
}

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)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)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<string>
using namespace std;

int main()
{
  int n,f,i,m;
  string s;
  cin>>n;
  while(n-->0){
    cin>>s;
    f=0;
    if(s[0]=='>' && s[1]=='\''){
      m=s.find('#');
      if(m!=string::npos){
	for(i=1;s[m-i]==s[m+i];)i++;
	if(i>1 && m-i==1 && m+i==s.size()-1 && s[m+i]=='~')f=1;
      }
    }else if(s[0]=='>' && s[1]=='^'){
      for(i=2;s[i]=='Q'&&s[i+1]=='=';)i+=2;
      if(i>2 && i==s.size()-2 && s[i]=='~' && s[i+1]=='~')f=2;
    }
    if(f)puts(f==1?"A":"B");
    else puts("NA");
  }
}