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>
#define PB push_back
#define MP make_pair
#define rep(i,n) for (int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
int main(){
  int n;
  cin>>n;
  while(n--){
    string s;
    cin>>s;
    int f1=0,f2=0,k=-1;
    bool f;
    for(int i=0;i<s.size();i++)if(s[i]=='>')k=i;
    if(k==-1){
      printf("NA\n");
      continue;
    }
    if(s[k+1]=='^'){
      f=false;
      for(int i=k;i<s.size()-1;i++)if(s[i]=='Q'&&s[i+1]=='=')f=true;
      if(f&&s[s.size()-1]=='~'&&s[s.size()-2]=='~'){
        printf("B\n");
        continue;
      }
    }
    int cnt=0;
    for(int i=k;i<s.size();i++){
      if(s[i]=='='){
        while(s[i]=='='&&i<s.size()){
          cnt++;i++;
        }
        k=i;
        break;
      }
    }
    f=false;
    for(int i=k;i<s.size();i++){
      if(s[i]=='#'){
        k=i;f=true;
      }
    }
    if(f){
      for(int i=k;i<s.size();i++){
        if(s[i]=='='){
          while(s[i]=='='&&i<s.size()){
            cnt--;i++;
          }
          break;
        }
      }
      if(cnt==0&&s[s.size()-1]=='~'){
        printf("A\n");
        continue;
      }
    }
    printf("NA\n");
  }
}

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

int main() {
  int n; cin >> n;
  for(int i = 0; i < n; i++) {
    string s; cin >> s;
    bool a[4] = {}, b[3] = {};
    int cnt = 0;
    for(int j = 0; j < s.size(); j++) {
      if(s[j] == '>') {
        if(!a[1]) a[0] = true;
        if(s[j+1] == '^') b[0] = true;
      } else if(a[0] && s[j] == '=') {
        a[1] = true;
        cnt++;
      } else if(b[0] && s.substr(j,2) == "Q=") {
        b[1] = true;
      } else if(a[1] && s[j] == '#') {
        a[0] = false;
        a[2] = true;
      } else if(a[2] && s[j] == '=') {
        cnt--;
      } else if(a[1] && cnt == 0 && j+1 == s.size() && s[j] == '~') {
        a[3] = true;
      } else if(b[1] && j+2 == s.size() && s.substr(j,2) == "~~") {
        b[2] = true;
      }
    }

    if(a[3]) cout << 'A' << endl;
    else if(b[2]) cout << 'B' << endl;
    else cout << "NA" << endl;
  }
}

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

int main(){
	int n;	cin >> n;
	while(n--){
		int i,eq1,eq2;
		string ret = "NA";
		
		string s; cin >> s;
		if(s.length()>100)while(1);
		if(s.substr(0,3) == ">'="){
			i = 2;
			
			eq1 = 0;
			while(s[i] == '=' && i < s.length() )i++,eq1++;
			if(s[i] != '#')goto label;
			
			i++; eq2 = 0;
			while(s[i] == '=' && i < s.length() )i++,eq2++;
			
			if(i == s.length()-1 && s[i] == '~' && eq1 == eq2)
				ret = "A"; 
		}else if(s.substr(0,4) == ">^Q="){
			for(i=2;i+1<s.length()-2;i+=2){
				if(s[i] == 'Q' && s[i+1] == '='){}
				else goto label;
			}
			if(s[s.length()-1] == '~' && s[s.length()-2] == '~' && s[s.length()-3] == '=' && s[s.length()-4] == 'Q')
				ret = "B";
			
		}
		label:;
		cout << ret << endl;
		
		
	}
}

