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 <vector>
#include <algorithm>
#include <string>
using namespace std;

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

int main(){
	int n;
	vector<string> sample;
	string aaa;

	cin >> n;
	for(int i=0;i<n;i++){
		cin >> aaa;
		sample.push_back(aaa);
	}

	for(unsigned int i=0;i<sample.size();i++){
		string snake = sample[i];
		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;
			aruyon = snake.find("~~");
			if(aruyon == snake.size() -2){
				if(aruyon%2==0){
					for(j=2;j<aruyon;j++){
						dou = false;
						if(snake.at(j) == 'Q') j++;
						else break;	
						if(snake.at(j) == '=') dou = true;
						else break;
					}
					if(dou) cout << 'B' << endl;
					else a();
				}
				else a();
			}
			else a();
				
		}
		else
			a();
	}

  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
	bool b;
	int n, i, j, t, c[2];
	char s[201];
	cin >> n;
	for (i = 0; i < n; i++)
	{
		b = true;
		cin >> s;
		if (s[0] == '>')
		{
			if (s[1] == '\'')
			{
				for (j = 2, t = c[0] = c[1] = 0; j < strlen(s)-1; j++)
				{
					if (s[j] == '=')
						c[t]++;
					else if (t == 0 && s[j] == '#')
						t++;
					else
					{
						b = false;
						break;
					}
				}
				if (b && c[0] > 0 && c[0] == c[1] && s[strlen(s)-1] == '~')
					cout << "A" << endl;
				else
					cout << "NA" << endl;
				continue;
			}
			else if (s[1] == '^')
			{
				for (j = 2; j < strlen(s)-2; j+=2)
				{
					if (s[j] != 'Q' || s[j+1] != '=')
					{
						b = false;
						break;
					}
				}
				if (b && s[strlen(s)-2] == '~' && s[strlen(s)-1] == '~')
					cout << "B" << endl;
				else
					b = false;
				continue;
			}
			else
				b = false;
		}
		else
			b = false;
		if (!b)
			cout << "NA" << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
  int T;
  cin >> T;
  while(T--) {
    string s;
    cin >> s;
    if(s[0]!='>'||s.size()<=3) cout << "NA" << endl;
    else if(s[1]=='\'') {
      if(s[s.size()-1]!='~'||s.size()%2) {
        cout << "NA" << endl;
        goto next;
      }
      s=s.substr(2,s.size()-3);
      for(int i=0; i<s.size()/2; i++) {
        if(s[i]!='='||s[i+s.size()/2+1]!='=') {
          cout << "NA" << endl;
          goto next;
        }
      }
      if(s.size()<3||s[s.size()/2]!='#') cout << "NA" << endl;
      else cout << "A" << endl;
    } else {
      if(s[s.size()-2]!='~'||s[s.size()-1]!='~'||s.size()%2) {
        cout << "NA" << endl;
        goto next;
      }
      s=s.substr(2,s.size()-4);
      for(int i=0; i<s.size(); i+=2) {
        if(s[i]!='Q'||s[i+1]!='=') {
          cout << "NA" << endl;
          goto next;
        }
      }
      if(s.size()>=2) cout << "B" << endl;
      else cout << "NA" << endl;
    }
  next:;
  }
  return 0;
}

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

void printNA() {
    cout << "NA" << endl;
}

void judgeB(string s) {
    if (s.substr(s.size() - 2, 2) != "~~") {
        printNA();
        return;
    }
    if (s.size() <= 4) {
        printNA();
        return;
    }
    for (int i = 2; i < s.size() - 2; i += 2) {
        if (s.substr(i, 2) != "Q=") {
            printNA();
            return;
        }
    }
    cout << "B" << endl;
}

void judgeA(string s) {
    int len = 0;
    int i;
    for (i = 2; s[i] == '='; i++) {
        len++;
    }
    if (len == 0 || s.size() < i || s[i] != '#') {
        printNA();
        return;
    }
    if (s.size() < 2 + 2 * len) {
        printNA();
        return;
    }
    for (i++ ; i <= 2 + 2 * len; i++) {
        if (s[i] != '=') {
            printNA();
            return;
        }
    }
    if (s.size() == i || s[i] != '~') {
        printNA();
    } else {
        cout << "A" << endl;
    }
}

