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;

static inline int in(){ int x; scanf("%d", &x); return x; }

int main()
{
  int n = in();
  int am[12][256] = {};
  am[1]['>'] = 2;
  am[2]['\''] = 3;
  am[3]['='] = 4;
  am[4]['='] = 4;
  am[4]['#'] = 5;
  am[5]['='] = 5;
  am[5]['~'] = 6;
  am[2]['^'] = 7;
  am[7]['Q'] = 8;
  am[8]['='] = 9;
  am[9]['Q'] = 8;
  am[9]['~'] = 10;
  am[10]['~'] = 11;
  const int typeA = 6;
  const int typeB = 11;

  for (int i = 0; i < n; i++){
    string snake;
    cin >> snake;
    int aid = 1;
    int c4, c5;
    c4 = c5 = 0;
    for (int j = 0; j < snake.size(); j++){
      aid = am[aid][snake[j]];
      if (aid == 4) c4++;
      if (aid == 5) c5++;
    }
    if (aid == typeA && c4 == c5 - 1){
      puts("A");
    }
    else if (aid == typeB){
      puts("B");
    }
    else {
      puts("NA");
    }
  }
  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;
			if((aruyon = snake.find("~~"))==snake.size() -2){
				if(aruyon%2==0){
					for(j=2;j<aruyon;j++){
						if(snake.at(j) == 'Q') j++;
						else{j=0; break;}	
						if(snake.at(j) != '='){a(); break;}
					}
					if(j == aruyon) cout << 'B' << endl;
					else a();
				}
				else a();
			}
			else a();
				
		}
		else
			a();
		}

  return 0;
}

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

string repeat(int n, string s) {
    string res = "";
    for (int i=0; i<n; ++i)
        res += s;
    return res;
}

int main() {
    int N; cin >> N;
    for (int i=0; i<N; ++i) {
        string snake; cin >> snake;

        int len = snake.size();
        string A = ">'" + repeat((len - 4)/2, "=") 
                    + "#" + repeat((len - 4)/2, "=") + "~";
        string B = ">^" + repeat((len - 4)/2, "Q=") + "~~";

        if (snake == A) {
            cout << "A" << endl;
        } else if (snake == B) {
            cout << "B" << endl;
        } else {
            cout << "NA" << endl;
        }
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
  string str;
  int n; cin >> n;
  while(n--){
    cin >> str;
    int idx = 2;
    int cnt = 0;
    bool flg = true;
    if(str[0] == '>' && str[1] == 39){
      bool used = false;
      while(idx < str.size() && str[idx] == '=') {
	used = true;
	cnt++;
	idx++;
      }
      if(str[idx] == '#'){
	idx++;
	while(idx < str.size() && str[idx] == '=') {
	  cnt--;
	  idx++;
	}
      }
      if(str[idx] == '~' && cnt == 0 && used) {
	cout << "A" << endl;
	flg = false;
      }
    } else if(str[0] == '>' && str[1] == '^'){
      while(idx+1 < str.size() && str[idx] == 'Q' && str[idx+1] == '='){
	idx += 2;
	cnt++;
      }
      if(cnt > 0 && str[idx] == '~' && str[idx+1] == '~') {
	cout << "B" << endl;
	flg = false;
      }
    }
    if(flg) cout << "NA" << endl;
  }
}

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

using namespace std;

int main(){
  int n;
  cin>>n;
  while(n--){
    string s;
    cin>>s;
    if(s.size()>=6&&s==">'"+string((s.size()-4)/2,'=')+'#'+string((s.size()-4)/2,'=')+'~'){
      cout<<'A'<<endl;
    }else{
      int r=(s.size()-4)/2;
      string b;
      while(r--){
	b+="Q=";
      }
      if(s.size()>=6&&s==">^"+b+"~~"){
	cout<<'B'<<endl;
      }else{
	cout<<"NA"<<endl;
      }
    }
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

char buf[1024];
int M;
bool checkA(){
  if( buf[0] != '>' ) return false;
  if( buf[1] != '\'') return false;
  int cnt1=0,cnt2=0;
  bool f = false;
  int i;
  for(i=2;i<M;i++){
    if(!f){
      if( buf[i] == '=' ) cnt1++;
      else if( buf[i] == '#' ) f = true;
      else return false;
    } else {
      if( buf[i] == '=' ) cnt2++;
      else break;
    }
  }
  if( buf[i] != '~' ) return false;
  i++;
  if( i == M && cnt1 == cnt2 && cnt1>0 && f ) return true;
  return false;
}

bool checkB(){
  if( buf[0] != '>' ) return false;
  if( buf[1] != '^' ) return false;
  int i;
  bool f = false;
  for(i=2;i<M-2;i+=2){
    if( buf[i] != 'Q' ) return false;
    if( buf[i+1] != '=' ) return false;
    f = true;
  }
  if( f && buf[i] == '~' && buf[i+1] == '~' && i + 2 == M ) return true;
  return false;
}

int main(){
  int n; cin >> n;
  for(int i=0;i<n;i++){
    cin >> buf; M = strlen(buf);
    if( checkA() ) cout << "A" << endl;
    else if( checkB() ) cout << "B" << endl;
    else cout << "NA" << endl;
  }
}

C++: #include <iostream>
#include <string>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
int main()
{
    int n; cin >> n;
    while (n--) {
        string s; cin >> s;
        string head = s.substr(0, 2);
        bool ok = true;
        if (head == ">'") {
            int c = 0;
            REP(i, 2, s.size()) { if (s[i] != '=') break; c++; }
            if (s[c+2] != '#') ok = false;
            else if (s.substr(c+3, c) != string(c, '=')) ok = false;
            else if (s.size() != c*2 + 4) ok = false;
            else if (s[s.size()-1] != '~') ok = false;
            puts(ok ? "A" : "NA");
        }
        else if (head == ">^") {
            for (int i = 2; i < s.size()-2; i+=2)
                if (s.substr(i, 2) != "Q=") { ok = false; break; }
            if (ok && s.substr(s.size()-2) != "~~") ok = false;
            puts(ok ? "B" : "NA");
        }
        else puts("NA");
    }
    return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
string s;
int main(){
	int n,t,now;
	cin>>n;
	while(n--){
		cin>>s;
		if(s.size()>=6&&s.substr(0,3)==">'="){
			t=0;
			while(s[3+(t++)]=='=');
			if(s[3+t-1]!='#')
				cout<<"NA"<<endl;
			else
			if(4+2*t!=s.size()||s.substr(3+t,t)!=s.substr(2,t)){

				cout<<"NA"<<endl;
			}
			else
			if(s[3+2*t]=='~')
				cout<<"A"<<endl;
		}
		else if(s.size()>=6&&s.substr(0,4)==">^Q="){
			now=4;
			int pre=0;
			while(1){
				if(s.size()-1-now<2){
					pre=1;
					break;
				}
				if(s.substr(now,2)!="Q=")
					break;
				now+=2;
			}
			if(pre==1)
			cout<<"NA"<<endl;
			else
			if(s.size()-now==2&&s.substr(now,2)=="~~"){
				
				cout<<"B"<<endl;
			}
			else {cout<<"NA"<<endl;

			}
		}
		else cout<<"NA"<<endl;
	}
	return 0;
}