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;

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 % 2 == 1) {
            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<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;
					}
					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<bits/stdc++.h>
using namespace std;

char buf[1024];
int M;
bool checkA(){
  if( buf[0] != '>' ) return false;
  if( buf[1] != '\'') return false;
  int cnt1=0,cnt2=0;
  bool f = false;
  int i;
  for(i=2;i<M;i++){
    if(!f){
      if( buf[i] == '=' ) cnt1++;
      else if( buf[i] == '#' ) f = true;
      else return false;
    } else {
      if( buf[i] == '=' ) cnt2++;
      else break;
    }
  }
  if( buf[i] != '~' ) return false;
  i++;
  if( i == M && cnt1 == cnt2 && cnt1>0 && f ) return true;
  return false;
}

bool checkB(){
  if( buf[0] != '>' ) return false;
  if( buf[1] != '^' ) return false;
  int i;
  bool f = false;
  for(i=2;i<M-2;i+=2){
    if( buf[i] != 'Q' ) return false;
    if( buf[i+1] != '=' ) return false;
    f = true;
  }
  if( f && buf[i] == '~' && buf[i+1] == '~' && i + 2 == M ) return true;
  return false;
}

int main(){
  int n; cin >> n;
  for(int i=0;i<n;i++){
    cin >> buf; M = strlen(buf);
    if( checkA() ) cout << "A" << endl;
    else if( checkB() ) cout << "B" << endl;
    else cout << "NA" << endl;
  }
}

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

int main()
{
  int n,f,i,m;
  string s;
  cin>>n;
  while(n-->0){
    cin>>s;
    f=0;
    if(s[0]=='>' && s[1]=='\''){
      m=s.find('#');
      if(m!=string::npos){
	for(i=1;s[m-i]==s[m+i];)i++;
	if(i>1 && m-i==1 && m+i==s.size()-1 && s[m+i]=='~')f=1;
      }
    }else if(s[0]=='>' && s[1]=='^'){
      for(i=2;s[i]=='Q'&&s[i+1]=='=';)i+=2;
      if(i==s.size()-2 && s[i]=='~' && s[i+1]=='~')f=2;
    }
    if(f)puts(f==1?"A":"B");
    else puts("NA");
  }
}

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+1]=='=')j+=2;
      if(s[j]!='~'||s[j+1]!='~')ans="NA";
	  j+=2;
	  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 <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>

using namespace std;

#define reep(i,f,t) for(int i=f ; i<int(t) ; ++i)
#define rep(i,n) reep(i, 0, n) 

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

bool isA(const char* str)
{
	int n = strlen(str);
	if(n<6) return false;
	if(str[0]!='>' || str[1]!='\'') return false;
	
	int cnt, i;
	for(cnt=0, i=2; i<n && str[i]=='='; ++cnt, ++i);
	if(cnt==0 || i>=n || str[i] != '#') return false;
	++i;
	
	int s;
	for(s=0; i<n && str[i]=='='; ++s, ++i);
	if(cnt!=s || i+1!=n) return false;
	
	return str[i] == '~';
}

bool isB(const char* str)
{
	int n = strlen(str);
	if(n<6) return false;
	if(str[0]!='>' || str[1]!='^') return false;
	
	int i;
	for(i=2; i+1<n && str[i]=='Q' && str[i+1]=='='; i+=2);
	if(i==2 || i+2!=n) return false;
	
	return str[i]=='~' && str[i+1] == '~';
}

int main()
{
	int n;
	scanf("%d", &n);
	rep(i, n){
		char str[256];
		scanf("%s", str);
		if(isA(str))
			puts("A");
		else if(isB(str))
			puts("B");
		else
			puts("NA");
	}
	
	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="  ";
	
if(str[0]=='>'){
	for(int j=0;j<2;j++)
		x[j]=str[j];
	
	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;
}