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 main(){
	int n;
	string s;
	cin >> n;
	for(int i = 0;i < n;i++){
		cin >> s;
		if(s.substr(0,2) == ">\'"){
			if(s[s.length() - 1] != '~') cout << "NA" << endl;
			else {
				int cnt = 0,pos = 2;
				bool flag = true;
				for(;;pos++){
					if(s[pos] == '#') break;
					if(s[pos] == '=') cnt++;
					else {
						flag = false;
						break;
					}
				}
				if(!flag) cout << "NA" << endl;
				else{
					pos++;
					for(int j = 0;j < cnt;j++){
						if(s[pos + j] != '='){
							flag = false;
							break;
						}
					}
					if(s[pos + cnt] != '~') flag = false;
					if(!flag) cout << "NA" << endl;
					else cout << "A" << endl;
				}
			}
		}else if(s.substr(0,2) == ">^"){
			if(s.length() % 2) cout << "NA" << endl;
			else {
				bool flag = true;
				for(int i = 1;i < s.length() / 2;i++){
					if(i != s.length() / 2 - 1 && s.substr(i * 2,2) != "Q=") flag = false;
					if(i == s.length() / 2 - 1 && s.substr(i * 2,2) != "~~") flag = false;
				}
				if(flag) cout << "B" << endl;
				else cout << "NA" << endl;
			}
		}else cout << "NA" << endl;
	}
	return 0;
}

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

bool A(string body) {
  if(int a = body.find("#") != -1 && body.size() % 2 == 0 && body.substr(body.size()-1,1) == "~") {
    for(int i=0;i<a;i++) {
      if(body.substr(i,1) == body.substr(body.size()-2-i,1) && body.substr(i,1) == "=") {

        continue;
      }
      else return false;
    }
  }
  else return false;
  return true;
}

bool B(string body) {

  if(body.substr(body.size()-2,2) == "~~" && body.size() % 2 == 0) {
    for(unsigned int i=0;i<body.size()-2;i+=2) {
      if(body.substr(i,2) == "Q=") {
        continue;
      }
      else return false;
    }
  }
  else return false;
  return true;
}