C++: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
main(){
  int n;
  cin>>n;
  while(n--){
  string s;
  cin>>s;
  if(s[0]=='>'){
    if(s[1]=='\''){
      int sum=0;
      r(i,s.size())if(s[i]=='=')sum++;
      if(s.size()%2==0&&s[s.size()/2]=='#'&&sum==s.size()-4&&s[s.size()-1]=='~')
        cout<<'A'<<endl;
      else goto L;
    }
    else if(s[1]=='^'){
      int c=2;
      if(s.size()%2)goto L;
      while(c<s.size()-3){
        if(s[c]!='Q')goto L;
        c++;
        if(s[c]!='=')goto L;
        c++;
      }
      if(s[c]=='~'&&s[c+1]=='~')cout<<'B'<<endl;
      else goto L;
    }
    else goto L;
  }
  else goto L;
  if(0)L:cout<<"NA"<<endl;
  }
}

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

string makeSnakeA( int a )
{
    string s = ">\'";
    for( int i = 0; i < a; i++ )
        s += "=";
    s += "#";
    for( int i = 0; i < a; i++ )
        s += "=";
    s += "~";
    return s;
}

string makeSnakeB( int a )
{
    string s = ">^";
    for( int i = 0; i < a; i++ )
        s += "Q=";
    s += "~~";
    return s;
}

int main()
{
    int n;
    string str, s[200], t[200];
    for( int i = 0; i < 200; i++ )
    {
        s[i] = makeSnakeA( i + 1 );
        t[i] = makeSnakeB( i + 1 );
    }

    cin >> n;

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

        int size = str.size();

        if( size >= 6 && s[(size-6)/2] == str )
            cout << "A" << endl;
        else if( size >= 6 && t[(size-6)/2] == str )
            cout << "B" << endl;
        else
            cout << "NA" << endl; 
    }

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
	bool b;
	int n, i, j, t, c[2];
	char s[201];
	cin >> n;
	for (i = 0; i < n; i++)
	{
		b = true;
		cin >> s;
		if (s[0] == '>')
		{
			if (s[1] == '\'')
			{
				for (j = 2, t = c[0] = c[1] = 0; j < strlen(s)-1; j++)
				{
					if (s[j] == '=')
						c[t]++;
					else if (t = 0 && s[j] == '#')
						t++;
					else
					{
						b = false;
						break;
					}
				}
				if (b && c[0] == c[1] && s[strlen(s)-1] == '~')
					cout << "A" << endl;
				else
					cout << "NA" << endl;
				continue;
			}
			else if (s[1] == '^')
			{
				for (j = 2; j < strlen(s)-2; j+=2)
				{
					if (s[j] != 'Q' || s[j+1] != '=')
					{
						b = false;
						break;
					}
				}
				if (b && s[strlen(s)-2] == '~' && s[strlen(s)-1] == '~')
					cout << "B" << endl;
				else
					b = false;
				continue;
			}
			else
				b = false;
		}
		else
			b = false;
		if (!b)
			cout << "NA" << endl;
	}
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define rep(i,n) for (int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
int main(){
  int n;
  cin>>n;
  while(n--){
    string s;
    cin>>s;
    int f1=0,f2=0,k=-1;
    bool f;
    for(int i=0;i<s.size();i++)if(s[i]=='>'){
      k=i;break;
    }
    if(k==-1||s.size()<5){
      printf("NA\n");
      continue;
    }
    if(s[k+1]=='^'){
      f=false;
      for(int i=k;i+1<s.size();i++)if(s[i]=='Q'&&s[i+1]=='=')f=true;
      if(f&&s[s.size()-1]=='~'&&s[s.size()-2]=='~'){
        printf("B\n");
        continue;
      }
    }
    int cnt=0,cnt1=0;
    for(int i=k;i<s.size();i++){
      if(s[i]=='='){
        while(s[i]=='='&&i<s.size()){
          cnt++;i++;
        }
        k=i;
        break;
      }
    }
    f=false;
    for(int i=k;i<s.size();i++){
      if(s[i]=='#'){
        k=i;f=true;
      }
    }
    if(f){
      for(int i=k;i<s.size();i++){
        if(s[i]=='='){
          while(s[i]=='='&&i<s.size()){
            cnt1++;i++;
          }
          break;
        }
      }
      if(cnt==cnt1&&s[s.size()-1]=='~'){
        printf("A\n");
        continue;
      }
    }
    printf("NA\n");
  }
}