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 <bits/stdc++.h>
using namespace std;

bool isOutOfStr(int ind, string str){
  return ind >= str.length();
}

int main(void){
  int n;
  cin >> n;
  while(n--){
    string str; cin >> str;
    if(!isOutOfStr(1, str) &&str[0] == '>' && str[1] == '\''){
      int sharp_cnt = 0;
      int ind;
      for(ind = 0; ind < str.length(); ind+=2){
        if(!(str[ind] == '>' && str[ind+1] == '\'')) break;
      }

      if(!isOutOfStr(ind, str) && str[ind] != '='){
        cout << "NA" << endl;
        continue;
      }

      for(;;ind++){
        if(isOutOfStr(ind, str) || str[ind] != '='){
          break;
        }
        sharp_cnt++;
      }

      if(isOutOfStr(ind, str) || str[ind] != '#'){
        cout << "NA" << endl;
        continue;
      }
      ind++;

      bool flg = true;
      for(int i=0; i < sharp_cnt; i++, ind++){
        if(isOutOfStr(ind, str) || str[ind] != '='){
          cout << "NA" << endl;
          flg = false;
          break;
        }
      }
      if(!flg) continue;
      
      if(!isOutOfStr(ind, str) && str[ind] == '~')
        cout << "A" << endl;
      else{
        cout << "NA" << endl;
      }



    }else if(str[0] == '>' && str[1] == '^'){
      int ind;
      for(ind = 0; ind < str.length(); ind+=2){
        if(!(str[ind] == '>' && str[ind+1] == '^')) break;
      }

      int flg = true;
      for(;;ind+=2){
        if(isOutOfStr(ind, str) || !(str[ind] == 'Q' && str[ind+1] == '=')){
          break;
        }
      }

      if(!isOutOfStr(ind, str) && !isOutOfStr(ind+1, str) && str[ind] == '~' && str[ind+1] == '~'){
        cout << "B" << endl;
      }else{
        cout << "NA" << endl;
      }
    }else{
      cout << "NA" << endl;
    }
  }
  return 0;
}

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

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair <INT, INT> II;
typedef vector <II> VII;

template<class T> ostream& operator << ( ostream& os, vector<T> v ) {
    for ( typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i ) {
        os << *it_i << ", ";
    }
    return os;
}


typedef set <string> SS;
SS SA;
SS SB;

bool checkA( string s ) {
    return SA.count( s ) > 0;
}

bool checkB( string s ) {
    return SB.count( s ) > 0;
}

string solve( string s ) {
    if ( checkA( s ) ) return "A";
    if ( checkB( s ) ) return "B";
    return "NA";
}

int main() {
    for ( int i = 1; i <= 100; ++ i ) {
        SA.insert( ">'" + string( i, '=' ) + "#" + string( i, '=' ) + "~" );
    }
    for ( int i = 1; i <= 100; ++ i ) {
        VS con( i, "Q=" );
        SB.insert( ">^" + accumulate( con.begin(), con.end(), string() ) + "~~" );
    }
    int n;
    cin >> n;
    for ( int i = 0; i < n; ++ i ) {
        string s;
        cin >> s;
        cout << solve( s ) << endl;
    }
    return 0;
}

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

using namespace std;

int main(int argc, char **argv)
{
	int n, f, m;
	char a[101], w;
	cin >> m;
	
	while(m--){
		f = 0;
		scanf("%s", a);
		w = (a[1] == '^')? 'B' : 'A';
		n = strlen(a);
		if(w == 'A'){
			if(a[n-1] == '~'){
				for(int i = 2; i < n-i; i++){
					if(a[i] != a[n-i]){
						f = 1;
						break;
					}
				}
			} else {
				f = 1;
			}
		} else {
			if(a[n-1] == '~' && a[n-2] == '~'){
				for(int i = 2; i < n-2; i+=2){
					if(a[i] != 'Q' && a[i+1] != '='){
						f = 1;
						break;
					}
				}
			} else {
				f = 1;
			}
		}
		
		if(f == 1){
			cout << "NA" << endl;
		} else {
			cout << w << endl;
		}
	}
	
	return 0;
}

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;

		for (i = 0; i < n; ++i){
			string res = species[2];
			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{				// new species.
					res = species[2];
				} // end if
			} // end if
			
			if (res == species[0] || res == species[1]){
				if (res == species[0]){	// A species?
					int prev = 0;
					int after = 0;
					int mid = 0;
					bool flag = false;
					for (j = 2; j < len - 1; ++j){
						if (str[j] == '='){
							if (!flag){
								++prev;
							}else{
								++after;
							} // end if
							continue;
						}else if (str[j] == '#'){
							flag = true;
							++mid;
							continue;
						}else{
							break;
						} // end if
					} // end for

					if (j == len - 1){
						if (str[j] == '~' && flag && mid == 1
						&& prev != 0 && after != 0 && prev == after ){
							res = species[0];
						}else{
							res = species[2];
						} // end if
					}else{
						res = species[2];
					} // end if
				}else{	// if (res == species[1]) // B species?
					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;
}