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;
int n;
string s;
int main(void) {
	cin >> n;
	for (int k = 0; k < n; k++) {
		cin >> s;
		bool f = true;
		if (s[0] == '>' && s[1] == '\'') {
			int c = 0;
			int i;
			for (i = 2; s[i] == '='; i++)
				c++;
			if (c && s[i] == '#') {
				for (i += 1; s[i] == '='; i++)
					c--;
				if (!c && (int) s.size() == i + 1 && s[i] == '~') {
					cout << 'A' << endl;
					f = false;
				}
			}
		} else if (s[0] == '>' && s[1] == '^' && s[2] == 'Q' && s[3] == '=') {
			int i;
			for (i = 4; s[i] == 'Q' && s[i + 1] == '='; i += 2)
				;
			if ((int) s.size() == i + 2 && s[i] == '~' && s[i + 1] == '~') {
				cout << 'B' << endl;
				f = false;
			}
		}
		if (f) {
			cout << "NA" << endl;
		}
	}
}

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

void a(){
	cout << "NA" << endl;
	return;
}

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

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

		if((aruyon = snake.find(">'"))==0){
			int equal=0;
			int j;
			for(j=2;snake.at(j)=='=';j++) equal++;
			if(equal == 0){a(); break;}
			if(snake.at(j) == '#') j++;
			else{a();break;}
			for(;snake.at(j)=='=';j++) equal--;
			if(equal != 0){a(); break;}
			if(snake.at(j)=='~' && j+1 == snake.size()) cout << 'A' << endl;
		}
		else if((aruyon = snake.find(">^"))==0){
			int j;
			if((aruyon = snake.find("~~"))!=snake.size() -2){a(); break;}
			if(aruyon%2!=0){a(); break;}
			for(j=2;j<aruyon;j++){
				if(snake.at(j) == 'Q') j++;
				else{a(); j=0; break;}	
				if(snake.at(j) != '='){a(); break;}
			}
			if(j == aruyon) cout << 'B' << endl;
		}
		else
			a();
	}

  return 0;
}

C++: # define _CRT_SECURE_NO_WARNINGS 1
# include <iostream>
# include <numeric>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iomanip>
# include <queue>
# include <sstream>
# include <climits>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <forward_list>
# include <map>
# include <stack>
using namespace std;

# define INF ((int)(1<<30))
# define REP(i,n) for(int i=0;i<(int)n;i++)
# define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
# define TORAD 2.0*M_PI/360.0
# define INT(x) int x;cin>>x;
# define ALL(x) (x).begin(),(x).end()
# define RALL(x) (x).rbegin(),(x).rend()
# define DEBUG(x) cout<<#x<<" :"<<x<<endl;
# define EPS 1e-12

template<class T> void debug(T a) { for (auto i : a)cout << i << endl; }

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = { 0,-1,1,0 }, dy[4] = { 1,0,0,-1 };

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		string a;
		cin >> a;
		bool ok = true;
		string ans;
		if (a[0] == '>'&&a[1] == '\'')ans = "A";
		else if (a[0] == '>'&&a[1] == '^')ans = "B";
		else ok = false;
		if (ans == "A")
		{
			int c = 0;
			for (int j = 2; j < a.size(); j++)
			{
				if (a[j] == '=')c++;
				else break;
			}
			if (c > a.size() / 2) {
				ok = false; goto end;
			}
			if (a[2 + c] != '#')
			{
				ok = false; goto end; 
			}
			int d = 0;
			for (int j = 2 + c +1; j < a.size(); j++)
			{
				if (a[j] == '=')d++;
				else break;
			}
			if (d != c)
			{
				ok = false; goto end;
			}
			int e = 0;
			for (int j = 2 + c + d + 1; j < a.size(); j++)
			{
				if (a[j] == '~')e++;
				else
				{
					ok = false;
					goto end;
				}
			}
			if (e != 1)ok = false;

		}
		if (ans == "B")
		{
			int j;
			for (j = 2; j < a.size() - 1; j += 2)
			{
				if (a[j] != 'Q' || a[j + 1] != '='){
					break;
				}
			}
			if (a[j] != '~' || a[j + 1] != '~')ok = false;
			if (j != a.size() - 2)ok = false;
		}
		end:
		if (ok)cout << ans << endl;
		else cout << "NA" << endl;

	}

}

C++: #include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

signed main(){
	int n;
	cin>>n;
	rep(i,0,n){
		string s;
		cin>>s;
		int ans=0;
		int sz=s.size();

		if(s[0]=='>'&&s[1]=='\''&&s[sz-1]=='~'&&sz%2==0&&s[sz/2]=='#'){
			rep(i,2,sz-1){
				if(i!=sz/2&&s[i]!='='){
					ans=-1; break;
				}
				if(i==sz-2) ans=1;
			}
		}else if(s[0]=='>'&&s[1]=='^'&&s[sz-2]=='~'&&s[sz-1]=='~'&&sz%2==0){
			rep(i,2,sz-2){
				if(i%2==0&&s[i]!='Q'){
					ans=-1; break;
				}if(i%2&&s[i]!='='){
					ans=-1; break;
				}
				if(i==sz-3) ans=2;
			}
		}else{
			ans=-1;
		}
		if(ans==-1) o("NA");
		else if(ans==1) o("A");
		else if(ans==2) o("B");
	}	
}

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[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++: #include <iostream>
#include <cstring>
using namespace std;

int get_type(char *s) {
  int l = strlen(s);
  int c, d, p;
  if (s <= 0) return 2;
  if (s[0] != '>') return 2;
  if (s[1] == '\'') {
    c = d = 0;
    while (c+2<l && s[c+2]=='=') ++c;
    if (!c || c+2>=l || s[c+2]!='#') return 2;
    while (d+c+3<l && s[d+c+3]=='=') ++d;
    if (c!=d || d+c+4!=l || s[d+c+3]!='~') return 2;
    return 0;
  } else if (s[1] == '^') {
    c = 0;
    while (2*c+3<l && s[2*c+2]=='Q' && s[2*c+3]=='=') ++c;
    if (!c || 2*c+4!=l || s[2*c+2]!='~' || s[2*c+3]!='~') return 2;
    return 1;
  }
  return 2;
}

int main() {
  int n, t;
  char snake[201];
  const char *type[] = {"A", "B", "NA"};
  cin >> n;
  for (int i=0; i<n; i++) {
    cin >> snake;
    cout << type[get_type(snake)] << endl;
  }
  return 0;
}