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 <bits/stdc++.h>

using namespace std;

#define int long long
#define FR first
#define SC second
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for(int i = (int)(f); i < (int)(n); i++)
#define each(a, b) for(auto& a : b)

typedef pair<int, int> P;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int n; cin >> n;
  while(n--) {
    string s; cin >> s;
    int kind = 0;
    int sz = s.size();
    if(s[0] == '>' && s[1] == '\'' && s[sz-1] == '~' && s.size() > 5) kind = 1;
    else if(s[0] == '>' && s[1] == '^' && s[sz-1] == '~' && s[sz-2] == '~' && sz%2 == 0 && s.size() > 4) kind = 2;
    else kind = 3;
    if(kind == 1) {
      int cnt = 0, cmp = 0;
      for(int i = 2; i < sz-1 && kind != 3; i++) {
	if(s[i] != '=' && s[i] != '#') kind = 3;
	else {
	  if(s[i] == '=') cnt++;
	  else if(s[i] == '#' && !cmp) cmp = cnt, cnt = 0;
	  else kind = 3;
	}
      }
      if(cnt != cmp) kind = 3;
    } else if(kind == 2) {
      for(int i = 2; i < sz-2 && kind != 3; i+=2) {
	if(s.substr(i, 2) != "Q=") kind = 3;
      }
    }
    if(kind == 1) puts("A");
    else if(kind == 2) puts("B");
    else if(kind == 3) puts("NA");
  }
  
  return 0;
}

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

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)

int main(void){
  int n;
  cin>>n;
  while(n--){
    string snake;
    cin>>snake;
    if(snake[0]=='>'&&snake[1]=='\''&&snake[snake.size()-1]=='~'){
      int count=0;
      int end;
      for(int i=2;i<snake.size()&&snake[i]=='='; i++){
        count++;
        end = i;
      }
      end++;
      if(count&&snake[end]=='#'&&end+count+2==snake.size()){
        bool yes = true;
        for(int i=end+1;i<snake.size()-1;i++){
          if(snake[i]!='=')yes = false;
        }
        if(yes){
          cout<<"A"<<endl;
        }else{
          cout<<"NA"<<endl;
        }
      }else{
        cout<<"NA"<<endl;
      }
    }else if(snake[0]=='>'&&snake[1]=='^'&&snake[snake.size()-1]=='~'&&snake[snake.size()-2]=='~'
        &&snake[snake.size()-3]=='='){
      bool yes = true;
      for(int i=2;i<snake.size()-2;i++){
        if(i%2==0&&snake[i]!='Q') yes = false;
        if(i%2==1&&snake[i]!='=') yes = false;
      }
      if(yes){
        cout<<"B"<<endl;
      }else{
        cout<<"NA"<<endl;
      }
    }else{
      cout<<"NA"<<endl;
    }
  }
  return 0;
}

C++: #include <stdio.h>
#include <string.h>

int main(void) {
	int max,data;
	int num;
	int cut_i;
	
	char hebi[201];
	scanf("%d",&max);
	
	for(data=0;data<max;data++) {
		scanf("%s",hebi);
	
		if(strlen(hebi)<2 || hebi[0]!='>') {
			puts("NA");
			continue;
		}
		
		if(hebi[1]=='\'') {
			num=0;
			cut_i=2;
		
			while(hebi[cut_i]=='=') {
				cut_i++;
				num++;
			}
			
			if(hebi[cut_i]!='#' || num<=0){
				puts("NA");
			}
			else {
				cut_i++;
				
				while(hebi[cut_i]=='=') {
					cut_i++;
					num--;
				}

				if(num!=0){
					puts("NA");
				}
				else if(hebi[cut_i]!='~' || hebi[cut_i+1]!=0){
					puts("NA");
				}
				else {
					puts("A");
				}
			}
		} 
		
		else if(hebi[1]=='^') {
			cut_i=2;
			while(hebi[cut_i]=='Q' && hebi[cut_i+1]=='='){
				cut_i+=2;
			}

			if(cut_i==2){
				puts("NA");
			}

			else if(hebi[cut_i]!='~' || hebi[cut_i+1]!='~' || hebi[cut_i+2]!=0){
				puts("NA");
			}
			else {
				puts("B");
			}
		} else {
			puts("NA");
		}
	}
	return 0;
}

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

using namespace std;

#define fr first
#define sc second
#define mp make_pair
#define foreach(c, it) for(typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)

typedef pair< int, int > iP;
typedef pair< iP, int > iiP;

int main(){
  
  int N;
  cin >> N;
  while(N--) {
    string s; cin >> s;
    if(s[0] != '>') {
      cout << "NA" << endl;
      continue;
    }
    int select = 0;
    if(s[1] == '\'') {
      int cnt = 0;
      if(!(s.size() % 2)) {
        for(int i = 2; i < s.size() / 2; i++) {
          if(s[i] != '=') {
            select = -1;
            break;
          }
          cnt++;
        }
        if(~select && !cnt) {
          cout << "NA" << endl;
          continue;
        }
        for(int i = s.size() / 2 + 1; i < s.size() - 1; i++) {
          if(s[i] != '=') {
            select = -1;
            break;
          }
          cnt--;
        }
      } else select = -1;
      if(!cnt && ~select && s[s.size() / 2] == '#' && s[s.size() - 1] == '~')
        cout << "A" << endl;
      else cout << "NA" << endl; 
    } else if(s[1] == '^') {
      if(!(s.size() % 2)) {
        for(int i = 2; i < s.size() - 2; i++) {
          if((!(i % 2) && s[i] != 'Q') || (i % 2 && s[i] != '=')) {
            select = -1;
            break;
          }
        }
        if(s.size() >= 6 && ~select && s[s.size() - 1] == s[s.size() - 2] && s[s.size() - 1] == '~') cout << "B" << endl;
        else cout << "NA" << endl;
      } else cout << "NA" << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}

C++: #include <iostream>

using namespace std;

bool chk_a(const string s)
{
    bool c = true;
    int f, b;

    f = b = 0;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] != '=' && s[i] != '#') 
            return false;
        if (c && s[i] == '#') 
            c = false;
        else if (s[i] == '=' && c)
            f++;
        else
            b++;
    }

    return (f == b) ? true : false;
}

bool chk_b(const string s)
{
    for (int i = 0; i < s.size(); i += 2) 
        if (s[i] != 'Q' || s[i + 1] != '=')
            return false;

    return true;
}

int main()
{
    int n;

    cin >> n;
    while (n--) {
        string inp;
        bool flag = false;

        cin >> inp;
        if (inp.size() >= 5 && inp[0] == '>' && inp[inp.size() - 1] == '~') {
            if (inp[1] == '\'') {  // Aの判定
                if (chk_a(inp.substr(2, inp.size() - 3))) {
                    cout << "A" << endl;
                    flag = true;
                }
            }
            else if (inp.size() >= 6 && inp[1] == '^' 
                     && inp[inp.size() - 2] == '~') {  // Bの判定
                if (chk_b(inp.substr(2, inp.size() - 4))) {
                    cout << "B" << endl;
                    flag = true;
                }
            }
        }

        if (!flag)
            cout << "NA" << endl;
    }

    return 0;
}