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

int main()
{
  int n,f,i,m;
  string s;
  cin>>n;
  while(n-->0){
    cin>>s;
    f=0;
    if(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[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");
  }
}

C++: #include<iostream>
#include<string>
using namespace std;
#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
string A(int s){			
  string res = ">\'";	 			
  rep(i,s/2-2)res += '=';
  res += '#';		
  rep(i,s/2-2)res += '=';
  res += "~";
  return res;
}
string B(int s){
  string res = ">^";
  rep(i,s/2-2)res+= "Q=";
  res+="~~";
 return res;
}
int main(void){
  string str;
  int n;
  cin>>n;
  while(n--){
    cin>>str;
    //cout<<"ori "<<str<<endl;
    B(str.size());
    if(A(str.size()) == str)cout<<"A"<<endl;
    else if(B(str.size()) == str)cout<<"B"<<endl;
    else cout<<"NA"<<endl;
  }
}

C++: #include <iostream>
#include <string>
#include <cstdio>
using namespace std;
 
string vbString(int Number,string Character){
        string res="";
        for(int i=0;i<Number;i++) res+=Character;
        return res;
}
string checksnake(string s){
        string t;
        for(int i=1;i<100;i++){
                t=">'"+vbString(i,"=")+"#"+vbString(i,"=")+"~";
                if(t==s) return "A";
        }
 
        for(int i=1;i<100;i++){
                t=">^"+vbString(i,"Q=")+"~~";
                if(t==s) return "B";
        }
       return "NA";
 
}
int main(){
        int n;
        cin >> n;
        while(n--){
		string s;
                cin >> s;
                cout << checksnake(s) << endl;
         }
}

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

using namespace std;

void solve()
{
	int n;
	cin >> n;
	while(n--)
	{
		string snake;
		cin >> snake;
		if(snake.size() < 4)
		{
			cout << "NA" << endl;
		}
		else if(snake[0] == '>' && snake[1] == '\'' && snake[snake.size() - 1] == '~')
		{
			bool first = true;
			int count1 = 0;
			int count2 = 0;
			for(int i = 2; i < snake.size() - 1; ++i)
			{
				if(snake[i] == '=')
				{
					if(first)
					{
						++count1;
					}
					else
					{
						++count2;
					}
				}
				else if(snake[i] == '#')
				{
					first = false;
				}
				else
				{
					cout << "NA" << endl;
					goto END;
				}
			}
			if(count1 == count2 && count1 >= 1)
			{
				cout << "A" << endl;
			}
			else
			{
				cout << "NA" << endl;
			}
		}
		else if(snake[0] == '>' && snake[1] == '^' && snake[snake.size() - 2] == '~' && snake[snake.size() - 1] == '~')
		{
			bool flag = true;
			int count = 0;
			for(int i = 2; i < snake.size() - 2; i += 2)
			{
				if(snake[i] != 'Q' || snake[i + 1] != '=')
				{
					flag = false;
					break;
				}
				if(snake[i] == 'Q' && snake[i + 1] == '=')
				{
					++count;
				}
			}
			if(flag && count)
			{
				cout << "B" << endl;
			}
			else
			{
				cout << "NA" << endl;
			}
		}
		else
		{
			cout << "NA" << endl;
		}
END:
		;
	}
}

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

C++: #include <string>
#include <iostream>
using namespace std;
int n; string s;
int check() {
	if(s.size() < 6) return 0;
	if(s[0] != '>') return 0;
	if(s[1] == '\'') {
		int cnt = 0, cntp = 0;
		for(int i = 2; i < s.size() - 1; i++) {
			if(s[i] != '=') break;
			cnt++; cntp++;
		}
		if(s[2 + cnt] != '#') return 0;
		for(int i = cnt + 3; i < s.size(); i++) {
			if(s[i] != '=') break;
			cnt--; cntp++;
		}
		if(cnt) return 0;
		if(2 + cntp + 2 != s.size()) return 0;
		return (s[2 + cntp + 1] == '~' ? 1 : 0);
	}
	if(s[1] == '^') {
		int cnt = 0;
		for(int i = 2; i < s.size() - 2; i += 2) {
			if(s[i] != 'Q' || s[i + 1] != '=') break;
			cnt += 2;
		}
		if(cnt + 4 != s.size()) return 0;
		return ((s[cnt + 2] == '~' && s[cnt + 3] == '~') ? 2 : 0);
	}
	return 0;
}
int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		cin >> s;
		int r = check();
		if(r == 0) cout << "NA" << endl;
		if(r == 1) cout << "A" << endl;
		if(r == 2) cout << "B" << endl;
	}
	return 0;
}

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

