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;
typedef long long ll;
int main(){
  int n;
  cin>>n;
  while(n--){
    string s;
    cin>>s;
    bool f=s[0]=='>';
    int l=s.size();
    f&=l>5;
    if(s[1]=='\''){
      int x=0;
      int i;
      for(i=2;i<l;i++){
	if(s[i]=='=') x++;
	else{
	  f&=s[i]=='#';
	  break;
	}
      }
      for(i++;i<l;i++){
	if(s[i]=='=') x--;
	else{
	  f&=s[i]=='~';
	  break;
	}
      }
      f&=i==l-1&&x==0;
    }else if(s[1]=='^'){
      int i;
      for(i=2;i<l;i+=2){
	if(!(i+1<l)){
	  f=0;
	  break;
	}
	if(s[i]=='Q'&&s[i+1]=='=') continue;
	if(s[i]=='~'&&s[i+1]=='~') continue;
      }
      f&=i==l;
    }else f=0;
    cout<<(f?(s[1]=='^'?"B":"A"):"NA")<<endl;
  }
  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;break;
    }
    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 <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <map> 
#include <sstream>
#include <cstdio>
#include <complex>
#include <cstring>
using namespace std;
 
const int MAX= 10000100;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
#define ll long long int
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a)
int GCD(int a, int b) {if(!b) return a; return gcd(b, a%b);}
int lcm(int a, int b) {return a*b / gcd(a, b);}

bool A(string s){
  if(s.size() < 6) return false;
  if(s.size()%2 != 0)return false;
  if(s[0] != '>')return false;
  if(s[1] != '\'')return false;
  if(s[s.size()-1] != '~')return false;
  if(s[s.size()/2] != '#')return false;
  else s[s.size()/2] = '=';
  loop(i,2,s.size()-1){
    if(s[i] != '=')return false;
  }
  return true;
}
bool B(string s){
  if(s.size() < 6) return false;
  if(s[0] != '>')return false;
  if(s[1] != '^')return false;
  loop(i,2,s.size()-2){
    if(i%2){
      if(s[i] != '=')return false;
    }else{
      if(s[i] != 'Q')return false;
    }
  }
  if(s[s.size()-2] != '~')return false;
  if(s[s.size()-1] != '~')return false;
  return true;
}
int main(void){
  int n;
  cin>>n;
  rep(i,n){
    string s;
    cin>>s;
    if(A(s))cout<<"A";
    else if(B(s))cout<<"B";
    else cout<<"NA";
    cout<<endl;
  }
}

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

using namespace std;

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

  for(int q=0;q<n;q++) {
    string str;
    cin >> str;
    if(str[0] != '>') {
      cout <<"NA"<<endl; 
      continue;
    }  else if(str[1] != '\''&& str[1] != '^') {
      cout << "NA" << endl;
      continue;
    }
    int flg = 0,sum;
    if(str[1] == '\'') {
      int t = 0;
      sum=0;
      for(int i=2;i<str.size();i++) {
	if(str[i] == '=') t++;
	else if(str[i] == '#' && sum==0) sum = t , t = 0;
	else if(str[i] == '~' && i == str.size() -1 && sum==t) flg = 1;
	else break;
      }
    }

      if(str[1] == '^' && flg == 0) {
	flg = 0;
	for(int i=2;i<str.size() ;i++) {
	  if(i%2 == 0 && str[i] != 'Q') break;
	  if(i%2 == 1 && str[i] !='=')  break;

	  if(str[str.size()-2] == '~' && str[str.size()-1] =='~' && i == str.size()-3&& i%2==1 ){
	    flg = 2;
	    break;
	  }
	}
      }

      if(flg == 0) cout << "NA" << endl;
      else if(flg == 1)cout <<"A" << endl;
      else if(flg == 2) cout << "B"<<endl;
  }     


  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)
#define all(a) (a).begin(), (a).end()

const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

int main(){
	int n; cin >> n;
	rep(q, n){
		string s; cin >> s;
		if(s.substr(0, 2) == ">'"){
			s = s.substr(2);
			int c = 0;
			bool p = true;
			for(int i=0; i < s.size(); i++) if(s[i] == '='){c++;} else break;
			if(s[c] != '#' || c == 0)p = false;
			s = s.substr(c+1);
			if(c+1 != s.size() || s[s.size()-1] != '~')p = false;
			rep(i, c) if(s[i] != '=')p = false;
			cout << (p ? "A" : "NA") << endl;
		}else if(s.substr(0, 2) == ">^"){
			bool p = true;
			s = s.substr(2);
			if(s.substr(0, 2) != "Q=") p = false;
			while(s.substr(0, 2) == "Q=") s = s.substr(2);
			cout << (s == "~~" && p ? "B" : "NA") << endl;
		}else {
			cout << "NA" << endl;
		}
	}
}

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

using namespace std;

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

  for(int q=0;q<n;q++) {
    string str;
    cin >> str;
    if(str[0] != '>') {
      cout <<"NA"<<endl; 
      continue;
    }  else if(str[1] != '\''&& str[1] != '^') {
      cout << "NA" << endl;
      continue;
    }
    int flg = 0,sum;
    if(str[1] == '\'') {
      int t = 0;
      sum=0;
      for(int i=2;i<str.size();i++) {
	if(str[i] == '=') t++;
	else if(str[i] == '#' && sum==0) sum = t , t = 0;
	else if(str[i] == '~' && i == str.size() -1 && sum==t) flg = 1;
	else break;
      }
    }

      if(str[1] == '^' && flg == 0) {
	flg = 0;
	for(int i=2;i<str.size() ;i++) {
	  if(i%2 == 0 && str[i] != 'Q') break;
	  if(i%2 == 1 && str[i] !='=')  break;

	  if(str[str.size()-2] == '~' && str[str.size()-1] =='~' && i == str.size()-3 ){
	    flg = 2;
	    break;
	  }
	}
      }

      if(flg == 0) cout << "NA" << endl;
      else if(flg == 1)cout <<"A" << endl;
      else if(flg == 2) cout << "B"<<endl;
  }     


  return 0;
}