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 <stdio.h>

int main(void) {
    int quantity, i, j, idx, count = 0;
    char snake[200] = {0};
    
    scanf("%d", &quantity);
    
    for(i = 0; i < quantity; i++) {
        scanf("%s", snake);
        if(snake[0] != '>') {
            puts("NA");
            break;
        } else if(snake[1] == '\'') {
            
            for(j = 2; j < 199; j++) {
                if(snake[j] == '=') {
                    count++;
                } else if(snake[j] == '#') {
                    idx = j;
                    break;
                }
            }
            
            for(j = idx + 1; j < 199; j++) {
                if(snake[j] != '=') {
                    idx = j;
                    break;
                }
                count--;
            }
            
            if(count == 0) {
                if(snake[idx] == '~')
                    puts("A");
                else
                    puts("NA");
            } else {
                puts("NA");
            }
            
        } else if(snake[1] == '^') {
            
            for(j = 2; j < 198; j += 2) {
                if(snake[j] == 'Q')
                    count++;
                if(snake[j + 1] == '=')
                    count--;
                if(snake[j] == '~' && snake[j + 1] == '~')
                    break;
                printf("count = %d\n", count);
                
            }
            
            if(count == 0)
                puts("B");
            else
                puts("NA");
            
        }
    }
    return(0);
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<regex>
#include<vector>
using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

signed main() {
	int n; cin >> n;
	regex b(R"(>\^(Q=)+~~)");
	rep(i, 0, n) {
		string s; cin >> s;
		int m = 0;
		rep(j, 0, s.size())if (s[j] == '=')m++;
		regex a(">'={" + to_string(m / 2) + "}#={" + to_string(m / 2) + "}~");
		if (regex_match(s, a)) {
			cout << "A" << endl;
		}
		else if (regex_match(s, b)) {
			cout << "B" << endl;
		}
		else cout << "NA" << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
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;
																																						           cin >> n;
																																								           for(i=0;i<n;i++){
																																										                   cin >> s;
																																														                   cout << checksnake(s) << endl;
																																																		           }
																																																				           return 0;
																																																						   }

C++: #include<string>
#include<algorithm>
#include<iostream>
using namespace std;
int main(){
    int N;
    cin>>N;
    while(N--){
        string str;
        cin>>str;
        bool Aflag,Bflag;
        Aflag=Bflag=true;
        if(str.length()<2){
            cout<<"NA"<<endl;
            continue;
        }
        if(str.substr(0,2)==">\'")Bflag=false;
        else if(str.substr(0,2)==">^")Aflag=false;
        else Aflag=Bflag=false;

        if(Aflag){

            int p=str.find("#");
            if(p==str.npos){
                Aflag=false;
                break;
            }
            int cnt=0;
            for(int i=2;i<p;i++){
                if(str[i]=='='&&str[p+i-1]=='=')cnt++;
                else{
                    Aflag=false;
                    break;
                }
            }
            if(cnt==0)Aflag=false;
            
            if(str[cnt*2+3]=='~'&&str.length()==cnt*2+4);
            else Aflag=false;

        }

        if(Bflag){
            int p=2;
            while(str.substr(p,2)=="Q=")p+=2;
            if(p==2){
                Bflag=false;
            }
            if(str.substr(p,2)=="~~"&&str.length()==p+2);
            else Bflag=false;
        }
        if(Aflag)cout<<"A"<<endl;
        else if(Bflag)cout<<"B"<<endl;
        else cout<<"NA"<<endl;
    }
    return 0;
}

C++: //11
#include<string>
#include<iostream>

using namespace std;

int main(){
  int n;
  cin>>n;
  while(n--){
    string s;
    cin>>s;
    if(s==">'"+string((s.size()-4)/2,'=')+'#'+string((s.size()-4)/2,'=')+'~'){
      cout<<'A'<<endl;
    }else{
      int r=(s.size()-4)/2;
      string b;
      while(r--){
	b+="Q=";
      }
      if(s==">^"+b+"~~"){
	cout<<'B'<<endl;
      }else{
	cout<<"NA"<<endl;
      }
    }
  }
  return 0;
}

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

int main() {
  int Tc; string s;
  while(cin>>Tc) {
    while(Tc--) {
      cin >> s;
      int const N = s.size();
      bool NA = 0;
      bool isA;
      if(s[0] != '>') NA = 1;
      if(s[1] == '\'') {
	for(int i=2; i<(N-3)/2+2; i++) if(s[i]!='=') NA=1;
	if(s.substr(2, (N-3)/2) + "~" != s.substr(3+(N-3)/2)) NA = 1;
	if(s[(N-3)/2+2] != '#') NA = 1;
	isA = 1;
      }
      else if(s[1] == '^') {
	int cnt;
	for(cnt=2; cnt<N-2; cnt+=2) {
	  if(!(s[cnt] == 'Q' && s[cnt+1] == '=')) NA = 1;
	}
	if(!(s[cnt] == '~' && s[cnt+1] == '~')) NA = 1;
	if(cnt+2 != N) NA = 1;
	isA = 0;
      }
      else {
	NA = 1;
      }
      
      if(NA) cout << "NA" << endl;
      else if(isA) cout << "A" << endl;
      else cout << "B" << endl;
    }
  }
  
  return 0;
}