int main() {
    int n;
    cin >> n;
    for (int c = 0; c < n; c++) {
        string s;
        cin >> s;
        if (s.substr(0, 2) == ">'") {
            judgeA(s);
        } else if (s.substr(0, 2) == ">^") {
            judgeB(s);
        } else {
            cout << "NA" << endl;
        }
    }
    return 0;
}

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

string solv(string s){
	int ss = s.size();
	if(s[0] != '>' || s[ss-1] != '~')return "NA";
	if(s[ss-2] == '~'){
		if(s[1] != '^')return "NA";
		for(int i = 0;i < ss-2;i++){
			if(s[i] == 'Q' && s[i+1] == '=')return "B";
		}
	}else{
		fi(s[1] != ''')return "NA";
		int a = 0, f = 0;
		for(int i = 1;i < ss-1;i++){
			if(s[i] == '=' && !f)a++;
			if(s[i] == '#')f = 1;
			if(f && !a)return "NA";
			if(s[i] == '=' && f)a--;
		}
		return a ? "NA" : "A";
	}
	return "NA";
}

int main(){
	int n;cin >> n;
	string s;
	while(n--){
		cin >> s;
		cout << solv(s) << endl;
	}
	return 0;
}

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

using namespace std;

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

	while(n--)
	{
		string str;
		string kp;
		string set;
		int flag=0;
		int count=0;
		int i=0;

		cin >> str;
		for(i=0;i<2;i++)kp+=str[i]++;
		
		if(kp==">'")
		{
			while(str[i]!='#')
			{
				if(str[i]=='=')count++;
				else flag=-1;

				if(flag==-1)break;
				i++;	
			}
			i++;
			while(str[i]!='~')
			{
				if(str[i]=='=')count--;
				else flag=-1;

				if(flag==-1)break;
				i++;
			}
			if(flag!=-1 && !count)flag=1;

			count=0;
			for(int j=i;j<str.length();j++)
			{
				if(str[i]!='~')flag=-1;
				else count++;

				if(flag==-1)break;
			}
			if(count!=1)flag=-1;
		}
		else if(kp==">^")
		{
			while(i<str.length()-2)
			{
				count=2;
				while(count--)
				{
					set+=str[i];
					i++;
				}
				if(set=="Q=")flag=2;
				else flag=-1;

				set.erase(set.begin(),set.end());
				if(flag==-1)break;
			}
			for(i=str.length()-2;i<str.length();i++)set+=str[i];
			if(set=="~~" && flag==2)flag=2;
			else flag=-1;
		}

		if(flag==1) cout << "A" << endl;
		else if(flag==2)cout << "B" << endl;
		else cout << "NA" << endl;
	}
	
	return 0;
}

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

int main()
{
	int i, count;
	string s, name;
	getline(cin, s);
	while (getline(cin, s)) {
		if (s[1]=='\'') {
			name = "A";
			if (s[2]!='=') name = "NA";
			else {
				count = 0;
				for (i = 2; s[i]=='='; i++) count++;
				if (s[i]!='#') name = "NA";
				else {
					for (; count > 0; count--)
						if (s[++i] != '=') {
							name = "NA";
							break;
						}
					if (s[++i]!='~'||i!=s.size()-1) name = "NA";
				}
			}
		} else if (s[1]=='^') {
			name = "B";
			for (i = 2; i < s.size()-2; i += 2) {
				if (s.size()&1) {
					if (s[i+2]!='~'||s[i+3]!='~') name="NA";
					break;
				}
				if (s[i]!='Q'||s[i+1]!='=') {
					if (s[i+2]!='~'||s[i+3]!='~') name="NA";
					break;
				}
			}
		}
		cout<<name<<endl;
	}
	return 0;
}