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 <string>
using namespace std;

void printNA() {
    cout << "NA" << endl;
}

void judgeB(string s) {
    if (s.substr(s.size() - 2, 2) != "~~") {
        printNA();
        return;
    }
    for (int i = 2; i < s.size() - 3; i += 2) {
        if (s.substr(i, 2) != "Q=") {
            printNA();
            return;
        }
    }
    cout << "B" << endl;
}

void judgeA(string s) {
    int len = 0;
    int i;
    for (i = 2; s[i] == '='; i++) {
        len++;
    }
    if (len == 0 || s.size() < i || s[i] != '#') {
        printNA();
        return;
    }
    if (s.size() < 2 + 2 * len) {
        printNA();
        return;
    }
    for (i++ ; i <= 2 + 2 * len; i++) {
        if (s[i] != '=') {
            printNA();
            return;
        }
    }
    if (s.size() == i || s[i] != '~') {
        printNA();
    } else {
        cout << "A" << endl;
    }
}

int main() {
    int n;
    cin >> n;
    for (int c = 0; c < n; c++) {
        string s;
        cin >> s;
        if (s.substr(0, 2) == ">'") {
            judgeA(s);
        } else if (s.substr(0, 2) == ">^") {
            judgeB(s);
        } else {
            cout << "NA" << endl;
        }
    }
    return 0;
}

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

bool isSnakeA(string snake) {
    if (snake[0] != '>' || snake[1] != '\'' || snake[snake.length() - 1] != '~') {
        return false;
    }
    int pre = 0, post = 0;
    bool sharpFlag = false;
    for (int i = 2; i < (int) snake.length() - 1; i++) {
        if (!sharpFlag) {
            switch (snake[i]) {
            case '=':
                pre++;
                break;
            case '#':
                sharpFlag = true;
                break;
            default:
                return false;
                break;
            }
        } else {
            if (snake[i] == '=') {
                post++;
            } else {
                return false;
            }
        }
    }
    return pre > 0 && pre == post;
}

bool isSnakeB(string snake) {
    if (snake[0] != '>' || snake[1] != '^' || snake[snake.length() - 2] != '~' || snake[snake.length() - 2] != '~') {
        return false;
    }
    int count = 0;
    bool QFlag = false;
    for (int i = 2; i < (int) snake.length() - 2; i++) {
        switch (snake[i]) {
        case 'Q':
            if (QFlag) {
                return false;
            } else {
                QFlag = true;
            }
            break;
        case '=':
            if (QFlag) {
                count++;
                QFlag = false;
            } else {
                return false;
            }
            break;
        default:
            return false;
            break;
        }
    }
    return count > 0;
}

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        string snake;
        cin >> snake;
        if (isSnakeA(snake)) {
            cout << "A" << endl;
        } else if (isSnakeB(snake)) {
            cout << "B" << endl;
        } else {
            cout << "NA" << endl;
        }
    }
}

C++: #include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main(void) {
  int n, len, flag=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 = 3 ; j < len - 2 ; j++) {
	if(snake[i][j] == '=' && flag == 0) cnt1++;
	if(snake[i][j] == '=' && flag == 1) cnt2++;
	if(snake[i][j] == '#') flag = 1;
	//cout << cnt1 << ' ' << cnt2 << endl;
      }
      if(snake[i][len-1] == '~' && cnt1 == cnt2) cout << 'A' << endl;
      else cout << "NA" << endl;
      flag = 0;
      cnt1 = 0; cnt2 = 0;
    }
    
    else if(snake[i][0] == '>' && snake[i][1] == '^' && snake[i][2] == 'Q' && snake[i][3] == '=') {
      if(snake[i][len-2] == '~' && snake[i][len-1] == '~') cout << 'B' << endl;
      else cout << "NA" << endl;
    }
    else cout << "NA" << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define f first
#define s second

main(){
  string hebi;
  int n;
  cin>>n;
  while(n--){
    cin>>hebi;
    bool a=true,b=true;
    if(hebi.size()<6 || hebi.size()%2){
      a=false;
      b=false;
    }
    rep(i,hebi.size()){
      if(i==0 && hebi[i]=='>')continue;
      if(i==1 && hebi[i]=='\'')continue;
      if(i==hebi.size()-1 && hebi[i]=='~')continue;
      else if(i>=2 && i<hebi.size()-1 && i!=hebi.size()/2 && hebi[i]=='=')continue;
      else if(i==hebi.size()/2 && hebi[i]=='#')continue;
      a=false;
    }

    rep(i,hebi.size()){
      if(i==0 && hebi[i]=='>')continue;
      if(i==1 && hebi[i]=='^')continue;
      if(i==hebi.size()-1 && hebi[i]=='~')continue;
      if(i==hebi.size()-2 && hebi[i]=='~')continue;
      else if(i%2==0 && hebi[i]=='Q')continue;
      else if(i%2==1 && hebi[i]=='=')continue;
      b=false;
    }

    if(a)puts("A");
    else if(b)puts("B");
    else puts("NA");
  }
}

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

int isfa(string);
int isfb(string);

int main(){
  int n,fa,fb;
  string str;
  cin >> n;

  for(int i = 0;i < n;i++){
    cin >> str;
    if(str.size()>5){
      fa = isfa(str);
      fb = isfb(str);
    }else fa=fb=0;

    if(fa == 1)
       cout << "A" << endl;
    else if(fb == 1)
      cout << "B" << endl;
    else 
      cout << "NA" << endl;
  }
  return 0;
}

int isfa(string n){
  int pos,c1,c2;
  c1=c2=0;
  pos=n.size()-1;
  if(n[0]!='>')return 0;
  if(n[1]!='\'')return 0;
  if(n[pos]!='~')return 0;
  pos--;
  while(n[pos]=='='){
    c1++;
    pos--;
  }
  if(n[pos]!='#')return 0;
  pos--;
  while(n[pos]=='='){
    c2++;
    pos--;
  }
  if(c1!=c2)return 0;
  if(pos!=1)return 0;

  return 1;
}

int isfb(string n){
  int pos;
  pos = n.size()-1;
  if(n[pos]!='~'||n[pos-1]!='~')
    return 0;
  if(n[0]!='>'||n[1]!='^')return 0;
  pos -= 2;
  while(n[pos]=='='&&n[pos-1]=='Q')pos-=2;
  if(pos!=1)return 0;
  return 1;
}