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 <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int solve(string &s){
  if(s.length() >= 6 && s.substr(0, 2) == ">'" && s[(int)s.length() - 1] == '~'){
    int c1 = 0, c2 = 0, t = 0, f = 0;
    for(int i = 2; i + 1 < (int)s.length(); ++i){
      if(s[i] != '=' && s[i] != '#') f = 1;
      if(s[i] == '#' && t >= 1) f = 1;
      if(f) break;
      if(s[i] == '#') ++t;
      else if(t) ++c2;
      else ++c1;
    }
    if(!f && c1 == c2) return 0;
  }
  if(s.length() >= 6 && s.substr(0, 2) == ">^" && s.substr((int)s.length() - 2) == "~~"){
    int f = 0;
    for(int i = 2; i + 2 < s.length(); i += 2) if(s.substr(i, 2) != "Q=") f = 1;
    if(!f) return 1;
  }
  return -1;
}

int main() {
  int T; cin >>T;
  REP(t, T){
    string s; cin >>s;
    int ans = solve(s);
    cout <<(ans == 0 ? "A" : (ans == 1 ? "B" : "NA")) <<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int main() {
	int n;
	cin >> n;
	for (int z = 0; z < n; z++) {
		string a;
		cin >> a;
		string b = a.substr(0, 2),c=a.substr(a.length()-2,2);
		if (b == ">'") {
			if (c[1] == '~') {
				int d[2] = { 0,0 } , e = 0;
				bool f = false;
				for (int i = 2; i < a.length() - 1; i++) {
					if (a[i] == '=') {
						while (a[i] == '=') {
							i++;
							d[e]++;
						}
						e++;
					}
					if (a[i] == '#') {
						f = true;
					}
				}
				if (f == true && d[0] == d[1]) {
					cout << "A" << endl;
					goto stop;
				}
			}
		}
		if (b == ">^") {
			if (c == "~~") {
				for (int i = 2; i < a.length() - 2; i++) {
					string d = a.substr(i, 2);
					if (d == "Q=") {
						cout << "B" << endl;
						goto stop;
					}
				}
			}
		}
		cout << "NA" << endl;
	stop:;
	}
}

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

int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		string line;
		cin>>line;
		string sol = "NA";
		if(line.find('#')!=string::npos){
			bool flag = false;
			string a = "";
			string b = "";
			for(int x=2;x<line.size()-1;++x){
				if(line[x] == '#'){
					flag = true;
					continue;
				}
				if(!flag){
					a += line[x];
				}else{
					b += line[x];
				}
			}
			if(line.size()> 3 && line[0]=='>' && line[1]=='\'' && line[line.size()-1]=='~' && a==b && a!="")sol = "A";
		}else{
			bool flag = true;
			for(int x=2;line.size()>4 && x<(line.size()-3);x+=2){
				if(line[x] !='Q' || line[x+1] != '=')flag = false;
			}
			if(line.size()>4 && line[0]=='>' && line[1]=='^' && line[line.size()-1]=='~' && line[line.size()-2]=='~' && flag)sol="B";	}
		cout<<sol<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
int main()
{
  int n,i;
  string s,t;
  cin >> n;
  while(n--){
    cin >> s;
    t = s.substr(0,2);
    s = s.substr(2);
    if(t == ">'"){
      t = s.substr(s.length()-1);
      s = s.substr(0,s.length()-1);
      if(t == "~" && (s.length() % 2) == 1){
	for(i=0;i<s.length();i++){
	  if(i != s.length() / 2 && s[i] != '=' ||
	     i == s.length() / 2 && s[i] != '#') break;
	}
	if(i>=s.length()) cout << "A" << endl;
	else cout << "NA" << endl;
      }else cout << "NA" << endl;
    }else if(t == ">^"){
      t = s.substr(s.length()-2);
      s = s.substr(0,s.length()-2);
      if(t == "~~" && (s.length() % 2) == 0){
	for(i=0;i<s.length();i++){
	  if((i % 2) == 0 && s[i] != 'Q' || (i % 2) == 1 && s[i] != '=') break;
	}
	if(i>=s.length()) cout << "B" << endl;
	else cout << "NA" << endl;
      }else cout << "NA" << endl;
    }else cout << "NA" << endl;
  }
  return 0;
}

C++: #include<iostream>

using namespace std;

string in;

int isA(){
  if(in.length() < 5) return 0;
  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(in[i] != '#' && in[i] != '=') return 0;
  }
  if(data[0] && data[0] == data[1]) return 1;
  return 0;
}

int isB(){
  if(in.length() < 5) return 0;
  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()-2; 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>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

void a(){
	cout << "NA" << endl;
	return;
}

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

	for(int i=0;i<n;i++){
		string snake;
		cin >> snake;
		string::size_type aruyon;

		if((aruyon = snake.find(">'"))==0){
			int equal=0;
			int j;
			for(j=2;snake.at(j)=='=';j++) equal++;
			if(equal > 0)
				if(snake.at(j) == '#'){
					j++;
					for(;snake.at(j)=='=';j++) equal--;
					if(equal == 0){
						if(snake.at(j)=='~' && j+1 == snake.size()) cout << 'A' << endl;
						else a();
					}
					else a();
				}
				else a();
			else a();
		}
		else if((aruyon = snake.find(">^"))==0){
			int j;
			bool dou = false;
			if((aruyon = snake.find("~~"))==snake.size() -2){
				if(aruyon%2==0){
					for(j=2;j<aruyon;j++){
						dou = false;
						if(snake.at(j) == 'Q') j++;
						else{j=0; break;}	
						if(snake.at(j) != '='){a(); break;}
						else dou == true;
					}
					if(dou) cout << 'B' << endl;
					else a();
				}
				else a();
			}
			else a();
				
		}
		else
			a();
	}

  return 0;
}