using namespace std;

string Sample;

//const regex REA("^>'(=+)#\1~$"),   REB("^>\\^(Q=)+~~$");
const regex REA("^>'(=+)#(=+)~$"), REB("^>\\^(Q=)+~~$");

int main() {
	int n;
	smatch smStr;

	cin >> n;
	while (n--) {
		cin >> Sample;
		//cout << Sample << endl;
		if (regex_match(Sample, smStr, REA)) {
			if (smStr[1].length() == smStr[2].length()) {
				cout << "A" << endl;
			}
			else {
				cout << "NA" << endl;
			}
		}
		else if (regex_match(Sample, REB)) {
			cout << "B" << endl;
		}
		else {
			cout << "NA" << endl;
		}
	}

	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <stdio.h>
#include <ctype.h>
#include <string>
#include <iostream>
#include <vector>
#include <stack>
#include <fstream>
#include <sstream>
#include <queue>
#include <exception>
#include <cmath>
#include <numeric>
#include <map>
#include <algorithm>
#include <bitset>
#include <set>
#include <functional>

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)

using namespace std;
typedef long long int lint;

void AOJ0139()
{
	string snake;
	cin >> snake;
	if (snake[0] != '>') {
		cout << "NA" << endl;
		return;
	}
	//A
	if (snake[1] == '\'') {
		if (snake[2] != '=') {
			cout << "NA" << endl;
			return;
		}
		int eq_count = 0;
		int pos = 2;
		while (snake[pos] == '=') {
			eq_count++;
			pos++;
		}
		if (snake[pos] != '#') {
			cout << "NA" << endl;
			return;
		}
		pos++;
		FOR(i, pos, pos + eq_count) {
			if (snake[i] != '=') {
				cout << "NA" << endl;
				return;
			}
		}
		pos += eq_count;
		if (snake.substr(pos) != "~") {
			cout << "NA" << endl;
			return;
		}
		cout << "A" << endl;
		return;
	}
	//B
	if (snake[1] == '^') {
		int Q_count = 0;
		int pos = 2;
		if (snake.substr(pos, 2) != "Q=") {
			cout << "NA" << endl;
			return;
		}
		while (snake.substr(pos, 2) == "Q=") {
			pos += 2;
		}
		if (snake.substr(pos) != "~~") {
			cout << "NA" << endl;
			return;
		}
		cout << "B" << endl;
		return;
	}
	cout << "NA" << endl;
	return;
}

int main()
{
	int n;
	cin >> n;
	REP(i, n) AOJ0139();

	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main() {
	int n,i,j,s;
	string c1,c2,c3;
	cin >> n;
	for (i=0;i<n;i++) {
		cin >> c1;
		c2=c3=c1;
		s=c1.size();
		if (s<6 || s % 2==1) { cout << "NA" << endl; continue; }
		for (j=0;j<s;j++) c2[j]=c3[j]='=';
        c2[0]=c3[0]='>'; c2[s-1]=c3[s-1]=c3[s-2]='~';
		c2[1]='\''; c2[s/2]='#'; c3[1]='^';
        for (j=2;j<s-2;j+=2) c3[j]='Q';
		if (c1==c2) cout << 'A'; else if (c1==c3) cout << 'B'; else cout << "NA";
		cout << endl;
	}
	return 0;
}