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 || count_Q == 0)
	{
		return false;
	}
	if (str_input[i - 1] == '~' && str_input[i] == '~' && str_input[i + 1] == '\0')
	{
		return true;
	}
	return false;
}

C++: #include<iostream>
using namespace std;
int n;
string str;
bool check_A(string t){
  int cnt=0,i=0,cnt2=0;

  while(t[i]=='='){cnt++;i++;}

  if(t[i]!='#')return false;
  if(cnt==0)return false;
  if((int)t.size()==i)return false;

  i++;
  while(t[i]=='='){cnt2++;i++;}

  if(cnt!=cnt2)return false;
  if((int)t.size()!=i+1)return false;

  if(t[i]!='~')return false;
  return true;
}

bool check_B(string t){
  int i=0,cnt=0;
  while(i+1<(int)t.size()&&t[i]=='Q'&&t[i+1]=='='){
    i+=2;
    cnt++;
  }
  if(cnt==0)return false;
  if(i+2!=(int)t.size())return false;
  if(t[i]=='~'&&t[i+1]=='~')return true;
  return false;
}


int main(){
  cin>>n;
  while(n--){
    cin>>str;
    if(str[0]=='>'&&str[1]=='\''){
      if(check_A(str.substr(2))){
	cout<<'A'<<endl;
      }else{
	cout<<"NA"<<endl;
      }
    }else if(str[0]=='>'&&str[1]=='^'){
      if(check_B(str.substr(2))){
	cout<<'B'<<endl;
      }else{
	cout<<"NA"<<endl;
      }
      
    }else{
      cout<<"NA"<<endl;
    }
  }
  return 0;
}

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 == ">'=~"){
            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 == ">^~~"){
            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<cstdio>
#include<algorithm>
using namespace std;

#define rep(i, n)for((i) = 0;(i) < (n);++(i))

string solve(string s){
	int i = 0, j;
	string tmp;
	rep(i, 100){
		tmp += "=";
		if(s == ">'" + tmp + "#" + tmp + "~~")return "A";
	}
	tmp.clear();
	rep(i, 100){
		tmp += "Q=";
		if(s == ">^" + tmp + "~~")return "B";
	}
	return "NA";
}

int main(void){
	int i, j, n;
	string s;
	scanf("%d", &n);
	for(;n--;){
		cin >> s;
		cout << solve(s) << endl;
	}
	
	return 0;
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#define pb(in,tmp) in.push_back(tmp)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) loop(i,0,b)
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
using namespace std;
int main(){
  int n;
  cin>>n;
  rep(i,n){
    string ans;
    string s;
	cin>>s;
	if(s[0]!='>'){cout<<"NA"<<endl;continue;}
	if(s[1]=='^'){
	  ans='B';
	  int h=2;
	  while(h<s.size()-2){
	    if(s[h++]!='Q'||s[h++]!='='){
		  ans="NA";
		  break;
		}
	  }
      if(s[h++]!='~'||s[h++]!='~')ans="NA";
	  if(h!=s.size())ans="NA";
	}else if(s[1]=='\''){
	  ans='A';
	  int h1=0,h2=0;
	  int j=1;
	  while(s[++j]=='=')h1++;
	  if(s[j]!='#')ans="NA";
	  while(s[++j]=='=')h2++;
	  if(s[j]!='~')ans="NA";
	  if(++j!=s.size())ans="NA";
	  if(h1!=h2)ans="NA";
	}
	cout<<ans<<endl;
  }
}