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 <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;

bool f(string s) {
  int l = 0, r = 0, sz = s.size();
  while (r < sz && s[r] == '=') r++;
  if (r - l == 0) return false;
  if (r < sz && s[r] != '#') return false;
  if (s.substr(r + 1, r - l) != string(r - l, '=')) return false;
  r += 1 + r - l;
  if (r + 1 != sz || s[r] != '~') return false;
  return true;
}

bool g(string s) {
  int sz = s.size();
  if (sz % 2 == 1 || sz == 2) return false;
  for (int i = 0; i < sz; i += 2) {
    if (s.substr(i, 2) == "Q=") continue;
    if (s.substr(i, 2) == "~~" && i + 2 == sz) break;
    return false;
  }
  return true;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string S;
    cin >> S;
    if (S.substr(0, 2) == ">'") {
      cout << (f(S.substr(2)) ? "A" : "NA") << endl;
    } else if (S.substr(0, 2) == ">^") {
      cout << (g(S.substr(2)) ? "B" : "NA") << endl;
    } else {
      cout << "NA" << endl;
    }
  }

  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int main(){
	string s;
	int n;
	cin>>n;
	rep(i,n){
		cin>>s;
		int size=s.size();
		string A="";
		string B="";
		rep(i,size){
			if(i==0){A+=">";B+=">";}
			else if(i==1){A+="'";B+="^";}
			else{
				if(i<size-1){
					if(i==(size-3)/2+2)A+="#";
					else A+="=";
				}else A+="~";
				if(i<size-2){
					if(i%2==0)B+="Q";else B+="=";
				}else{B+="~";}
			}
		}
		if(s==A)cout<<"A"<<endl;
		else if(s==B)cout<<"B"<<endl;
		else cout<<"NA"<<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;

      if(isOutOfStr(ind, str) || !(str[ind] == 'Q' && str[ind+1] == '=')){
        cout << "NA" << endl;
      }else{
        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;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

string solve(string s){
    if(s.substr(0,2)==">'"){
        int eq=0;
        for(int i=2;i<s.size();i++){
            if(s[i]!='=')break;
            else{
                eq++;
            }
        }
        
        if(eq==0)return "NA";
        
        if( s[ (2 + eq -1) +1 ] != '#')return "NA";
        
        if( s.substr( ((2+eq-1)+1) +1 ,eq )   !=   s.substr( 2,eq ) )return "NA";

        if( 2+eq+1+eq+1 == s.size() && s[ (2+eq+1+eq+1) -1 ]=='~')return "A";
        
    }else if(s.substr(0,2)==">^"){
        int c=2;
        while( s[c]=='Q' && s[c+1]=='=' ){
            c+=2;
        }
        
        if( c+1==s.size()-1 && s[c]=='~' && s[c+1]=='~')return "B";
        else return "NA";
        
        
    }else return "NA";

    return "NA";
}


int main(){
    int n;
    cin>>n;
    rep(i,n){
        string s;
        cin>>s;
        
        cout<<solve(s)<<endl;
    }

    return 0;
}

C++: #include<iostream>

using namespace std;

string in;

int isA(){
  if(in.length() < 5) return 0;
  if(in[0] != '>') return 0;
  if(in[1] != '\'') return 0;
  if(!(in[in.length()-1] == '~' && in[in.length()-2] == '=')) return 0;
  int data[2] = {0,0};
  int status = 0;
  for(int i = 2; i < in.length()-1; i++){
    if(in[i] == '#') status = 1;
    if(in[i] == '=') data[status]++;
  }
  if(data[0] && data[0] == data[1]) return 1;
  return 0;
}

int isB(){
  if(in.length() < 5) return 0;
  if(in[0] != '>') return 0;
  if(in[1] != '^') return 0;
  if(!(in[in.length()-1] == '~' && in[in.length()-2] == '~')) return 0;
  for(int i = 2; i < in.length()-2; i+=2){
    if(!(in[i] == 'Q' && in[i+1] == '=')) return 0;
  }
  return 1;
}


int main(){
  int num;
  cin >> num;
  for(int i = 0; i < num; i++){
    cin >> in;
    if(isA()) cout << "A" << endl;
    else if(isB()) cout << "B" << endl;
    else cout << "NA" << endl;
  }
  return 0;
}

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

int main(){
  string str;
  int n; cin >> n;
  while(n--){
    cin >> str;
    int idx = 2;
    int cnt = 0;
    bool flg = true;
    if(str[0] == '>' && str[1] == 39){
      bool used = false;
      while(idx < str.size() && str[idx] == '=') {
	used = true;
	cnt++;
	idx++;
      }
      idx++;
      while(idx < str.size() && str[idx] == '=') {
	cnt--;
	idx++;
      }
      if(cnt == 0 && used) {
	cout << "A" << endl;
	flg = false;
      }
    } else if(str[0] == '>' && str[1] == '^'){
      while(idx+1 < str.size() && str[idx] == 'Q' && str[idx+1] == '='){
	idx += 2;
	cnt++;
      }
      if(cnt > 0 && str[idx] == '~' && str[idx+1] == '~') {
	cout << "B" << endl;
	flg = false;
      }
    }
    if(flg) cout << "NA" << endl;
  }
}

C++: #include<iostream>
#include<string>
using namespace std;
string s;
int main(){
	int n,t,now;
	cin>>n;
	while(n--){
		cin>>s;
		if(s.size()>=6&&s.substr(0,3)==">'="){
			t=0;
			while(s[3+(t++)]=='=');
			if(s[3+t-1]!='#')
				cout<<"NA"<<endl;
			else
			if(4+2*t!=s.size()||s.substr(3+t,t)!=s.substr(2,t)){

				cout<<"NA"<<endl;
			}
			else
			if(s[3+2*t]=='~')
				cout<<"A"<<endl;
		}
		else if(s.size()>=6&&s.substr(0,4)==">^Q="){
			now=4;
			while(s.size()-1-now>=2){
				if(s.substr(now,2)!="Q=")
					break;
				now+=2;
			}
			if(s.size()-now==2&&s.substr(now,2)=="~~"){
				
				cout<<"B"<<endl;
			}
			else {cout<<"NA"<<endl;

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