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++: #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 < 3 || 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++: #include <bits/stdc++.h>
using namespace std;

bool isOutOfStr(int ind, string str){
  return ind >= str.length();
}

int main(void){
  int n;
  cin >> n;
  while(n--){
    string str; cin >> str;
    if(!isOutOfStr(1, str) &&str[0] == '>' && str[1] == '\''){
      int sharp_cnt = 0;
      int ind;
      ind = 2;

      if(!isOutOfStr(ind, str) && str[ind] != '='){
        cout << "NA" << endl;
        continue;
      }

      for(;;ind++){
        if(isOutOfStr(ind, str) || str[ind] != '='){
          break;
        }
        sharp_cnt++;
      }

      if(isOutOfStr(ind, str) || str[ind] != '#'){
        cout << "NA" << endl;
        continue;
      }
      ind++;

      bool flg = true;
      for(int i=0; i < sharp_cnt; i++, ind++){
        if(isOutOfStr(ind, str) || str[ind] != '='){
          cout << "NA" << endl;
          flg = false;
          break;
        }
      }
      if(!flg) continue;
      
      if(!isOutOfStr(ind, str) && str[ind] == '~' && ind == str.length() - 1)
        cout << "A" << endl;
      else{
        cout << "NA" << endl;
      }



    }else if(str[0] == '>' && str[1] == '^'){
      int ind;
      ind+=2;

      int flg = true;
      for(;;ind+=2){
        if(isOutOfStr(ind, str) || !(str[ind] == 'Q' && str[ind+1] == '=')){
          break;
        }
      }

      if(!isOutOfStr(ind, str) && !isOutOfStr(ind+1, str) && str[ind] == '~' && str[ind+1] == '~' && ind+1 == str.length() - 1){
        cout << "B" << endl;
      }else{
        cout << "NA" << endl;
      }
    }else{
      cout << "NA" << endl;
    }
  }
  return 0;
}

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

bool checkA(char* s){
	if(s[0] != '>' || s[1] != '\'' || s[2] != '='){
		return false;
	}
	int slen = 0;
	while(s[slen+2] == '='){
		slen++;
	}
	if(s[slen+2] != '#'){
		return false;
	}
	for(int i = slen+3; i < slen*2+3;i++){
		if(s[i] != '='){
			return false;
		}
	}
	return (s[slen*2+3] == '~' && s[slen*2+4] == '\0');
}

bool checkB(char* s){
	if(s[0] != '>' || s[1] != '^' || s[2] != 'Q' || s[3] != '='){
		return false;
	}
	int slen = 0;
	while(s[slen*2+2] == 'Q' && s[slen*2+3] == '='){
		slen++;
	}
	return (s[slen*2+2] == '~' && s[slen*2+3] == '~' && s[slen*2+4] == '\0');
}

int main(){
	int n;
	char S[1024];

	scanf("%d",&n);
	for(int step = 0; step < n; step++){
		scanf("%s",S);
		if(checkA(S)){
			printf("A\n");
		}
		else if(checkB(S)){
			printf("B\n");
		}
		else{
			printf("NA\n");
		}
	}

	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

static inline int in(){ int x; scanf("%d", &x); return x; }
vector<string> split(const string& s, char delim)
{
  stringstream ss(s);
  string token;
  vector<string> res;
  while (getline(ss, token, delim)){
    res.push_back(token);
  }
  return res;
}

int main()
{
  int n = in();
  for (int i = 0; i < n; i++){
    string snake;
    cin >> snake;
    string head = snake.substr(0, 2);
    bool ok = true;
    if (head == ">'"){
      vector<string> ts = split(snake.substr(2), '#');
      if (ts.size() == 2 && ts[0] + '~' == ts[1]){
        puts("A");
      }
      else ok = false;
    }
    else if (head == ">^"){
      vector<string> ts = split(snake.substr(2), '=');
      for (int j = 0; j < ts.size() - 1; j++){
        if (ts[j] != "Q"){
          ok = false;
        }
      }
      if (ok) ok = ts[ts.size() - 1] == "~~";
      if (ok) puts("B");
    }
    else ok = false;
    if (!ok) puts("NA");
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<sstream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

int main(){
	int n;

		for(int x=0;x<n;++x){
			string line;
			cin>>line;
			string sol = "NA";
			if(line.find('#')!=string::npos){
				bool flag = false;
				string a = "";
				string b = "";
				for(int x=2;x<line.size()-1;++x){
					if(line[x] == '#'){
						flag = true;
						continue;
					}
					if(!flag){
						a += line[x];
					}else{
						b += line[x];
					}
				}
				if(line.size()> 3 && line[0]=='>' && line[1]=='\'' && line[line.size()-1]=='~' && a==b)sol = "A";
			}else{
				bool flag = true;
				for(int x=2;line.size()>4 && x<(line.size()-3);x+=2){
				
					//cout<<"OK"<<endl;
					if(line[x] !='Q' || line[x+1] != '=')flag = false;
				}
				if(line.size()>4 && line[0]=='>' && line[1]=='^' && line[line.size()-1]=='~' && line[line.size()-2]=='~' && flag)sol = "B";
			}
			cout<<sol<<endl;
		}
	
	return 0;
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#define pb(in,tmp) in.push_back(tmp)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) loop(i,0,b)
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
using namespace std;
int main(){
  int n;
  cin>>n;
  rep(i,n){
    string ans;
    string s;
	cin>>s;
	if(s[0]!='>'){cout<<"NA"<<endl;continue;}
	if(s[1]=='^'){
	  ans='B';
	  int h=2;
	  while(s[h++]=='Q'&&s[h++]=='=');
	  h--;
      if(s[h++]!='~'||s[h++]!='~')ans="NA";
	  if(h!=s.size())ans="NA";
	}else if(s[1]=='\''){
	  ans='A';
	  int h1=0,h2=0;
	  int j=1;
	  while(s[++j]=='=')h1++;
	  if(s[j]!='#')ans="NA";
	  while(s[++j]=='=')h2++;
	  if(s[j]!='~')ans="NA";
	  if(++j!=s.size())ans="NA";
	  if(h1!=h2)ans="NA";
	}
	cout<<ans<<endl;
  }
}