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;

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

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;


int main() {
	int n;
	string s;
	cin >> n;
	rep(i, n) {
		cin >> s;
		string ans;
		if (s[0] == '>'&&s[s.size() - 1] == '~') {
			if (s[1] == '\'') {
				ans = "A";
				for (int i = 2; i < s.size() - 1) {
					if (i == s.size() / 2) {
						if (s[i] != '#') {
							ans = "NA";
							break;
						}
					}
					else {
						if (s[i] != '=') {
							ans = "NA";
							break;
						}
					}
				}
			}
			else if (s[1] == '^'&&s[s.size()-2]=='~') {
				ans = "B";
				for (int i = 2; i < s.size() - 2; i += 2) {
					if (s[i] != 'Q' || s[i + 1] != '=') {
						ans = "NA";
						break;
					}
				}
			}
			else ans = "NA";
		}
		else {
			ans = "NA";
		}
		cout << ans << endl;
	}
}

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

int main(){
  int n,j,fa,fb,acount,acount2,i;
  string data;
  
  cin >> n;
  
  getline(cin,data);
  for(j = 0; j < n; j++){
    fa = 0;
    fb = 0;
    acount = acount2 = 0;
    getline(cin,data);
    if(data.length() < 6)
      data = "OOHAZURE";
    if(data[0] == '>' && data[1] == '\''){
      fa = 1;
      i = 2;
      while(data[i] == '='){
	acount++;
	i++;
      }
      if(data[i] != '#')
	fa = 0;
      i++;
      while(data[i] == '='){
	acount2++;
	i++;
      }
      if(i != data.length()-1)
	fa = 0; 
      if(acount != acount2)
	fa = 0;
      if(data[data.length()-1] != '~'){
	fa = 0;
      }
    }
    if(data[0] == '>' && data[1] == '^'){
      fb = 1;
      for(i = 2; i < data.length()-3; i = i+2){
	if(data[i] != 'Q' || data[i+1] != '=')
	  fb = 0;
      }
      if(i != data.length()-2)
	fb = 0;
      if(data[data.length()-1] != '~' || data[data.length()-2] != '~')
	fb = 0;
    }
    if(fa == 1)
      cout << "A" << endl; 
    else if(fb == 1)
      cout << "B" << endl;
    else 
      cout << "NA" << endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#define INF 999999
using namespace std;
int main(){
    int N,j;
    cin>>N;
    while(N--){
        int runcnt=0,j;
        char S[150]={0};
        cin>>S;
            if(S[0]=='>'&&S[1]==39){
                int K=0;
                for(j=2;j<1000;j++){
                    if(S[j]=='='){
                        runcnt++;
                        K=1;
                    }
                    else if(S[j]=='#'){
                        break;
                    }
                    else {
                        cout<<"NA"<<endl;
                        goto end;
                    }
                }
                for(int i=j+1;i<1000;i++){
                    if(S[i]=='='){
                        runcnt--;
                    }
                    else if(runcnt==0&&S[i]=='~'&&K==1){
                        cout<<"A"<<endl;
                        goto end;
                    }
                    else{
                        cout<<"NA"<<endl;
                        goto end;
                    }
                }
            }
            else if(S[0]=='>'&&S[1]=='^'){
                int F=0;
                for(j=2;j<1000;){
                    if(S[j]=='Q'&&S[j+1]=='='){
                        j+=2;
                        F=1;
                    }
                    else if(S[j]=='~'&&S[j+1]=='~'&&F==1){
                        cout<<"B"<<endl;
                        goto end;
                    }
                    else{
                        cout<<"NA"<<endl;
                        goto end;
                    }
                }
            }
            else cout<<"NA"<<endl;
            end:;
    }
    return 0;
}

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 && i!=2) sum = t , t = 0;
    else break;
    if(str[str.size()-1] == '~' && i == str.size() -2 && sum==t ) flg = 1;
     
      }
    }
 
      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;
}