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>
using namespace std;
int n;
string s;
main()
{
    cin>>n;
    for(;n--;)
    {
        cin>>s;
        string t="NA";
        if(s.substr(0,2)==">'")
        {
            int cnt=0;
            int i=2;
            while(i<s.size()&&s[i]=='=')i++,cnt++;
            if(cnt>0&&s[i]=='#')
            {
                int j=i+1;
                while(j<s.size()&&s[j]=='=')j++,cnt--;
                if(cnt==0&&j+1==s.size()&&s.substr(j,1)=="~")t="A";
            }
        }
        else if(s.substr(0,2)==">^")
        {
            int i=2;
            while(i+2<s.size()&&s.substr(i,2)=="Q=")i+=2;
            if(i>2&&i+2==s.size()&&s.substr(i,2)=="~~")
            {
                t="B";
            }
        }
        cout<<t<<endl;
    }
}

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

using namespace std;

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

	while(n--)
	{
		string str;
		string kp;
		string set;
		int flag=0;
		int count=0;
		int i=0;

		cin >> str;
		for(i=0;i<2;i++)kp+=str[i]++;
		
		if(kp==">'")
		{
			while(str[i]!='#')
			{
				if(str[i]=='=')count++;
				else flag=-1;

				if(flag==-1)break;
				i++;	
			}
			i++;
			while(str[i]!='~')
			{
				if(str[i]=='=')count--;
				else flag=-1;

				if(flag==-1)break;
				i++;
			}
			if(flag!=-1 && !count)flag=1;
		}
		else if(kp==">^")
		{
			while(i<str.length()-2)
			{
				count=2;
				while(count--)
				{
					set+=str[i];
					i++;
				}
				if(set=="Q=")flag=2;
				else flag=-1;

				set.erase(set.begin(),set.end());
				if(flag==-1)break;
			}
			for(i=str.length()-2;i<str.length();i++)set+=str[i];
			if(set=="~~" && flag==2)flag=2;
			else flag=-1;
		}

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

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

int n;
string s;

bool checkA(string s) {
	if (s.length() < 6) return false;
	if (s[0] != '>') return false;
	if (s[1] != '\'') return false;
	
	int i;
	for (i = 2; i < s.length() && s[i] == '='; i++);
	if (i == s.length() || s[i] != '#') return false;
	int cnt = i - 2;
	for (i = i + 1; i < s.length() && s[i] == '='; i++, cnt--);
	if (cnt != 0) return false;
	if (s.length() != i + 1) return false;
	if (s[i] != '~') return false;
	return true;
}

bool checkB(string s) {
	if (s.length() < 6) return false;
	if (s[0] != '>') return false;
	if (s[1] != '^') return false;
	int i;
	for (i = 2; i < s.length(); i += 2) {
		if (i + 1 >= s.length()) return false;
		if (s[i] != 'Q') break;
		if (s[i + 1] != '=') return false;
	}
	if (s.length() != i + 2) return false;
	if (s[i] != '~' || s[i + 1] != '~') return false;
	return true;
}

int main() {
	cin >> n;
	while (n--) {
		cin >> s;
		if (checkA(s)) cout << "A" << endl;
		else if (checkB(s)) cout << "B" << endl;
		else cout << "NA" << endl;
	}
	return 0;
}

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

using namespace std;

bool check_A(string str){
	if(str.size() < 6 || str.size() % 2 == 1){
		return false;
	}
	if(str.substr(0, 2) != ">'"){
		return false;
	}
	int count = 0;
	for(int i = 2; i < (str.size() - 4) / 2 + 2; i++){
		if(str[i] != '='){
			return false;
		}
	}
	if(str[str.size() / 2] != '#'){
		return false;
	}
	for(int i = str.size() / 2 + 1; i <= str.size() - 2; i++){
		if(str[i] != '='){
			return false;
		}
	}
	if(str[str.size() - 1] != '~'){
		return false;
	}
	return true;
}

bool check_B(string str){
	if(str.size() < 6 || str.size() % 2 == 1){
		return false;
	}
	if(str.substr(0, 2) != ">^"){
		return false;
	}
	for(int i = 2; i < str.size() - 2; i += 2){
		if(str.substr(i, 2) != "Q="){
			return false;
		}
	}
	if(str.substr(str.size() - 2, 2) != "~~"){
		return false;
	}
	return true;
}

int main(){
	
	int n;
	
	string str;
	
	cin >> n;
	
	for(int loop = 0; loop < n; loop++){
		cin >> str;
		if(check_A(str)){
			printf("A\n");
		}else if(check_B(str)){
			printf("B\n");
		}else{
			printf("NA\n");
		}
		
	}
	
	return 0;
}

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

int n;

int main()
{
    cin >> n;

    for (int i = 0; i < n; i++) {
        string s, t, w;

        cin >> s;
        t = "NA";

        int m = s.size();

        if (m > 4 && m % 2 == 0) {
            string u(m - 4, '=');
            u.insert(u.begin() + m / 2 - 2, '#');
            if (s == ">'" + u + '~')
                t = "A";

            for (int j = 0; j < (m / 2 - 2); j++)
                w += "Q=";

            if (s == ">^" + w + "~~")
                t = "B";            
        }

        cout << t << endl;
    }

    return 0;
}

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

using namespace std;

int main()
{
	int n;
	cin >> n;
	for(int D = 0; D < n; ++D){
		string s;
		cin>>s;
		
		if(s.compare(0,2,">'")== 0){
			int bef_eq = 0;
			int aft_eq = 0;
			int shp = 0;
			int pos_shp = 0;
			int pos_tld = 0;

			for(unsigned int i = 2; i < s.length(); ++i ){
				if( s[i] == '=' )
					if( shp == 0 )
						++bef_eq;
					else
						++aft_eq;
				else if( s[i] == '#' ){
					shp++;
				}else if( s[i] == '~' ){
					pos_tld = i;
					break;
				}else{
					shp = 2;
					break;
				}
			}
			if( bef_eq && aft_eq && pos_tld && bef_eq == aft_eq &&
				shp == 1 && pos_tld == s.length() - 1 )
				cout << "A\n";
			else
				cout << "NA\n";
		}else if(s.compare(0,2,">^")==0){
			int cnt_qeq = 0;
			int pos_tltl = 0;
			bool invalid = false;
			
			if( s.length() <= 2 || s.length() % 2 == 1 )
				invalid = true;
			
			for(unsigned int i = 2; i < s.length() && !invalid; i+=2){
				if( s.compare(i,2,"Q=") == 0 )
					++cnt_qeq;
				else if( s.compare(i,2,"~~") == 0 ){
					pos_tltl = i;
					break;
				}else
					invalid = true;
			}
			if( cnt_qeq == 0 || pos_tltl != s.length() - 2 ){
				invalid = true;
			}
			if( invalid )
				cout << "NA\n";
			else
				cout << "B\n";
		}else{
			cout <<"NA\n";
		}
	}
	return 0;
}