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>
using namespace std;
int main(){
  int n,size;
  string s,a,b;
  for(cin>>n;n>0;n--){
    cin>>s;
    size = s.size();
    a=">'";
    b=">^";
    for(int i=0;i<size/2-2;i++){a=a+"=";b=b+"Q=";}
    a+="#";
    for(int i=0;i<size/2-2;i++){a=a+"=";}
    a+="~";
    b+="~~";
    if (s==a)cout<<"A"<<endl;
    else if (s==b)cout<<"B"<<endl;
    else cout<<"NA"<<endl;
  }
  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();
        if (len <= 4) {
            cout << "NA" << endl; continue;
        }

        string body = repeat(len/2 - 2, "=");
        string A = ">'" + body + "#" + body + "~";
        if (snake == A) {
            cout << "A" << endl; continue;
        }

        body = repeat(len/2 - 2, "Q=");
        string B = ">^" + body + "~~";
        if (snake == B) {
            cout << "B" << endl; continue;
        }

        cout << "NA" << endl;
    }
    return 0;
}

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;
        cin >> n;
        while(n--){
                cin >> s;
                cout << checksnake(s) << endl;
         }
}

C++: #include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main() {
    int n; scanf("%d",&n);
    for (int i=0; i<n; i++) {
        string snake; cin >> snake;
        if (snake.length()<2) {
            puts("NA");
            continue;
        }
        if (snake[0]=='>'&&snake[1]=='\'') {
            int cnt=0;
            int j;
            for (j=2;j<snake.length();j++) {
                if (snake[j]=='#') break;
                cnt++;
            }
            int n=0;
            for (j+=1;j<snake.length();j++) {
                if (snake[j]=='~') break;
                n++;
            }
            // printf("%d %d\n",cnt,n);
            if(cnt!=n||j!=snake.length()-1) {
                puts("NA");
            } else {
                puts("A");
            }
        } else if (snake[0]=='>'&&snake[1]=='^') {
            bool flag=true;
            string s="";
            for (int i=2; i<snake.length(); i++) {
                if (snake[i]=='~') break;
                s+=snake[i];
                if (s.length()==2) {
                    if (s!="Q="||s!="~~") flag=false;
                    s="";
                }
            }
            if (flag) puts("B");
            else puts("NA");
        } else {
            puts("NA");
        }
    }
}

C++: #include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main() {
    int n; scanf("%d",&n);
    for (int i=0; i<n; i++) {
        string snake; cin >> snake;
        if (snake.length()<2) {
            puts("NA");
            continue;
        }
        if (snake[0]=='>'&&snake[1]=='\'') {
            int cnt=0;
            int j;
            for (j=2;j<snake.length();j++) {
                if (snake[j]=='#') break;
                cnt++;
            }
            int n=0;
            for (j+=1;j<snake.length();j++) {
                if (snake[j]=='~') break;
                n++;
            }
            // printf("%d %d\n",cnt,n);
            if(cnt!=n||snake[snake.length()-1]!='~') {
                puts("NA");
            } else {
                puts("A");
            }
        } else if (snake[0]=='>'&&snake[1]=='^') {
            bool flag=true;
            string s="";
            for (int i=2; i<snake.length(); i++) {
                if (snake[i]=='~') break;
                s+=snake[i];
                if (s.length()==2) {
                    if (s!="Q=") flag=false;
                    s="";
                }
            }
            if (flag&&snake[snake.length()-1]=='~') puts("B");
            else puts("NA");
        } else {
            puts("NA");
        }
    }
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;

int main()
{
//	cut here before submit 
//	freopen ("testcase.snack", "r", stdin );
	const string species[] = { "A", "B", "NA" };

	string str = "";
	int n;

	while (getline (cin, str ) ){
		stringstream ssn(str);
		ssn >> n;
		if (n == 0 ){
			break;
		} // end if

		int i,j;


		for (i = 0; i < n; ++i){
			string res = species[2];
			getline (cin, str );
			int len = str.length();

			if (str[0] == '>'){
				if (str[1] == '\''){	// A species? 
					res = species[0];
				}else if (str[1] == '^'){ // B species?
					res = species[1];
				}else{
					res = species[2];
				} // end if
			} // end if
			
			if (res == species[0] || res == species[1]){
				if (res == species[0]){
					int prev = 0;
					int after = 0;
					bool flag = false;
					for (j = 2; j < len - 1; ++j){
						if (str[j] == '='){
							if (!flag){
								++prev;
							}else{
								++after;
							} // end if
							continue;
						}else if (str[j] == '#'){
							flag = true;
							continue;
						}else{
							break;
						} // end if
					} // end for

					if (j == len - 1){
						if (str[j] == '~' && flag 
						&& prev != 0 && after != 0 && prev == after ){
							res = species[0];
						}else{
							res = species[2];
						} // end if
					}else{
						res = species[2];
					} // end if
				}else{	// if (res == species[1])
					for (j = 2; j < len - 2; j += 2){
						if (str[j] == 'Q' && str[j+1] == '='){
							continue;
						}else{
							break;
						} // end if
					} // end for
					if (j == len - 2 && str[j] == '~' && str[j+1] == '~'){
						res = species[1];
					}else{
						res = species[2];
					} // end if
				} // end if
			} // end if
			cout << res << endl;
		} // end for
	} // end loop
		
	return 0;
}