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 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; flag2 = 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; flag = 0; }
      else cout << "NA" << endl;
    }
    else 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 <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#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>
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 EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#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)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);

int main() {
	int n;
	cin >> n;
	REP(i, n) {
		string s;
		cin >> s;
		string h = s.substr(0, 2);
		if(h == ">'") {
			s += "!";
			if(s.find('#') != string::npos) {
				int pos = s.find('#');
				int size = pos-2;
				REP(i, pos-2) {
					if(s[i+2] == s[i+pos+1] && s[i+2] == '=') {

					} else {
						cout << "NA" << endl;
						goto next;
					}
				}
				if(s[pos+size+1] == '~') {
					cout << "A" << endl;
					goto next;
				} else {
					cout << "NA" << endl;
					goto next;
				}
				 
			} else {
				cout << "NA" << endl;
				goto next;
			}			
		} else if(h == ">^") {
			s += "!!";
			int b = 2;
			while(s.substr(b, 2) == "Q=") {
				b += 2;
			}
			if(s.substr(b, 2) == "~~") {
				cout << "B" << endl;
				goto next;
			} else {
				cout << "NA" << endl;
				goto next;
			}
		} else {
			cout << "NA" << endl;
			goto next;
		}
		next:;
	}

	return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>

using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 10000000
#define rep(i,j) for(int i=0;i<(j);++i)
#define reps(i,j,k) for(int i=j;i<k;++i)
typedef long long ll;
typedef unsigned long long ull;
char snake[256]={0};
int check(int len){
	if(len % 2 == 1)return INF;
	if(snake[0] == '>'){
		if(snake[1] == 39){
			if(snake[len-1] != '~')return INF;
			int cnt = 0;
			int x = 0;
			rep(i,len){
				if(snake[i] == '#'){
					cnt++;
					x = i;
				}
			}
			if(cnt != 1)return INF;
			reps(i,2,x){
				if(snake[i] != '=')return INF;
			}
			reps(i,x+1,len-1){
				if(snake[i] != '=')return INF; 
			}
			return (x-2 == len-x-2) ? 1 : INF; 
		}
		else if(snake[1] == '^'){
			if(snake[len-1] != '~' || snake[len-2] != '~')return INF;
			for(int i = 2; i < len-2; i += 2){
				if(snake[i] != 'Q' || snake[i+1] != '=')return INF;
			}
			return 0;
		}
	}
	return INF;
}
int main(){
	int n;
	scanf("%d",&n);
	rep(q,n){
		memset(snake,0,sizeof(snake));
		scanf("%s",snake);
		int len = strlen(snake);
		int res = check(len);
		(res == 1) ? puts("A") : (res == 0) ? puts("B") : puts("NA");
	}
	return 0;
}

C++: #include<iostream>
#include<string>
main(){int f,i,m;std::string s;for(std::cin>>f;std::cin>>s;f?puts(f-2?"A":"B"):puts("NA")){f=0;for(i=1;s[0]+s[1]==101&(m=s.find(35))!=-1&s[m-i]==s[m+i];)m-++i-1|m+i-s.size()+1|s[m+i]-'~'||(f=1);for(i=2;s[0]+s[1]==156&s[i]==81&s[i+1]==61;)++++i-s.size()+2|s[i]+s[i+1]-252||(f=2);}}

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