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

//入力文字列
char str_input[110] = {NULL};

//----- 初期化関数 -----//
void f_syokika();

//----- '='の始端と終端の位置から割り出す -----//
int f_A(int);

//----- Bかどうかの関数 -----//
bool f_B();

//文字列
const char str_A[] = ">'=#~";
const char str_B[] = ">^Q=~~";

int main(void)
{
	//入力数
	int n = 0;

	cin >> n;

	for (int i = 0; i < n; i++)
	{
		//文字列初期化
		f_syokika();
		//文字列を読み込む
		cin >> str_input;
		//A , Bどちらかの可能性があるflag
		bool flag_A = false;
		bool flag_B = false;
		//=の個数を数える
		int ikoru = 0;
		//配列で使うカウント変数
		int count_h_A = 0;
		int count_h_B = 0;

		//NAflag
		bool flag_NA = true;

		int count = 0;

		//A , Bかどうか
		if (str_input[count++] == str_A[count_h_A])
		{
			count_h_A++;
			count_h_B++;
			//A
			if (str_input[1] == str_A[count_h_A])
			{
				count_h_A++;
				count++;
				while (str_input[count] != '\0')
				{
					if (str_input[count] != '=' && str_input[count] != '#' && str_input[count] != '~')
					{
						break;
					}
					if (str_input[count] == '#')
					{
						int kettei = 0;
						//戻り値が-1ならば違う
						if ((kettei = f_A(count)) == -1)
						{
							cout << "NA" << endl;
							break;
						}
						//次とその先次第
						if (str_input[kettei] == '~' && str_input[kettei + 1] == '\0')
						{
							cout << "A" << endl;
						}
						else
						{
							cout << "NA" << endl;
						}
					}

					count++;
				}
			}
			else if (str_input[1] == str_B[count_h_B])
			{
				count_h_B++;
				count++;

				if (f_B() == true)
				{
					cout << "B" << endl;
				}
				else
				{
					cout << "NA" << endl;
				}
			}
			else
			{
				cout << "NA" << endl;
			}
		}
		else
		{
			cout << "NA" << endl;
		}
	}

	return 0;
}



//*****----- 初期化関数 -----*****//

void f_syokika()
{
	for (int i = 0; i < 110; i++)
	{
		str_input[i] = '\0';
	}
}



//*****----- '='の始端と終端の位置から割り出す -----*****//

int f_A(int index)
{
	//後ろ・前に動かす
	int usiro = index - 1;
	int mae = index + 1;
	//後ろ、前カウント変数
	int count_usiro = 0;
	int count_mae = 0;

	while (true)
	{
		//インデックスの範囲内
		if (usiro < 0)
		{
			break;
		}
		//違う文字なら抜ける
		if (str_input[usiro] != '=')
		{
			break;
		}
		//抜けてない = '='である
		count_usiro++;
		usiro--;
	}

	//インデックスはOK
	while (str_input[mae] == '=')
	{
		mae++;
		count_mae++;
	}

	//同じ数且つ0でない
	if (count_usiro == count_mae && count_usiro != 0)
	{
		return mae;
	}
	return -1;
}



//*****----- Bかどうかの関数 -----*****//

bool f_B()
{
	//Qと＝のカウント変数
	int count_Q = 0;
	int count_ikoru = 0;
	
	int i = 2;
	while (str_input[i] == 'Q')
	{
		count_Q++;
		i += 2;
	}

	i = 3;
	while (str_input[i] == '=')
	{
		count_ikoru++;
		i += 2;
	}

	if (count_Q != count_ikoru)
	{
		return false;
	}
	if (str_input[i - 1] == '~' && str_input[i] == '~' && str_input[i + 1] == '\0')
	{
		return true;
	}
	return false;
}

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

int main(){
	int n,a,c,f;
	string str = "\0";
	cin >> n;
	for(int i = 0;i < n;++i){
		cin >> str;
		c = f = 0;
		a = 2;
		if(str[0] == '>'){
			if(str[1] == '\''){
				while(str[a] == '='){
					++a;
					++c;
				}
				if(str[a] != '#') goto end;
				++a;
				while(str[a] == '='){
					++a;
					++f;
				}
				if(str[a] == '~' && c == f) cout << "A" << endl;
				else goto end;
			}
			else if(str[1] == '^'){
				while(str[a] == 'Q' && str[a+1] == '='){
					a += 2;
				}
				if(str[a] == '~' && str[a+1] == '~') cout << "B" << endl;
				else goto end;
			}
			else goto end;
		}
		else {
			end:
			cout << "NA" << endl;
		}
	}
	return 0;
}

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

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <map> 
#include <sstream>
#include <cstdio>
#include <complex>
#include <cstring>
using namespace std;
 
const int MAX= 10000100;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
#define ll long long int
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a)
int GCD(int a, int b) {if(!b) return a; return gcd(b, a%b);}
int lcm(int a, int b) {return a*b / gcd(a, b);}

bool A(string s){
  if(s.size() < 6) return false;
  if(s.size()%2 != 0)return false;
  if(s[0] != '>')return false;
  if(s[1] != '\'')return false;
  if(s[s.size()-1] != '~')return false;
  if(s[s.size()/2] != '#')return false;
  else s[s.size()/2] = '=';
  loop(i,2,s.size()-1){
    if(s[i] != '=')return false;
  }
  return true;
}
bool B(string s){
  if(s.size() < 6) return false;
  if(s[0] != '>')return false;
  if(s[1] != '^')return false;
  loop(i,2,s.size()-2){
    if(i%2){
      if(s[i] != '=')return false;
    }else{
      if(s[i] != 'Q')return false;
    }
  }
  if(s[s.size()-2] != '~')return false;
  if(s[s.size()-1] != '~')return false;
  return true;
}
int main(void){
  int n;
  cin>>n;
  rep(i,n){
    string s;
    cin>>s;
    if(A(s))cout<<"A";
    else if(B(s))cout<<"B";
    else cout<<"NA";
    cout<<endl;
  }
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>


typedef long long ll;
const double Eps = 1e-6;

using namespace std;

int main()
{
	char buf[256];
	int n;
	cin.getline(buf, sizeof(buf));
	sscanf(buf, "%d", &n);
	while (n--)
	{
		memset(buf, '\0', sizeof(buf));
		cin.getline(buf, sizeof(buf));
		if (strlen(buf) < 3)
		{
			puts("NA");
			continue;
		}

		char* p = buf+2;
		if (buf[0] == '>' && buf[1] == '\'')
		{
			int c[4];
			memset(c, 0, sizeof(c));
			int sharp, tilde;
			int i;
			for (i = 0; p[i] != '\0'; ++i)
			{
				if (p[i] == '=')
					++c[0];
				else if (p[i] == '#')
				{
					++c[1];
					sharp = i;
				}
				else if (p[i] == '~')
				{
					++c[2];
					tilde = i;
				}
				else
					++c[3];
			}
			if (c[0] > 0 && (c[0] & 1) == 0 && c[1] == 1 && sharp == i/2-1
				&& c[2] == 1 && tilde == i-1)
				puts("A");
			else
				puts("NA");
		}
		else if (buf[0] == '>' && buf[1] == '^')
		{
			int i;
			for (i = 0; p[i] != '\0'; i += 2)
			{
				if (p[i] != 'Q' || p[i+1] != '=')
					break;
			}
			if (i >= 2 && !strcmp(p+i, "~~"))
				puts("B");
			else
				puts("NA");
		}
		else
			puts("NA");
	}

	return 0;
}