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 <algorithm>
#include <string>
using namespace std;

int main() {
  int Tc; string s;
  while(cin>>Tc) {
    while(Tc--) {
      cin >> s;
      int const N = s.size();
      bool NA = 0;
      bool isA;
      if(s[0] != '>') NA = 1;
      if(s[1] == '\'') {
	if(s[2] != '=') NA = 1;
	for(int i=2; i<(N-3)/2+2; i++) if(s[i]!='=') NA=1;
	if(s.substr(2, (N-3)/2) + "~" != s.substr(3+(N-3)/2)) NA = 1;
	if(s[(N-3)/2+2] != '#') NA = 1;
	isA = 1;
      }
      else if(s[1] == '^') {
	int cnt;
	if( !(s[2] == 'Q' && s[3] == '=') ) NA = 1;
	for(cnt=2; cnt<N-2; cnt+=2) {
	  if(!(s[cnt] == 'Q' && s[cnt+1] == '=')) NA = 1;
	}
	if(!(s[cnt] == '~' && s[cnt+1] == '~')) NA = 1;
	if(cnt+2 != N) NA = 1;
	isA = 0;
      }
      else {
	NA = 1;
      }
      
      if(NA) cout << "NA" << endl;
      else if(isA) cout << "A" << endl;
      else cout << "B" << endl;
    }
  }
  
  return 0;
}

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(sz<6){
			ans=-1;
		}else 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>
#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)
				if(snake.at(j) == '#'){
					j++;
					for(;snake.at(j)=='=';j++) equal--;
					if(equal == 0){
						if(snake.at(j)=='~' && j+1 == snake.size()) cout << 'A' << endl;
						else a();
					}
					else a();
				}
				else a();
			else a();
		}
		else if((aruyon = snake.find(">^"))==0){
			int j;
			bool dou = false;
			if((aruyon = snake.find("~~"))==snake.size() -2){
				if(aruyon%2==0){
					for(j=2;j<aruyon;j++){
						dou = false;
						if(snake.at(j) == 'Q') j++;
						else{j=0; break;}	
						if(snake.at(j) != '='){a(); break;}
						else dou == true;
					}
					if(j == aruyon && dou == true) cout << 'B' << endl;
					else a();
				}
				else a();
			}
			else a();
				
		}
		else
			a();
	}

  return 0;
}

C++: #include <cstdio>
#include <utility>
#include <cctype>
#include <string>
#include <regex>
using namespace std;

#ifdef __linux
#define _U(s) s##_unlocked
#else
#define _U(s) _##s##_nolock
#define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
#define gcu _U(getchar)
#define pcu _U(putchar)
#define _DEF(r, n, ...) inline r n(__VA_ARGS__) noexcept
#define _T template <typename T>
#define _HT template <typename H,typename... T>
#define _OP(t) inline operator t()
struct _in {
#ifdef _GLIBCXX_STRING
	_OP(string){string s;for(char c;c=gcu(),c!=' '&&c!='\n'&&c!=EOF;)s+=c;return s;}
#define _S
#endif
	_OP(char){char c=gcu();gcu();return c;}
	_OP(double){double d; scanf("%lf",&d); gcu();return d;}
	_T _OP(T){T n{},m{1},c;while(isspace(c = gcu()));if(c=='-')m=-1,c=gcu();do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
} in;
#define _SCAN(...) _DEF(bool,scan,__VA_ARGS__)
#ifdef _S
_SCAN(string &o) {int c{gcu()};if(c==EOF)return false;else{ungetc(c,stdin);string t=move(in);o=t;return true;}}
#endif
_T _SCAN(T &o) {int c{gcu()};return c==EOF?false:(ungetc(c,stdin),o=in,true);}
_HT _SCAN(H &h,T&&... t){return scan(h)&&scan(t...);}
#define _OUT(...) _DEF(void,out,__VA_ARGS__)
#define _OUTL(...) _DEF(void,outl,__VA_ARGS__)
_OUT(bool b){pcu('0'+b);}
_OUT(const char *s){while(*s)pcu(*s++);}
_OUT(char c){pcu(c);}
#ifdef _S
_OUT(string &s){for(char c:s)pcu(c);}
#endif
_T _OUT(T n){static char b[20];char *p=b;T m=n<0?pcu('-'),-1:1;
	if(!n)*p++='0';else while(n)*p++=(char)(n%10*m+'0'),n/=10;while(p!=b)pcu(*--p);}
_OUTL(){out('\n');}
#ifdef _GLIBCXX_VECTOR
	_T _OUT(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);}
#endif
	_HT _OUT(H &&h, T... t){out(h);out(t...);}
	template <typename... T> _OUTL(T... t){out(t...);outl();}
	struct range{
		int e,b=0,s=1;range(int _b,int _e,int _s):e(_e),b(_b),s(_s){} range(int _b,int _e): e(_e), b(_b){} range(int _e):e(_e){}
		struct it{int v,s; it(int _v,int _s):v(_v),s(_s){} operator int()const{return v;} operator int&(){return v;}int operator*()const{return v;}
			it& operator++(){v+=s;return *this;} }; it begin(){return {b,s};} it end(){return {e,s};}};
#define times(i,n) for(int i=n;i;i--)

int main() {
	regex a {">'(=+)#(=+)\\~"} ,b {">\\^(Q=)+\\~\\~"};
	smatch m;
	times(i, in) {
		string s = in;
		outl(regex_match(s, m, a) ? m[1].length() == m[2].length() ? "A" : "NA" :
			 regex_match(s, b) ? "B" : "NA");
	}
}

C++: #include <iostream>
#include <string>
using namespace std;
 
 string vbString(int Number,string Character){
         string res="";
		         for(int i=0;i<Number;i++) res+=Character;
				         return res;
						 }
						 string checksnake(string s){
						         string t;
								         for(int i=1;i<60;i++){
										                 t=">\'"+vbString(i,"=")+"#"+vbString(i,"=")+"~";
														                 if(t==s) return "A";
																		         }
																				  
																				          for(int i=1;i<60;i++){
																						                  t=">^"+vbString(i,"Q=")+"~~";
																										                  if(t==s) return "B";
																														          }
																																          return "NA";
																																		   
																																		   }
																																		   int main(){
																																		           string s;
																																				           int n,i=0;
																																						           cin >> n;
																																								           for(i=0;i<n;i++){
																																										                   cin >> s;
																																														                   cout << checksnake(s) << endl;
																																																		           }
																																																				           return 0;
																																																						   }