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 <bits/stdc++.h>
using namespace std;
int main(void){
    int n;
    cin>>n;
    for (int i=0;i<n;i++) {
        string s;
        cin>>s;
        if (s.size()<6||s.size()%2!=0){
            cout<<"NA"<<endl;
        } else {
            if (s.at(0)=='>'&&s.at(1)=='\''&&s.at(s.size()-1)=='~') {
                string tmp=s.substr(2,s.size()-3);
                string front=tmp.substr(0,(tmp.size()-1)/2);
                string back=tmp.substr((tmp.size()+2)/2,(tmp.size()-1)/2);
                if (front==back) {
                    cout<<"A"<<endl;
                } else {
                    cout<<"NA"<<endl;
                }
            } else if (s.at(0)=='>'&&s.at(1)=='^'&&s.at(s.size()-1)=='~'&&s.at(s.size()-2)=='~') {
                string center=s.substr(2,s.size()-4);
                for (int j=0;j<center.size();j+=2) {
                    if (center.at(j)!='Q'||center.at(j+1)!='=') {
                        cout<<"NA"<<endl;
                        break;
                    }
                    if (j==center.size()-2) {
                        cout<<"B"<<endl;
                    }
                }
            } else {
                cout<<"NA"<<endl;
            }
        }
    }
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
int main(){
    int n; cin>>n;
    rep(hoge,n){
        string s;cin>>s;
        int state=0;
        if(s[0]=='>'&&s[1]=='\'')state=1;
        else if(s[0]=='>'&&s[1]=='^')state=2;
        int cal=0,cnt=0; bool mybool=true,flag=true,key;
        if(state){
            rep(i,s.size()){
                if(i==0||i==1)continue;
                if(state==1){
                    if(s[i]=='#')mybool=false;
                    else if(mybool&&s[i]=='=')cnt++;
                    else if(mybool==false&&s[i]=='=')cnt--;
                    else if(s[i]=='~'&&i==s.size()-1)continue;
                    else flag=false;
                }else if(state==2){
                    if(!(s[s.size()-1]=='~'&&s[s.size()-2]=='~'))flag=false;
                    if(i>=s.size()-2)continue;
                    if((s[i]=='Q'&&s[i+1]=='=')){i++;}
                    else flag=false;
                }
            }
            if(state==1&&cnt==0&&flag)cout<<"A"<<endl;
            else if(state==2&&flag)cout<<"B"<<endl;
            else cout<<"NA"<<endl;
        }
    }
}

C++: #include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main(){
  int i,j,n,a;
  string str,ans;
  cin >> n;
  for(i=0;i<n;i++){  
    cin >> str;
    ans = "NA";

    if(str[0] == '>'){
      if(str[1] == '\''){
	a = 2;
	int c=0,c2=0;
	if(str[a] == '='){
	  while(str[a] == '='){
	    a++;
	    c++;
	  }
	  if(str[a] == '#'){
	    a++;
	    if(str[a] == '='){
	      while(str[a] == '='){
		c2++;
		a++;
	      }
	      if(str[a] == '~' && a + 1 == str.size() && c == c2) ans = "A";
	    }
	  }
	}
      }
      else if(str[1] == '^'){
	a = 2;
	if(str[a] == 'Q' && str[a+1] == '='){
	  while(str[a] == 'Q' && str[a+1] == '=') a += 2;
	  if(str[a] == '~' && str[a+1] == '~' && a + 2 == str.size()) ans = "B";
	}
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#define INF 999999
using namespace std;
int main(){
    int N,j;
    cin>>N;
    while(N--){
        int runcnt=0,j;
        char S[128000]={0};
        cin>>S;
            if(S[0]=='>'&&S[1]==39){
                int K=0;
                for(j=2;;j++){
                    if(S[j]=='='){
                        runcnt++;
                        K=1;
                    }
                    else if(S[j]=='#'){
                        break;
                    }
                    else {
                        cout<<"NA"<<endl;
                        goto end;
                    }
                }
                for(int i = j + 1;;i++){
                    if(S[i] == '='){
                        runcnt--;
                    }
                    else if(runcnt == 0 && S[i] == '~' && K == 1){
                        cout<<"A"<<endl;
                        goto end;
                    }
                    else{
                        cout<<"NA"<<endl;
                        goto end;
                    }
                }
            }
            else if(S[0] == '>' && S[1] == '^'){
                int F = 0;
                for(j = 2;;){
                    if(S[j] == 'Q' && S[j + 1] == '='){
                        j += 2;
                        F = 1;
                    }
                    else if(S[j] == '~' && S[j+1] == '~' && F == 1){
                        cout<<"B"<<endl;
                        goto end;
                    }
                    else{
                        cout<<"NA"<<endl;
                        goto end;
                    }
                }
            }
            else cout<<"NA"<<endl;
            end:;
    }
    return 0;
}

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

using namespace std;

int main()
{
	int n,i,j;
	string str;
	
	cin >> n;
	
	for(i=0;i<n;i++)
	{
		cin >> str;
		
		if(str[0]=='>')
		{
			if(str[1]=='\'')
			{
				int num=0;
				for(j=2;j<str.size();j++)
				{
					if(str[j]=='=')
						num++;
					else if(num>0 && str[j]=='#')
					{
						for(j+=1;j<str.size();j++)
						{
							if(j==str.size()-1 && str[j]=='~' && num==0)
								cout << "A" << endl;
							else if(j!=str.size()-1 && str[j]=='=')
								num--;
							else
							{
								cout << "NA" << endl;
								break;
							}
						}
						break;
					}
					else
					{
						cout << "NA" << endl;
						break;
					}
				}
			}
			else if(str[1]=='^')
			{
				for(j=2;j<str.size();j++)
				{
					if(j==str.size()-2 && str[j]=='~' && str[j+1]=='~')
					{
						cout << "B" << endl;
						break;
					}
					else if(j==str.size()-2 && (str[j]!='~' || str[j+1]!='~'))
					{
						cout << "NA" << endl;
						break;
					}
					else if((j%2==0 && str[j]!='Q') || (j%2==1 && str[j]!='='))
					{
						cout << "NA" << endl;
						break;
					}
				}
			}
			else
				cout << "NA" << endl;
		}
		else
			cout << "NA" << endl;
	}
}