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 <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define vi vector<int>
#define VS vector<string>
#define all(x) x.begin(),x.end()
#define mp make_pair
#define pb push_back

using namespace std;

int main(){
    int n;
    cin >> n;
    for(int i=0; i < n; i++){
        string input;
        cin >> input;
        string check = input.substr(0, 2) + input.substr(input.size()-2, 2);

        bool isCorrect = true;
        if(check == ">'=~"){
            if(input.size() == 4) isCorrect = false;

            int count = 0;
            for(int j=2; input.substr(j,1) == "="; j++) count++;
            if(input.substr(count+2, 1) != "#") isCorrect = false;
            for(int j=2+count+1; j < 2+count*2+1; j++) if(input.substr(j, 1) != "=") isCorrect = false;
            if(input.size() != count*2+2+1*2) isCorrect = false;

            if(isCorrect) cout << "A" << endl;
        }else if(check == ">^~~"){
            if(input.size() == 4) isCorrect = false;
            for(int j=2; j < input.size()-2; j+=2) if(input.substr(j, 2) != "Q=") isCorrect = false;

            if(isCorrect) cout << "B" << endl;
        }else {
            cout << "NA" << endl;
        }
        if(!isCorrect) cout << "NA" << endl;
    }
}

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

int main(){
  string snake;
  int n,len;

  cin >> n;
  for(int i=0;i<n;i++){
    cin >> snake;
    len = snake.size();
    int ans = -1;
    if(snake[0] == '>' && snake[1] == '\''){
      int pos = 2;
      int turn = 0;
      int flag = 0;
      int count = 0;
      do{
	if(turn && snake[pos] == '=')count--;
	else if(snake[pos] == '=') count++;
	else if(snake[pos] == '#')turn = 1;
	else flag = 0;
	if(pos == 2 && count > 0)flag = 1;
	pos++;
      }while(pos < len-1);
      if(count == 0 && flag && snake[pos] == '~')ans = 0;
    }else if(snake[0] == '>' && snake[1] == '^'){
      int pos = 2;
      int flag = 1;
      do{
	if(snake[pos] != 'Q' || snake[pos+1] != '='){
	  flag = 0;
	  break;
	}
	pos += 2;
      }while(pos < len-2);
      if(flag && snake[pos] == '~' && snake[pos+1] == '~')ans = 1;
    }
    if(ans == 0)cout << 'A' << endl;
    else if(ans == 1)cout << 'B' << endl;
    else cout << "NA" << endl;
  }

  return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;main(){int f,i,m;string s;for(cin>>f;cin>>s;f?puts(f-2?"A":"B"):puts("NA")){f=0;for(i=1;s[0]+s[1]==101&(m=s.find(35))!=string::npos&s[m-i]==s[m+i];)m-++i-1|m+i-s.size()+1|s[m+i]-'~'||(f=1);for(i=2;s[0]+s[1]==156&s[i]==81&s[i+1]==61;)++++i-s.size()+2|s[i]+s[i+1]-252||(f=2);}}

C++: #include<iostream>
#include<string>
using namespace std;
int main() {
	int n;
	cin >> n;
	for (int z = 0; z < n; z++) {
		string a;
		cin >> a;
		string b = a.substr(0, 2),c=a.substr(a.length()-2,2);
		if (b == ">'") {
			if (c[1] == '~') {
				int d[2] = { 0,0 } , e = 0;
				bool f = false;
				for (int i = 2; i < a.length() - 1; i++) {
					if (a[i] == '=') {
						while (a[i] == '=') {
							i++;
							d[e]++;
						}
						e++;
					}
					if (a[i] == '#'&&e==1) {
						f = true;
					}
				}
				if (f == true && d[0] == d[1]) {
					cout << "A" << endl;
					goto stop;
				}
			}
		}
		if (b == ">^") {
			if (c == "~~") {
				for (int i = 2; i < a.length() - 2; i++) {
					string d = a.substr(i, 2);
					if (d == "Q=") {
						cout << "B" << endl;
						goto stop;
					}
				}
			}
		}
		cout << "NA" << endl;
	stop:;
	}
}

C++: #include "stdio.h"
#include "math.h"

#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

const int INF = 10000000;
using namespace std;
typedef long long  ll;

/** Problem0139 : Snakes **/
int main()
{
	int n;
	string s;
	cin >> n;
	rep(k, n) {
		cin >> s;
		if (s.length()<2) {
			cout << "NA" << endl;
			goto end;
		}
		else if (s[0]=='>' && s[1]=='\'') {
			int num1=0, num2=0, i;
			for(i=2; i<s.length()-1;i++) {
				if (s[i]=='=')
					num1++;
				else if (s[i]=='#')
					break;
				else {
					cout << "NA" << endl;
					goto end;
				}
			}
			for (; i<s.length()-1; i++) {
				if (s[i]=='=')
					num2++;
			}
			if (num1 == num2 && s[s.length()-1]=='~') {
				cout << "A" << endl;
			} else {
				cout << "NA" << endl;
				goto end;
			}
		}
		else if (s[0]=='>' && s[1]=='^') {
			bool f=true;
			for (int i=2; i<s.length()-2; i++) {
				if (i%2==0 && s[i]!='Q')
					f=false;
				else if (i%2==1 && s[i]!='=')
					f=false;
			}
			if (f && s[s.length()-2]=='~' && s[s.length()-1]=='~') {
				cout << "B" << endl;
			} else {
				cout << "NA" << endl;
				goto end;
			}
		}
	end:;
	}
}

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

int main(){
  char buf[210];
  int bi,n,eqc,ti;

  cin >> n;

  for (int i=0;i<n;i++){
    bi=0;
    cin >> buf;
    if (buf[bi++]!='>') {
      cout << "NA" << endl;
      continue;
    }
    if (buf[bi]=='\''){
      bi++;
      eqc=0;
      if (buf[bi]!='='){
	cout << "NA" << endl;
	continue;
      }
      while (buf[bi+eqc]=='=')eqc++;
      if (buf[bi+eqc]!='#'){
	cout << "NA" << endl;
	continue;
      }
      bi += eqc+1;
      for (ti=0;ti<eqc;ti++)
	if (buf[bi+ti]!='=')break;
      if (ti <eqc){
	cout << "NA" << endl;
	continue;
      }
      bi+=ti;
      if (buf[bi]=='~'&&buf[bi+1]=='\0')cout << "A" << endl;
      else cout << "NA" << endl;
    }else if(buf[bi]=='^'){
      bi++;
      //cout <<"dbg "<< buf[bi] << buf[bi+1] << endl;
      if(buf[bi]!='Q'||buf[bi+1]!='='){
	cout << "NA" << endl;
	continue;
      }
      bi+=2;
      while (buf[bi]=='Q'&&buf[bi+1]=='=')bi +=2;
      //cout <<"dbg "<< buf[bi] << buf[bi+1] << endl;
      if (buf[bi]=='~'&&buf[bi+1]=='~'){
	if (buf[bi+2]=='\0')cout << "B" << endl;
	else cout << "NA" << endl;
      }else cout << "NA" << endl;
    }else{
      cout << "NA" << endl;
      continue;
    }
  }
  return 0;
}