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;

bool isOutOfStr(int ind, string str){
  return ind >= str.length();
}

int main(void){
  int n;
  cin >> n;
  while(n--){
    string str; cin >> str;
    if(!isOutOfStr(1, str) &&str[0] == '>' && str[1] == '\''){
      int sharp_cnt = 0;
      int ind;
      ind = 2;

      if(!isOutOfStr(ind, str) && str[ind] != '='){
        cout << "NA" << endl;
        continue;
      }

      for(;;ind++){
        if(isOutOfStr(ind, str) || str[ind] != '='){
          break;
        }
        sharp_cnt++;
      }

      if(isOutOfStr(ind, str) || str[ind] != '#'){
        cout << "NA" << endl;
        continue;
      }
      ind++;

      bool flg = true;
      for(int i=0; i < sharp_cnt; i++, ind++){
        if(isOutOfStr(ind, str) || str[ind] != '='){
          cout << "NA" << endl;
          flg = false;
          break;
        }
      }
      if(!flg) continue;
      
      if(!isOutOfStr(ind, str) && str[ind] == '~')
        cout << "A" << endl;
      else{
        cout << "NA" << endl;
      }



    }else if(str[0] == '>' && str[1] == '^'){
      int ind;
      ind+=2;

      int flg = true;
      for(;;ind+=2){
        if(isOutOfStr(ind, str) || !(str[ind] == 'Q' && str[ind+1] == '=')){
          break;
        }
      }

      if(!isOutOfStr(ind, str) && !isOutOfStr(ind+1, str) && str[ind] == '~' && str[ind+1] == '~'){
        cout << "B" << endl;
      }else{
        cout << "NA" << endl;
      }
    }else{
      cout << "NA" << endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int main(){
  int n,size;
  string s,a,b;
  for(cin>>n;n>0;n--){
    cin>>s;
    size = s.size();
    if(size%2){cout<<"NA"<<endl;continue}
    a=">'";
    b=">^";
    for(int i=0;i<size/2-2;i++){a=a+"=";b=b+"Q=";}
    a+="#";
    for(int i=0;i<size/2-2;i++){a=a+"=";}
    a+="~";
    b+="~~";
    if (s==a)cout<<"A"<<endl;
    else if (s==b)cout<<"B"<<endl;
    else cout<<"NA"<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
int main(){
    int n; cin>>n;
    rep(hoge,n){
        string s;cin>>s;
        int state=0;
        if(s.size()<=5){cout<<"NA"<<endl; continue;}
        if(s[0]=='>'&&s[1]=='\'')state=1;
        else if(s[0]=='>'&&s[1]=='^')state=2;
        int cal=0,cnt=0; bool mybool=true,flag=true,key;
        if(state){
            rep(i,s.size()){
                if(i==0||i==1)continue;
                if(state==1){
                    if(s[i]=='#')mybool=false;
                    else if(mybool&&s[i]=='=')cnt++;
                    else if(mybool==false&&s[i]=='=')cnt--;
                    else if(s[i]=='~'&&i==s.size()-1)continue;
                    else flag=false;
                }else if(state==2){
                    if(!(s[s.size()-1]=='~'&&s[s.size()-2]=='~'))flag=false;
                    if(i>=s.size()-2)continue;
                    if((s[i]=='Q'&&s[i+1]=='=')){i++;}
                    else flag=false;
                }
            }
            if(state==1&&cnt==0&&flag)cout<<"A"<<endl;
            else if(state==2&&flag)cout<<"B"<<endl;
            else cout<<"NA"<<endl;
        }
    }
}

C++: #include <stdio.h>

bool checkA(char *p){
    int eqCnt=0;
    
    if (*p++!='>') return false;
    if (*p++!='\'') return false;
    if (*p!='=') return false;
    while (*p++=='=') eqCnt++;
    p--;
    if (*p++!='#') return false;
    while (*p++=='=') eqCnt--;
    p--;
    if (eqCnt!=0) return false;
    if (*p++!='~') return false;
    if (*p!='\0') return false;
    
    return true;
}

bool checkB(char *p){
    if (*p++!='>') return false;
    if (*p++!='^') return false;
    if (*p!='Q') return false;
    while (*p++=='Q'){
        if (*p++!='=') return false;
    }
    p--;
    if (*p++!='~' || *p++!='~') return false;
    if (*p!='\0') return false;
    
    return true;
}

int main(void){
    int n;
    char str[256];
    
    scanf("%d", &n);
    for (int i=0; i<n; i++){
        scanf("%s", str);
        if (checkA(str)){
            puts("A");
        } else if (checkB(str)){
            puts("B");
        } else {
            puts("NA");
        }
    }
    
    return 0;
}

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

int main()
{
	int n;
	cin >> n;
	for(int i=0;i<n;i++){
		bool a=false,b=false;
		string str;
		cin >> str;
		if(str[0] == '>' && str[1] == '\'' && str.size()%2==0){a = true;}
		if(str[0] == '>' && str[1] == '^' && str.size()%2==1){b = true;}
		if(a){
			for(unsigned j=2;j<str.size()-1;j++){
				if(j != (str.size()/2) && str[j] != '='){
					a = false;
					break;
				}
			}
			if(str[str.size()/2] != '#' || str[str.size()-1] != '~')
				a=false;
		}		
		if(b){
			for(unsigned j=2;j<str.size()-2;j+=2){
				if(str[j] != 'Q' || str[j+1] != '='){
					b = false;
					break;
				}
			}
			if(str[str.size()-2] != '~' || str[str.size()-1] != '~')
				b=false;
		}
		if(a)
			cout << "A" << endl;
		else if(b)
			cout << "B" << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}

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

using namespace std;

bool kind_a(string str){
	
	if(str[0] != '>' || str[1] != '\'' || str[str.size()-1] != '~'){
		return false;
	}
	
	string su = str.substr(2, str.size()-3);
	
	if(su.size()<3)
		return false;
	
	for(int i=0; i<su.size(); ++i){
		
		int j = su.size()-1-i;
		
		if(i>=j)
			break;
			
		if(su[i]!='=' || su[i]!=su[j]){
			return false;
		}	
	}
	
	return su[(su.size()-1)/2]=='#';
}

bool kind_b(string str){
		
	if(str[0] != '>'  || str[1] != '^' || 
		str[str.size()-2] != '~' || str[str.size()-1] != '~'){
		return false;
	}
	
	string su = str.substr(2, str.size()-4);
	
	if(su.size()<2)
		return false;
	
	for(int i=0; i<su.size(); ++i){
		
		int j = su.size()-1-i;
		
		if(i>=j)
			break;
			
		if( !(su[i]=='Q' && su[j]=='=' || su[j]=='Q' && su[i]=='=') ){
			return false;
		}
	}
	
	return true;
}

string solve(string str){
	
	if(kind_a(str)){
		return "A";
	}
	else if(kind_b(str)){
		return "B";
	}
	else{
		return "NA";
	}
}


int main(){

	int n;
	cin>>n;
	
	while(n--){
		
		string str;
		cin>>str;
		cout << solve(str) << endl;
	}
	
	return 0;
}