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>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
bool check_A( string &S )
{
    auto sz = S.size();
    //?????????[>'=], ????°????[~]???????????????, ????????? size ??? 6 ??\?????????????????? false
    if( S.find( ">'=" ) != 0 || S.find( "~" ) != sz - 1 || sz < 6 )
    {
        return 0;
    }
    //??????[>']??¨????°?[~]???????????????
    string R = S.substr( 2, sz - 3 );
    sz = R.size();
    //R ???[#,=]??\???????????????????????????????????? false
    if( R.find_first_not_of( "#=", 0 ) != string::npos )
    {
        return 0;
    }
    if( sz % 2 == 0 )
    {
        return 0;
    }
    //R ?????????????????????????????£???[#]???????????\??????[#]????????£?????? false
    auto pos = R.find( "#" );
    if( R.find( "#", pos + 1 ) != string::npos )
    {
        return 0;
    }
    // S ???????????????[#]???????????£?????? false
    if( R[ sz / 2 ] != '#' )
    {
        return 0;
    }
    return 1;
}
bool check_B( string &S )
{
    auto sz = S.size();
    //?????????[>^Q=], ????°????[~~]???????????????, ????????? size ??? 6 ??\?????????????????? false
    if( S.find( ">^Q=" ) != 0 || S.find( "~~" ) != sz - 2 || sz < 6 )
    {
        return 0;
    }
    string R = S.substr( 2, sz - 4 );
    sz = R.size();
    // S ???size????\???°?????£?????? false
    if( sz % 2 != 0 )
    {
        return 0;
    }
    for( int i = 0; i < sz; i += 2 )
    {
        //[Q=]?????£?¶???§????????£?????? false
        if( R[ i ] != 'Q' && R[ i + 1 ] != '=' )
        {
            return 0;
        }
    }
    return 1;
}
int main()
{
    int N;
    cin >> N;
    
    REP( i, N )
    {
        string S;
        cin >> S;
        
        string res{ "NA" };
        if( check_A( S ) )
        {
            res = "A";
        }
        if( check_B( S ) )
        {
            res = "B";
        }
        cout << res << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>

using namespace std;

void solve()
{
	int n;
	cin >> n;
	while(n--)
	{
		string snake;
		cin >> snake;
		if(snake.size() < 4)
		{
			cout << "NA" << endl;
		}
		else if(snake[0] == '>' && snake[1] == '\'' && snake[snake.size() - 1] == '~')
		{
			bool first = true;
			int count1 = 0;
			int count2 = 0;
			for(int i = 2; i < snake.size() - 1; ++i)
			{
				if(snake[i] == '=')
				{
					if(first)
					{
						++count1;
					}
					else
					{
						++count2;
					}
				}
				else if(snake[i] == '#')
				{
					first = false;
				}
			}
			if(count1 == count2 && count1 >= 1)
			{
				cout << "A" << endl;
			}
			else
			{
				cout << "NA" << endl;
			}
		}
		else if(snake[0] == '>' && snake[1] == '^' && snake[snake.size() - 2] == '~' && snake[snake.size() - 1] == '~')
		{
			bool flag = true;
			int count = 0;
			for(int i = 2; i < snake.size() - 2; i += 2)
			{
				if(snake[i] != 'Q' || snake[i + 1] != '=')
				{
					flag = false;
					break;
				}
				if(snake[i] == 'Q' && snake[i + 1] == '=')
				{
					++count;
				}
			}
			if(flag && count)
			{
				cout << "B" << endl;
			}
			else
			{
				cout << "NA" << endl;
			}
		}
		else
		{
			cout << "NA" << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}

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

string kaga(string s,int num){
	string san;
	for(int i=0;i<num;i++)san+=s;
	return san;
}

void check(string s){
	for(int i=1;i<99;i++){
		string a,b;
		a=">'"+kaga("=",i)+'#'+kaga("=",i)+'~';
		b=">^"+kaga("Q=",i)+"~~";
		if(a==s){
			cout<<"A"<<endl;
			goto end;
		}
		if(b==s){
			cout<<"B"<<endl;
			goto end;
		}
	}
	cout<<"NA"<<endl;
	end:;
}

int main(){
	int n;
	string s;
	for(cin>>n;n>0;n--){
		cin>>s;
		check(s);
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define int long long
#define FR first
#define SC second
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for(int i = (int)(f); i < (int)(n); i++)
#define each(a, b) for(auto& a : b)

typedef pair<int, int> P;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int n; cin >> n;
  while(n--) {
    string s; cin >> s;
    if(s.size() < 4) {
      puts("NA");
      continue;
    }
    int kind = 0;
    int sz = s.size();
    if(s[0] == '>' && s[1] == '\'' && s[sz-1] == '~') kind = 1;
    else if(s[0] == '>' && s[1] == '^' && s[sz-1] == '~' && s[sz-2] == '~' && sz%2 == 0) kind = 2;
    else kind = 3;
    if(kind == 1) {
      int cnt = 0, cmp = 0;
      for(int i = 2; i < sz-1 && kind != 3; i++) {
	if(s[i] != '=' && s[i] != '#') kind = 3;
	else {
	  if(s[i] == '=') cnt++;
	  else if(s[i] == '#' && !cmp) cmp = cnt, cnt = 0;
	  else kind = 3;
	}
      }
      if(cnt != cmp) kind = 3;
    } else if(kind == 2) {
      for(int i = 2; i < sz-2 && kind != 3; i+=2) {
	if(s.substr(i, 2) != "Q=") kind = 3;
      }
    }
    if(kind == 1) puts("A");
    else if(kind == 2) puts("B");
    else if(kind == 3) puts("NA");
  }
  
  return 0;
}

C++: #include <iostream>
#include <string>

using namespace std;

string afunc(string str)
{
	string res = "NA";
	
	for(int i = 0, j = str.size()-1; i <= j; i++, j--){
		if(i == j){
			if(str[i] == '#'){
				res = "A";
			}
		} else if(str[i] != '=' || str[j] != '='){
			break;
		}
	}
	
	return res;
}

string bfunc(string str)
{
	string res = "B";
	
	for(int i = 0; i < str.size(); i+=2){
		if(str[i] != 'Q' || str[i+1] != '='){
			res = "NA";
			break;
		}
	}
	
	return res;
}

int main()
{
	int n;
	string str, res;
	
	while(cin >> n && n){
		while(n--){
			cin >> str;
			res = "NA";
			if('>' == str[0]){
				str.erase(str.begin());
				int l = str.size();
				if(str[0] == '\'' && str[l-1] == '~'){
					str.erase(str.end()-1);
					str.erase(str.begin());
					if(str.size()%2 == 1){
						res = afunc(str);
					}
				} else if(str[0] == '^' && str[l-1] == str[l-2] && str[l-1] == '~'){
					str.erase(str.end()-1);
					str.erase(str.end()-1);
					str.erase(str.begin());
					
					if(str.size()%2 == 0){
						res = bfunc(str);
					}
				}
			}
			
			cout << res << endl;
		}
	}
	
	return 0;
}