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(const string& s){
	if( s.size() >= 1 && s[0] != '>' ) return false;
	if( s.size() >= 2 && s[1] != '\'' ) return false;
	
	bool a_flag = true;
	bool equal_flag = false;
	int cnt = 0;
	int j;
	for(int i=1 ; i < s.size() ; i++ ){
		if( equal_flag == false ){
			if( s[i] == '=' ){
				equal_flag = true;
				for(j=i ; j < s.size() && s[j] == '=' ; j++ ){
					cnt++;
				}
				if( j >= s.size() ){
					a_flag = false;
					break;
				}else if( s[j] != '#' ){
					a_flag = false;
					break;
				}else{
					j++;
				}
			}
		}else{
			for( ; j < s.size() && s[j] == '=' ; j++ )
				cnt--;
		}
	}
	if( cnt != 0 ) return false;
	if( a_flag == false ) return false;
	if( equal_flag == false ) return false;
	if( s[s.size()-1] != '~' ) return false;
	return true;
}

bool isB(const string& s){
	if( s.size() >= 1 && s[0] != '>' ) return false;
	if( s.size() >= 1 && s[1] != '^' ) return false;
	
	bool q_equal_flag = false;
	for(int i=3 ; i < s.size() ; i++ ){
		if( s[i-1] == 'Q' && s[i] == '=' ){
			q_equal_flag = true;
		}
	}
	
	if( s[s.size()-2] != '~' || s[s.size()-1] != '~' ){
		return false;
	}
	if( q_equal_flag == false ) return false;
	return true;
}

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