int main() {
  int n;
  string str;
  scanf("%d\n",&n);
  for(int i=0;i<n;i++) {
    getline(cin,str);
    if(str.substr(0,2) == ">'") {
      if(A(str.substr(2))) cout << "A" << endl;
      else cout << "NA" << endl;
    }
    else if(str.substr(0,2) == ">^") {
      if(B(str.substr(2))) cout << "B" << endl;
      else cout << "NA" << endl;
    }
    else cout << "NA" << endl;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <map> 
#include <sstream>
#include <cstdio>
#include <complex>
#include <cstring>
using namespace std;
 
const int MAX= 10000100;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
#define ll long long int
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a)
int GCD(int a, int b) {if(!b) return a; return gcd(b, a%b);}
int lcm(int a, int b) {return a*b / gcd(a, b);}

bool A(string s){
  if(s.size()%2 != 0)return false;
  if(s[0] != '>')return false;
  if(s[1] != '\'')return false;
  if(s[s.size()-1] != '~')return false;
  if(s[s.size()/2] != '#')return false;
  else s[s.size()/2] = '=';
  loop(i,2,s.size()-1){
    if(s[i] != '=')return false;
  }
  return true;
}
bool B(string s){
  if(s[0] != '>')return false;
  if(s[1] != '^')return false;
  loop(i,2,s.size()-2){
    if(i%2){
      if(s[i] != '=')return false;
    }else{
      if(s[i] != 'Q')return false;
    }
  }
  if(s[s.size()-2] != '~')return false;
  if(s[s.size()-1] != '~')return false;
  return true;
}
int main(void){
  int n;
  cin>>n;
  rep(i,n){
    string s;
    cin>>s;
    if(A(s))cout<<"A";
    else if(B(s))cout<<"B";
    else cout<<"NA";
    cout<<endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++) 
#define ALL(n) begin(n),end(n)
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
bool check_A( string S )
{
    auto sz = S.size();
    if( S.find( ">'" ) == 0 && S.find( "~" ) == sz - 1 && sz >= 6 )
    {
        S = S.substr( 2, sz - 3 );
        sz = S.size();
        if( S.find_first_not_of( "#=", 0 ) != string::npos )
        {
            return 0;
        }
        auto pos = S.find( "#" );
        if( S.find( "#", pos + 1 ) != string::npos )
        {
            return 0;
        }
        return S[ sz / 2 ] == '#' ? 1 : 0;
    }
    return 0;
}
bool check_B( string S )
{
    auto sz = S.size();
    if( S.find( ">^" ) == 0 && S.find( "~~" ) == sz - 2 && sz >= 6 )
    {
        S = S.substr( 2, sz - 4 );
        sz = S.size();
        if( sz % 2 != 0 )
        {
            return 0;
        }
        for( int i = 0; i < sz; i += 2 )
        {
            if( S[ i ] != 'Q' && S[ i + 1 ] != '=' )
            {
                return 0;
            }
        }
        return 1;
    }
    return 0;
}
int main()
{
    int N;
    cin >> N;
    
    vector<string> res{ "NA", "A", "B" };
    REP( i, N )
    {
        string S;
        cin >> S;
        cout << res[ check_A( S ) ? 1 : check_B( S ) ? 2 : 0 ] << endl;
    }
    return 0;
}

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

int main(void){
	int n;
	cin >> n;
	for(int i=0;i<n;i++){
		string str;
		cin >> str;
		if(str.size()<2)cout << "NA" << endl;
		else if(str[0]=='>' && str[1]=='\''){
			int j=2;
			int l[2]={0,0};
			int ck=0;
			bool ch[2]={false,true};
			while(j<str.size()){
				if(str[j]=='=')l[ck]++;
				else if(str[j]=='#' && l[0]>0)ch[0]=true,ck=1;
				else if(str[j]=='~')break;
				else ch[1]=false;
				j++;
			}
			if(ch[0]==true && ch[1]==true && l[0]==l[1])cout << "A" << endl;
			else cout << "NA" << endl;
		}else if(str[0]=='>' && str[1]=='^'){
			if(str.size()%2==1)cout << "NA" << endl;
			else{
				bool ch=true;
				for(int j=2;j<str.size()-2;j+=2){
					if(str[j]!='Q' || str[j+1]!='=')ch=false;
				}
				if(str[str.size()-2]!='~' || str[str.size()-1]!='~')ch=false;
				if(ch==true)cout << "B" << endl;
				else cout << "NA" << endl;
			}
		}else cout << "NA" << endl;
	}
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;

int main(){

	int n;
	cin>>n;
	for(int i = 0; i < n; i++){
		string str;
		cin>>str;
		if(str.size()<=4)
			cout<<"NA"<<endl;
		if(str.substr(0,2)==">'"&&str[str.size()-1]=='~'&&str.size()%2==0){
			string ss=str.substr(2,str.size()-3);
			bool f=true;
			for(int j = 0; j < ss.size(); j++){
				if(j==ss.size()/2){
					if(ss[j]!='#'){
						f=false;
						break;
					}
				}
				else{
					if(ss[j]!='='){
						f=false;
						break;
					}
				}
			}
			if(f)
				cout<<"A"<<endl;
			else
				cout<<"NA"<<endl;
		}
		else if(str.substr(0,2)==">^"&&str.substr(str.size()-2,2)=="~~"&&str.size()%2==0){
			string ss=str.substr(2,str.size()-4);
			bool f=true;
			for(int j = 0; j < ss.size(); j+=2){
				if(ss.substr(j,2)!="Q="){
					f=false;
					break;
				}
			}
			if(f)
				cout<<"B"<<endl;
			else
				cout<<"NA"<<endl;

		}
		else
			cout<<"NA"<<endl;
	}

	return 0;
}