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

int main(){
	cin >> n;
	REP(i, n){
		F = 0;
		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++: #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++;
	}

	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 <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;


int main() {
	int n;
	string s;
	cin >> n;
	rep(i, n) {
		cin >> s;
		string ans;
		if (s.size() % 2 == 1) {
			ans = "NA";
		}
		else if (s[0] == '>'&&s[s.size() - 1] == '~') {
			if (s[1] == '\'') {
				ans = "A";
				for (int i = 2; i < s.size() - 1; i++) {
					if (i == s.size() / 2) {
						if (s[i] != '#') {
							ans = "NA";
							break;
						}
					}
					else {
						if (s[i] != '=') {
							ans = "NA";
							break;
						}
					}
				}
			}
			else if (s[1] == '^'&&s[s.size()-2]=='~') {
				ans = "B";
				for (int i = 2; i < s.size() - 2; i += 2) {
					if (s[i] != 'Q' || s[i + 1] != '=') {
						ans = "NA";
						break;
					}
				}
			}
			else ans = "NA";
		}
		else {
			ans = "NA";
		}
		cout << ans << endl;
	}
}

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

bool isA(const string& str, int indx) {
	if(str[indx++] != '>')
		return false;
	if(str[indx++] != '\'')
		return false;

	int count0 = 0;
	for(; indx < str.size(); indx++) {
		if(str[indx] == '=')
			count0++;
		else
			break;
	}
	if(indx >= str.size())
		return false;
	if(str[indx++] != '#' || count0 == 0)
		return false;
	int count1 = 0;
	for(; indx < str.size(); indx++) {
		if(str[indx] == '=')
			count1++;
		else
			break;
	}

	if(count0 == count1 && indx + 1 == str.size() && str[indx] == '~')
		return true;
	return false;
}

bool isB(const string & str, int indx) {
	if(str[indx++] != '>')
		return false;
	if(str[indx++] != '^')
		return false;

	int count = 0;
	for(; indx + 1 < str.size(); indx += 2) {
		if(string(str.begin() + indx, str.begin() + indx + 2) == "Q=")
			count++;
		else
			break;
	}
	if(count > 0 && string(str.begin() + indx, str.begin() + indx + 2) == "~~" && indx + 2 == str.size())
		return true;
	return false;
}

int main() {
	string str;
	int n;

	cin >> n;
	for(int t = 0; t < n; t++) {
		int type = 0;
		cin >> str;
		for(int i = 0; i < str.size(); i++) {
			if(str[i] == '>') {
				if(isA(str, i)) {
					type = 1;
					break;
				}
				else if(isB(str, i)) {
					type = 2;
					break;
				}
			}
		}

		switch(type) {
			case 0:
				cout << "NA" << endl;
				break;
			case 1:
				cout << "A" << endl;
				break;
			case 2:
				cout << "B" << endl;
				break;
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
  string str;
  int n; cin >> n;
  while(n--){
    cin >> str;
    int idx = 2;
    int cnt = 0;
    bool flg = true;
    if(str[0] == '>' && str[1] == 39){
      while(str[idx] != '#') {
	cnt++;
	idx++;
      }
      idx++;
      while(str[idx] != '~') {
	cnt--;
	idx++;
      }
      if(cnt == 0) {
	cout << "A" << endl;
	flg = false;
      }
    } else if(str[0] == '>' && str[1] == '^'){
      while(str[idx] == 'Q' && str[idx+1] == '='){
	idx += 2;
      }
      if(str[idx] == '~' && str[idx+1] == '~') {
	cout << "B" << endl;
	flg = false;
      }
    }
    if(flg) cout << "NA" << endl;
  }
}