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"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;

int a;
bool check(const string st,const int isa) {
	a = 0;
	if (isa) {
		if (st[a] != '>')return false;
		a++;
		if (st[a] != '\'')return false;
		a++;
		int num = 0;
		while (st[a] == '=') {
			num++;
			a++;
		}
		if (!num)return false;
		if (st[a] != '#')return false;
		a++;
		int num2 = 0;
		while (st[a] == '=') {
			num2++;
			a++;
		}
		if (num != num2)return false;
		if (st[a] != '~')return false;
		a++;
		if (st[a] != ' ')return false;
		return true;
	}
	else {
		if (st[a] != '>')return false;
		a++;
		if (st[a] != '^')return false;
		a++;
		int num = 0;
		while (1) {
			if (st[a] == 'Q') {
				num++;
				a++;
				if (st[a] != '=') {
					return false;
				}
				else {
					a++;
				}
			}
			else {
				break;
			}
		}
		if (!num)return false;
		
		
		if (st[a] != '~')return false;
		a++;
		if (st[a] != '~')return false;
		a++;
		if (st[a] != ' ')return false;
		return true;
	}
}

int main() {
	int N; cin >> N;
	while (N--) {
		string st; cin >> st;
		for (int i = 0; i < 100; ++i) {

			st.push_back(' ');
		}
		if (check(st, true)) {
			cout << "A" << endl;
		}
		else if (check(st, false)) {
			cout << "B" << endl;
		}
		else {
			cout << "NA" << endl;
		}
	}
	return 0;
}

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

using namespace std;

int main() {
  int Tc; string s;
  while(cin>>Tc) {
    while(Tc--) {
      cin >> s;
      int const N = s.size();
      bool NA = 0;
      bool isA;
      if(s[0] != '>') NA = 1;
      if(s[1] == '\'') {
	for(int i=2; i<(N-3)/2+2; i++) if(s[i]!='=') NA=1;
	if(s.substr(2, (N-3)/2) + "~" != s.substr(3+(N-3)/2)) NA = 1;
	if(s[(N-3)/2+2] != '#') NA = 1;
	isA = 1;
      }
      else if(s[1] == '^') {
	int cnt;
	for(cnt=2; cnt<N-2; cnt+=2) {
	  if(!(s[cnt] == 'Q' && s[cnt+1] == '=')) NA = 1;
	}
	if(!(s[cnt] == '~' && s[cnt+1] == '~')) NA = 1;
	if(cnt+2 != N) NA = 1
	isA = 0;
      }
      else {
	NA = 1;
      }
      
      if(NA) cout << "NA" << endl;
      else if(isA) cout << "A" << endl;
      else cout << "B" << endl;
    }
  }
  
  return 0;
}

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

int main()
{
	int i, count;
	string s, name;
	getline(cin, s);
	while (getline(cin, s)) {
		if (s[1]=='\'') {
			name = "A";
			if (s[2]!='=') name = "NA";
			else {
				count = 0;
				for (i = 2; s[i]=='='; i++) count++;
				if (s[i]!='#') name = "NA";
				else {
					for (; count > 0; count--)
						if (s[++i] != '=') {
							name = "NA";
							break;
						}
					if (s[++i]!='~'||i!=s.size()-1) name = "NA";
				}
			}
		} else if (s[1]=='^') {
			name = "B";
			for (i = 2; i < s.size(); i += 2) {
				if (s.size()&1) {
					name="NA";
					break;
				}
				if (s[i]!='Q'||s[i+1]!='=') {
					if (s[i]!='~'||s[i+1]!='~'||i+1!=s.size()-1) name="NA";
					break;
				}
			}
		}
		cout<<name<<endl;
	}
	return 0;
}

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

int main(void){
	int n;
	cin >> n;
	for(int i=0;i<n;i++){
		string str;
		cin >> str;
		if(str.size()<2)cout << "NA" << endl;
		else if(str[0]=='>' && str[1]=='\''){
			int j=2;
			int l[2]={0,0};
			int ck=0;
			bool ch[2]={false,true};
			while(j<str.size()){
				if(str[j]=='=')l[ck]++;
				else if(str[j]=='#' && l[0]>0)ch[0]=true,ck=1;
				else if(str[j]=='~')break;
				else ch[1]=false;
				j++;
			}
			if(ch[0]==true && ch[1]==true && l[0]==l[1])cout << "A" << endl;
			else cout << "NA" << endl;
		}else if(str[0]=='>' && str[1]=='^'){
			if(str.size()%2==1 || str.size()<6)cout << "NA" << endl;
			else{
				bool ch=true;
				for(int j=2;j<str.size()-2;j+=2){
					if(str[j]!='Q' || str[j+1]!='=')ch=false;
				}
				if(str[str.size()-2]!='~' || str[str.size()-1]!='~')ch=false;
				if(ch==true)cout << "B" << endl;
				else cout << "NA" << endl;
			}
		}else cout << "NA" << endl;
	}
    return 0;
}

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

bool A(string body) {
  if(int a = body.find("#") != -1 && body.size() % 2 == 0 && body.substr(body.size()-1,1) == "~" && body.size() >= 4) {
    for(int i=0;i<a;i++) {
      if(body.substr(i,1) == body.substr(body.size()-2-i,1) && body.substr(i,1) == "=") {

        continue;
      }
      else return false;
    }
  }
  else return false;
  return true;
}

bool B(string body) {

  if(body.substr(body.size()-2,2) == "~~" && body.size() % 2 == 0 && body.size() >= 4) {
    for(unsigned int i=0;i<body.size()-2;i+=2) {
      if(body.substr(i,2) == "Q=") {
        continue;
      }
      else return false;
    }
  }
  else return false;
  return true;
}

int main() {
  int n;
  string str;
  scanf("%d\n",&n);
  for(int i=0;i<n;i++) {
    getline(cin,str);
    if(str.substr(0,2) == ">'") {
      if(A(str.substr(2))) cout << "A" << endl;
      else cout << "NA" << endl;
    }
    else if(str.substr(0,2) == ">^") {
      if(B(str.substr(2))) cout << "B" << endl;
      else cout << "NA" << endl;
    }
    else cout << "NA" << endl;
  }
}

C++: #include <iostream>
#include <string>
#include <cstdio>
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<600;i++){
                t=">'"+vbString(i,"=")+"#"+vbString(i,"=")+"~";
                if(t==s) return "A";
        }
 
        for(int i=1;i<600;i++){
                t=">^"+vbString(i,"Q=")+"~~";
                if(t==s) return "B";
        }
       return "NA";
 
}
int main(){
        int n;
        cin >> n;
        while(n--){
		string s;
                cin >> s;
                cout << checksnake(s) << endl;
         }
}