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 <string>
using namespace std;

void a(){
	cout << "NA" << endl;
	return;
}

int main(){
	int n;
	vector<string> sample;
	string aaa;

	cin >> n;
	for(int i=0;i<n;i++){
		cin >> aaa;
		sample.push_back(aaa);
	}

	for(int i=0;i<n;i++){
		string snake = sample[i];
		string::size_type aruyon;

		if((aruyon = snake.find(">'"))==0){
			int equal=0;
			int j;
			for(j=2;snake.at(j)=='=';j++) equal++;
			if(equal > 0)
				if(snake.at(j) == '#'){
					j++;
					for(;snake.at(j)=='=';j++) equal--;
					if(equal == 0){
						if(snake.at(j)=='~' && j+1 == snake.size()) cout << 'A' << endl;
						else a();
					}
					else a();
				}
				else a();
			else a();
		}
		else if((aruyon = snake.find(">^"))==0){
			int j;
			bool dou = false;
			if((aruyon = snake.find("~~"))==snake.size() -2){
				if(aruyon%2==0){
					for(j=2;j<aruyon;j++){
						dou = false;
						if(snake.at(j) == 'Q') j++;
						else{j=0; break;}	
						if(snake.at(j) != '='){a(); break;}
						else dou == true;
					}
					if(dou) cout << 'B' << endl;
					else a();
				}
				else a();
			}
			else a();
				
		}
		else
			a();
	}

  return 0;
}

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

typedef string::iterator S;
string ss;

string snakeA(S it){
    int count = 0;
    for(;it!=ss.end()&&*it=='=';it++,count++);
    if(it==ss.end()||*it!='#') return "NA";
    it++;
    for(;it!=ss.end()&&*it=='=';it++,count--);
    if(it==ss.end()||*it!='~') return "NA";
    it++;
    if(*it||count) return "NA";
    return "A";
}

string snakeB(S it){
    while(true){
        if(it==ss.end()||*it++!='Q') return "NA";
        if(it==ss.end()||*it++!='=') return "NA";
        if(it==ss.end()) return "NA";
        if(*it=='~') break;
    }
    if(*++it=='~'&&!*++it) return "B";
    return "NA";
}

string snake(string ss){
    S it = ss.begin();
    if( *it++ != '>' ) return "NA";
    if( *it == '\'' ) return snakeA(++it);
    else if( *it == '^' ) return snakeB(++it);
    else return "NA";
}

int main(){
    int n;
    for(cin>>n,cin.ignore();n--;){
        getline(cin, ss);
        cout<<snake(ss)<<endl;
    }
    return 0;
}

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

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  string s;
  cin >> n;
  rep(i,n){
    cin >> s;
    if(s[0] != '>'|| s.size() < 6 || s.size()%2 == 1|| (s[1] != '\'' && s[1] != '^') ){
      cout << "NA"<<endl;
    }
    else if(s[1] == '\''){
      int cnt = 0;
      bool f = true;
      REP(i,2,s.size()-1){
        if(s[i] == '#' && f){
          cnt *= -1;
          f = false;
        }
        else if(s[i] == '='){
          ++cnt;
        }
        else{
          cnt = -1;
          break;
        }
      }
      if(cnt != 0 || s[s.size()-1] != '~'){
        cout << "NA"<<endl;
      }
      else{
        cout << "A"<<endl;
      }
    }
    else{
      bool f = false;
      for(int i = 2; i < s.size(); i+=2){
        if(!(s[i] == 'Q' && s[i+1] == '=')){
          if(s[i] == '~' && s[i+1] == '~' && i == s.size()-2){
            cout << "B"<<endl;
            f = true;
          }
          else{
            break;
          }
        }
      }
      if(!f) cout << "NA"<<endl;
    }
  }
  return 0;
}

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

string vbString(int Number,string Character){
string res="";
for(int i=0;i<Number;i++) res+=Character;
return res;
}
string checksnake(string s){
string t;
for(int i=1;i<49;i++){
t=">\'"+vbString(i,"=")+"#"+vbString(i,"=")+"~";
if(t==s) return "A";
}
for(int i=1;i<49;i++){
t=">^"+vbString(i,"Q=")+"~~";
if(t==s) return "B";
}
return "NA";
}
int main(){
string s;
int n,i=0;
cin >> n;
for(i=0;i<n;i++){
cin >> s;
cout << checksnake(s) << endl;
}
return 0;
}

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

int main(){
  int n,j,fa,fb,acount,acount2,i;
  string data;
  
  cin >> n;
  
  getline(cin,data);
  for(j = 0; j < n; j++){
    fa = 0;
    fb = 0;
    acount = acount2 = 0;
    getline(cin,data);
    if(data[0] == '>' && data[1] == '\''){
      fa = 1;
      i = 2;
      while(data[i] == '='){
	acount++;
	i++;
      }
      if(data[i] != '#')
	fa = 0;
      i++;
      while(data[i] == '='){
	acount2++;
	i++;
      }
      if(acount != acount2)
	fa = 0;
      if(data[data.length()-1] != '~'){
	fa = 0;
      }
    }
    if(data[0] == '>' && data[1] == '^'){
      fb = 1;
      for(i = 2; i < data.length()-3; i = i+2){
	if(data[i] != 'Q' || data[i+1] != '=')
	  fb = 0;
      }
      if(data[data.length()-1] != '~' || data[data.length()-2] != '~')
	fb = 0;
    }
    if(fa == 1)
      cout << "A" << endl; 
    else if(fb == 1)
      cout << "B" << endl;
    else 
      cout << "NA" << endl;
  }
  return 0;
}