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>
using namespace std;

int main(){
	int n,a,c,f;
	string str = "\0";
	cin >> n;
	for(int i = 0;i < n;++i){
		cin >> str;
		c = f = 0;
		a = 2;
		if(str[0] == '>'){
			if(str[1] == '\''){
				while(str[a] == '='){
					++a;
					++c;
				}
				if(str[a] != '#' || c == 0) goto end;
				++a;
				while(str[a] == '='){
					++a;
					++f;
				}
				if(str[a] == '~' && c == f) cout << "A" << endl;
				else goto end;
			}
			else if(str[1] == '^'){
				while(str[a] == 'Q' && str[a+1] == '='){
					a += 2;
				}
				if(str[a] == '~' && str[a+1] == '~') cout << "B" << endl;
				else goto end;
			}
			else goto end;
		}
		else {
			end:
			cout << "NA" << endl;
		}
	}
	return 0;
}

C++: //Name: Snakes
//Level: 2
//Category: 文字列,オートマトン
//Note: 

/**
 * AとBは別々に判定する。
 */
#include <iostream>
#include <string>

using namespace std;

bool snakeA(const string &s) {
    auto it = begin(s);
    int cnt = 0;
    if(*it++ != '>') return false;
    if(*it++ != '\'') return false;
    while(true) {
        const char c = *it++;
        if(c == '#') break;
        else if(c == '=') ++cnt;
        else return false;
    }
    if(cnt == 0) return false;
    while(cnt--) {
        if(*it++ != '=') return false;
    }
    if(*it++ != '~') return false;
    return it == end(s);
}

bool snakeB(const string &s) {
    auto it = begin(s);
    if(*it++ != '>') return false;
    if(*it++ != '^') return false;
    bool ok = false;
    while(true) {
        const char c = *it++;
        if(c == '~') break;
        else if(c != 'Q') return false;

        if(*it++ != '=') return false;
        ok = true;
    }
    if(!ok) return false;
    if(*it++ != '~') return false;
    return it == end(s);
}

bool solve(bool first) {
    string s;
    cin >> s;
    if(snakeA(s)) cout << "A" << endl;
    else if(snakeB(s)) cout << "B" << endl;
    else cout << "NA" << endl;
    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout.setf(ios::fixed);
    cout.precision(2);

    bool first = true;
    int N;
    cin >> N;
    while(N-- && solve(first)) {
        first = false;
    }
    return 0;
}

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;
                for(e=2;str[e]=='Q';e+=2){
                    if(str[e+1]!='='){
                        cout<<"NA"<<endl;
                        break;
                    }
                }
                if(str[e]=='~'&&str[e+1]=='~'&&str[e+2]=='\0')cout<<"B"<<endl;
                else cout<<"NA"<<endl;

            }
            else cout<<"NA"<<endl;
        }

    }

    return 0;
}