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 <cstdio>
#include <cstring>
using namespace std;
int main()
{
	bool b;
	int n, i, j, t, c[2];
	char s[201];
	cin >> n;
	for (i = 0; i < n; i++)
	{
		b = true;
		cin >> s;
		if (s[0] == '>')
		{
			if (s[1] == '\'')
			{
				for (j = 2, t = c[0] = c[1] = 0; j < strlen(s)-1; j++)
				{
					if (s[j] == '=')
						c[t]++;
					else if (t == 0 && s[j] == '#')
						t++;
					else
					{
						b = false;
						break;
					}
				}
				if (b && c[0] > 0 && c[0] == c[1] && s[strlen(s)-1] == '~')
					cout << "A" << endl;
				else
					cout << "NA" << endl;
				continue;
			}
			else if (s[1] == '^')
			{
				for (j = 2, c[0] = 0; j < strlen(s)-2; j+=2)
				{
					if (s[j] != 'Q' || s[j+1] != '=')
					{
						b = false;
						break;
					}
					else
						c[0]++;
				}
				if (b && c[0] > 0 && s[strlen(s)-2] == '~' && s[strlen(s)-1] == '~')
					cout << "B" << endl;
				else
					cout << "NA" << endl;
				continue;
			}
			else
				b = false;
		}
		else
			b = false;
		if (!b)
			cout << "NA" << endl;
	}
}

C++: #include<iostream>

using namespace std;

string in;

int isA(){
  if(in[0] != '>') return 0;
  if(in[1] != '\'') return 0;
  if(!(in[in.length()-1] == '~' && in[in.length()-2] == '=')) return 0;
  int data[2] = {0,0};
  int status = 0;
  for(int i = 2; i < in.length()-1; i++){
    if(in[i] == '#') status = 1;
    if(in[i] == '=') data[status]++;
  }
  if(data[0] && data[0] == data[1]) return 1;
  return 0;
}

int isB(){
  if(in[0] != '>') return 0;
  if(in[1] != '^') return 0;
  if(!(in[in.length()-1] == '~' && in[in.length()-2] == '~')) return 0;
  for(int i = 2; i < in.length()-3; i+=2){
    if(!(in[i] == 'Q' && in[i+1] == '=')) return 0;
  }
  return 1;
}