C++: #include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#define pb(in,tmp) in.push_back(tmp)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) loop(i,0,b)
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
using namespace std;
int main(){
  int n;
  cin>>n;
  rep(i,n){
    string ans;
    string s;
	cin>>s;
	if(s[0]!='>'){cout<<"NA"<<endl;continue;}
	if(s[1]=='^'){
	  ans='B';
	  int j=2;
	  while(s[j++]=='Q'&&s[j++]=='=');
	  j--;
      if(s[j++]!='~'||s[j++]!='~')ans="NA";
	  if(j!=s.size())ans="NA";
	}else if(s[1]=='\''){
	  ans='A';
	  int h1=0,h2=0;
	  int j=1;
	  while(s[++j]=='=')h1++;
	  if(s[j]!='#')ans="NA";
	  while(s[++j]=='=')h2++;
	  if(s[j]!='~')ans="NA";
	  if(++j!=s.size())ans="NA";
	  if(h1!=h2)ans="NA";
	}else ans="NA";
	cout<<ans<<endl;
  }
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int main(){
	string s;
	int n;
	cin>>n;
	rep(i,n){
		cin>>s;
		int size=s.size();
		string A="";
		string B="";
		rep(i,size){
			if(i==0){A+=">";B+=">";}
			else if(i==1){A+="'";B+="^";}
			else{
				if(i<size-1){
					if(i==(size-3)/2+2)A+="#";
					else A+="=";
				}else A+="~";
				if(i<size-2){
					if(i%2==0)B+="Q";else B+="=";
				}else{B+="~";}
			}
		}
		if(size%2==0){
			if(s==A)cout<<"A"<<endl;
			else if(s==B)cout<<"B"<<endl;
			else cout<<"NA"<<endl;
		}else cout<<"NA"<<endl;
	}
	return 0;
}

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

using namespace std;

int main() {
  int Tc; string s;
  while(cin>>Tc) {
    while(Tc--) {
      cin >> s;
      int const N = s.size();
      bool NA = 0;
      bool isA;
      if(s[0] != '>') NA = 1;
      if(s[1] == '\'') {
	for(int i=2; i<(N-3)/2+2; i++) if(s[i]!='=') NA=1;
	if(s.substr(2, (N-3)/2) + "~" != s.substr(3+(N-3)/2)) NA = 1;
	if(s[(N-3)/2+2] != '#') NA = 1;
	isA = 1;
      }
      else if(s[1] == '^') {
	if( !(s[2] == 'Q' && s[3] == '=') ) NA = 1;
	int cnt;
	for(cnt=2; cnt<N-2; cnt+=2) {
	  if(!(s[cnt] == 'Q' && s[cnt+1] == '=')) NA = 1;
	}
	if(!(s[cnt] == '~' && s[cnt+1] == '~')) NA = 1;
	isA = 0;
      }
      else {
	NA = 1;
      }
      
      if(NA) cout << "NA" << endl;
      else if(isA) cout << "A" << endl;
      else cout << "B" << endl;
    }
  }
  
  return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<string>
#include<math.h>
#include<algorithm>
using namespace std;


int main(){
	string str;
	
	int N;
	cin>>N;
	for(int i=0;i<N;i++){
	cin>>str;
		string x="  ";
	

	for(int j=0;j<2;j++)
		x[j]=str[j];
	if(str[0]=='>'){
	if(str[1]=='\''&&str[str.size()-1]=='~'){
		bool f=true;int C=0;
		int mid=(2+str.size()-2)/2;
		for(int j=2;j<=str.size()-2;j++)
		{if(j!=mid&&str[j]!='='){f=false;}
		else if(j==mid&&str[j]!='#')f=false;
		if(str[j]=='=')C++;
		}
		if(C%2!=0||C==0)f=false;
		
		if(f==true)cout<<"A"<<endl;
		else cout<<"NA"<<endl;
	}

	else if(str[1]=='^'&&str[str.size()-1]=='~'&&str[str.size()-2]=='~'){
		bool f=true;int C=0;
		
		for(int j=2;j<str.size()-2;j++)
		{
			C++;
			if(C%2==0&&str[j]!='=')f=false;
			if(C%2==1&&str[j]!='Q')f=false;

		}
	
		if(C%2!=0||C==0)f=false;
		
		if(f==true)cout<<"B"<<endl;
		else cout<<"NA"<<endl;
	
	
	}
	}
	else cout<<"NA"<<endl;


	}
	return 0;
}

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

string repeat(int n, string s) {
    string res = "";
    for (int i=0; i<n; ++i)
        res += s;
    return res;
}

int main() {
    int N; cin >> N;
    for (int i=0; i<N; ++i) {
        string snake; cin >> snake;

        int len = snake.size();
        if (len <= 4) {
            cout << "NA" << endl;
            continue;
        }

        string A = ">'" + repeat((len - 4)/2, "=") 
                    + "#" + repeat((len - 4)/2, "=") + "~";
        string B = ">^" + repeat((len - 4)/2, "Q=") + "~~";

        if (snake == A) {
            cout << "A" << endl;
        } else if (snake == B) {
            cout << "B" << endl;
        } else {
            cout << "NA" << endl;
        }
    }
    return 0;
}

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

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)

int main() {
	int N;
	cin >> N;
	REP(n, N) {
		string str;
		int type = 0, pos = 0;
		cin >> str;
		
		if (str[pos] == '>') {
			pos++;
			if (str[pos] == '\'') {
				pos++;
				int cnt = 0;
				while (pos < str.size() && str[pos] == '=') { cnt++; pos++; }
				
				if (cnt != 0 && str[pos] == '#') {
					pos++;
					while (pos < str.size() && str[pos] == '=') { cnt--; pos++; }
					if (cnt == 0 && str[pos] == '~') type = 1;
				}
				
			}
			else if (str[pos] == '^') {
				bool f = false;
				pos++;
				while (pos + 1 < str.size()) {
					if (str[pos] == 'Q' && str[pos + 1] == '=') { pos += 2; f = true; }
					else break;
				}
				if (f && pos + 2 == str.size() && str[pos] == '~' && str[pos + 1] == '~')
					type = 2;
			}
		}
		
		
		switch (type) {
		case 0: cout << "NA" << endl; break;
		case 1: cout << "A" << endl; break;
		case 2: cout << "B" << endl; break;
		}
	}
	return 0;
}