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<climits>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstring>
#include<stack>
#include<functional>
using namespace std;

bool isA(string s){
	int n = 0;
	if(s.at(n)!='>' || s.at(++n)!='\'' ||s.at(++n)!='=') return false;
	int cnt = 1;
	for(;s.at(++n)=='='; cnt++);
	if(s.at(n)!='#') return false;
	for(int i=0; i<cnt; i++){
		if(s.at(++n)!='=') return false;
	}
	if(s.at(++n)!='~' || s.size()>n+1) return false;
	return true;
}

bool isB(string s){
	int n = 0;
	if(s.at(n)!='>' || s.at(++n)!='^' || s.at(++n)!='Q' || s.at(++n)!='=') return false;
	while(true){
		if(s.at(++n)=='Q'){
			if(s.at(++n)!='=') return false;
		}
		else break;
	}
	if(s.at(n)!='~' || s.at(++n)!='~' || s.size()>n+1) return false;
	return true;
}

int main(){
	string S;
	int N;
	cin>>N;
	for(int i=0;i<N;i++){
		cin>>S;
		if(isA(S)) puts("A");
		else if(isB(S)) puts("B");
		else puts("NA");
	}
	return 0;
}

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

string solve(string T) {
	int chain = 0, ok = 0;
	if (T.size() >= 3) {
		if (T[T.size() - 1] == '~') {
			if (T.substr(0, 2) == ">'") {
				for (int i = 2; i < T.size() - 1; i++) {
					if (T[i] == '#') {
						ok++;
					}
					else {
						if (ok == 0) { chain++; }
						else { chain--; }
					}
				}
				if (ok == 1 && chain == 0) { return "A"; }
			}

			if (T.substr(0, 2) == ">^" && T[T.size() - 2] == '~') {
				for (int i = 2; i < T.size() - 2; i += 2) {
					if (T.substr(i, 2) != "Q=") {
						goto E;
					}
				}
				return "B";
			E:;
			}
		}
	}
	return "NA";
}

int main() {
	cin >> n;
	for (int h = 0; h < n; h++) {
		cin >> S;
		cout << solve(S) << endl;
	}
}

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


typedef long long ll;
const double Eps = 1e-6;

using namespace std;