int main(){
  int num;
  cin >> num;
  for(int i = 0; i < num; i++){
    cin >> in;
    if(isA()) cout << "A" << endl;
    else if(isB()) cout << "B" << endl;
    else cout << "NA" << endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int n;
string str;
bool check_A(string t){
  int cnt=0,i=0,cnt2=0;

  while(t[i]=='='){cnt++;i++;}

  if(t[i]!='#')return false;
  if(cnt==0)return false;
  if((int)t.size()==i)return false;

  i++;
  while(t[i]=='='){cnt2++;i++;}

  if(cnt!=cnt2)return false;
  if((int)t.size()!=i+1)return false;

  if(t[i]!='~')return false;
  return true;
}

bool check_B(string t){
  int i=0;
  while(i+1<(int)t.size()&&t[i]=='Q'&&t[i+1]=='='){
    i+=2;
  }
  
  if(i+2!=(int)t.size())return false;
  if(t[i]=='~'&&t[i+1]=='~')return true;
  return false;
}


int main(){
  cin>>n;
  while(n--){
    cin>>str;
    if(str[0]=='>'&&str[1]=='\''){
      if(check_A(str.substr(2))){
	cout<<'A'<<endl;
      }else{
	cout<<"NA"<<endl;
      }
    }else if(str[0]=='>'&&str[1]=='^'){
      if(check_B(str.substr(2))){
	cout<<'B'<<endl;
      }else{
	cout<<"NA"<<endl;
      }
      
    }else{
      cout<<"NA"<<endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main(void) {
  int n, len, flag=0, flag2=0, cnt1=0, cnt2=0;
  char snake[10000][200];
  cin >> n;
  for(int i = 0 ; i < n ; i++) {
    scanf("%s", snake[i]);
    len = strlen(snake[i]);
    //cout << "len = " << len << endl;
    if(snake[i][0] == '>' && snake[i][1] == '\'' /*&& snake[i][2] == '='*/) {
      for(int j = 2 ; j < len - 1 ; j++) {
	if(snake[i][j] == '=' && flag == 0) {cnt1++; flag2 = 1; }
	if(snake[i][j] == '=' && flag == 1) {cnt2++; flag2 = 1; }
	if(snake[i][j] == '#') flag = 1;
	//cout << cnt1 << ' ' << cnt2 << endl;
      }
      if(snake[i][len-1] == '~' && cnt1 == cnt2 && flag2 == 1) { cout << 'A' << endl; flag = 0; flag2 = 0; cnt1 = 0; cnt2 = 0; }
      else { cout << "NA" << endl;
      flag = 0; flag2 = 0;
      cnt1 = 0; cnt2 = 0; }
    }
    
    else if(snake[i][0] == '>' && snake[i][1] == '^' /*&& snake[i][2] == 'Q' && snake[i][3] == '='*/) {
      for(int j = 2 ; j < len - 2 ; j++) {
	if(snake[i][j] == 'Q' && snake[i][j+1] == '=') flag = 1;
      }
      if(snake[i][len-2] == '~' && snake[i][len-1] == '~'  && flag == 1) { cout << 'B' << endl; flag = 0; }
      else { cout << "NA" << endl; flag = 0; }
    }
    else {cout << "NA" << endl; flag = 0;}
  }
  return 0;
}

C++: #include "stdio.h"
#include "math.h"

#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

const int INF = 10000000;
using namespace std;
typedef long long  ll;

/** Problem0139 : Snakes **/
int main()
{
	int n;
	string s;
	cin >> n;
	rep(k, n) {
		cin >> s;
		if (s.length()<2) {
			cout << "NA" << endl;
			goto end;
		}
		else if (s[0]=='>' && s[1]=='\'') {
			int num1=0, num2=0, i;
			for(i=2; i<s.length()-1;i++) {
				if (s[i]=='=')
					num1++;
				else if (s[i]=='#') {
					i++;
					break;
				} else {
					goto NA;
				}
			}
			for (; i<s.length()-1; i++) {
				if (s[i]=='=')
					num2++;
				else if (s[i]!='=') {
					goto NA;
				}
			}
			if (num1 == num2 && s[s.length()-1]=='~') {
				cout << "A" << endl;
				goto end;
			} else {
				goto NA;
			}
		}
		else if (s[0]=='>' && s[1]=='^') {
			bool f=true;
			for (int i=2; i<s.length()-2; i++) {
				if (i%2==0 && s[i]!='Q')
					f=false;
				else if (i%2==1 && s[i]!='=')
					f=false;
			}
			if (f && s[s.length()-2]=='~' && s[s.length()-1]=='~') {
				cout << "B" << endl;
				goto end;
			} else {
				goto NA;
			}
		}
	NA:
		cout << "NA" << endl;
	end:;
	}
}

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

using namespace std;

bool isA(string s)
{
	int len=s.length();
	if(len<6)					return false;
	if(s.substr(0,2)!=">'")		return false;
	if(s.substr(len-1)!="~")	return false;

	s=s.substr(2,len-3);
	len=s.length();
	if(len%2==0)		return false;
	if(s[len/2]!='#')	return false;
	if(s.substr(0,len/2)!=string(len/2,'='))	return false;
	if(s.substr(len/2+1)!=string(len/2,'='))	return false;
	return true;
}

bool isB(string s)
{
	int len=s.length();
	if(len<6)					return false;
	if(s.substr(0,2)!=">^")		return false;
	if(s.substr(len-2)!="~~")	return false;

	s=s.substr(2,len-4);
	len=s.length();
	if(len%2==1)	return false;
	bool b=true;
	for(int i=0;i<len;i+=2){
		if(s[i+0]!='Q')	b=false;
		if(s[i+1]!='=')	b=false;
	}
	return b;
}

int main()
{
	int n;	cin>>n;
	string snake;
	while(n--){
		cin>>snake;
		if     (isA(snake))	cout<<'A'<<endl;
		else if(isB(snake))	cout<<'B'<<endl;
		else				cout<<"NA"<<endl;
	}
	return 0;
}