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<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;
	  if(s[j]!='Q'||s[j+1]!='=')ans="NA";
	  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||!h1)ans="NA";
	}else ans="NA";
	cout<<ans<<endl;
  }
}

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

using namespace std;

int count( string str, string t, int &curr ){
    int cnt = 0;
    for( ; curr + t.size() - 1 < str.size(); cnt++, curr += t.size() ){
        if( str.substr(curr, t.size()) != t ) break;
    }
    return cnt;
}

bool isA( string str, int curr ){
    if ( str.substr(0, curr) != ">'" ) return false;
    int cnt = count(str, "=", curr);
    if ( cnt < 1 || str[curr++] != '#' ) return false;
    if ( cnt != count(str, "=", curr) ) return false;
    return ( curr == str.size()-1 && str[curr] == '~');
}

bool isB( string str, int curr ){
    if ( str.substr(0, curr) != ">^" ) return false;
    int cnt = count(str, "Q=", curr);
    return ( cnt && curr == str.size()-2 && str.substr(str.size()-2, 2) == "~~");
}

int main(){
    int tcase; cin >> tcase;
    for ( int i = 0; i < tcase; i++ ) {
        string snake; cin >> snake;
        if ( isA(snake, 2) ) cout << "A" << endl;
        else if ( isB(snake, 2) ) cout << "B" << endl;
        else cout << "NA" << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int main(){
    int n;
    string s;
    cin>>n;
    for(int u=0;u<n;u++){
        cin>>s;
        bool a=true,b=true;
        if(s[0]!='>'||s.size()%2!=0||s.size()<6)a=false,b=false;
        if(s[1]!='\''||s[s.size()-1]!='~')a=false;
        if(s[1]!='^'||s[s.size()-1]!='~'||s[s.size()-2]!='~')b=false;
        if(a){
            if(s[s.size()/2]!='#')a=false;
            int count=0;
            for(int i=2;i<s.size()-1;i++)if(s[i]!='=')count++;
            if(count!=1)a=false;
            if(a)cout<<"A"<<endl;
        }
        if(b){
            for(int i=2;i<s.size()-2;i++){
                if(i%2==0&&s[i]!='Q')b=false;
                if(i%2==1&&s[i]!='=')b=false;
            }
            if(b)cout<<"B"<<endl;
        }
        if(!a&&!b)cout<<"NA"<<endl;
    }
    return 0;
}

C++: #include <stdio.h>
#include <math.h>
int main(){
    int syucnt[1000]={0},n,i,t,l,gou=0;
	char input[10000];
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%s",input);
		l = strlen(input);
        if(input[l-1]=='~'&&input[l-2]=='~'){//B種の調べ
            for(t=2;t<=l-4;t=t+2){
                if(input[t]=='Q'&&input[t+1]=='='){
                    gou++;
                }
            }
            if(gou*2==l-4){
                syucnt[i]=2;
            }else syucnt[i]=3;
            gou=0;
        }else if(input[l/2]=='#'&&input[l-1]=='~'&&(l-4)%2==0){
            int j=2;
            for(t=0;t<(l-4)/2;t++){
                if(input[j]=='='&&input[l-j]=='='){
                    gou++;
                }
                j++;
            }
            if(gou==((l-4)/2)){
                syucnt[i]=1;
            }else syucnt[i]=3;
            gou=0;
        }else syucnt[i]=3;
    }
    for(i=0;i<n;i++){//出力
        if(syucnt[i]==1){
            puts("A");
        }else if(syucnt[i]==2){
            puts("B");
        }else if(syucnt[i]==3){
            puts("NA");
        }
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
string judgeSnakeType(string s){
  if(s.find( ">'") == 0 && s.find("~") == s.size()-1)
      {
	int mark = 0;
	int cnt = 0;
	for( int i = 2 ; i < s.size() ; i++){
	  if( mark == 0 && s[i] == '='){
	    mark = 1;
	    cnt++;
	  }
	  else if( mark == 1 && s[i] == '='){
	    cnt++;
	  }
	  else if( s[i] == '#' ){
	    if( mark == 0 ) return "NA";
	    if( mark == 1 ) mark = 2;
	  }
	  else if( mark == 2 && s[i] == '=' ){
	    mark = 3;
	    cnt--;
	  }
	  else if( mark == 3 && s[i] == '=' ){
	    cnt--;
	  }
	}
	if( mark != 3 || cnt != 0 ) return "NA";
	return "A";
      }
  else if( s.find( ">^") == 0 && s.find("~~") == s.size()-2)
      {
	bool isMark = false;
	for( int i = 2 ; i < s.size()-2; i++){
	  if( !((i % 2 == 0 && s[i] == 'Q')
	      ||
		( i % 2 == 1 && s[i] == '=')))
	    {
	      return "NA";
	    }
	  else{
	    isMark = true;
	  }
	}
	if( !isMark ) return "NA";
	return "B";
      }
    else
      {
	return "NA";
      }
}
void solve(string snake){
  cout << judgeSnakeType( snake ) << endl;
}
int main(){
  int d;
  cin >> d;
  string snake;
  for(int i = 0 ; i < d; i++ )
    {
      cin >> snake;
      solve(snake);
    }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main(void) {
  int n, len, flag=0, flag2=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 = 2 ; j < len - 1 ; j++) {
	if(snake[i][j] == '=' && flag == 0) {cnt1++; flag2 = 1; }
	if(snake[i][j] == '=' && flag == 1) {cnt2++; flag2 = 1; }
	if(snake[i][j] == '#') flag = 1;
	//cout << cnt1 << ' ' << cnt2 << endl;
      }
      if(snake[i][len-1] == '~' && cnt1 == cnt2 && flag2 == 1) 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 - 2 ; 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;
}