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 <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;
//		cout << "n: " << n << endl;
		if (n == 0 ){
			break;
		} // end if

		int i,j;

		for (i = 0; i < n; ++i){
			string res = species[2];
			getline (cin, str );
			
			if (str.empty() ){
				break;
			} // end if
			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?
					bool flag = false;
					for (j = 2; j < len - 2; j += 2){
						if (str[j] == 'Q' && str[j+1] == '='){
							if (!flag ){
								flag = true;
							} // end if
							continue;
						}else{
							break;
						} // end if
					} // end for
					if (j == len - 2 && flag && 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;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;
typedef pair<int, string> PSI;

int n;
string s;
bool F = 0;

int main(){
	cin >> n;
	REP(i, n){
		cin >> s;
		if (s[0] == '>'&&s[s.size() - 1] == '~'){
			if (s[1] == '\''){
				int icnt = 0;
				bool f = 1;
				FOR(j, 2, s.size() - 1){
					if (s[j] == '='){
						if (f){
							icnt++;
						}
						else{
							icnt--;
						}
					}
					else if (s[j] == '#'){
						if (f)f = 0;
						else {
							F = 1;
							break;
						}
					}
					else{
						F = 1;
						break;
					}
				}
				if (F == 0 && icnt == 0 && s.size() >= 6){
					cout << "A" << endl;
				}
				else {
					cout << "NA" << endl;
				}
			}
			else if (s[1] == '^'&&s.size() % 2 == 0 && s.size() >= 6
				&& s[s.size() - 1] == '~'&&s[s.size() - 2] == '~'){
				FOR(j, 2, s.size() - 2){
					if (j % 2 == 0){
						if (s[j] != 'Q'){
							F = 1;
							break;
						}
					}
					else{
						if (s[j] != '='){
							F = 1;
							break;
						}
					}
				}
				if (F){
					cout << "NA" << endl;
				}
				else{
					cout << "B" << endl;
				}
			}
			else {
				cout << "NA" << endl;
			}
		}
		else{
			cout << "NA" << endl;
		}
	}
}

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

int main(){

  int in;

  cin >> in;

  for(int i = 0; i < in; i++){
    int status = 0;
    string snake;
    cin >> snake;
    if(snake[0] != '>') {
      cout << "NA" << endl;
      continue;
    }
    if(snake[1] == '^'){

      for(int j = 2; j < snake.size()-2; j+=2){
	if(snake[j] != 'Q' || snake[j+1] != '='){
	  cout << "NA" << endl;
	  status = 1;
	  break;
	}
      }
      if(snake[snake.size()-2] == '~' && snake[snake.size()-1] == '~' && status == 0){
	cout << "B" << endl;
      }
      continue;
    }else if(snake[1] == '\''){
      int head = 0, tale = 0, join;

      for(int j = 2; j < snake.size(); j++){
	if(snake[j] == '#'){
	  join = j;
	  break;
	}
	head++;
      }

      for(int j = join+1; j < snake.size()-1; j++){
	tale++;
      }

      if(head == tale && snake[snake.size()-1] == '~'){
	cout << "A" << endl;
	continue;
      }
    }
    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.substr(0,2) != ">'") return false;
	string rear = str.substr(2,str.size()-2);
	
	int eq = count(rear.begin(),rear.end(),'=');
	int shrp = count(rear.begin(),rear.end(),'#');
	int til = count(rear.begin(),rear.end(),'~');

	if(shrp != 1 || (eq + shrp+til) != rear.size() || til != 1) return false;

	int lhs=0;
	int rhs=0;
	bool flag=true;
	for(int i=0;i<rear.size()-1;i++){
		if(rear[i]=='#'){
			flag = false;
			continue;
		}
		flag ? lhs++ : rhs++;
	}

	return lhs == rhs && lhs != 0;
}

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;
		}
	}
	
}