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 <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++) 
#define ALL(n) begin(n),end(n)
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
bool check_A( string S )
{
    auto sz = S.size();
    if( S.find( ">'" ) == 0 && S.find( "~" ) == sz - 1 && sz >= 6 )
    {
        S = S.substr( 2, sz - 3 );
        sz = S.size();
        if( S.find_first_not_of( "#=", 0 ) != string::npos )
        {
            return 0;
        }
        auto pos = S.find( "#" );
        if( S.find( "#", pos + 1 ) != string::npos )
        {
            return 0;
        }
        return S[ sz / 2 ] == '#' ? 1 : 0;
    }
    return 0;
}
bool check_B( string S )
{
    auto sz = S.size();
    if( S.find( ">^" ) == 0 && S.find( "~~" ) == sz - 2 && sz >= 6 )
    {
        S = S.substr( 2, sz - 4 );
        sz = S.size();
        if( sz % 2 != 0 )
        {
            return 0;
        }
        for( int i = 0; i < sz; i += 2 )
        {
            if( S[ i ] != 'Q' && S[ i + 1 ] != '=' )
            {
                return 0;
            }
        }
        return 1;
    }
    return 0;
}
int main()
{
    int N;
    cin >> N;

    REP( i, N )
    {
        string S;
        cin >> S;
        if( check_A( S ) )
        {
            cout << "A" << endl;
        }
        else if( check_B( S ) )
        {
            cout << "B" << endl;
        }
        else
        {
            cout << "NA" << endl;
        }
    }
    return 0;
}

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

using namespace std;

void solve()
{
	int n;
	cin >> n;
	while(n--)
	{
		string str;
		cin >> str;
		if(str[0] == '>' && str[1] == '\'' && str[str.size() - 1] == '~')
		{
			int count[2] = {0, 0};
			int pos = 0;
			for(int i = 2; i < str.size(); ++i)
			{
				if(str[i] == '=')
				{
					count[pos]++;
				}
				else if(str[i] == '#')
				{
					pos++;
				}
			}
			if(count[0] == count[1])
			{
				cout << "A" << endl;
			}
			else 
			{
				cout << "NA" << endl;
			}
		}
		else if(str[0] == '>' && str[1] == '^' && str[str.size() - 2] == '~' && str[str.size() - 1] == '~')
		{
			for(int i = 2; i < str.size() - 1; ++i)
			{
				if(str[i] == 'Q' && str[i + 1] == '=')
				{
					cout << "B" << endl;
					goto NEXT;
				}
			}
			cout << "NA" << endl;
		}
		else
		{
			cout << "NA" << endl;
		}
NEXT:
		;
	}
}

int main()
{
	solve();
	return(0);
}

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

int N;
int main()
{
  cin >> N;
  for(int i = 0; i < N; i++)
  {
    string s; 
    cin >> s;
    int p = 0;
    try
    {
      if(s[p++] != '>')
        goto NA;
      int m = 0;
      switch(s[p])
      {
        case '\'':
          while(s[++p] == '=')
            m++;
          if(s[p++] != '#')
            goto NA;
          for(int j = 0; j < m; j++, p++)
            if(s[p] != '=')
              goto NA;
          if(s[++p] != '~')
            goto NA;
          if(p != s.length())
            goto NA;
          cout << "A" << endl;
          continue;
        case '^':
          p++;
          for(; p < s.length() - 2; p++)
          {
            if(p % 2 == 0 && s[p] != 'Q' || p % 2 == 1 && s[p] != '=')
              goto NA;
          }
          if(s[p++] != '~' && s[p] != '~')
            goto NA;
          cout << "B" << endl;
          continue;
      }
    }
    catch(string str)
    {
      cout << "NA" << endl;
      continue;
    }
    continue;
NA:
    cout << "NA" << endl;
    continue;
  }
}

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

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

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

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

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
int main(){
    int n; cin>>n;
    rep(hoge,n){
        string s;cin>>s;
        int state=0;
        if(s[0]=='>'&&s[1]=='\'')state=1;
        else if(s[0]=='>'&&s[1]=='^')state=2;
        int cal=0,cnt=0; bool mybool=true,flag=true,key;
        if(state){
            rep(i,s.size()){
                if(i==0||i==1)continue;
                if(state==1){
                    if(s[i]=='#')mybool=false;
                    else if(mybool&&s[i]=='=')cnt++;
                    else if(mybool==false&&s[i]=='=')cnt--;
                    else if(s[i]=='~'&&i==s.size()-1)continue;
                    else flag=false;
                }else if(state==2){
                    if(!(s[s.size()-1]=='~'&&s[s.size()-2]=='~'))flag=false;
                    if(i>=s.size()-2)continue;
                    if(!(s[i]=='Q'&&s[i+1]=='='))flag=false;
                    else i++;
                }
            }
            if(state==1&&cnt==0&&flag)cout<<"A"<<endl;
            else if(state==2&&flag)cout<<"B"<<endl;
            else cout<<"NA"<<endl;
        }
    }
}