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<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<regex>
#include<vector>
using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

signed main() {
	int n; cin >> n;
	regex b(R"(>\^(Q=)+~~)");
	rep(i, 0, n) {
		string s; cin >> s;
		int m = 0;
		rep(j, 0, s.size())if (s[j] == '=')m++;
		regex a(">'={" + to_string(m / 2) + "}#={" + to_string(m / 2) + "}~");
		if (regex_match(s, a) && m) { cout << "A" << endl; }
		else if (regex_match(s, b)) { cout << "B" << endl; }
		else cout << "NA" << endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>


typedef long long ll;
const double Eps = 1e-6;

using namespace std;

int main()
{
	char buf[256];
	int n;
	scanf("%d\n", &n);
	while (n--)
	{
		memset(buf, '\0', sizeof(buf));
		cin.getline(buf, sizeof(buf));
		if (strlen(buf) < 3)
		{
			puts("NA");
			continue;
		}

		const char* p = buf+2;
		if (buf[0] == '>' && buf[1] == '\'')
		{
			int c[4];
			memset(c, 0, sizeof(c));
			int sharp, tilde;
			int i;
			for (i = 0; p[i] != '\0'; ++i)
			{
				if (p[i] == '=')
					++c[0];
				else if (p[i] == '#')
				{
					++c[1];
					sharp = i;
				}
				else if (p[i] == '~')
				{
					++c[2];
					tilde = i;
				}
				else
					++c[3];
			}
			if (c[0] > 0 && c[0] % 2 == 0 && c[1] == 1 && sharp == i/2-1
				&& c[2] == 1 && tilde == i-1)
				puts("A");
			else
				puts("NA");
		}
		else if (buf[0] == '>' && buf[1] == '^')
		{
			int i;
			for (i = 0; p[i] != '\0'; i += 2)
			{
				if (p[i] != 'Q' || p[i+1] != '=')
					break;
			}
			if (i >= 2 && strcmp(p+i, "~~") == 0)
				puts("B");
			else
				puts("NA");
		}
		else
			puts("NA");
	}

	return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
bool check(string s,int n) {
	if (s.size() <= n)return true;
	return false;
}
void solve() {
	string s; cin >> s;
	int n = 0;
	if (s[n] == '>') {
		n++;
		if (check(s, n));
		else if (s[n] == '\'') {
			n++;
			int cnt = 0;
			while (s[n] == '=' && !check(s, n)) { cnt++; n++; }
			if (s[n] == '#') {
				n++;
				if (check(s, n))goto exit;
				while (s[n] == '=' && !check(s, n)) { cnt--; n++; }
				if (cnt == 0) {
					if (s[n] == '~') {
						n++;
						if (n == s.size()) {
							cout << "A\n";
							return;
						}
					}
				}
			}
		}
		else if (s[n] == '^') {
			n++;
			if (check(s, n))goto exit;
			while (true) {
				if (check(s, n + 1))goto exit;
				else {
					if (s[n] == 'Q'&&s[n + 1] == '=')n += 2;
					else if (s[n] == '~'&&s[n + 1] == '~') {
						cout << "B\n";
						return;
					}
					else goto exit;
				}
			}
			n++;
			if (check(s, n))goto exit;
			if (s[n] == '~') {
				n++;
				if (check(s, n))goto exit;
				if (s[n] == '~') {
					n++;
					if (n == s.size()) {
						cout << "B\n";
						return;
					}
				}
			}
		}
	}
exit:;
	cout << "NA\n";
	return;
}
int main() {
	int n; cin >> n;
	for (int i = 0; i < n; i++)solve();
}

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

using namespace std;

int main()
{
	int n;
	cin >> n;
	for(int D = 0; D < n; ++D){
		string s;
		cin>>s;
		
		if(s.compare(0,2,">'")== 0){
			int bef_eq = 0;
			int aft_eq = 0;
			int shp = 0;
			int pos_shp = 0;
			int pos_tld = 0;

			for(unsigned int i = 2; i < s.length(); ++i ){
				if( s[i] == '=' )
					if( shp == 0 )
						++bef_eq;
					else
						++aft_eq;
				else if( s[i] == '#' ){
					shp++;
				}else if( s[i] =='~' ){
					pos_tld = i;
					break;
				}else{
					shp = 2;
					break;
				}
			}
			if( bef_eq && aft_eq && pos_tld && bef_eq == aft_eq &&
				shp == 1 && pos_tld == s.length() - 1 )
				cout << "A\n";
			else
				cout << "NA\n";
		}else if(s.compare(0,2,">^")==0){
			int cnt_qeq = 0;
			int pos_tltl = 0;
			bool invalid = false;
			
			if( s.length() <= 2 || s.length() % 2 == 1 )
				invalid = true;
			
			for(unsigned int i = 2; i < s.length() && !invalid; i+=2){
				if( s.compare(i,2,"Q=") == 0 )
					++cnt_qeq;
				else if( s.compare(i,2,"~~") == 0 ){
					pos_tltl = i;
					break;
				}else
					invalid = true;
			}
			if( pos_tltl != s.length() - 2 ){
				invalid = true;
			}
			if( invalid )
				cout << "NA\n";
			else
				cout << "B\n";
		}else{
			cout <<"NA\n";
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
    int n;
    cin >> n;
    while (n--) {
	string snake;
	cin >> snake;
 
	string s = snake.substr(0, 2);
 
	if (s != ">^" && s != ">\'") {
	    puts("NA");
	    continue;
	}
 
	if (s == ">\'") {
	    int a = 0;
	    int p = 2;
	    bool flg = true;
	    int len = (int)snake.size();
	    for (int i = 2; i < len; i++, p++) {
		if (snake[i] == '#') break;
		else if (snake[i] == '=') a++;
		else {
		    flg = false;
		    break;
		}
	    }
 
	    if (!flg) {
		puts("NA");
		continue;
	    }      
 
	    int b = 0, q = p + 1;
	    for (int i = q; i < len; i++, p++) {
		if (snake[i] == '~') break;
		else if (snake[i] == '=') b++;
		else {
		    flg = false;
		    break;
		}
	    }
 
	    if (!flg || a == 0 || b == 0 || a != b) {
		puts("NA");
		continue;
	    }  
 
	    if (a == b && snake[len-1] == '~') {
		puts("A");
	    } else {
		puts("NA");
	    }
	} else {
	    bool ok = true;
	    int p = 0;
	    string ss = "";
	    int len = (int)snake.size();
 
	    if (len < 6 || len % 2) {
		puts("NA");
		continue;
	    } 
 
	    for (int i = 2; i < len-2; i++, p++) {
		ss += snake[i];
		if (p == 1) {
		    p = -1;
		    if (ss != "Q=") {
			ok = false;
			break;
		    }
		    ss = "";
		}
	    }
 
	    if (snake[len-2] == '~' && snake[len-1] == '~' && ok) {
		puts("B");
	    } else {
		puts("NA");
	    }
	}
    }
    return 0;
}