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;
int main(){

    int n;
    cin>>n;
    while(n--){

        string str;
        cin>>str;
        if(str[0]!='>')cout<<"NA"<<endl;
        else{
            if(str[1]=='\''&&str[2]=='='){
                int e=2;
                for(;str[e]=='=';e++);
                if(str[e]=='#'){
                    int f=e+1;
                    for(;str[f]=='=';f++);
                    if(e==f-e+1&&str[f]=='~'&&str[f+1]=='\0')cout<<"A"<<endl;
                    else cout<<"NA"<<endl;
                }
                else cout<<"NA"<<endl;
            }
            else if(str[1]=='^'){
                int e,flg=0;
                for(e=2;str[e]=='Q';e+=2){
                    if(str[e+1]!='='){
                        cout<<"NA"<<endl;
                        flg=1;
                        break;
                    }
                }
                if(!flg){
                    if(str[e]=='~'&&str[e+1]=='~'&&str[e+2]=='\0')cout<<"B"<<endl;
                    else cout<<"NA"<<endl;
                }
            }
            else cout<<"NA"<<endl;
        }

    }

    return 0;
}

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

using namespace std;

void jagdeA( const string& str )
{
	int count = 0;
	int i;


	for( i = 2;i < str.size();i++ ){
		if( str[i] == '=' ) count++;
		else if( count && str[i] == '#' ) break;
		else{
			cout << "NA" << endl;
			return;
		}
	}
	if( i == str.size() ){
		cout << "NA" << endl;
		return;
	}

	i++;
	for(; i < str.size(); i++ ){
		if( str[i] == '=' ) count--;
		else if( str[i] == '~' ) break;
		else{
			cout << "NA" << endl;
			return;
		}
	}

	if( count || i == str.size() ) {
		cout << "NA" << endl;
		return;
	}

	cout << "A" << endl;
	return;
}

void jagdeB( const string& str )
{
	int count = 0;
	int i;

	for( i = 2;i < str.size();i += 2 ){
		if( i + 1 >= str.size() ){
			cout << "NA" << endl;
			return;
		}
		if( str[i] == 'Q' && str[i + 1] == '=' ){
			count++;
		} else if( str[i] == '~' && str[i + 1] == '~' && count ){
			cout << "B" << endl;
			return;
		} else {
			cout << "NA" << endl;
			return;
		}
	}

	cout << "NA" << endl;
	return;
}



int main(int argc, char const* argv[])
{
	int n;
	string str;

	cin >> n;

	for( int i = 0;i < n;i++ ){
		cin >> str;
		if( str.size() < 6 || str[0] != '>' ){
			cout << "NA" << endl;
			continue;
		}

		switch( str[1] ){
			case '\'':
				jagdeA( str );
				break;
			case '^':
				jagdeB( str );
				break;
			default:
				cout << "NA" << endl;
				break;
		}
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
int main()
{
	int n;
	cin >> n;
	for(int i=0;i<n;i++)
	{
		string s;
		cin >> s;
		int sz=s.size();
		if(s[0]!='>'||sz<6)
		{
			cout << "NA" << endl;
			continue;
		}
		if(s[1]=='\'')
		{
			int t=sz-3;
			if(t%2==0)
			{
				cout << "NA" << endl;
				continue;
			}
			for(int j=2;j<2+t;j++)
			{
				if(j==2+(t+1)/2&&s[j]!='#')
				{
					cout << "NA" << endl;
					goto end;
				}
				else if(j!=2+(t+1)/2&&s[j]!='=')
				{
					cout << "NA" << endl;
					goto end;
				}
			}
			if(s[sz-1]!='~')
			{
				cout << "NA" << endl;
				continue;
			}
			cout << "A" << endl;
			end:;
		}
		else if(s[1]=='^')
		{
			int t=sz-4;
			if(t%2==1)
			{
				cout << "NA" << endl;
				continue;
			}
			for(int j=0;j<t;j++)
			{
				if(j%2==0&&s[j+2]!='Q')
				{
					cout << "NA" << endl;
					goto end2;
				}
				else if(j%2==1&&s[j+2]!='=')
				{
					cout << "NA" << endl;
					goto end2;
				}
			}
			if(s[sz-1]!='~'||s[sz-2]!='~')
			{
				cout << "NA" << endl;
				continue;
			}
			cout << "B" << endl;
			end2:;
		}
		else
		{
			cout << "NA" << endl;
			continue;
		}
	}
	return 0;
}

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;
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define rep(i,n) for (int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
int main(){
  int n;
  cin>>n;
  while(n--){
    string s;
    cin>>s;
    int a=0,b=0;
    bool f;
    if(s[0]=='>'){
      if(s[1]=='^'){
        f=false;
        for(int i=0;i<s.size()-1;i++){
          if(s[i]=='Q'&&s[i+1]=='=')f=true;
        }
        if(f&&s[s.size()-1]=='~'&&s[s.size()-2]=='~'){
          cout<<"B"<<endl;
          continue;
        }
      }
      int cnt=0,k=0;
      f=false;
      while(k<s.size()){
        if(s[k]=='='){
          while(s[k]=='='){
            cnt++;k++;
          }
          break;
        }
        k++;
      }
      if(s[k]=='#')f=true;
      while(k<s.size()){
        if(s[k]=='='){
          while(s[k]=='='){
            cnt--;k++;
          }
          break;
        }
        k++;
      }
      if(f&&cnt==0){
        cout<<"A"<<endl;
        continue;
      }
    }
    cout<<"NA"<<endl;
  }
}