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(){
long n,f,i,j,k,m;
string s;
cin>>n;
for(i=0;i<n;i++){
cin>>s;
if(s.substr(0,2)==">'"){
if(s.substr(2,1)!="="){f=2;goto E;}
j=s.find_first_not_of('=',3);
m=j-2;
if(s.substr(j,1)!="#"){f=2;goto E;}
k=s.find_first_not_of('=',j+1);
if(k-j-1!=m){f=2;goto E;}
if(s.substr(k,1)!="~"){f=2;goto E;}
if(k+1!=s.size()){f=2;goto E;}
f=0;
}else if(s.substr(0,2)==">^"){
if(s.substr(2,2)!="Q="){f=2;goto E;}
for(j=4;;j+=2){
if(s.substr(j,2)!="Q=")break;
}
if(s.substr(j,2)!="~~")f=2;else f=1;
}else{
f=2;
}
E:
if(f==0)cout<<"A"<<endl;
else if(f==1)cout<<"B"<<endl;
else cout<<"NA"<<endl;
}
return 0;
}

C++: #include<string>
#include<algorithm>
#include<iostream>
using namespace std;
int main(){
    int N;
    cin>>N;
    while(N--){
        string str;
        cin>>str;
        bool Aflag,Bflag;
        Aflag=Bflag=true;
        if(str.length()<6)Aflag=Bflag=false;
        if(str.substr(0,2)==">'")Bflag=false;
        else if(str.substr(0,2)==">^")Aflag=false;
        else Aflag=Bflag=false;

        if(Aflag){

            int p=str.find("#");
            if(p==str.npos){
                Aflag=false;
                break;
            }
            int cnt=0;
            for(int i=2;i<p;i++){
                if(str[i]=='='&&str[p+i-1]=='=')cnt++;
                else{
                    Aflag=false;
                    break;
                }
            }
            if(str[cnt*2+3]=='~'&&str.length()==cnt*2+4);
            else Aflag=false;

        }

        if(Bflag){
            int p=2;
            while(str.substr(p,2)=="Q=")p+=2;
            if(str.substr(p,p+2)=="~~"&&str.length()==p+2);
            else Bflag=false;
        }
        if(Aflag)cout<<"A"<<endl;
        else if(Bflag)cout<<"B"<<endl;
        else cout<<"NA"<<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;
//		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?
					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;
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int solve(string &s){
  if(s.length() >= 3 && s.substr(0, 2) == ">'" && s[(int)s.length() - 1] == '~'){
    int c1 = 0, c2 = 0, t = 0, f = 0;
    for(int i = 2; i + 1 < (int)s.length(); ++i){
      if(s[i] != '=' && s[i] != '#') f = 1;
      if(s[i] == '#' && t >= 1) f = 1;
      if(f) break;
      if(s[i] == '#') ++t;
      else if(t) ++c2;
      else ++c1;
    }
    if(!f && c1 == c2) return 0;
  }
  if(s.length() >= 4 && s.substr(0, 2) == ">^" && s.substr((int)s.length() - 2) == "~~"){
    int f = 0;
    for(int i = 2; i + 2 < s.length(); i += 2) if(s.substr(i, 2) != "Q=") f = 1;
    if(!f) return 1;
  }
  return -1;
}

int main() {
  int T; cin >>T;
  REP(t, T){
    string s; cin >>s;
    int ans = solve(s);
    cout <<(ans == 0 ? "A" : (ans == 1 ? "B" : "NA")) <<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
string judgeSnakeType(string s){
  if(s.find( ">'") == 0 && s.find("~") == s.size()-1)
    {
      s.erase(s.size()-1,1); s.erase(0,2);
      int cnt = 0;
      while( s.size() > 0 && s[0] == '=' ){
	if(s[0] == '='){
	  s.erase(0,1);
	  cnt++;
	}
      }
      if( s[0] != '#' || cnt == 0)return "NA";
      s.erase(0,1);
      while( s.size() > 0 && s[0] == '=' ){
	if(s[0] == '='){
	  s.erase(0,1);
	  cnt--;
	}
      }
      if(s.size() == 0 && cnt == 0) return "A";
      else return "NA";
    }
  else if( s.find( ">^") == 0 && s.find("~~") == s.size()-2)
    {
      s.erase(s.size()-2,2); s.erase(0,2);
      while( s.size() > 0 && s.size() % 2 == 0){
	if(s[0] == 'Q' && s[1] == '='){
	  s.erase(0,2);
	}
	else {
	  return "NA";
	}
      }
      return s == "" ? "B" : "NA";
    }
  else
    {
      return "NA";
    }
  return "NA";
}
void solve(string snake){
  cout << judgeSnakeType( snake ) << endl;
}
int main(){
  int d;
  cin >> d;
  string snake;
  for(int i = 0 ; i < d; i++ )
    {
      cin >> snake;
      solve(snake);
    }
  return 0;
}