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<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int N;
    cin>>N;
    for(int i=0;i<N;i++){
        string sn;
        cin>>sn;
        int type=0;
        if(sn[0]=='>'&&sn[1]==39)type=1;
        if(sn[0]=='>'&&sn[1]=='^')type=2;
        int flg=1;
        if(type==1){
            int cou=0;
            int aftercou=0;
            int p;
            for(int j=2;j<sn.size();j++){
                if(sn[j]=='=')cou++;
                else if(sn[j]=='#'){
                    p=j;
                    break;
                }
                else{
                    flg=0;
                    goto syutu;
                }
            }
            for(int k=p+1;k<sn.size();k++){
                if(sn[k]=='=')aftercou++;
                else if(sn[k]=='~'&&k==sn.size()-1){
                    if(cou!=aftercou)flg=0;
                    goto syutu;
                }
                else{
                    flg=0;
                    goto syutu;
                }
            }
            if(sn[sn.size()-1]!='~')flg=0;
syutu:
            if(flg&&cou)puts("A");
            else puts("NA");
        }
        else if(type==2){
            for(int j=2;j<sn.size()-1;j+=2){
                if(sn[j]=='~'&&sn[j+1]=='~'&&j!=2)break;
                if(!(sn[j]=='Q'&&sn[j+1]=='='))flg=0;
            }
            if(sn[sn.size()-2]!='~'||sn[sn.size()-1]!='~')flg=0;
            if(flg)puts("B");
            else puts("NA");
        }
        else puts("NA");
    }
    return 0;
}

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

int main() {
  int n; cin >> n;
  for(int i = 0; i < n; i++) {
    string s; cin >> s;
    bool a[4] = {}, b[3] = {};
    int cnt = 0;
    for(int j = 0; j < s.size(); j++) {
      if(s[j] == '>') {
        if(!a[1] && s[j+1] == '\'') a[0] = true;
        if(s[j+1] == '^') b[0] = true;
      } else if(a[0] && s[j] == '=') {
        a[1] = true;
        cnt++;
      } else if(b[0] && s.substr(j,2) == "Q=") {
        b[1] = true;
      } else if(a[1] && s[j] == '#') {
        a[0] = false;
        a[2] = true;
      } else if(a[2] && s[j] == '=') {
        cnt--;
      } else if(a[2] && cnt == 0 && j+1 == s.size() && s[j] == '~') {
        a[3] = true;
      } else if(b[1] && j+2 == s.size() && s.substr(j,2) == "~~") {
        b[2] = true;
      }
    }

    if(a[3]) cout << 'A' << endl;
    else if(b[2]) cout << 'B' << endl;
    else cout << "NA" << endl;
  }
}

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

int main(void)
{
	char str[]="特に意味のない文章";
	int cut_i,cut_j;
	int len,kaisu;
	int hantei;

	scanf("%d",&kaisu);

	for(cut_i=0;cut_i<kaisu;cut_i++){
		scanf("%s",str);
		len = strlen(str);
		hantei = 0;

		if((len < 5 )|| (str[0] != '>') || (len % 2 != 0)){
			printf("NA\n");
			continue;
		}
		
		len--;		

		if((str[1] == '\'') && (str[len] == '~') ){
			cut_j = 2;
			while(str[cut_j] == '='){
				hantei++;
				cut_j++;
			}

			if(str[cut_j] != '#'){
				printf("NA\n");
			}
			else{
				cut_j++;

				while(str[cut_j] == '='){
					hantei--;
					cut_j++;
				}

				if( hantei == 0){
					printf("A\n");
				}
				else {
					printf("NA\n");
				}
			}
		}

		else if((str[1] == '^') &&(str[len-1] == str[len]) && (str[len] == '~')){
			for(cut_j =2;cut_j<len-2;cut_j+=2){
				if((str[cut_j] != 'Q') || (str[cut_j+1] != '=')){
					hantei = -1;
				}
			}
			printf("B\n");
		}
		else {
			printf("NA\n");
			continue;
		}			
	}	
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>

using namespace std;

typedef long long ll;
typedef pair<double,int> P;

static const double eps = 10e-9;

bool isA(string str){
	if(str[0] != '>') return false;
	if(str[1] != '\'') return false;

	int hist[] = {0,0};
	for(int i=2,j=0;i<str.size()-1;i++){
		if(str[i] == '='){
			hist[j]++;
		}
		else if(str[i] == '#') j++;
	}

	if(hist[0] != hist[1]) return false;
	if(hist[0] == 0 || hist[1] == 0) return false;
	if(str[str.size()-1] != '~') return false;

	return true;

}

bool isB(string str){
	if(str.substr(0,2) != ">^") return false;
	string rear = str.substr(2,str.size()-2);

	bool isok=false;
	int i;
	for(i=2;i<rear.size();){
		if(rear.substr(i,2) == "Q="){
			isok=true;
			i+=2;
		}
		else break;
	}

	return (isok && rear.substr(i,2) == "~~") && (i+2 == rear.size());
}

int main(){
	int n;
	while(~scanf("%d",&n)){
		for(int i=0;i<n;i++){
			string str;
			cin >> str;

			if(isA(str)) cout << "A" << endl;
			else if(isB(str)) cout << "B" << endl;
			else cout << "NA" << endl;
		}
	}
	
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;

int main()
{
//	cut here before submit 
//	freopen ("testcase.snack", "r", stdin );
	const string species[] = { "A", "B", "NA" };

	string str = "";
	int n;

	while (getline (cin, str ) ){
		stringstream ssn(str);
		ssn >> n;
		if (n == 0 ){
			break;
		} // end if

		int i,j;

		string res = species[2];

		for (i = 0; i < n; ++i){
			getline (cin, str );
			int len = str.length();

			if (str[0] == '>'){
				if (str[1] == '\''){	// A species? 
					res = species[0];
				}else if (str[1] == '^'){ // B species?
					res = species[1];
				}else{
					res = species[2];
				} // end if
			} // end if
			
			if (res == species[0] || res == species[1]){
				if (res == species[0]){
					int prev = 0;
					int after = 0;
					bool flag = false;
					for (j = 2; j < len; ++j){
						if (str[j] == '='){
							if (!flag){
								++prev;
							}else{
								++after;
							} // end if
							continue;
						}else if (str[j] == '#'){
							flag = true;
							continue;
						}else if (j == len - 1 && str[j] == '~' 
							&& flag 
							&& prev != 0 && after != 0 && prev == after ){
							res = species[0];
						}else{
							res = species[2];
							break;
						} // end if
					} // end for
				}else{	// if (res == species[1])
					for (j = 2; j < len - 2; j += 2){
						if (str[j] == 'Q' && str[j+1] == '='){
							continue;
						}else{
							break;
						} // end if
					} // end for
					if (j == len - 2 && str[j] == '~' && str[j+1] == '~'){
						res = species[1];
					}else{
						res = species[2];
					} // end if
				} // end if
			} // end if
			cout << res << endl;
		} // end for
	} // end loop
		
	return 0;
}