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 )
    {
        if( S.find_first_not_of( "#=", 2 ) != sz - 1 )
        {
            return 0;
        }
        S = S.substr( 2, sz - 3 );
        sz = S.size();
        auto pos = S.find( "#" );
        if( S.find( "#", pos + 1 ) != string::npos )
        {
            return 0;
        }
        return sz - 1 - pos == pos  ? 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;
    
    vector<string> res{ "NA", "A", "B" };
    REP( i, N )
    {
        string S;
        cin >> S;
        cout << res[ check_A( S ) ? 1 : check_B( S ) ? 2 : 0 ] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main(void) {
  int n, len, flag=0, cnt1=0, cnt2=0;
  char snake[10000][200];
  cin >> n;
  for(int i = 0 ; i < n ; i++) {
    scanf("%s", snake[i]);
    len = strlen(snake[i]);
    if(snake[i][0] == '>' && snake[i][1] == '\'' && snake[i][2] == '=') {
      for(int j = 3 ; j < len - 2 ; j++) {
	if(snake[i][j] == '=' && flag == 0) cnt1++;
	if(snake[i][j] == '=' && flag == 1) cnt2++;
	if(snake[i][j] == '#') flag = 1;
	//cout << cnt1 << ' ' << cnt2 << endl;
      }
      if(snake[i][len-1] == '~' && cnt1 == cnt2) cout << 'A' << endl;
      else cout << "NA" << endl;
      flag = 0;
      cnt1 = 0; cnt2 = 0;
    }
    
    else if(snake[i][0] == '>' && snake[i][1] == '^' /*&& snake[i][2] == 'Q' && snake[i][3] == '='*/) {
      for(int j = 2 ; j < len - 3 ; j++) {
	if(snake[i][j] == 'Q' && snake[i][j+1] == '=') flag = 1;
      }
      if(snake[i][len-2] == '~' && snake[i][len-1] == '~'  && flag == 1) cout << 'B' << endl;
      else cout << "NA" << endl;
    }
    else cout << "NA" << endl;
  }
  return 0;
}

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

typedef string::iterator S;
string ss;

string snakeA(S it){
    int count = 0;
    for(;it!=ss.end()&&*it=='=';it++,count++);
    if(!count) return "NA";
    if(it==ss.end()||*it!='#') return "NA";
    it++;
    for(;it!=ss.end()&&*it=='=';it++,count--);
    if(it==ss.end()||*it!='~') return "NA";
    it++;
    if(*it||count) return "NA";
    return "A";
}

string snakeB(S it){
    while(true){
        if(it==ss.end()||*it++!='Q') return "NA";
        if(it==ss.end()||*it++!='=') return "NA";
        if(it==ss.end()) return "NA";
        if(*it=='~') break;
    }
    if(*++it=='~'&&!*++it) return "B";
    return "NA";
}

string snake(string ss){
    S it = ss.begin();
    if( *it++ != '>' ) return "NA";
    if( *it == '\'' ) return snakeA(++it);
    else if( *it == '^' ) return snakeB(++it);
    else return "NA";
}

int main(){
    int n;
    for(cin>>n,cin.ignore();n--;){
        getline(cin, ss);
        cout<<snake(ss)<<endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
bool check_A( string &S )
{
    auto sz = S.size();
    //size ??? 6 ??\?????????????????? false
    if( sz < 6 )
    {
        return 0;
    }
    //?????????[>'=], ????°????[~]?????????????????´?????? false
    if( S[ 0 ] != '>' || S[ 1 ] != '\'' || S[ 2 ] != '=' || S[ sz - 1 ] != '~' )
    {
        return 0;
    }
    //??????[>']??¨????°?[~]???????????????
    string R = S.substr( 2, sz - 3 );
    sz = R.size();
    //R ???[#,=]??\???????????????????????????????????? false
    if( R.find_first_not_of( "#=", 0 ) != string::npos )
    {
        return 0;
    }
    if( sz % 2 == 0 )
    {
        return 0;
    }
    //R ?????????????????????????????£???[#]???????????\??????[#]????????£?????? false
    auto pos = R.find( "#" );
    if( R.find( "#", pos + 1 ) != string::npos )
    {
        return 0;
    }
    // S ???????????????[#]???????????£?????? false
    if( R[ sz / 2 ] != '#' )
    {
        return 0;
    }
    return 1;
}
bool check_B( string &S )
{
    auto sz = S.size();
    //size ??? 6 ??\?????????????????? false
    if( sz < 6 )
    {
        return 0;
    }
    //?????????[>^], ????°????[~~]?????????????????´?????? false
    if( S[ 0 ] != '>' || S[ 1 ] != '^' || S[ sz - 1 ] != '~' || S[ sz - 2 ] != '~' )
    {
        return 0;
    }
    string R = S.substr( 2, sz - 4 );
    sz = R.size();
    // S ???size????\???°?????£?????? false
    if( sz % 2 != 0 )
    {
        return 0;
    }
    for( int i = 0; i < sz; i += 2 )
    {
        //[Q=]?????£?¶???§????????£?????? false
        if( R[ i ] != 'Q' || R[ i + 1 ] != '=' )
        {
            return 0;
        }
    }
    return 1;
}
int main()
{
    int N;
    cin >> N;
    
    REP( i, N )
    {
        string S;
        cin >> S;
        
        string res{ "NA" };
        if( check_A( S ) )
        {
            res = "A";
        }
        else if( check_B( S ) )
        {
            res = "B";
        }
        cout << res << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

bool isa(string s){
    if(s.substr(0,2) != ">'") return false;
    int x = 2;
    int c = 0;
    while(s[x+c] == '='){
	c++;
    }
    if(s[x+c] != '#') return false;
    x = x + c + 1;
    for(int i=0;i<c;i++){
	if(s[x+i] != '=') return false;
    }
    x += c;
    if(s[x] != '~') return false;
    return true;
}

bool isb(string s){
    if(s[0] != '>') return false;
    if(s[1] != '^') return false;
    int x = 2;
    int c = 0;
    while(s[x+c] == 'Q' && s[x+1+c] == '='){
	c += 2;
    }
    x += c;
    if(s[x] != '~') return false;
    if(s[x+1] != '~') return false;
    return true;
}

int main(){
    string s = "";
    int n;
    cin >> n;
    REP(i,n){
	cin >> s;
	if(s.size() < 6){
	    cout << "NA" << endl;
	    continue;
	}
	if(isa(s)) cout << "A"<<endl;
	else if(isb(s)) cout << "B" << endl;
	else cout << "NA" << endl;
    }
    return 0;
}