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;

bool isA(string s) {
  int cnt1 = 0;
  for (int i = 2; i < s.size(); ++i) {
    if (s[i] == '=') {
      ++cnt1;
    } else if (s[i] == '#') {
      if (cnt1 == 0) return false;
      break;
    } else {
      return false;
    }
    if (i == s.size() - 1) return false;
  }
  int cnt2 = 0;
  for (int i = cnt1 + 3; i < s.size(); ++i) {
    if (s[i] == '=') {
      ++cnt2;
    } else if (s[i] == '~') {
      return (cnt1 == cnt2 && i == s.size() - 1);
    } else {
      return false;
    }
  }
  return false;
}

bool isB(string s) {
  if (s.size() % 2) return false;
  int cnt = 0;
  for (int i = 2; i < s.size(); i += 2) {
    if (s[i] == 'Q' && s[i + 1] == '=') {
      ++cnt;
    } else if (s[i] == '~' && s[i + 1] == '~') {
      return (i + 1 == s.size() - 1) && cnt;
    } else {
      return false;
    }
  }
  return false;
}

int main() {
  int n; cin >> n;
  vector<string> s(n); for (int i = 0; i < n; ++i) cin >> s[i];
  for (int i = 0; i < n; ++i) {
    if (s[i][0] != '>' || s[i].size() == 3) {
      cout << "NA" << endl;
      continue;
    }
    if (s[i][1] == '\'') {
      cout << ( (isA(s[i])) ? "A" : "NA") << endl;
    } else if (s[i][1] == '^') {
      cout << ( (isB(s[i])) ? "B" : "NA") << endl;
    } else {
      cout << "NA" << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main(void) {
  int n, len, flag=0, flag2=0, cnt1=0, cnt2=0;
  char snake[10000][200];
  cin >> n;
  for(int i = 0 ; i < n ; i++) {
    scanf("%s", snake[i]);
    len = strlen(snake[i]);
    if(snake[i][0] == '>' && snake[i][1] == '\'' /*&& snake[i][2] == '='*/) {
      for(int j = 2 ; j < len - 1 ; j++) {
	if(snake[i][j] == '=' && flag == 0) {cnt1++; flag2 = 1; }
	if(snake[i][j] == '=' && flag == 1) {cnt2++; flag2 = 1; }
	if(snake[i][j] == '#') flag = 1;
	cout << cnt1 << ' ' << cnt2 << endl;
      }
      if(snake[i][len-1] == '~' && cnt1 == cnt2 && flag2 == 1) cout << 'A' << endl;
      else cout << "NA" << endl;
      flag = 0; flag2 = 0;
      cnt1 = 0; cnt2 = 0;
    }
    
    else if(snake[i][0] == '>' && snake[i][1] == '^' /*&& snake[i][2] == 'Q' && snake[i][3] == '='*/) {
      for(int j = 2 ; j < len - 2 ; j++) {
	if(snake[i][j] == 'Q' && snake[i][j+1] == '=') flag = 1;
      }
      if(snake[i][len-2] == '~' && snake[i][len-1] == '~'  && flag == 1) cout << 'B' << endl;
      else cout << "NA" << endl;
    }
    else cout << "NA" << endl;
  }
  return 0;
}

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

string solv(string s){
	int ss = s.size();
	if(s[0] != '>' || s[ss-1] != '~')return "NA";
	if(s[ss-2] == '~'){
		if(s[1] != '^')return "NA";
		for(int i = 0;i < ss-2;i++){
			if(s[i] == 'Q' && s[i+1] == '=')return "B";
		}
	}else{
		int a = 0, f = 0;
		for(int i = 1;i < ss-1;i++){
			if(s[i] == '=' && !f)a++;
			if(s[i] == '#')f = 1;
			if(f && !a)return "NA";
			if(s[i] == '=' && f)a--;
		}
		return a ? "NA" : "A";
	}
	return "NA";
}

int main(){
	int n;cin >> n;
	string s;
	while(n--){
		cin >> s;
		cout << solv(s) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>

using namespace std;

int main(int argc, char **argv)
{
	int n, m, s, t;
	string str, p;
	cin >> m;
	while(m--){
		cin >> str;
		n = str.size();
		
		p = (str[1] == '^')? "B" : "A";
		if(str[n-1] != '~'){
			cout << "NA" << endl;
			continue;
		}
		
		if(p == "A"){
			s = (n+1)/2;
			if(str.substr(2, s-1) != str.substr(s+1, s-1) && str[s] != '#'){
				cout << "NA" << endl;
				continue;
			}
		} else {
			if(str[n-2] != '~'){
				cout << "NA" << endl;
				continue;
			}
			for(int i = 2; i < n-2; i+=2){
				if(str.substr(i, 2) != "Q="){
					n = -1;
					cout << "NA" << endl;
					break;
				}
			}
			if(n == -1){
				continue;
			}
		}
		cout << p << endl;
	}
	return 0;
}

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

int main(){
  char buf[210];
  int bi,n,eqc,ti;

  cin >> n;

  for (int i=0;i<n;i++){
    bi=0;
    cin >> buf;
    if (buf[bi++]!='>') {
      cout << "NA" << endl;
      continue;
    }
    if (buf[bi]=='\''){
      bi++;
      eqc=0;
      while (buf[bi+eqc]=='=')eqc++;
      if (buf[bi+eqc]!='#'){
	cout << "NA" << endl;
	continue;
      }
      bi += eqc+1;
      for (ti=0;ti<eqc;ti++)
	if (buf[bi+ti]!='=')break;
      if (ti <eqc){
	cout << "NA" << endl;
	continue;
      }
      bi+=ti;
      if (buf[bi]=='~'&&buf[bi+1]=='\0')cout << "A" << endl;
      else cout << "NA" << endl;
    }else if(buf[bi]=='^'){
      bi++;
      //cout <<"dbg "<< buf[bi] << buf[bi+1] << endl;
      while (buf[bi]=='Q'&&buf[bi+1]=='=')bi +=2;
      //cout <<"dbg "<< buf[bi] << buf[bi+1] << endl;
      if (buf[bi]=='~'&&buf[bi+1]=='~'){
	if (buf[bi+2]=='\0')cout << "B" << endl;
	else cout << "NA" << endl;
      }else cout << "NA" << endl;
    }else{
      cout << "NA" << endl;
      continue;
    }
  }
  return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <fstream>
#include <sstream>
#include <math.h>
#include <queue>
#include <stack>
#include <math.h>
#include <string.h>



using namespace std;

bool hebi_a(string hebi){
    if(!(hebi[0]=='>' && hebi[1]=='\''))return false;
    if(hebi[2]!='=')return false;
    
    int now=3;
    int count=1;
    
    while(1){
        if(hebi[now]=='#'){
            now++;
            break;
        }else if(hebi[now]=='='){
            now++;
            count++;
        }else{
            return false;
        }
    }
    
    while(now!=hebi.length()){
        if(hebi[now]=='='){
            now++;
            count--;
        }else if(hebi[now]=='~'){
            now++;
            break;
        }else{
            return false;
        }
    }
    if(now==hebi.length()&& count==0)return true;
    else return false;
}


bool hebi_b(string hebi){
    if(!(hebi[0]=='>' && hebi[1]=='^'))return false;
    if(hebi[2]!='Q')return false;
    if(hebi[3]!='=')return false;
    int now=4;
    while(1){
        if(hebi[now]!='Q'){
            break;
        }else{
            now++;
        }
        
        if(now==hebi.length())return false;
        
        if(hebi[now]!='='){
            return false;
        }else{
            now++;
        }
        
        if(now==hebi.length())return false;
    }
    if(now==hebi.length())return false;
    
    if(hebi[now]!='~')return false;
    now++;
    if(now==hebi.length())return false;
    if(hebi[now]!='~')return false;
    now++;
    if(now==hebi.length())return true;
    else return false;

}


int main(){
    int n;
    cin>>n;
    for(int roop=0; roop<n; roop++){
        string hebi;
        cin>>hebi;
        if(hebi_a(hebi))cout<<"A"<<endl;
        else if(hebi_b(hebi))cout<<"B"<<endl;
        else cout<<"NA"<<endl;
        
    }
}