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 <cstdio>
#include <string.h>
using namespace std;
int checksnake(string, int);

int main(void) {
  int n, len;
  char snake[200];
  int type;
  cin >> n;
  for(int i = 0 ; i < n ; i++) {
    scanf("%s", snake);
    len = strlen(snake);
    type = checksnake(snake, len);
    if(type == 1) cout << 'A' << endl;
    else if(type == 2) cout << 'B' << endl;
    else if(type == 3) cout << "NA" << endl;
    for(int a = 0 ; a < 200 ; a++) {
      snake[a] = 0;
    }
    len = 0;
  }
  return 0;
}

int checksnake(string snake, int len) {
  int flag1=0, flag2=0, flag3=0;
  int true_flag=0, true_flag2=0, true_flag3=0;
  int cnt1=0, cnt2=0;
  
  if(true_flag == 0) {
    for(int i = 0 ; i < len - 1 ; i++) {
      if(snake[i] != '>' && snake[i] != '#' && snake[i] != '=' && snake[i] != '\'') { true_flag2 = 1; break; }
      if(snake[i] == '>' && snake[i+1] == '\'') flag2 = 1;
      if(snake[i] == '=' && flag1 == 0 && flag2 == 1) {cnt1++; flag3 = 1; }
      if(snake[i] == '=' && flag1 == 1 && flag2 == 1) {cnt2++; flag3 = 1; }
      if(snake[i] == '#' && flag2 == 1 && flag3 == 1) flag1 = 1;
      //cout << cnt1 << ' ' << cnt2 << endl;
    }
    if(snake[len-1] == '~' && cnt1 == cnt2 && flag2 == 1 && flag3 == 1 && true_flag2 == 0) {
      return 1;
      true_flag = 1;
    }
  }
  
  if(true_flag == 0) {
    for(int i = 0 ; i < len - 2 ; i++) {
      if(snake[i] != '>' && snake[i] != '^' && snake[i] != '=' && snake[i] != 'Q') { true_flag3 = 1; break; }
      if(snake[i] == '>' && snake[i+1] == '^') flag3 = 1;
      if(snake[i] == 'Q' && snake[i+1] == '=' && flag3 == 1) flag1 = 1;
    }
    if(snake[len-2] == '~' && snake[len-1] == '~'  && flag1 == 1 && flag3 == 1 && true_flag3 == 0) {
      return 2;
      true_flag = 1;
    }
  }

  if(true_flag == 0 || true_flag2 == 1 || true_flag3 == 1) {
    return 3;
  }
}

C++: 
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

int main()
{
    int n;
	cin >> n;
	
	while(n--)
	{
		string snk;
		cin >> snk;
		
		if(snk.size() < 6 || snk.size() % 2 != 0 || (snk.substr(0, 2) != ">'" && snk.substr(0, 2) != ">^"))
		{
			cout << "NA" << endl;
		}
		else if(snk.substr(0, 2) == ">^")
		{
			if(snk.substr(2, 2) == "Q=" && snk.substr(snk.size() - 2, 2) == "~~")
			{
				for(int i = 4; i < snk.size() - 2; i += 2)
				{
					if(snk.substr(i, 2) != "Q=")
					{
						goto ng;
					}
				}
				
				cout << "B" << endl;
			}
			else
			{
			ng:;
				cout << "NA" << endl;
			}				
		}
		else
		{
			if(snk[(snk.size() - 3) / 2 + 2] == '#' && snk[snk.size() - 1] == '~')
			{
				rep(i, (snk.size() - 3) / 2)
				{
					if(snk[i + 2] != '=' || snk[i + 3 + (snk.size() - 3) / 2] != '=')
					{
						goto ng;
					}
				}
				
				cout << "A" << endl;
			}
			else
			{
				cout << "NA" << endl;
			}
		}
	}
}

C++: #include <regex>
#include <iostream>
#include <string>
using namespace std;
main(){
	int n;
	string s;
	smatch m;
	for(cin>>n,getline(cin,s);n;n--){
		getline(cin,s);
		if(regex_match(s,m,regex(">'(=+)#(=+)~"))&&m[1].length()==m[2].length())cout<<'A'<<endl;
		else if(regex_match(s,regex(">^(Q=)+~~")))cout<<'B'<<endl;
		else cout<<"NA"<<endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n;
string s;

int main(){
	cin>>n;
	while(cin>>s){
		if(s.size()<6||s[0]!='>'){cout<<"NA"<<endl;break;}
		int cur=2,l=0,l2=0,f=0,f2=0;
		if(s[1]=='\''){
			while(cur<s.size()){
				if(f2){break;}
				else if(s[cur]=='='){
					if(!f)l++;
					else l2++;
				}
				else if(s[cur]=='#'){
					if(!l||f)break;
					else f=1;
				}
				else if(s[cur]=='~'){
					if(l&&l==l2){f2=1;}
					else break;
				}
				else break;
				cur++;
			}
		}
		else if(s[1]=='^'){
			while(cur<s.size()){
				if(f2){break;}
				if(s[cur]=='Q'){
					if(!f)f=1;
					else break;
				}
				else if(s[cur]=='='){
					if(f==1)f=0;
					else break;
				}
				else if(s[cur]=='~'){
					if(!f)f=2;
					else if(f==2)f2=2;
					else break;
				}
				else break;
				cur++;
			}
		}
		if(f2==1)cout<<"A"<<endl;
		else if(f2==2)cout<<"B"<<endl;
		else cout<<"NA"<<endl;
	}
}

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

bool isa(string s) {
    int pos = 2;
    while(s[pos] == '=') pos++;
    if(pos == 2 || s[pos] != '#') return false;

    int cnt = pos++ - 2;
    for(int i=0; i<cnt; ++i) {
        if(s[pos+i] != '=') return false;
    }
    pos += cnt;
    if(pos == s.length()-1 && s[pos] == '~') return true;
    return false;
}

bool isb(string s) {
    int pos = 2;
    for(; pos < s.length()-2; pos += 2)
        if(s[pos] != 'Q' || s[pos+1] != '=') return false;

    if(s[s.length()-1] == s[s.length()-2] && s[s.length()-1] == '~') return true;
    return false;
}

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

    string s;
    for(int i=0; i<n; ++i) {
        cin>>s;
        if(s[1] == '\'') {
            if(isa(s)) {
                cout<<"A"<<endl;
            }else{
                cout<<"NA"<<endl;
            }
        } else {
            if(isb(s)) {
                cout<<"B"<<endl;
            }else{
                cout<<"NA"<<endl;
            }
        }
    }
}