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>
#include <string>

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

using namespace std;

int main()
{
	int n;
	cin >> n;
	
	rep(i,n)
	{
		string s;
		cin >> s;

		if(s[0] == '>' && s[1] == '\'' && s[2] == '=')
		{
			int i = 2;
			int cnt = 0;
			while(s[i] == '=')
			{
				i++;
				cnt++;
			}

			if(s[i] == '#' && i < s.size())
			{
				i++;
				while(s[i] == '=')
				{
					if(i == s.size()-1) break;
					i++;
					cnt--;
				}
			}

			if(cnt == 0 && s[i] == '~') cout << "A" << endl;
			else cout << "NA" << endl;
		}
		else if(s[0] == '>' && s[1] == '^' && s[2] == 'Q')
		{
			int i = 2;
			int cnt = 0;
			bool end = false;
			while(i+1 < s.size())
			{
				if(s[i] == 'Q' && s[i+1] == '=')
				{
					i += 2;
				}
				else if(s[i] == '~' && s[i+1] == '~')
				{
					end = true;
					cout << "B" << endl;
					i += 2;
				}
				else
				{
					break;
				}
			}

			if (!end) cout << "NA" << endl;
		}
		else
		{
			cout << "NA" << endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++) 
#define ALL(n) begin(n),end(n)
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
int check( string S )
{
    //????????????¢>???£, ?????°???????¢~???£ ????????????????????????NA
    if( S[ 0 ] != '>' || S[ S.size() - 1 ] != '~' )
    {
        return 0;
    }
    //A??¨?
    if( S[ 1 ] == '\'' )
    {
        if( S.substr( 2, S.size() - 3 ).find_first_not_of( "#=", 0 ) != string::npos ) return 0;
        string tmpB = S.substr( 2, S.find('#') - 2 );
        string tmpA = S.substr( S.find('#') + 1, S.size() - ( S.find('#') + 2 ) );
        return tmpB.size() == tmpA.size() ? 1 : 0;
    }
    //B??¨?
    if( S[ 1 ] == '^' && S[ S.size() - 2 ] == '~' )
    {
        if( S.substr( 2, S.size() - 4 ).find_first_not_of( "Q=", 0 ) != string::npos ) return 0;
        for( int i = 2; i < S.size() - 2; i += 2 )
        {
            if( S[ i ] != 'Q' && S[ i + 1 ] != '=' ) return 0;
        }
        return 2;
    }
}
int main()
{
    int N;
    cin >> N;
     
    vector<string> res{ "NA", "A", "B" };
    string S;
    while( cin >> S )
    {
        cout << res[ check( S ) ] << endl;
    }
    return 0;
}

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

using namespace std;
#define repeat(n, i) for (int i = 0; i < (n); i++) 

bool is_a(string& s) {
    if (s[0] != '>' || s[1] != '\'') return false;
    int eq_count = 0;
    int i;
    for (i = 2; s[i] == '='; i++) {
        eq_count++;
    }
    if (s[i++] != '#') return false;
    for (; s[i] == '='; i++) {
        eq_count--;
    }
    if (eq_count == 0 && s[i] == '~') return true;
    return false;
}

bool is_b(string& s) {
    if (s[0] != '>' || s[1] != '^') return false;
    int len = s.size();
    if (len % 2 == 1) return false;
    int i;
    for (i = 2; i < len-2; i+=2) {
        if (s[i] != 'Q' || s[i+1] != '=') return false;
    }
    if (s[i] != '~' || s[i+1] != '~') return false;
    return true;
}

int main() {
    int n; cin >> n;
    repeat(n, i) {
        string s; cin >> s;
        if (is_a(s)) {
            cout << "A" << endl;
            continue;
        } else if (is_b(s)) {
            cout << "B" << endl;
            continue;
        } else {
            cout << "NA" << endl;
        }
    }
    return 0;
}

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;
			}
		}
		else
			cout << "NA" << endl;
	end:;
	}
}

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||s.size()<6)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 <vector>
#include <algorithm>
#include <string>
using namespace std;

void a(){
	cout << "NA" << endl;
	return;
}

int main(){
	int n;
	cin >> n;

	for(int i=0;i<n;i++){
		string snake;
		cin >> snake;
		string::size_type aruyon;

		if((aruyon = snake.find(">'"))==0){
			int equal=0;
			int j;
			for(j=2;snake.at(j)=='=';j++) equal++;
			if(equal > 0)
				if(snake.at(j) == '#'){
					j++;
					for(;snake.at(j)=='=';j++) equal--;
					if(equal == 0){
						if(snake.at(j)=='~' && j+1 == snake.size()) cout << 'A' << endl;
						else a();
					}
					else a();
				}
				else a();
			else a();
		}
		else if((aruyon = snake.find(">^"))==0){
			int j;
			bool dou = false;
			if((aruyon = snake.find("~~"))==snake.size() -2){
				if(aruyon%2==0){
					for(j=2;j<aruyon;j++){
						if(snake.at(j) == 'Q') j++;
						else{j=0; break;}	
						if(snake.at(j) != '='){a(); break;}
						else dou == true;
					}
					if(j == aruyon && dou == true) cout << 'B' << endl;
					else a();
				}
				else a();
			}
			else a();
				
		}
		else
			a();
	}

  return 0;
}