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;

void solve()
{
	int n;
	cin >> n;
	while(n--)
	{
		string snake;
		cin >> snake;
		if(snake.size() < 4)
		{
			cout << "NA" << endl;
		}
		else if(snake[0] == '>' && snake[1] == '\'' && snake[snake.size() - 1] == '~')
		{
			bool first = true;
			int count1 = 0;
			int count2 = 0;
			for(int i = 2; i < snake.size() - 1; ++i)
			{
				if(snake[i] == '=')
				{
					if(first)
					{
						++count1;
					}
					else
					{
						++count2;
					}
				}
				else if(snake[i] == '#')
				{
					first = false;
				}
			}
			if(count1 == count2 && count1 >= 1)
			{
				cout << "A" << endl;
			}
			else
			{
				cout << "NA" << endl;
			}
		}
		else if(snake[0] == '>' && snake[1] == '^' && snake[snake.size() - 2] == '~' && snake[snake.size() - 1] == '~')
		{
			bool flag = true;
			int count = 0;
			for(int i = 2; i < snake.size() - 2; i += 2)
			{
				if(snake[i] != 'Q' || snake[i + 1] != '=')
				{
					flag = false;
					break;
				}
				else
				{
					++count;
				}
			}
			if(flag && count)
			{
				cout << "B" << endl;
			}
			else
			{
				cout << "NA" << endl;
			}
		}
		else
		{
			cout << "NA" << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  string s;
  cin >> n;
  rep(i,n){
    cin >> s;
    if(s[0] != '>'|| s.size() < 6 || s.size()%2 == 1|| (s[1] != '\'' && s[1] != '^') ){
      cout << "NA"<<endl;
    }
    else if(s[1] == '\''){
      int cnt = find(all(s),'#') - s.begin()-2;
      if(2*cnt+3 == s.size()-1 && s[2*cnt+3] == '~' ){
        cout << "A"<<endl;
      }
      else{
        cout <<"NA"<<endl;
      }
    }
    else{
      bool f = false;
      for(int i = 2; i < s.size(); i+=2){
        if(!(s[i] == 'Q' && s[i+1] == '=')){
          if(s[i] == '~' && s[i+1] == '~' && i == s.size()-2){
            cout << "B"<<endl;
            f = true;
          }
          else{
            break;
          }
        }
      }
      if(!f) cout << "NA"<<endl;
    }
  }
  return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);

int main() {
	int n;
	cin >> n;
	REP(i, n) {
		string s;
		cin >> s;
		string h = s.substr(0, 2);
		if(h == ">'") {
			s += "!";
			if(s.find('#') != string::npos && s.find('=') != string::npos) {
				int pos = s.find('#');
				int size = pos-2;
				REP(i, pos-2) {
					if(s[i+2] == s[i+pos+1] && s[i+2] == '=') {

					} else {
						cout << "NA" << endl;
						goto next;
					}
				}
				if(s[pos+size+1] == '~' && s[pos+size+2] == '!') {
					cout << "A" << endl;
					goto next;
				} else {
					cout << "NA" << endl;
					goto next;
				}
				 
			} else {
				cout << "NA" << endl;
				goto next;
			}			
		} else if(h == ">^") {
			s += "!!";
			int b = 2;
			bool flag = false;
			while(s.substr(b, 2) == "Q=") {
				b += 2;
				flag = true;
			}

			if(s.substr(b, 2) == "~~" && s.substr(b+2, 2) == "!!" && flag) {
				cout << "B" << endl;
				goto next;
			} else {
				cout << "NA" << endl;
				goto next;
			}
		} else {
			cout << "NA" << endl;
			goto next;
		}
		next:;
	}

	return 0;
}

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

/*s[i]ツつゥツづァツ督ッツつカツ閉カツ篠堋つェツ連ツ堕アツつオツづつ「ツづゥツ青板づーツ陛板つキ*/
int seqlen(string &s,int i){
	char c = s[i];
	int ret=0;
	while(s[i++]==c)ret++;
	return ret;
}

int main(){
	int n;
	int flag = 0;
	string s;
	cin >> n;
	while(n--){
		flag = 0;
		cin >> s;
		/*A*/
		if(s[0]=='>' && s[1]==39){
			int x=2;
			x += seqlen(s,x);
			if(s[x]!='#')flag = 1;
			x ++ ;
			if(seqlen(s,2) != seqlen(s,x))flag = 1;
			x += seqlen(s,x);
			if(s[x]!='~')flag = 1;
			if(!flag)flag='A';
		}
		else if(s[0]=='>' && s[1]=='^'){
			int pointa = 0;
			for(int i=2;i<s.length()-2;i++){
				if(i%2){
					if(s[i] != '=')flag = 1;
					else pointa++;
				}else{
					if(s[i] != 'Q')flag = 1;
					else pointa++;
				}
			}
			if(s[s.length()-2] != '~' || s[s.length()-1] != '~')flag = 1;
			if(!flag && pointa>=2)flag='B';
			else flag = 1;
		}
		else {flag = 1;}
		
		if(flag != 1)cout << (char)flag << endl;
		else cout << "NA" << endl;
	}
}