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

using namespace std;

int main(){

	int n;
	cin>>n;
	for(int i = 0; i < n; i++){
		string str;
		cin>>str;
		if(str.size()<=4)
			cout<<"NA"<<endl;
		if(str.substr(0,2)==">'"&&str[str.size()-1]=='~'&&str.size()%2==0){
			string ss=str.substr(2,str.size()-3);
			if(ss.size()>=3){
				bool f=true;
				for(int j = 0; j < ss.size(); j++){
					if(j==0){
						if(ss[j]!='='){
							f=false;
							break;
						}
					}
					else if(j==ss.size()/2){
						if(ss[j]!='#'){
							f=false;
							break;
						}
					}
					else{
						if(ss[j]!='='){
							f=false;
							break;
						}
					}
				}
				if(f)
					cout<<"A"<<endl;
				else
					cout<<"NA"<<endl;
			}
			else{
				cout<<"NA"<<endl;
			}
		}
		else if(str.substr(0,2)==">^"&&str.substr(str.size()-2,2)=="~~"&&str.size()%2==0){
			string ss=str.substr(2,str.size()-4);
			bool f=true;
			if(ss.size()>=2){
				for(int j = 0; j < ss.size(); j+=2){
					if(ss.substr(j,2)!="Q="){
						f=false;
						break;
					}
				}
				if(f)
					cout<<"B"<<endl;
				else
					cout<<"NA"<<endl;
			}
			else
				cout<<"NA"<<endl;
		}
		else
			cout<<"NA"<<endl;
	}

	return 0;
}

C++: #include <iostream>

int main()
{
	std::string sn;	getline(std::cin, sn);
	int n = std::stoi(sn);
	for (int i=0; i<n; i++) {
		std::string snake; 
		getline(std::cin, snake);
		int state = 0;
		int equal_count = 0;
		for (int k=0; k<snake.length(); k++) {
			char c = snake[k];
			switch (state) {
			case 0:
				if (c == '>') state = 1;
				else state = 9;
				break;
			case 1:
				if (c == '\'') state = 10;
				else if (c == '^') state = 11;
				else state = 9;
				break;
			case 2:
				if (c == '=') equal_count++;
				else if (c == '#') state = 3;
				else state = 9;
				break;
			case 3:
				if (c == '=') {
					equal_count--;
					if (equal_count < 0) state = 9;
				}
				else if (equal_count == 0 && c == '~') state = 4;
				else state = 9;
				break;
			case 4:
				state = 9;
				break;
			case 5:
				if (c == 'Q') state = 6;
				else if (c == '~') state = 7;
				else state = 9;
				break;
			case 6:
				if (c == '=') state = 5;
				else state = 9;
				break;
			case 7:
				if (c == '~') state = 8;
				else state = 9;
				break;
			case 8:
				state = 9;
				break;
			case 10:
				if (c == '=') {
					equal_count++; state = 2;
				}
				else state = 9;
				break;
			case 11:
				if (c == 'Q') state = 12;
				else state = 9;
				break;
			case 12:
				if (c == '=') state = 5;
				else state = 9;
				break;
			}
		}
		if (state == 4) std::cout << "A" << std::endl;
		else if (state == 8) std::cout << "B" << std::endl;
		else std::cout << "NA" << std::endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;

int main(){

	int n;
	cin>>n;
	for(int i = 0; i < n; i++){
		string str;
		cin>>str;
		if(str.size()<=4)
			cout<<"NA"<<endl;
		if(str.substr(0,2)==">'"&&str[str.size()-1]=='~'&&str.size()%2==0){
			string ss=str.substr(2,str.size()-3);
			if(ss.size()>=3){
				bool f=true;
				for(int j = 0; j < ss.size(); j++){
					if(j==0){
						if(ss[j]!='='){
							f=false;
							break;
						}
					}
					else if(j==(ss.size()/2)){
						if(ss[j]!='#'){
							f=false;
							break;
						}
					}
					else{
						if(ss[j]!='='){
							f=false;
							break;
						}
					}
				}
				if(f)
					cout<<"A"<<endl;
				else
					cout<<"NA"<<endl;
			}
			else{
				cout<<"NA"<<endl;
			}
		}
		else if(str.substr(0,2)==">^"&&str.substr(str.size()-2,2)=="~~"&&str.size()%2==0){
			string ss=str.substr(2,str.size()-4);
			bool f=true;
			if(ss.size()>=2){
				for(int j = 0; j < ss.size(); j+=2){
					if(ss.substr(j,2)!="Q="){
						f=false;
						break;
					}
				}
				if(f)
					cout<<"B"<<endl;
				else
					cout<<"NA"<<endl;
			}
			else
				cout<<"NA"<<endl;
		}
		else
			cout<<"NA"<<endl;
	}

	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair <INT, INT> II;
typedef vector <II> VII;

template<class T> ostream& operator << ( ostream& os, vector<T> v ) {
    for ( typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i ) {
        os << *it_i << ", ";
    }
    return os;
}


typedef set <string> SS;

bool checkA( string s ) {
    SS S;
    for ( int i = 1; i <= 100; ++ i ) {
        S.insert( ">'" + string( i, '=' ) + "#" + string( i, '=' ) + "~" );
    }
    return S.count( s ) > 0;
}

bool checkB( string s ) {
    SS S;
    for ( int i = 1; i <= 100; ++ i ) {
        VS con( i, "Q=" );
        S.insert( ">^" + accumulate( con.begin(), con.end(), string() ) + "~~" );
    }
    return S.count( s ) > 0;
}

string solve( string s ) {
    if ( checkA( s ) ) return "A";
    if ( checkB( s ) ) return "B";
    return "NA";
}

int main() {
    int n;
    cin >> n;
    for ( int i = 0; i < n; ++ i ) {
        string s;
        cin >> s;
        cout << solve( s ) << endl;
    }
    return 0;
}

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

bool is_A(string s){
	int size = s.size();
	if(size < 6) return false;
	if(s[0] != '>' || s[1] != '\'') return false;
	if(s[size - 1] != '~') return false;
	if(s[2] != '=') return false;
	int count = 0;
	int i = 2;
	while(s[i] == '='){ count++; i++; }
	if(s[i++] != '#') return false;
	for(int j = 0; j < count; j++, i++){
		if(s[i] != '=') return false;
	}
	if(i == size - 1) return true;
	else return false;
}

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

int main(){
	string s;
	int n;
	cin >> n;
	getline(cin, s);
	for(int i = 0; i < n; i++){
		getline(cin, s);
		if(is_A(s)) cout << "A";
		else if(is_B(s)) cout << "B";
		else cout << "NA";
		cout << endl;
	}
	return 0;
}