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 <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;
		cout << check() << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

bool check_A(string str){
	if(str.size() < 6 || str.size() % 2 == 1){
		return false;
	}
	if(str.substr(0, 2) != ">'"){
		return false;
	}
	int count = 0;
	for(int i = 2; i < (str.size() - 4) / 2 + 2; i++){
		if(str[i] != '='){
			return false;
		}
	}
	if(str[str.size() / 2] != '#'){
		return false;
	}
	for(int i = str.size() / 2 + 1; i < str.size() - 2; i++){
		if(str[i] != '='){
			return false;
		}
	}
	if(str[str.size() - 1] != '~'){
		return false;
	}
	return true;
}

bool check_B(string str){
	if(str.size() < 6 || str.size() % 2 == 1){
		return false;
	}
	if(str.substr(0, 2) != ">^"){
		return false;
	}
	for(int i = 2; i < str.size() - 2; i += 2){
		if(str.substr(i, 2) != "Q="){
			return false;
		}
	}
	if(str.substr(str.size() - 2, 2) != "~~"){
		return false;
	}
	return true;
}

int main(){
	
	int n;
	
	string str;
	
	cin >> n;
	
	for(int loop = 0; loop < n; loop++){
		cin >> str;
		if(check_A(str)){
			printf("A\n");
		}else if(check_B(str)){
			printf("B\n");
		}else{
			printf("NA\n");
		}
		
	}
	
	return 0;
}

C++: #include <string>
#include <iostream>
using namespace std;
int n; string s;
bool check() {
	if(s.size() < 6) return 0;
	if(s[0] != '>') return 0;
	if(s[1] != '\'') {
		int cnt = 0;
		for(int i = 2; i < s.size() - 1; i++) {
			if(s[i] != '=') break;
			cnt++;
		}
		if(s[2 + cnt] != '#') return 0;
		for(int i = cnt + 3; i < s.size(); i++) {
			if(s[i] != '=') break;
			cnt--;
		}
		if(cnt) return 0;
		if(2 + cnt + 1 != s.size()) return 0;
		return s[2 + cnt] == '~' ? 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;
		printf("%s\n", !check() ? "NA" : (check() == 1 ? "A" : "B"));
	}
	return 0;
}

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

using namespace std;

int main(){
  int n;

  cin >> n;
  while(n--){
    string snake;
    cin >> snake;

    string s = snake.substr(0,2);
 

    if(s != ">^" && s != ">\'"){
      puts("NA");
      continue;
    }

    if(s == ">\'"){
      int a = 0;
      int p = 2;
      bool flg = true;
      int len = (int)snake.size();
      for(int i = 2 ; i < len ; i++,p++){
	if(snake[i] == '#') break;
	else if(snake[i] == '=') a++;
	else{
	  flg = false;
	  break;
	}
      }

      if(!flg){
	puts("NA");
	continue;
      }      

      int b = 0,q = p + 1;
      for(int i = q ; i < len ; i++,p++){
	if(snake[i] == '~') break;
	else if(snake[i] == '=') b++;
	else{
	  flg = false;
	  break;
	}
      }

      if(!flg || a == 0 || b == 0 || a != b){
	puts("NA");
	continue;
      }  

      if(a == b && snake[len-1] == '~'){
	puts("A");
      }else{
	puts("NA");
      }
    }else{
      bool ok = true;
      int p = 0;
      string ss = "";
      int len = (int)snake.size();

      if(len < 6 || len % 2){
	puts("NA");
	continue;
      } 

      for(int i = 2 ; i < len-2 ; i++,p++){
	ss += snake[i];
	if(p == 1){
	  p = -1;
	  if(ss != "Q="){
	    ok = false;
	    break;
	  }
	  ss = "";
	}
      }

      if(snake[len-2] == '~' && snake[len-1] == '~' && ok){
	puts("B");
      }else{
	puts("NA");
      }
    }
  }

  return 0;
}

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

bool isA( string& s )
{
	int ptr = 0;
	if( s.size() < 6 || s[ 0 ] != '>' || s[ 1 ] != '\'' || s[ s.size() - 1 ] != '~' ){
		return false;
	}
	ptr = 2;
	while( ptr < s.size() - 1 && s[ ptr ] == '=' ){
		++ptr;
	}
	if( s[ ptr ] != '#' ){
		return false;
	}
	int halfLength = ptr - 2;
	if( s.size() != 4 + halfLength * 2 ){
		return false;
	}
	++ptr;
	for( int i = 0; i < halfLength; ++i, ++ptr ){
		if( s[ ptr ] != '=' ){
			return false;
		}
	}
	return true;
}

bool isB( string& s )
{
	int ptr = 0;
	if( s.size() < 6 || s[ 0 ] != '>' || s[ 1 ] != '^' || s[ s.size() - 1 ] != '~' || s[ s.size() - 2 ] != '~' ){
		return false;
	}
	int restLength = s.size() - 4;
	if( restLength % 2 > 0 ){
		return false;
	}
	ptr = 2;
	while( ptr < restLength + 2 ){
		if( s[ ptr ] != 'Q' || s[ ptr + 1 ] != '=' ){
			return false;
		}
		ptr += 2;
	}
	return true;
}

int main()
{
	int n;

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

			string result;
			if( isA( snake ) )      result = "A";
			else if( isB( snake ) ) result = "B";
			else                    result = "NA";

			cout << result << endl;
		}
	}

	return 0;
}

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

#define pb push_back
#define fr first
#define sc second
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int,pii> pip; 
const int INF = (1<<25);
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int n, len;
string s;

int checkA() {
  int i=2, j=len-2;
  while(i<j) {
    if(s[i] != '=' || s[j] != '=') return 0;
    i++; j--;
  }

  if(s[i] == '#' && s[len-1] == '~') return 1;
  return 0;
}

int checkB() {
  if(s[len-1]!='~' || s[len-2]!='~') return 0;
  for(int i=2; i<len-2; i+=2) {
    if(s[i] != 'Q' || s[i+1] != '=') return 0;
  }

  return 2;
}

int solve() {
  if(len<6 || len%2 == 1) return 0;
  if(s[0] == '>' && s[1] == '\'') return checkA();
  if(s[0] == '>' && s[1] == '^')  return checkB();
  return 0;
}

int main() {

  cin>> n;
  while(n--) {
    cin>> s;
    len = s.size();
    int ans = solve();
    if(ans == 0) cout<< "NA";
    if(ans == 1) cout<< 'A';
    if(ans == 2) cout<< 'B';
    cout<< endl;
  }
}