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;


bool isA(string &s){
	if(s.size()<6)return false;
	if(s[0]!='>'||s[1]!='\'')return false;
	int i=2;
	while(s[i]=='=')i++;
	int cnt=i-2;
	if(cnt==0||s[i]!='#')return false;
	for(i++;i<s.size()&&s[i]=='=';i++)cnt--;
	if(cnt||i==s.size()||s[i]!='~')return false;
	return i+1==s.size();
}
bool isB(string &s){
	if(s.size()<6)return false;
	if(s[0]!='>'||s[1]!='^')return false;
	int i=2;
	for(;i<s.size()&&s[i]=='Q';){
		if(i+1<s.size()&&s[i+1]=='=')i+=2;
		else break;
	}
	if(i==2)return false;
	if(i+2==s.size()&&s[i]=='~'&&s[i+1]=='~')return true;
	return false;
}


int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		string s;
		cin>>s;
		if(isA(s))cout<<"A\n";
		else if(isB(s))cout<<"B\n";
		else  cout<<"NA\n";
	}
	return 0;
}

C++: #include<iostream>
#include<string>
 
using namespace std;
 
int check(string str)
{
  if(str[0] != '>'){ return -1; }
  if(str[1] != '\'' && str[1] != '^'){ return -1; }
  if(str[2] == '='){
    if(str[str.size()-1] != '~' || str[str.size()-2] != '='){ return -1; }
    int i=3,cnt1=1,cnt2=0;
    while(str[i] != '#'){
      if(i >= str.size()){ return -1; }
      if(str[i] != '=')  { return -1; }
      i++;
      cnt1++;
    }
    i++;
    while(str[i] == '='){
      if(i >= str.size()){ return -1; }
      i++;
      cnt2++;
    }
    if(cnt1 == cnt2){ return 0; }
    else{ return -1; }
  }
  else if(str[2] == 'Q'){
    if(str[str.size()-1] != '~' ||
       str[str.size()-2] != '~' ||
       str[str.size()-3] != '='){ return -1; }
    int i = 3;
    while(1){
      if(i >= str.size()-2){ return 1; }
      if(i&1){
        if(str[i] != '='){ return -1; }
      }
      else{
        if(str[i] != 'Q'){ return -1; }
      }
      i++;
    }
    return 1;
  }
  else{ return -1; }
  
}
 
int main(void)
{
 
  int n;
  string str;
 
  cin >> n;
 
  while(n-->0){
    cin >> str;
    int ans = check(str);
    if(ans <  0){ cout << "NA" << endl; }
    if(ans == 0){ cout << "A"  << endl; }
    if(ans == 1){ cout << "B"  << endl; }
  }
 
  return 0;
}

C++: #include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define fr first
#define sc second
#define mp make_pair
#define foreach(c, it) for(typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)

typedef pair< int, int > iP;
typedef pair< iP, int > iiP;

int main(){
  
  int N;
  cin >> N;
  while(N--) {
    string s; cin >> s;
    if(s[0] != '>') {
      cout << "NA" << endl;
      continue;
    }
    int select = 0;
    if(s[1] == '\'') {
      int cnt = 0;
      if(!(s.size() % 2)) {
        for(int i = 2; i < s.size() / 2; i++) {
          if(s[i] != '=') {
            select = -1;
            break;
          }
          cnt++;
        }
        for(int i = s.size() / 2 + 1; i < s.size() - 1; i++) {
          if(s[i] != '=') {
            select = -1;
            break;
          }
          cnt--;
        }
      } else select = -1;
      if(!cnt && ~select && s[s.size() / 2] == '#' && s[s.size() - 1] == '~')
        cout << "A" << endl;
      else cout << "NA" << endl; 
    } else if(s[1] == '^') {
      if(!(s.size() % 2)) {
        for(int i = 2; i < s.size() - 2; i++) {
          if((!(i % 2) && s[i] != 'Q') || (i % 2 && s[i] != '=')) {
            select = -1;
            break;
          }
        }
        if(~select && s[s.size() - 1] == s[s.size() - 2] && s[s.size() - 1] == '~') cout << "B" << endl;
        else cout << "NA" << endl;
      } else cout << "NA" << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}

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

using namespace std;

int main(void){

  string s;
  int n;
  cin >> n;

  while(n--){

    cin >> s;
    if( s[1] == '\'' ){
      int cnt1 = 0;
      int i;
      for( i = 2 ; s[i] == '=' ; i++ ){
	cnt1++;
      }
      if( s[i] != '#' ){
	cout << "NA" << endl;
	goto skip;
      }
      else i++;
      int cnt2 = 0;
      for( ; s[i] == '=' ; i++ ){
	cnt2++;
      }
      if( cnt1 == cnt2 && s[i] == '~' )
	cout << "A" << endl;
      else cout << "NA" << endl;
    }
    else if( s[1] == '^' ){
      int x = 2;
      while( s[x] == 'Q' && s[x + 1] == '=' ) x += 2;
      if(s[x] == '~' && s[x + 1] == '~' && x + 2 == s.size() ){
	cout << "B" << endl;
      }
    }
    else cout << "NA" << endl;
  skip:;
  }
  return 0;
}