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 <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define EACH(t,i,c) for(t::iterator i=(c).begin(); i!=(c).end(); ++i)
const double EPS = 1e-10;
const double PI  = acos(-1.0);

int main(){
	int n;
	cin>>n;
	REP(t,n){
		string s;
		cin>>s;
		if(s[0]!='>')goto na;
		if(s[1]=='\''){
			int state=0;
			int cnt1=0;
			int cnt2=0;
			FOR(i,2,s.size()){
				if(s[i]=='='&&state==0){
					cnt1++;
				}else if(s[i]=='#'&&state==0){
					state=1;
				}else if(s[i]=='='&&state==1){
					state=2;
					cnt2++;
				}else if(s[i]=='='&&state==2){
					cnt2++;
				}else if(s[i]=='~'&&state==2){
					state=3;
				}else{
					state=-1;
					break;
				}
			}
			if(state==3&&cnt1==cnt2){
				goto a;
			}else{
				goto na;
			}
		}else if(s[1]=='^'){
			int state=0;
			int flag=0;
			FOR(i,2,s.size()){
				if(s[i]=='Q'&&state==0){
					state=1;
				}else if(s[i]=='='&&state==1){
					state=0;
					flag=1;
				}else if(s[i]=='~'&&state==0){
					state=2;
				}else if(s[i]=='~'&&state==2){
					state=3;
				}else{
					state=-1;
					break;
				}
			}
			if(state==3){
				goto b;
			}else{
				goto na;
			}
		}else{
			goto na;
		}
a:
		cout<<"A"<<endl;
		continue;
b:
		cout<<"B"<<endl;
		continue;
na:
		cout<<"NA"<<endl;
		continue;
	}

}

C++: #include <iostream>
#include <string>
#include <cstdio>
using namespace std;
 
string vbString(int Number,string Character){
        string res="";
        for(int i=0;i<Number;i++) res+=Character;
        return res;
}
string checksnake(string s){
        string t;
        for(int i=1;i<60;i++){
                t=">\'"+vbString(i,"=")+"#"+vbString(i,"=")+"~";
                if(t==s) return "A";
        }
 
        for(int i=1;i<60;i++){
                t=">^"+vbString(i,"Q=")+"~~";
                if(t==s) return "B";
        }
        return "NA";
 
}
int main(){
        string s;
        int n,i=0;
        scanf("%d\n",&n);
        for(i=0;i<n;i++){
                getline(cin, s);
                cout << checksnake(s) << endl;
        }
        return 0;
}

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

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

C++: #include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

const int INF = 10000000;
using namespace std;
typedef long long  ll;

/** Problem0139 : Snakes **/
int main()
{
	int n;
	string s;
	cin >> n;
	rep(k, n) {
		cin >> s;
		REP(i, 1, 100) {
			string a=">'", b=">^";

			rep(j, i) {
				a+='=';
				b+="Q=";
			}
			a+='#';
			rep(j, i)
				a+='=';
			a+='~';
			b+="~~";
			if (s == a) {
				cout << "A" << endl;
				goto end;
			} else if (s == b) {
				cout << "B" << endl;
				goto end;
			}
		}
		cout << "NA" << endl;
	end:;
	}
	return 0;
}

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

#define rep(i, n)for((i) = 0;(i) < (n);++(i))

string solve(string s){
	int i = 0, j;
	string tmp;
	rep(i, 100){
		tmp += "=";
		if(s == ">'" + tmp + "#" + tmp + "~")return "A";
	}
	tmp.clear();
	rep(i, 100){
		tmp += "Q=";
		if(s == ">^" + tmp + "~~")return "B";
	}
	return "NA";
}

int main(void){
	int i, j, n;
	string s;
	scanf("%d", &n);
	for(;n--;){
		cin >> s;
		cout << solve(s) << endl;
	}
	
	return 0;
}

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 == 0) cout << "NA" << endl;
  }
}

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] == '#') 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;
    }
  }

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

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

string repeat(int n, string s) {
    string res = "";
    for (int i=0; i<n; ++i)
        res += s;
    return res;
}

int main() {
    int N; cin >> N;
    for (int i=0; i<N; ++i) {
        string snake; cin >> snake;
        int len = snake.size();

        string A = ">'" + repeat((len - 4)/2, "=") 
                    + "#" + repeat((len - 4)/2, "=") + "~";
        
        string B = ">^" + repeat((len - 4)/2, "Q=") + "~~";

        if (snake == A) {
            cout << "A" << endl;
        } else if (snake == B) {
            cout << "B" << endl;
        } else {
            cout << "NA" << endl;
        }
    }
    return 0;
}