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,j,fa,fb,acount,acount2,i;
  string data;
  
  cin >> n;
  
  getline(cin,data);
  for(j = 0; j < n; j++){
    fa = 0;
    fb = 0;
    acount = acount2 = 0;
    getline(cin,data);
    if(data[0] == '>' && data[1] == '\''){
      fa = 1;
      i = 2;
      while(data[i] == '='){
	acount++;
	i++;
      }
      if(data[i] != '#')
	fa = 0;
      i++;
      while(data[i] == '='){
	acount2++;
	i++;
      }
      if(i != data.length()-1)
	fa = 0; 
      if(acount != acount2)
	fa = 0;
      if(data[data.length()-1] != '~'){
	fa = 0;
      }
    }
    if(data[0] == '>' && data[1] == '^'){
      fb = 1;
      for(i = 2; i < data.length()-3; i = i+2){
	if(data[i] != 'Q' || data[i+1] != '=')
	  fb = 0;
      }
      if(i != data.length()-2)
	fb = 0;
      if(data[data.length()-1] != '~' || data[data.length()-2] != '~')
	fb = 0;
    }
    if(fa == 1)
      cout << "A" << endl; 
    else if(fb == 1)
      cout << "B" << endl;
    else 
      cout << "NA" << endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#define INF 999999
using namespace std;
int main(){
    int N,j;
    cin>>N;
    while(N--){
        int runcnt=0,j;
        char S[150]={0};
        cin>>S;
            if(S[0]=='>'&&S[1]==39){
                int K=0;
                for(j=2;j<1000;j++){
                    if(S[j]=='='){
                        runcnt++;
                        K=1;
                    }
                    else if(S[j]=='#'){
                        break;
                    }
                    else {
                        cout<<"NA"<<endl;
                        break;
                    }
                }
                for(int i=j+1;i<1000;i++){
                    if(S[i]=='='){
                        runcnt--;
                    }
                    else if(runcnt==0&&S[i]=='~'&&K==1){
                        cout<<"A"<<endl;
                        break;
                    }
                    else{
                        cout<<"NA"<<endl;
                        break;
                    }
                }
            }
            else if(S[0]=='>'&&S[1]=='^'){
                int F=0;
                for(j=2;j<1000;){
                    if(S[j]=='Q'&&S[j+1]=='='){
                        j+=2;
                        F=1;
                    }
                    else if(S[j]=='~'&&S[j+1]=='~'&&F==1){
                        cout<<"B"<<endl;
                        break;
                    }
                    else{
                        cout<<"NA"<<endl;
                        break;
                    }
                }
            }
            else cout<<"NA"<<endl;
    }
    return 0;
}

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

using namespace std;

int checkA(string);
int checkB(string);

int main(){
  int i, n;
  string snake;

  cin >> n;

  for(i=0; i<n; ++i){
    cin >> snake;

    if(checkA(snake) == 1)
      cout << "A" << endl;
    else if(checkB(snake) == 1)
      cout << "B" << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}

int checkA(string snake){
  int i, j, count1=0, count2=0;

  if(snake[0] != '>' || snake[1] != '\'' || snake[2] != '=') return 0;

  for(i=2; snake[i] == '='; ++i) ++count1;
  if(snake[count1 + 2] != '#') return 0;

  for(j=count1+3; snake[j] == '='; ++j) ++count2;
  if(count1 != count2 || j != snake.size()-1 || snake[snake.size()-1] != '~') return 0;

  return 1;
}

int checkB(string snake){
  int i;

  if(snake[0] != '>' || snake[1] != '^' || snake[2] != 'Q' || snake[3] != '=') return 0;

  for(i=2; i < snake.size()-2; i+=2){
    if(snake[i] != 'Q' || snake[i+1] != '=') return 0;
  }

  if(snake[snake.size()-2] != '~' || snake[snake.size()-1] != '~') return 0;

  return 1;
}

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

using namespace std;

int main() {
  int Tc; string s;
  while(cin>>Tc) {
    while(Tc--) {
      cin >> s;
      int const N = s.size();
      bool NA = 0;
      bool isA;
      if(s[0] != '>') NA = 1;
      if(s[1] == '\'') {
	for(int i=2; i<(N-3)/2+2; i++) if(s[i]!='=') NA=1;
	if(s.substr(2, (N-3)/2) + "~" != s.substr(3+(N-3)/2)) NA = 1;
	if(s[(N-3)/2+2] != '#') NA = 1;
	isA = 1;
      }
      else if(s[1] == '^') {
	int cnt;
	for(cnt=2; cnt<N-2; cnt+=2) {
	  if(!(s[cnt] == 'Q' && s[cnt+1] == '=')) NA = 1;
	}
	if(!(s[cnt] == '~' && s[cnt+1] == '~')) NA = 1;
	if(cnt+2 != N) NA = 1
	isA = 0;
      }
      else {
	NA = 1;
      }
      
      if(NA) cout << "NA" << endl;
      else if(isA) cout << "A" << endl;
      else cout << "B" << endl;
    }
  }
  
  return 0;
}

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 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;
}