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<stdio.h>
#include<string.h>

bool checkA(char st[1001]){
	int len = strlen(st);
	if(st[len-1]!='~')return false;
	if(len%2 == 1)return false;
	for(int i = 0; i < len/2-1; i++){
		if(!(st[i] == st[len-2-i] && st[i] == '='))return false;
	}
	return true;
	
}
bool checkB(char st[1001]){
	if(st[strlen(st)-1] != '~' || st[strlen(st)-2] != '~')return false;
	for(int i = 0 ; i < strlen(st)-2; i+=2){
		if(!(st[i]=='Q' && st[i+1] =='='))return false;
	}
	return true;
}


int main(void){
	int n;
	scanf("%d",&n);
	for(int i = 0; i < n ;i++){
		char str[1001];
		scanf("%s",str);
		
		bool Af = false;
		bool Bf = false;
		
		if(str[1] == '\''){
			Af = checkA(str+2);
		}else {
			Bf = checkB(str+2);
		}
		
		if(Af == Bf){
			puts("NA");
		}else if(Af == true){
			puts("A");
		}else{
			puts("B");
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define rep(i,n) for (int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
int main(){
  int n;
  cin>>n;
  while(n--){
    string s;
    cin>>s;
    int f1=0,f2=0,k=-1;
    bool f;
    for(int i=0;i<s.size();i++)if(s[i]=='>')k=i;
    if(k==-1||s.size()<5){
      printf("NA\n");
      continue;
    }
    if(s[k+1]=='^'){
      f=false;
      for(int i=k;i+1<s.size();i++)if(s[i]=='Q'&&s[i+1]=='=')f=true;
      if(f&&s[s.size()-1]=='~'&&s[s.size()-2]=='~'){
        printf("B\n");
        continue;
      }
    }
    int cnt=0;
    for(int i=k;i<s.size();i++){
      if(s[i]=='='){
        while(s[i]=='='&&i<s.size()){
          cnt++;i++;
        }
        k=i;
        break;
      }
    }
    f=false;
    for(int i=k;i<s.size();i++){
      if(s[i]=='#'){
        k=i;f=true;
      }
    }
    if(f){
      for(int i=k;i<s.size();i++){
        if(s[i]=='='){
          while(s[i]=='='&&i<s.size()){
            cnt--;i++;
          }
          break;
        }
      }
      if(cnt==0&&s[s.size()-1]=='~'){
        printf("A\n");
        continue;
      }
    }
    printf("NA\n");
  }
}

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

string solve(string T) {
	int chain = 0, ok = 0;
	if (T.size() >= 3) {
		if (T[T.size() - 1] == '~') {
			if (T.substr(0, 2) == ">'" && T.size() >= 6) {
				for (int i = 2; i < T.size() - 1; i++) {
					if (T[i] == '#') {
						ok++;
					}
					else if (T[i] == '=') {
						if (ok == 0) { chain++; }
						else { chain--; }
					}
					else {
						goto NA;
					}
				}
				if (ok == 1 && chain == 0) { return "A"; }
			}

			if (T.substr(0, 2) == ">^" && T[T.size() - 2] == '~' && T.size() >= 6) {
				for (int i = 2; i < T.size() - 2; i += 2) {
					if (T.substr(i, 2) != "Q=") {
						goto E;
					}
				}
				return "B";
			E:;
			}
		}
	}
NA:;
	return "NA";
}

int main() {
	cin >> n;
	for (int h = 0; h < n; h++) {
		cin >> S;
		cout << solve(S) << endl;
	}
}

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

bool checkA(char st[1001]){
	int len = strlen(st);
	if(st[len-1]!='~')return false;
	if(len%2 == 1)return false;
	st[len/2-1] = '=';
	for(int i = 0; i < len-1; i++){
		if(st[i] !='=')return false;
	}
	return true;
	
}
bool checkB(char st[1001]){
	if(st[strlen(st)-1] != '~' || st[strlen(st)-2] != '~')return false;
	for(int i = 0 ; i < strlen(st)-2; i+=2){
		if(!(st[i]=='Q' && st[i+1] =='='))return false;
	}
	return true;
}


int main(void){
	int n;
	scanf("%d",&n);
	for(int i = 0; i < n ;i++){
		char str[1001];
		scanf("%s",str);
		
		bool Af = false;
		bool Bf = false;
		
		if(str[1] == '\''){
			Af = checkA(str+2);
		}else {
			Bf = checkB(str+2);
		}
		if(str[0] != '>'){
			puts("NA");
		}else if(Af == Bf){
			puts("NA");
		}else if(Af == true){
			puts("A");
		}else{
			puts("B");
		}
	}
	return 0;
}