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 <iostream>
#include <string>
#include <cstdio>
using namespace std;

enum { A, B, NA };
int f(string s){
    if(s.size() < 6) return NA;
    if(s.substr(0,2)==">'"){
        size_t i=2;
        while(i<s.size() && s[i]=='=') i++;
        if(i==2 || i==s.size() || s[i]!='#') return NA;
        size_t j=i+1;
        while(j<s.size() && s[j]=='=') j++;
        if(i-2 != j-i-1 || j==s.size() || s[j]!='~') return NA;
        return (j+1==s.size() && s[j]=='~') ? A : NA;
    } else if(s.substr(0,2)==">^"){
        size_t i=2;
        while(i<s.size() && s[i]=="Q="[i&1]) i++;
        if((i&1)==1 || i==2 || i==s.size() || s[i]!='~') return NA;
        return (i+2==s.size() && s.substr(i)=="~~") ? B : NA;
    } else {
        return NA;
    }
}

int main(){
    int n;
    cin >> n;
    for(int i=0;i<n;i++){
        string s;
        cin >> s;
        char res[3][3] = {"A","B","NA"};
        puts(res[f(s)]);
    }
}

C++: #include <iostream>
#include <string>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)

int main() {
	string s;
	int n;
	cin >> n;
	rep(i,n) {
		cin >> s;
		int len = s.length();
		bool fa = 0, fb = 0;
		if(len > 5 && len % 2 == 0) {
			fa = 1;
			string ns;
			ns += s.substr(2,len/2-2);
			ns += s.substr(len/2+1,len/2-2);
			rep(i,ns.length()) {
				if(ns[i] != '=') fa = 0;
			}
			if(s[0] != '>' || s[1] != '\'' || s[len/2] != '#' || s[len-1] != '~') {
				fa = 0;
			}
			fb = 1;
			rep2(i,1,(len-2)/2) {
				if(s[2*i] != 'Q' || s[2*i+1] != '=') fb = 0;
			}
			if(s[0] != '>' || s[1] != '^' || s[len-2] != '~' || s[len-1] != '~') {
				fb = 0;
			}
		}
		if(fa) cout << "A" << endl;
		else if(fb) cout << "B" << endl;
		else cout << "NA" << endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<string.h>
 
int n,len;
char t[300];
 
int checkA(){
  if(t[len-1]!='~')return 0;
  int i=2,j=len-2; 
  while(i<j){
    if(t[i]!='=')return 0;
    if(t[j]!='=')return 0;
    i++;
    j--;
  }
  if(t[i]=='#')return 1;
  else return 0;
}
 
int checkB(){
  if(t[len-2]!='~')return 0;
  if(t[len-1]!='~')return 0;
  int i;
  for(i=2;i<len-2;i+=2){
    if(t[i]!='Q')return 0;
    if(t[i+1]!='=')return 0;
  }
  return 2;
}
 
int solve(){
  if(len<6 || len%2==1)return 0;
  if(t[0]=='>'&&t[1]=='\'')return checkA();
  if(t[0]=='>'&&t[1]=='^')return checkB();
  return 0;
}
 
int main(){
  scanf("%d",&n);
  while(n>0){
    n--;
    scanf("%s",t);
    len=strlen(t);
    int ans=solve();
    if(ans==0)printf("NA\n");
    if(ans==1)printf("A\n");
    if(ans==2)printf("B\n");
  }
  return 0;
}

C++: 
#include<iostream>
#include<string>
using namespace std;
main(){
  int f,i,m;
  string s;
  for(cin>>f;cin>>s;f?puts(f-2?"A":"B"):puts("NA")){
    f=0;
    for(i=1;s[0]+s[1]==101 & (m=s.find(35))!=string::npos & s[m-i]==s[m+i];)
      m-++i-1 | m+i-s.size()+1 | s[m+i]-'~'||(f=1);
    for(i=2;s[0]+s[1]==156 & s[i]==81 & s[i+1]==61;)
      ++++i-s.size()+2 | s[i]+s[i+1]-252 || (f=2);
  }
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);

int main() {
	int n;
	cin >> n;
	REP(i, n) {
		string s;
		cin >> s;
		string h = s.substr(0, 2);
		if(h == ">'") {
			s += "!";
			if(s.find('#') != string::npos) {
				int pos = s.find('#');
				int size = pos-2;
				REP(i, pos-2) {
					if(s[i+2] == s[i+pos+1] && s[i+2] == '=') {

					} else {
						cout << "NA" << endl;
						goto next;
					}
				}
				if(s[pos+size+1] == '~' && s[pos+size+1] == '!') {
					cout << "A" << endl;
					goto next;
				} else {
					cout << "NA" << endl;
					goto next;
				}
				 
			} else {
				cout << "NA" << endl;
				goto next;
			}			
		} else if(h == ">^") {
			s += "!!";
			int b = 2;
			bool flag = false;
			while(s.substr(b, 2) == "Q=") {
				b += 2;
				flag = true;
			}

			if(s.substr(b, 2) == "~~" && s.substr(b+2, 2) == "!!" && flag) {
				cout << "B" << endl;
				goto next;
			} else {
				cout << "NA" << endl;
				goto next;
			}
		} else {
			cout << "NA" << endl;
			goto next;
		}
		next:;
	}

	return 0;
}

C++: #include <cstdio>
#include <regex.h>
#include <cstring>

int main() {
  int n;
  regex_t regst[2];
  regmatch_t match[3];
  const char *pattern[] = {"^>'(=+)#(=+)~$", "^>\\^(Q=)+~~$"};
  const char *type_label[] = {"A", "B", "NA"};
  int type;
  char snake[201];
  
  scanf("%d", &n);
  for (int x=0; x<n; x++) {
    scanf("%s", snake);
    type = 2;
    for (int i=0; i<2; i++) {
      regcomp(&regst[i], pattern[i], REG_EXTENDED);
    }
    if (!regexec(&regst[0], snake, 3, match, 0)) {
      if (match[1].rm_eo - match[1].rm_so == match[2].rm_eo - match[2].rm_so) {
        type = 0;
      }
    } else if (!regexec(&regst[1], snake, 0, NULL, 0)) {
      type = 1;
    }
    printf("%s\n", type_label[type]);
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	cin >> n;

	while(n--)
	{
		string str;
		string kp;
		string set;
		int flag=0;
		int count=0;
		int count_b=0;
		int i=0;

		cin >> str;
		for(i=0;i<2;i++)kp+=str[i]++;
		
		if(kp==">'")
		{
			while(str[i]!='#')
			{
				if(str[i]=='=')count++;
				else flag=-1;

				if(flag==-1)break;
				i++;	
			}
			i++;
			while(str[i]!='~')
			{
				if(str[i]=='=')count_b++;
				else flag=-1;

				if(flag==-1)break;
				i++;
			}
			if(flag!=-1 && count==count_b && count>=1)flag=1;

			count=0;
			for(int j=i;j<str.length();j++)
			{
				if(str[i]!='~')flag=-1;
				else count++;

				if(flag==-1)break;
			}
			if(count!=1)flag=-1;
		}
		else if(kp==">^")
		{
			while(i<str.length()-2)
			{
				count=2;
				while(count--)
				{
					set+=str[i];
					i++;
				}
				if(set=="Q=")flag=2;
				else flag=-1;

				set.erase(set.begin(),set.end());
				if(flag==-1)break;
			}
			for(i=str.length()-2;i<str.length();i++)set+=str[i];
			if(set=="~~" && flag==2)flag=2;
			else flag=-1;
		}

		if(flag==1) cout << "A" << endl;
		else if(flag==2)cout << "B" << endl;
		else cout << "NA" << endl;
	}
	
	return 0;
}