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]=='^')
			{
				if(str.size()<6)
					cout << "NA" << endl;
				else
				{
					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;
		}
		else
			cout << "NA" << endl;
	}
}

C++: #include<iostream>
#include<string>
using namespace std;
int main(){
  int n,size;
  string s,a,b;
  for(cin>>n,getline(cin,s,'\n');n>0;n--){
    getline(cin,s,'\n');
    //cin>>s;
    size = s.size();
    if(size<6||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>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int main()
{
	int n;
	cin >> n;
	
	rep(i,n)
	{
		string s;
		cin >> s;

		if(s[0] == '>' && s[1] == '\'' && s[2] == '=')
		{
			int i = 2;
			int cnt = 0;
			while(s[i] == '=')
			{
				i++;
				cnt++;
			}

			if(s[i] == '#' && i < s.size())
			{
				i++;
				while(s[i] == '=')
				{
					if(i == s.size()-1) break;
					i++;
					cnt--;
				}
			}

			if(cnt == 0 && s[i] == '~') cout << "A" << endl;
			else cout << "NA" << endl;
		}
		else if(s[0] == '>' && s[1] == '^' && s[2] == 'Q')
		{
			int i = 2;
			int cnt = 0;
			bool end = false;
			while(i+1 < s.size())
			{
				if(s[i] == 'Q' && s[i+1] == '=')
				{
					i += 2;
				}
				else if(s[i] == '~' && s[i+1] == '~')
				{
					end = true;
					cout << "B" << endl;
					i += 2;
				}
				else
				{
					break;
				}
			}

			if (!end) cout << "NA" << endl;
		}
		else
		{
			cout << i << " NA" << endl;
		}
	}
	return 0;
}