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<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<functional>
#include<map>
using namespace std;

int main() {
	int a;
	cin >> a;
	for (int b = 0; b < a; b++) {
		string c; cin >> c;
		if ((c.length() & 1)==0&&c.length()>=6) {
			if (c[0] == '>'&&c[1] == '^'&&c[c.length() - 1] == '~'&&c[c.length() - 2] == '~') {
				bool e = true;
				for (int d = 2; d < c.length() - 2; d += 2) {
					if (c[d] != 'Q' || c[d + 1] != '=')e = false;
				}
				if (e) {
					puts("B");
					continue;
				}
			}
			if (c[0] == '>'&&c[1] == '\''&&c[c.length() - 1] == '~') {
				bool k = true;
				for (int j = 2; j < c.length() - 1; j++) {
					if (j == c.length() / 2) {
						if (c[j] != '#')k = false;
					}
					else {
						if (c[j] != '=')k = false;
					}
				}
				if (k) { puts("A"); continue; }
			}
		}
		puts("NA");
	}
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int main() {
	int n; scanf("%d", &n);
	rep(i, n) {
		string s; cin >> s;
		if (s.size() < 4 || s.size() % 2 == 1) {
			puts("NA");
			continue;
		}
		if (s[0] == '>'&&s[1] == '\''&&s[s.size() - 1] == '~') {
			int cnt = 0;
			bool flag = false;
			for (int i = 2; i < s.size() - 1; i++) {
				if (s[i] != '='&&s[i] != '#') { puts("NA"); goto g; }
				if (flag&&s[i] == '#') { puts("NA"); goto g; }
				if (s[i] == '#')flag = true;
				else {
					if (flag)cnt--;
					else cnt++;
				}
			}
			puts(cnt ? "NA" : "A");
		}
		else if (s[0] == '>'&&s[1] == '^'&&s[s.size() - 2] == '~'&&s[s.size() - 1] == '~') {
			for (int i = 2; i < s.size() - 2; i += 2) {
				if (s[i] != 'Q' || s[i + 1] != '=') {
					puts("NA"); goto g;
				}
			}
			puts("B");
		}
		else puts("NA");
	g:;
	}
}

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

int main()
{
	int n;
	cin >> n;
	for(int i=0;i<n;i++){
		bool a=false,b=false;
		string str;
		cin >> str;
		if(str[0] == '>' && str[1] == '\''){a = true;}
		if(str[0] == '>' && str[1] == '^'){b = true;}
		if(a){
			for(unsigned j=2;j<str.size()-1;j++){
				if((j != (str.size()-3)/2+2) && str[j] != '='){
					a = false;
					break;
				}
			}
			if(str[(str.size()-3)/2+2] != '#' || str[str.size()-1] != '~')
				a=false;
		}		
		if(b){
			for(unsigned j=2;j<str.size()-2;j+=2){
				if(str[j] == 'Q' && str[j+1] != '='){
					b = false;
					break;
				}
			}
			if(str[str.size()-2] != '~' || str[str.size()-1] != '~')
				b=false;
		}
		if(a)
			cout << "A" << endl;
		else if(b)
			cout << "B" << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++) 
#define ALL(n) begin(n),end(n)
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
bool check_A( string S )
{
    auto sz = S.size();
    if( S.find( ">'=" ) == 0 && S.find( "~" ) == sz - 1 && sz >= 6 )
    {
        S = S.substr( 2, sz - 3 );
        sz = S.size();
        if( S.find_first_not_of( "#=", 0 ) != string::npos )
        {
            return 0;
        }
        auto pos = S.find( "#" );
        if( S.find( "#", pos + 1 ) != string::npos )
        {
            return 0;
        }
        if( S[ sz / 2 ] != '#' )
        {
            return 0;
        }
        return S.erase( pos, 1 ).size() % 2 == 0 ? 1 : 0;
    }
    return 0;
}
bool check_B( string S )
{
    auto sz = S.size();
    if( S.find( ">^Q=" ) == 0 && S.find( "~~" ) == sz - 2 )
    {
        S = S.substr( 2, sz - 4 );
        sz = S.size();
        if( sz % 2 != 0 )
        {
            return 0;
        }
        for( int i = 0; i < sz; i += 2 )
        {
            if( S[ i ] != 'Q' && S[ i + 1 ] != '=' )
            {
                return 0;
            }
        }
        return 1;
    }
    return 0;
}
int main()
{
    int N;
    cin >> N;
    
    vector<string> res{ "NA", "A", "B" };
    REP( i, N )
    {
        string S;
        cin >> S;
        cout << res[ check_A( S ) ? 1 : check_B( S ) ? 2 : 0 ] << endl;
    }
    return 0;
}

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

void a(){
	cout << "NA" << endl;
	return;
}

int main(){
	int n;
	cin >> n;

	for(int i=0;i<n;i++){
		string snake;
		cin >> snake;
		string::size_type aruyon;

		if((aruyon = snake.find(">'"))==0){
			int equal=0;
			int j;
			for(j=2;snake.at(j)=='=';j++) equal++;
			if(equal > 0)
				if(snake.at(j) == '#'){
					j++;
					for(;snake.at(j)=='=';j++) equal--;
					if(equal == 0){
						if(snake.at(j)=='~' && j+1 == snake.size()) cout << 'A' << endl;
						else a();
					}
					else a();
				}
				else a();
			else a();
		}
		else if((aruyon = snake.find(">^"))==0){
			int j;
			bool dou = false;
			if((aruyon = snake.find("~~"))==snake.size() -2){
				if(aruyon%2==0){
					for(j=2;j<aruyon;j++){
						if(snake.at(j) == 'Q') j++;
						else{j=0; dou=false; break;}	
						if(snake.at(j) != '='){a(); dou=false; break;}
						else dou == true;
					}
					if(j == aruyon && dou == true) cout << 'B' << endl;
					else a();
				}
				else a();
			}
			else a();
				
		}
		else
			a();
	}

  return 0;
}

C++: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
main(){
  int n;
  cin>>n;
  while(n--){
  string s;
  cin>>s;
  if(s[0]=='>'){
    if(s[1]=='\''){
      int sum=0;
      if(s.size()<6)goto L;
      r(i,s.size())if(s[i]=='=')sum++;
      if(s.size()%2==0&&s[s.size()/2]=='#'&&sum==s.size()-4&&s[s.size()-1]=='~')
        cout<<'A'<<endl;
      else goto L;
    }
    else if(s[1]=='^'){
      int c=2;
      if(s.size()%2||s.size()<6)goto L;
      while(c<s.size()-3){
        if(s[c]!='Q')goto L;
        c++;
        if(s[c]!='=')goto L;
        c++;
      }
      if(s[c]=='~'&&s[c+1]=='~')cout<<'B'<<endl;
      else goto L;
    }
    else goto L;
  }
  else goto L;
  if(0)L:cout<<"NA"<<endl;
  }
}