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 <algorithm>
using namespace std;
string judgeSnakeType(string s){
  if(s.find( ">'") == 0 && s.find("~") == s.size()-1)
    {
      s.erase(s.size()-1,1); s.erase(0,2);
      int cnt = 0;
      while( s.size() > 0 && s[0] == '=' ){
	if(s[0] == '='){
	  s.erase(0,1);
	  cnt++;
	}
      }
      if( s[0] != '#' )return "NA";
      s.erase(0,1);
      while( s.size() > 0 && s[0] == '=' ){
	if(s[0] == '='){
	  s.erase(0,1);
	  cnt--;
	}
      }
      if(s.size() == 0 && cnt == 0) return "A";
      else return "NA";
    }
  else if( s.find( ">^") == 0 && s.find("~~") == s.size()-2)
    {
      s.erase(s.size()-2,2); s.erase(0,2);
      while( s.size() > 0 && s.size() % 2 == 0){
	if(s[0] == 'Q' && s[1] == '='){
	  s.erase(0,2);
	}
	else {
	  return "NA";
	}
      }
      return s == "" ? "B" : "NA";
    }
  else
    {
      return "NA";
    }
  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 <string>
using namespace std;

int main(){
	int n;
	cin >> n;
	for( ; n-- ; ){
		string s;
		cin >> s;
		
		bool flag = false;
		if( s[0] != '>' ){
			flag = true;
		}
		bool a_flag = true;
		bool b_flag = true;
		bool equal_flag = false;
		int cnt = 0;
		for(int i=1 ; i < s.size() ; i++ ){
			int j;
			if( equal_flag == false ){
				if( s[i] == '=' ){
					equal_flag = true;
					for(j=i ; j < s.size() && s[j] == '=' ; j++ ){
						cnt++;
					}
					if( j >= s.size() ){
						a_flag = false;
						break;
					}else if( s[j] != '#' ){
						a_flag = false;
						break;
					}else{
						j++;
					}
				}
			}else{
				for( ; j < s.size() && s[j] == '=' ; j++ )
					cnt--;
			}
		}
		if( s.size() < 6 ){
			b_flag = false;
		}else if( s[1] != '^' ){
			b_flag = false;
		}
		bool q_equal_flag = false;
		for(int i=3 ; i < s.size() ; i++ ){
			if( s[i-1] == 'Q' && s[i] == '=' ){
				q_equal_flag = true;
			}
		}
		if( s[s.size()-2] != '~' || s[s.size()-1] != '~' ){
			b_flag = false;
		}
		
		if( flag ){
			cout << "NA" << endl;
		}else if( a_flag && equal_flag && cnt == 0 && s[s.size()-1] == '~' ){
			cout << "A" << endl;
		}else if( b_flag && q_equal_flag ){
			cout << "B" << endl;
		}else{
			cout << "NA" << endl;
		}
	}
}

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

bool isA(const string &s) {
  if(s.size() < 6) return false;
  if(s.substr(0,2) != ">'") return false;
  if(*(s.rbegin()) != '~') return false;
  string t = s.substr(2, (int)s.size()-3);
  int m = t.find("#");
  if(m == string::npos) return false;
  if(t.substr(0,m) != string(m, '=')) return false;
  if(t.substr(m+1) != string(m, '=')) return false;
  return true;
}

bool isB(const string &s) {
  if(s.size() < 6) return false;
  if(s.substr(0,2) != ">^") return false;
  if(s.substr((int)s.size()-2) != "~~") return false;
  string t = s.substr(2, (int)s.size()-4);
  if(t.size()&1) return false;
  for(int i = 0; i < t.size(); i += 2) {
    if(t[i] != 'Q' || t[i+1] != '=') return false;
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  while(n--) {
    string s;
    cin >> s;
    if(isA(s)) cout << "A" << endl;
    else if(isB(s)) cout << "B" << endl;
    else cout << "NA" << endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)
//#define INF (1LL<<59)

typedef string::const_iterator State;

int main(){
    int n;
    cin>>n;
    while(n--){
        string s;
        cin>>s;
    
        State beg = s.begin();
        try{
            if(*beg!='>')throw -1;
            beg++;
            if(*beg=='\''){
                beg++;
                int eq_count=0;
                while(*beg=='='){
                    if(beg==s.end())throw -1;
                    eq_count++;
                    beg++;
                }
                if(*beg!='#')throw -1;
                beg++;
                int c=0;
                while(*beg=='='){
                    if(beg==s.end())throw -1;
                    c++;
                    beg++;
                }
                if(eq_count!=c)throw -1;
                if(*beg!='~')throw -1;
                beg++;
                if(beg!=s.end())throw -1;
                
                cout<<"A"<<endl;
            }
            else if(*beg=='^'){
                beg++;
                bool f=false;
                while(1){
                    if(f&&*beg!='Q')break;
                    f=true;
                    if(*beg!='Q')throw -1;
                    beg++;
                    if(*beg!='=')throw -1;
                    beg++;
                }
                if(*beg!='~')throw -1;
                beg++;
                if(*beg!='~')throw -1;
                beg++;
                if(beg!=s.end())throw -1;
                
                cout<<"B"<<endl;
            }else throw -1;
            
            
            
        }catch(int e){
            cout<<"NA"<<endl;
            continue;
        }
    }
}