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<iostream>
#include<string>
using namespace std;

bool isA(string s){

  if(s.size() < 6 || s.size() % 2 == 1) return false;
  if(s.substr(0,2) != ">\'") return false;
  if(s[s.size() - 1] != '~') return false;

  int a, b;
  for( a = 2, b = s.size() - 2 ; a != b ; a++, b--){
    if(s[a] != '=' || s[b] != '=') return false;
  }

  return s[a] == '#';
}
bool isB(string s){

  if(s.size() < 6 || s.size() % 2 == 1) return false;
  if(s.substr(0,2) != ">^") return false;

  if(s.substr(s.size() - 2) != "~~") return false;

  for(int i = 2 ; i < s.size() - 2 ; i += 2){
    if(s.substr( i, 2) != "Q=") return false;
  }
  return true;
}

int main(){
  int n;
  cin >> n;
  while(n--){
    string s;
    cin >> s;
    if(isA(s)) cout << "A\n";
    else if(isB(s)) cout << "B\n";
    else cout << "NA\n";
  }
}

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 main() {

  int n;
  int flg=0;
  cin>> n;
  while(n--) {
    string s;
    int cnt=0;
    flg=0;
    cin>> s;
    if(s[0] == '>') {
      if(s[1] == '\'') {
	int equal[2]={0}; // 0:左 1:右
	int now=0; 
	for(int i=2; i<s.size(); i++) {
	  if(s[i] == '=') {
	    equal[now]++;
	  }
	  else if(s[i] == '#') {
	    now=1;
	  }
	}
	if(equal[0] == equal[1] && s[s.size()-1]=='~') {
	  flg=1;
	}
      }

      else if(s[1] == '^') {
	for(int i=2; i<s.size()-2; i+=2) {
	  if(s[i] != 'Q' && s[i] != '=') {
	    break;
	  }
	  cnt=i;
	}
	if(cnt == (s.size()-4) && s[s.size()-2] == '~' &&  s[s.size()-1] == '~') {
	  flg=2;
	}
      }
    }

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

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main(){
    int N;
    cin>>N;
    while(N--){
        string str;
        cin>>str;
        int len=str.size();
        int ans=-1;
        if(str[0]=='>'&&str[1]=='\''){
            int pos=2;
            int cnt=0;
            int turn=0;
            int flag=0;
            do{
                if(str[pos]=='=')
                    cnt+=(turn)?(-1):(1);
                else if(str[pos]=='#')turn=1;
                else flag=0;
                if(pos==2&&cnt>0)flag=1;
                pos++;
            }while(pos<len-1);
            if(cnt==0&&flag&&str[pos]=='~')ans=0;
        }
        else if(str[0]=='>'&&str[1]=='^'){
            int pos=2;
            int flag=1;
            do{
                if(str[pos]!='Q'||str[pos+1]!='='){
                    flag=0;
                    break;
                }
                pos+=2;
            }while(pos<len-2);
            if(flag&&str[pos]=='~'&&str[pos+1]=='~')ans=1;
        }
        if(ans==0)cout<<"A"<<endl;
        else if(ans==1)cout<<"B"<<endl;
        else cout<<"NA"<<endl;
    }
    return 0;
}

C++: #include <iostream>

int main(){
	int n;
	std::cin >> n;
	while(n--){
		std::string sn, res = "";
		std::cin >> sn;

		//Type A
		//ªª>'CKöª~C©Â¨ ÌªªïÅ é
		if(sn.substr(0,2) == ">'" &&
			 sn[sn.size()-1] == '~' && (sn.size()-3) % 2 == 1){
			std::string st = sn.substr(2,sn.size()-3);
			//¨ Ì^ñª#
			if(st[st.size()/2] == '#'){
				st.replace(st.size()/2,1,"");
				
				//#ÈOª·×Ä=
				// int eq = 1;
				// for(int i=0;i<st.size();i++){
				// 	if(st[i] != '='){eq = 0;break;}
				// }
				// if(eq)std::cout << "A" << std::endl;
				if(st != ""){
					int pos;
					while(pos = st.find("="), pos != std::string::npos){
						st.replace(pos,1,"");
					}
					if(st == "")res = "A\n";
				}
			}
		}

		//Type B
		if(sn.substr(0,2) == ">^" &&
			 sn.substr(sn.size()-2,2) == "~~" &&
			 (sn.size()-4) % 2 == 0){
			std::string st = sn.substr(2,sn.size()-4);
			if(st != ""){
				int pos;
				while(pos = st.find("Q="), pos != std::string::npos){
					st.replace(pos,2,"");
				}
				if(st == "")res = "B\n";
			}
		}

		if(res == "")res = "NA\n";
		std::cout << res;
	}
}

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] == '^'){b = true;}
		if(a){
			for(unsigned j=2;j<str.size()-1;j++){
				if((j != (str.size()-3)/2+2) && str[j] != '='){
					a = false;
					break;
				}
			}
			if(str[(str.size()-3)/2+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 <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n;
string s;

int main(){
	cin>>n;
	while(cin>>s){
		if(s.size()<6||s[0]!='>'){cout<<"NA"<<endl;continue;}
		int cur=2,l=0,l2=0,f=0,f2=0;
		if(s[1]=='\''){
			while(cur<s.size()){
				if(f2){f2=0;break;}
				else if(s[cur]=='='){
					if(!f)l++;
					else l2++;
				}
				else if(s[cur]=='#'){
					if(!l||f)break;
					else f=1;
				}
				else if(s[cur]=='~'){
					if(l&&l==l2){f2=1;}
					else break;
				}
				else break;
				cur++;
			}
		}
		else if(s[1]=='^'){
			while(cur<s.size()){
				if(f2){f2=0;break;}
				if(s[cur]=='Q'){
					if(!f)f=1;
					else break;
				}
				else if(s[cur]=='='){
					if(f==1)f=0;
					else break;
				}
				else if(s[cur]=='~'){
					if(!f)f=2;
					else if(f==2)f2=2;
					else break;
				}
				else break;
				cur++;
			}
		}
		if(f2==1)cout<<"A"<<endl;
		else if(f2==2)cout<<"B"<<endl;
		else cout<<"NA"<<endl;
	}
}

C++: 
#include <bits/stdc++.h>
using namespace std;
int main(){
  int i,n;
  string s;
  cin>>n;
  while(n--){
    cin>>s;
    if(s[0]=='>'&&s[1]=='\''&&s.size()%2>5){
      for(i=2;i<s.size();i++){
	if(i==s.size()/2&&s[i]!='#')break;
	if(i==s.size()/2){i++;continue;}
	if(i==s.size()-1&&s[i]=='~'){
	  cout<<"A"<<endl;
	  goto L;
	}
	if(s[i]!='='){cout<<i;break;}
      }
    }
    else if(s[0]=='>'&&s[1]=='^'&&s.size()%2==0&&s.size()>4){
        for(i=2;i<s.size();i++){
	if(i==s.size()-2&&s[i]=='~'&&s[i+1]=='~'){
	  cout<<"B"<<endl;
	  goto L;
	}
	if(s[i]!='Q'||s[i+1]!='=')break;
	else i++;
      }
    }
    cout<<"NA"<<endl;
  L:;
  }
}