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 <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <istream>
#include <sstream>
#include <iomanip>
#include <iterator>
#include <climits>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef vector<int> VI;
typedef vector< VI > VVI;

typedef long long LL;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define X first
#define Y second

bool check1(string s) {
	int n = s.size();

	if (n < 6 || s[0] != '>' || s[1] != '\'' || s[n - 1] != '~') {
		return false;
	}

	string sub = s.substr(2, n - 3);
	n = sub.size();

	if (!(n % 2) || sub[n / 2] != '#') {
		return false;
	}

	for (int i = 0; i < n / 2; i++) {
		if (sub[i] != '=' || sub[n - i - 1] != '=') {
			return false;
		}
	}

	return true;
}

bool check2(string s) {
	int n = s.size();

	if (n < 6 || s[0] != '>' || s[1] != '^' || s[n - 1] != '~' || s[n - 2] != '~') {
		return false;
	}

	string sub = s.substr(2, n - 4);
	n = sub.size();

	for (int i = 0; i < n / 2; i++) {
		if (sub[i * 2] != 'Q' || sub[i * 2 + 1] != '=') {
			return false;
		}
	}

	return true;
}

int main(void) {
	int n;
	cin >> n;

	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;

		if (check1(s)) {
			cout << "A" << endl;
		}else if (check2(s)) {
			cout << "B" << endl;
		}else {
			cout << "NA" << 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 j=2;
	  if(s[j]!='Q'||s[j+1]!='=')ans="NA";
	  while(s[j]=='Q'&&s[j+1]=='=')j+=2;
      if(s[j]!='~'||s[j+1]!='~')ans="NA";
	  j+=2;
	  if(j!=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&&h1)ans="NA";
	}else ans="NA";
	cout<<ans<<endl;
  }
}

C++: #include<iostream>
#include<string>
using namespace std;
int main(){
	int n;
	string s;
	cin>>n;
	for(int u=0;u<n;u++){
		cin>>s;
		bool a=true,b=true;
		if(s[0]!='>'||s.size()%2!=0)a=false,b=false;
		if(s[1]!='\''||s[s.size()-1]!='~')a=false;
		if(s[1]!='^'||s[s.size()-1]!='~'||s[s.size()-2]!='~')b=false;
		if(a){
			if(s[s.size()/2]!='#')a=false;
			int count=0;
			for(int i=2;i<s.size()-1;i++)if(s[i]!='=')count++;
			if(count!=1)a=false;
			if(a)cout<<"A"<<endl;
		}
		if(b){
			for(int i=2;i<s.size()-2;i++){
				if(i%2==0&&s[i]!='Q')b=false;
				if(i%2==1&&s[i]!='=')b=false;
			}
			if(b)cout<<"B"<<endl;
		}
		if(!a&&!b)cout<<"NA"<<endl;
	}
	return 0;
}

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

using namespace std;

string get_str(int c, string s)
{
    string ret = "";
    for(int i = 0; i < c; i++)
        ret += s;
    return ret;
}

string check(string s)
{
       string t;
        for(int i = 1; i < 60; i++) {
            t = ">\'" + get_str(i, "=") + "#" + get_str(i, "=") + "~";
            if(s == t) return "A";
        }

        for(int i = 1; i < 60; i++) {
            t = ">^" + get_str(i, "Q=") + "~~";                
            if(s == t) return "B";
        }
        
        return "NA";
}

int main()
{
    int n;
    string snake;
    cin >> n;
    while(n--) {
        char c;
        cin >> snake;
        cout << check(snake) << endl;
    }
}

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] == '#') {
      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;
  for (int i = 2; i < s.size(); i += 2) {
    if (s[i] == 'Q' && s[i + 1] == '=') {
      continue;
    } else if (s[i] == '~' && s[i + 1] == '~') {
      return i + 1 == s.size() - 1;
    } 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() == 2) {
      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<string>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

bool asnake(string s){
	int n=s.size();
	string com=">\'";
	rep(i,(n-4)/2)com+='=';
	com+='#';
	rep(i,(n-4)/2)com+='=';
	com+='~';
	if(s==com)return true;
	else return false;
}

bool bsnake(string s){
	int n=s.size();
	string com=">^";
	rep(i,(n-4)/2)com+="Q=";
	com+="~~";
	if(s==com)return true;
	else return false;
}

int main(){
	int n;
	cin>>n;
	rep(p,n){
		string s;
		cin>>s;
		if(asnake(s))cout<<"A"<<endl;
		else if(bsnake(s))cout<<"B"<<endl;
		else cout<<"NA"<<endl;
	}
	return 0;
}

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

using namespace std;
 
int main(void){
  
  int i,j,n,a;
  string s,ans;
  cin >> n;
  
  for(i = 0 ; i < n ; i++ ){  
    
    cin >> s;
    ans = "NA";
    
    if( s[0] == '>'){
      if( s[1] == '\''){
	a = 2;
	int cnt1 = 0,cnt2 = 0;
	if( s[a] == '='){
	  while( s[a] == '='){
	    a++;
	    cnt1++;
	  }
	  if( s[a] == '#' ){
	    a++;
	    if( s[a] == '='){
	      while( s[a] == '='){
		cnt2++;
		a++;
	      }
	      if( s[a] == '~' && a + 1 == s.size() && cnt1 == cnt2) ans = "A";
	    }
	  }
	}
      }
      else if( s[1] == '^'){
	a = 2;
	if( s[a] == 'Q' && s[a + 1] == '='){
	  while( s[a] == 'Q' && s[a + 1] == '=') a += 2;
	  if( s[a] == '~' && s[a + 1] == '~' && a + 2 == s.size()) ans = "B";
	}
      }
    }
    cout << ans << endl;
  }
  return 0;
}