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 <string>
using namespace std;

int main()
{
  const string Type[] = {"A", "B", "NA"};

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

      int ans = 2;
      int length = static_cast<int>(snake.size());
      if (snake[0] == '>' && snake[length-1] == '~') {
	if (length >= 5 && snake[1] == '\'' && snake.find_first_of("#") != string::npos) {
	  int sharp = snake.find_first_of("#");
	  bool valid = true;
	  for (int j = 2; j < sharp && valid; ++j) {
	    if (snake[j] != '=')
	      valid = false;
	  }
	  for (int j = sharp+1; j < length-1 && valid; ++j) {
	    if (snake[j] != '=')
	      valid = false;
	  }

	  if (valid && sharp-2 == length-2-sharp)
	    ans = 0;
	} else if (length >= 6 && snake[1] == '^' && snake[length-2] == '~') {
	  bool valid = true;
	  for (int j = 2; j < length-2 && valid; j += 2) {
	    if (snake[j] != 'Q' || snake[j+1] != '=')
	      valid = false;
	  }

	  if (valid)
	    ans = 1;
	}
      }
      cout << Type[ans] << endl;
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define rep(i,n) for (int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
int main(){
  int n;
  cin>>n;
  while(n--){
    string s;
    cin>>s;
    int a=0,b=0,k=-1;
    bool f=false;
    for(int i=0;i<s.size();i++)if(s[i]=='>')k=i;
    if(k!=-1){
      if(s[k+1]=='^'){
        f=false;
        for(int i=k;i<s.size()-1;i++){
          if(s[i]=='Q'&&s[i+1]=='=')f=true;
        }
        if(f&&s[s.size()-1]=='~'&&s[s.size()-2]=='~'){
          cout<<"B"<<endl;
          continue;
        }
      }
      int cnt=0;
      f=false;
      while(k<s.size()){
        if(s[k]=='='){
          while(s[k]=='='){
            cnt++;k++;
          }
          break;
        }
        k++;
      }
      if(s[k]=='#')f=true;
      while(k<s.size()){
        if(s[k]=='='){
          while(s[k]=='='){
            cnt--;k++;
          }
          break;
        }
        k++;
      }
      if(f&&cnt==0){
        cout<<"A"<<endl;
        continue;
      }
    }
    cout<<"NA"<<endl;
  }
}

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

using namespace std;

string afunc(string str)
{
	string res = "NA";
	if(str[0] == '#'){
		return res;
	}
	
	for(int i = 0, j = str.size()-1; i <= j; i++, j--){
		if(i == j){
			if(str[i] == '#'){
				res = "A";
			}
		} else if(str[i] != '=' || str[j] != '='){
			break;
		}
	}
	
	return res;
}

string bfunc(string str)
{
	string res = "B";
	if(str.size() == 0){
		return "NA";
	}
	for(int i = 0; i < str.size(); i+=2){
		if(str[i] != 'Q' || str[i+1] != '='){
			res = "NA";
			break;
		}
	}
	
	return res;
}

int main()
{
	int n;
	string str, res;
	
	while(cin >> n && n){
		while(n--){
			cin >> str;
			res = "NA";
			if('>' == str[0]){
				str.erase(str.begin());
				int l = str.size();
				if(str[0] == '\'' && str[l-1] == '~'){
					str.erase(str.end()-1);
					str.erase(str.begin());
					if(str.size()%2 == 1){
						res = afunc(str);
					}
				} else if(str[0] == '^' && str[l-1] == str[l-2] && str[l-1] == '~'){
					str.erase(str.end()-1);
					str.erase(str.end()-1);
					str.erase(str.begin());
					
					if(str.size()%2 == 0){
						res = bfunc(str);
					}
				}
			}
			
			cout << res << endl;
		}
	}
	
	return 0;
}

C++: #include<iostream>

using namespace std;

string in;

int isA(){
  if(in[0] != '>') return 0;
  if(in[1] != '\'') return 0;
  if(!(in[in.length()-1] == '~' && in[in.length()-2] == '=')) return 0;
  int data[2] = {0,0};
  int status = 0;
  for(int i = 2; i < in.length()-1; i++){
    if(in[i] == '#') status = 1;
    if(in[i] == '=') data[status]++;
  }
  if(data[0] == data[1]) return 1;
  return 0;
}

int isB(){
  if(in[0] != '>') return 0;
  if(in[1] != '^') return 0;
  if(!(in[in.length()-1] == '~' && in[in.length()-2] == '~')) return 0;
  for(int i = 2; i < in.length()-2; i+=2){
    if(!(in[i] == 'Q' && in[i+1] == '=')) return 0;
  }
  return 1;
}


int main(){
  int num;
  cin >> num;
  for(int i = 0; i < num; i++){
    cin >> in;
    if(isA()) cout << "A" << endl;
    else if(isB()) cout << "B" << endl;
    else cout << "NA" << endl;
  }
  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.size() >= 1 && str[0] != '>') return false;
	if(str.size() >= 2 && 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++;
		else return false;
	}

	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.size() >= 1 && str[0] != '>') return false;
	if(str.size() >= 2 && str[1] != '^') return false;

	if(str.size() <= 3) return false;

	string rear = str.substr(2,str.size()-2);

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

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

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 <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

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

    while(--n >= 0){
        string s;
        cin >> s;
        int n = s.size();
        if(n < 6 || n % 2 == 1){
            cout << "NA" << endl;
            continue;
        }

        string sa(n, '=');
        sa[0] = '>';
        sa[1] = '\'';
        sa[n/2] = '#';
        sa[n-1] = '~';
        if(s == sa){
            cout << 'A' << endl;
            continue;
        }

        string sb(n, '=');
        sb[0] = '>';
        sb[1] = '^';
        for(int i=2; i<n-2; i+=2)
            sb[i] = 'Q';
        sb[n-2] = '~';
        sb[n-1] = '~';
        if(s == sb)
            cout << 'B' << endl;
        else
            cout << "NA" << endl;
    }

    return 0;
}