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

Python: for _ in [0]*int(input()):
    s=input()
    if s[:2]==">'" and s[-1]=='~' and '#' in s:
        s=s[2:-1].split('#')
        print(['NA','A'][set(s[0])==set(s[1])=={'='} and len(set(s))==1])
    elif s[:2]=='>^' and s[-2:]=='~~':
        s=s[2:-2]
        print(['NA','B'][len(s)==2*s.count('Q=') and len(s)>0])

Python: import re
for i in range(int(input())):
    s=input()
    if re.match(">'(=+)#(=+)~",s):print('A')
    elif re.match(">\^(Q=)+~~",s):print('B')
    else:print('NA')

Python: # -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0139

"""
import sys
from sys import stdin
import re
input = stdin.readline


def identify_snake(snake):
    # ????????????>`???????¶???????1?????\?????????=]??????#??????????????§1?????\?????????=??????????????????~??????????????¨?????????=?????°?????????
    patternA = re.compile("^>'(=+)#(=+)~")
    resultA = patternA.search(snake)
    if resultA:
        pre, post = resultA.groups()
        if pre == post:
            return 'A'

    # ????????????>^???????¶???????1?????\?????????Q=]???????????????????????????~~???
    patternB = re.compile("^>\^(Q=)+~~")
    resultB = patternB.search(snake)
    if resultB:
        return 'B'

    return 'NA'


def main(args):
    n = int(input())
    for _ in range(n):
        snake = input()
        result = identify_snake(snake)
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])

Python: n=int(input())
for times in range(n):
	s=input()
	ans="NA"

	#A???????????§
	if s[:2]==">'" and s[-1]=="~":
		t=s[2:-1].split("#")

		if len(t)==2 and len(t[0])>0:
			valid=True
			for v in t[0]:
				if v!="=":
					valid=False
					break
			if valid and t[0]==t[1]:
				ans="A"

	#B???????????§
	elif s[:2]==">^" and s[-2:]=="~~":
		t=s[2:-2]

		if len(t)>0 and len(t)%2==0:
			valid=True
			for i in range(0,len(t),2):
				if t[i:i+2]!="Q=":
					valid=False
					break
			if valid:
				ans="B"

	print(ans)

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{
		int a = 0, f = 0;
		for(int i = 1;i < ss-1;i++){
			if(s[i] == '=' && !f)a++;
			if(s[i] == '#')f = 1;
			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 <iostream>
#include <string>
using namespace std;

int main(){
	int n;
	string s;
	cin >> n;
	for(int i = 0;i < n;i++){
		cin >> s;
		if(s.substr(0,2) == ">\'"){
			if(s[s.length() - 1] != '~') cout << "NA" << endl;
			else {
				int cnt = 0,pos = 2;
				bool flag = true;
				for(;;pos++){
					if(s[pos] == '#') break;
					if(s[pos] == '=') cnt++;
					else {
						flag = false;
						break;
					}
				}
				if(!flag || !cnt) cout << "NA" << endl;
				else{
					pos++;
					for(int j = 0;j < cnt;j++){
						if(s[pos + j] != '='){
							flag = false;
							break;
						}
					}
					if(s[pos + cnt] != '~') flag = false;
					if(!flag) cout << "NA" << endl;
					else cout << "A" << endl;
				}
			}
		}else if(s.substr(0,2) == ">^"){
			if(s.length() % 2 || s.length() <= 4) cout << "NA" << endl;
			else {
				bool flag = true;
				for(int i = 1;i < s.length() / 2;i++){
					if(i != s.length() / 2 - 1 && s.substr(i * 2,2) != "Q=") flag = false;
					if(i == s.length() / 2 - 1 && s.substr(i * 2,2) != "~~") flag = false;
				}
				if(flag) cout << "B" << endl;
				else cout << "NA" << endl;
			}
		}else cout << "NA" << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#define INF 999999
using namespace std;
int main(){
    int N,j;
    cin>>N;
    while(N--){
        int runcnt=0,j;
        char S[150];
        cin>>S;
            if(S[0]=='>'&&S[1]==39){
                int K=0;
                for(j=2;;j++){
                    if(S[j]=='='){
                        runcnt++;
                        K=1;
                    }
                    else if(S[j]=='#'){
                        break;
                    }
                    else {
                        cout<<"NA"<<endl;
                        break;
                        goto end;
                    }
                }
                for(int i=j+1;;i++){
                    if(S[i]=='='){
                        runcnt--;
                    }
                    else if(runcnt==0&&S[i]=='~'&&K==1){
                        cout<<"A"<<endl;
                        break;
                    }
                    else{
                        cout<<"NA"<<endl;
                        break;
                    }
                }
            }
            else if(S[0]=='>'&&S[1]=='^'){
                int F=0;
                for(j=2;;){
                    if(S[j]=='Q'&&S[j+1]=='='){
                        j+=2;
                        F=1;
                    }
                    else if(S[j]=='~'&&S[j+1]=='~'&&F==1){
                        cout<<"B"<<endl;
                        goto end;
                    }
                    else{
                        cout<<"NA"<<endl;
                        break;
                    }
                }
            }
            else cout<<"NA"<<endl;
            end:;
    }
    return 0;
}

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

using namespace std;

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		string a;
		cin >> a;
		if (a.substr(0, 2) == ">'") {
			int counter = 0;
			bool hantei = false;
			int i;
			for (i = 2; i < a.size(); i++) {
				if (a[i] == '=') {
					counter++;
				} else if (a[i] == '#') {
					hantei = true;
					break;
				} else {
					hantei = false;
					break;
				}
			}
			if (hantei) {
				int j;
				for (j = i+1; j < a.size(); j++) {
					if (a[j] == '=') {
						counter--;
					} else if (a[j] == '~') {
						hantei = true;
						break;
					} else {
						hantei = false;
						break;
					}
				}
				if (hantei) {
					if (counter == 0 && j == a.size()-1) {
						cout << 'A' << endl;
					} else {
						cout << "NA" << endl;
					}
				} else {
					cout << "NA" << endl;
				}
			} else {
				cout << "NA" << endl;
			}
		} else if (a.substr(0, 2) == ">^") {
			int i;
			bool hantei = false;
			for (i = 2; i < a.size()-1; i += 2) {
				if (a.substr(i, 2) == "Q=") {
				} else if (a.substr(i, 2) == "~~" && i == a.size()-2) {
					hantei = true;
					break;
				} else {
					hantei = false;
					break;
				}
			}
			if (hantei) {
				cout << 'B' << endl;
			} else {
				cout << "NA" << endl;
			}
		}else {
			cout << "NA" << endl;
		}
	}
	return 0;
}