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 stateA,stateB;
int anum;

void inA(char arg){
	//cout << stateA << endl;
	if(stateA == 0){
		if(arg == '>'){
			stateA = 1;
		}
	}else if(stateA == 1){
		if(arg == '\''){
			stateA = 2;
		}else{
			stateA = -1;
		}
	}else if(stateA == 2){
		if(arg == '='){
			stateA = 3;
			anum++;
		}else{
			stateA = -1;
		}
	}else if(stateA == 3){
		if(arg == '='){
			anum++;
		}else if(arg == '#'){
			stateA = 4;
		}else{
			stateA = -1;
		}
	}else if(stateA == 4){
		if(arg == '='){
			anum--;
		}else{
			stateA = -1;
		}
		if(anum==0){
			stateA = 5;
		}
	}else if(stateA == 5){
		if(arg == '~'){
			stateA = 6;
		}else{
			stateA = -1;
		}
	}else if(stateA == 6){
		stateA = -1;
	}
}

void inB(char arg){
	if(stateB == 0){
		if(arg == '>'){
			stateB = 1;
		}else{
			stateB = -1;
		}
	}else if(stateB == 1){
		if(arg == '^'){
			stateB = 2;
		}else{
			stateB = -1;
		}
	}else if(stateB == 2){
		if(arg == 'Q'){
			stateB = 3;
		}else{
			stateB = -1;
		}
	}else if(stateB == 3){
		if(arg == '='){
			stateB = 4;
		}else{
			stateB = -1;
		}
	}else if(stateB == 4){
		if(arg == 'Q'){
			stateB = 5;
		}else if(arg == '~'){
			stateB = 6;
		}else{
			stateB = -1;
		}
	}else if(stateB == 5){
		if(arg == '='){
			stateB = 4;
		}else{
			stateB = -1;
		}
	}else if(stateB == 6){
		if(arg == '~'){
			stateB = 7;
		}else{
			stateB = -1;
		}
	}else if(stateB == 7){
		stateB = -1;
	}
}

main(){
	int n;
	string str;
	
	cin >> n;
	cin.ignore();
	while(n && getline(cin,str)){
		stateA=0;
		stateB=0;
		anum=0;
		bool A=false, B=false;
		
		int l = str.size();
		for(int i=0;i<l;i++){
			inA(str[i]);
			inB(str[i]);
		}
		if(stateA == 6){
			cout << "A" << endl;
		}else if(stateB == 7){
			cout << "B" << endl;
		}else{
			cout << "NA" << endl;
		}
		
		n--;
	}
}

C++: #define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;

int Judge(string &s){
	if(s[0] != '>') return -1;
	if(s.size() < 2) return -1;
	if(s[1] == '\''){
		if(s[s.size()-1] != '~') return -1;
		int len = sz(s) - 3;
		if(len % 2 == 0) return -1;
		FOR(i,(len-1)/2){
			if(s[2+i] != '=' || s[2+len/2+1] != '=') return -1;
		}
		if(s[2+len/2] != '#') return -1;
		return 0;
	} else if(s[1] == '^'){
		if(s[s.size()-1] != '~' || s[s.size()-2] != '~')return -1;
		int len = sz(s) - 4;
		if(len < 2 || len % 2 != 0) return -1;
		FOR(i,len/2){
			if(s[2+2*i] != 'Q' || s[2+2*i+1] != '=') return -1;
		}
			return 1;
	} else {
		return -1;
	}
}

int main(){
	int n; cin>>n;
	string out[] = {"NA","A","B"};
	FOR(i,n){
		string s; cin>>s;
		int a = Judge(s);
		cout << out[a+1] << endl;
	}
	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()
{
	vector<string> a, b;
	for (int i = 1; i <= 100; i++)
	{
		string s = ">\'";
		for (int j = 0; j < i; j++)s += "=";
		s += "#";
		for (int j = 0; j < i; j++)s += "=";
		s += "~";
		a.push_back(s);
	}
	for (int i = 1; i <= 100; i++)
	{
		string s = ">^";
		for (int j = 0; j < i; j++)s += "Q=";
		s += "~~";
		b.push_back(s);
	}
	
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		string s;
		cin >> s;
		if (find(ALL(a), s)!=a.end())cout << "A" << endl;
		else if (find(ALL(b), s) != b.end())cout << "B" << endl;
		else cout << "NA" << endl;
	}

}

C++: #include<iostream>
using namespace std;
int main()
{
  int n;
  for(cin>>n;n--;)
    {
      string s;
      cin>>s;
      string ans="NA";

	if(s.substr(0,2)==">'" &&s[s.size()-1]=='~')
	{
	  string tmp=s.substr(2,s.size()-3);
	  //	  cout<<tmp<<endl;
	  if(tmp.size()%2!=0 && tmp[tmp.size()/2]=='#' &&tmp.substr(0,tmp.size()/2)==tmp.substr(tmp.size()/2+1,tmp.size()/2) && tmp.substr(0,tmp.size()/2)==*(new string(tmp.size()/2,'=')))
	    {
	      
	      ans="A";
	    }
	}
      else if(s.substr(0,2)==">^" && s.substr(s.size()-2,2)=="~~")
	    {
	      string tmp=s.substr(2,s.size()-4);
	      //	      cout<<tmp<<endl;
	      while(tmp.size()>0)
		{
		  if(tmp.substr(0,2)=="Q=")
		      tmp=tmp.substr(2,tmp.size()-2);
		  else
		    break;
		}
	      if(tmp.size()==0)
		{
		  ans="B";
		}
	    }
	  cout<<ans<<endl;
    }
}

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;
				else if (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;
			bool b = false;
			while (true) {
				if (check(s, n + 1))goto exit;
				else {
					if (s[n] == 'Q'&&s[n + 1] == '=') { n += 2; b = true; }
					else if (s[n] == '~'&&s[n + 1] == '~') {
						if (b) {
							cout << "B\n";
							return;
						}
						else goto exit;
					}
					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();
}