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 <cstdio>
#include <string.h>
using namespace std;
int checksnake(string, int);

int main(void) {
  int n, len, flag=0, flag2=0, flag3=0, cnt1=0, cnt2=0;
  char snake[200];
  int type;
  cin >> n;
  for(int i = 0 ; i < n ; i++) {
    scanf("%s", snake);
    len = strlen(snake);
    type = checksnake(snake, len);
    if(type == 1) cout << 'A' << endl;
    else if(type == 2) cout << 'B' << endl;
    else if(type == 3) cout << "NA" << endl;
  }
  return 0;
}

int checksnake(string snake, int len) {
  int flag=0, flag2=0, flag3=0;
  int true_flag=0;
  int cnt1=0, cnt2=0;
  
  for(int i = 0 ; i < len - 1 ; i++) {
    if(snake[i] == '>' && snake[i+1] == '\'') flag2 = 1;
    if(snake[i] == '=' && flag == 0 && flag2 == 1) {cnt1++; flag3 = 1; }
    if(snake[i] == '=' && flag == 1 && flag2 == 1) {cnt2++; flag3 = 1; }
    if(snake[i] == '#' && flag2 == 1 && flag3 == 1) flag = 1;
    //cout << cnt1 << ' ' << cnt2 << endl;
  }
  if(snake[len-1] == '~' && cnt1 == cnt2 && flag2 == 1 && flag3 == 1) {
    return 1;
    true_flag = 1;
  }
  
  if(true_flag == 0) {
    for(int i = 0 ; i < len - 2 ; i++) {
      if(snake[i] == '>' && snake[i+1] == '^') flag3 = 1;
      if(snake[i] == 'Q' && snake[i+1] == '=' && flag3 == 1) flag = 1;
    }
    if(snake[len-2] == '~' && snake[len-1] == '~'  && flag == 1 && flag3 == 1) {
      return 2;
      true_flag = 1;
    }
  }

  if(true_flag == 0) {
    return 3;
  }
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
int main()
{
	int n;
	cin >> n;
	for(int i=0;i<n;i++)
	{
		string s;
		cin >> s;
		int sz=s.size();
		if(s[0]!='>'||sz<6)
		{
			cout << "NA" << endl;
			continue;
		}
		if(s[1]=='\'')
		{
			int t=sz-3;
			if(t%2==0)
			{
				cout << "NA" << endl;
				continue;
			}
			for(int j=2;j<2+t;j++)
			{
				if(j==2+t/2&&s[j]!='#')
				{
					cout << "NA" << endl;
					goto end;
				}
				else if(j!=2+t/2&&s[j]!='=')
				{
					cout << "NA" << endl;
					goto end;
				}
			}
			if(s[sz-1]!='~')
			{
				cout << "NA" << endl;
				continue;
			}
			cout << "A" << endl;
			end:;
		}
		else if(s[1]=='^')
		{
			int t=sz-4;
			if(t%2==1)
			{
				cout << "NA" << endl;
				continue;
			}
			for(int j=0;j<t;j++)
			{
				if(j%2==0&&s[j+2]!='Q')
				{
					cout << "NA" << endl;
					goto end2;
				}
				else if(j%2==1&&s[j+2]!='=')
				{
					cout << "NA" << endl;
					goto end2;
				}
			}
			if(s[sz-1]!='~'||s[sz-2]!='~')
			{
 
				cout << "NA" << endl;
				continue;
			}
			cout << "B" << endl;
			end2:;
		}
		else
		{
			cout << "NA" << endl;
			continue;
		}
	}
	return 0;
}

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

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

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

using namespace std;


int main() {

  int n;
  string snake;

  int idx;

  int count;


  cin >> n;

  getline(cin,snake);

  for(int i=0;i<n;i++) {

    getline(cin,snake);

    //明らかにどちらのヘビでもない
    if(snake[0]!='>') {

      cout << "NA" << endl;

      break;

    }

    else {

      //A種であるか判定
      if(snake[1]=='\'') {

	if(snake[2]!='=') {

	  cout << "NA" << endl;

	  goto NA;

	}

	idx=3;

	count=1;

	while(1) {

	  if(snake[idx]!='=' && snake[idx]!='#') {
	    
	    cout << "NA" << endl;

	    goto NA;

	  }

	  else if(snake[idx]=='=') {

	    idx++;
	    
	    count++;

	  }

	  else if(snake[idx]=='#') {

	    idx++;

	    break;

	  }

	}

	while(1) {

	  if(snake[idx]!='=' && snake[idx]!='~') {
	  
	    cout << "NA" << endl;

	    goto NA;

	  }

	  else if(snake[idx]=='=') {

	    idx++;

	    count--;

	  }

	  else if(snake[idx]=='~' && idx==snake.length()-1) {

	    if(count==0) break;

	    else {
	      
	      cout << "NA" << endl;

	      goto NA;

	    }
	      
	  }

	  else {

	    cout << "NA" << endl;

	    goto NA;

	  }

	}

	cout << "A" << endl;

      }

      //B種であるか判定
      else if(snake[1]=='^') {

	if(snake[2]!='Q') {

	  cout << "NA" << endl;

	  goto NA;p

	}

	if(snake[3]!='=') {

	  cout << "NA" << endl;

	  goto NA;

	}

	idx=4;

	while(1) {

	  if(snake[idx]!='Q') break;

	  idx++;

	  if(snake[idx]!='=') {

	    cout << "NA" << endl;

	    goto NA;

	  }

	  idx++;

	}

	if((snake[idx]!='~' || snake[idx+1]!='~') || snake.length()>idx+2) {

	  cout << "NA" << endl;

	  goto NA;

	}

	cout << "B" << endl;

      }

      else {

	cout << "NA" << endl;

	goto NA;

      }

    }

  NA:
    ;

  }

  return 0;

}