int main()
{
	char buf[256];
	int n;
	cin.getline(buf, sizeof(buf));
	sscanf(buf, "%d", &n);
	while (n--)
	{
		memset(buf, '\0', sizeof(buf));
		cin.getline(buf, sizeof(buf));
		if (strlen(buf) < 3)
		{
			puts("NA");
			continue;
		}

		char* p = buf+2;
		if (buf[0] == '>' && buf[1] == '\'')
		{
			int c[4];
			memset(c, 0, sizeof(c));
			int sharp, tilde;
			int i;
			for (i = 0; p[i] != '\0'; ++i)
			{
				if (p[i] == '=')
					++c[0];
				else if (p[i] == '#')
				{
					++c[1];
					sharp = i;
				}
				else if (p[i] == '~')
				{
					++c[2];
					tilde = i;
				}
				else
					++c[3];
			}
			if (c[0] > 0 && (c[0] & 1) == 0 && c[1] == 1 && sharp == i/2-1
				&& c[2] == 1 && tilde == i-1)
				puts("A");
			else
				puts("NA");
		}
		else if (buf[0] == '>' && buf[1] == '^')
		{
			int i;
			for (i = 0; p[i] != '\0'; i += 2)
			{
				if (p[i] != 'Q' || p[i+1] != '=')
					break;
			}
			if (!strcmp(p+i, "~~"))
				puts("B");
			else
				puts("NA");
		}
		else
			puts("NA");
	}

	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;
int check( string S )
{
    //???????????¢>??£, ????°??????¢~??£ ????????????????????????NA
    if( S[ 0 ] != '>' || S[ S.size() - 1 ] != '~' )
    {
        return 0;
    }
    //A?¨?
    if( S[ 1 ] == '\'' )
    {
        S = S.substr( 2, S.size() - 3 );
        if( S.find_first_not_of( "#=", 0 ) != string::npos ) return 0;
        string tmpB = S.substr( 0, S.find('#') );
        string tmpA = S.substr( S.find( '#' ) + 1, S.size() - 1 );
        return tmpB.size() == tmpA.size() ? 1 : 0;
    }
    //B?¨?
    if( S[ 1 ] == '^' && S[ S.size() - 2 ] == '~' )
    {
        S = S.substr( 2, S.size() - 4 );
        if( S.find_first_not_of( "Q=", 0 ) != string::npos ) return 0;
        for( int i = 0; i < S.size(); i += 2 )
        {
            if( S[ i ] != 'Q' && S[ i + 1 ] != '=' ) return 0;
        }
        return 2;
    }
}
int main()
{
    int N;
    cin >> N;
    
    vector<string> res{ "NA", "A", "B" };
    string S;
    while( cin >> S )
    {
        cout << res[ check( S ) ] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <cstdio>

using namespace std;

int main(){
  int n;

  cin >> n;
  while(n--){
    string snake;
    cin >> snake;

    string s = snake.substr(0,2);
 

    if(s != ">^" && s != ">\'"){
      puts("NA");
      continue;
    }

    if(s == ">\'"){
      int a = 0;
      int p = 2;
      for(int i = 2 ; i < (int)snake.size() ; i++,p++){
	if(snake[i] == '#') break;
	a++;
      }
      
      int b = 0,q = p + 1; p += 2;
      for(int i = q ; i < (int)snake.size() ; i++,p++){
	if(snake[i] == '~') break;
	b++;
      }

      if(a == b && p == (int)snake.size()){
	puts("A");
      }else{
	puts("NA");
      }
    }else{
      bool ok = true;
      int p = 0;
      string ss = "";

      for(int i = 2 ; i < (int)snake.size()-2 ; i++,p++){
	ss += snake[i];
	if(p == 1){
	  p = -1;
	  if(ss != "Q="){
	    ok = false;
	    break;
	  }
	  ss = "";
	}
      }

      int len = (int)snake.size();

      if(snake[len-2] == '~' && snake[len-1] == '~' && ok){
	puts("B");
      }else{
	puts("NA");
      }
    }
  }

  return 0;
}

C++: #include <iostream>
//#include <fstream>

#define NA cout << "NA" << endl; goto LABEL

using namespace std;

int main() {
	//ifstream fin("in.txt");

	int snakeCount;
	cin >> snakeCount;

	for(int loopCount = 0; loopCount < snakeCount; loopCount++) {
		char arr[202];
		cin >> arr;
		int i = 0;

		if(arr[i]!='>') {
			NA;
		}
		i++;

		if(arr[i]=='\'') {
			int equalCount = 0;
			bool mode = false;
			for(int j = i+1; j < 202; j++) {
				if(arr[j]=='=') {
					if(!mode) {
						equalCount++;
					}else {
						equalCount--;
					}
				}else if(arr[j]=='#') {
					if(!mode && equalCount>0) {
						mode = !mode;
					}else {
						NA;
					}
				}else if(arr[j]=='~') {
					if(arr[j+1]=='\0'&&equalCount==0&&mode) {
						cout << "A" << endl;
						goto LABEL;
					}else {
						NA;
					}
				}else {
					NA;
				}
			}
		}else if(arr[i]=='^') {
			int count = 0;
			for(int j = i+1; j < 202; j++) {
				if(arr[j]=='Q'&&arr[j+1]=='=') {
					j++;
					count++;
					continue;
				}else if(arr[j]=='~'&&arr[j+1]=='~'&&arr[j+2]=='\0'&&count>0) {
					cout<< "B" << endl;
					goto LABEL;
				}else {
					NA;
				}
			}
		}else {
			NA;
		}
		LABEL:;
	}

	return 0;
}