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: class Ex(Exception):
    pass
n=int(input())
for i in range(n):
    try:
        snk=input()
        l=len(snk)
        head=snk[:2]
        body=snk[2:l-1]
        tail=snk[l-1]
        if tail!='~' or snk[0]!='>':
            print("NA")
            continue
        spl_a=body.split('#')
        if len(spl_a)==2 and spl_a[0]==spl_a[1] and len(spl_a[0])!=0:
            for char in spl_a[0]:
                if char!='=':
                    print("NA")
                    raise Ex()
            if head[1]=='\'':
                print('A')
                raise Ex()
            else:
                print("NA")
                raise Ex()
        bars=body.split('=')
        for j in range(len(bars)-1):
            if bars[j]!='Q':
                print("NA")
                raise Ex()
        if head[1]=='^'and bars[len(bars)-1]=='~':print('B')
        else:print("NA")
    except Ex:pass

Python: n = int(input())
ans_list = []
for i in range(n):
    s = input()
    if s[0] == "~" and s[-1] == ">":
        s = s[::-1]
    if s[0] == ">" and s[1] == "'" and s[-2] == "=" and s[-1] == "~":
        check_s = s[2:-1].split("#")
        if len(check_s) == 2:
            front = check_s[0]
            back = check_s[1]
            if front == back and set(front) == {"="}:
                ans = "A"
            else:
                ans = "NA"
        else:
            ans = "NA"
    elif s[0] == ">" and s[1] == "^" and s[-2] == "~" and s[-1] == "~":
        check_s = s[2:-2]
        check_list = set(check_s.split("Q="))
        if check_list == {""} and "Q=" in check_s:
            ans = "B"
        else:
            ans = "NA"
    else:
        ans = "NA"
    ans_list.append(ans)
for a in ans_list:
    print(a)

Python: def checkA(s):

    l = len(s)
    if l % 2 == 0:
        return False

    ll = (l-1)//2

    for i in range(ll):
        if s[i] != "=" or s[l-1-i] != "=":
            return False

    if s[ll] != "#":
        return False

    return True

def checkB(s):
    l = len(s)

    if l % 2 == 1:
        return False

    ll = l // 2
    for i in range(ll):
        if s[2*i] != "Q" or s[2*i+1] != "=":
            return False

    return True

def check(s):
    if len(s) < 6:
        return "NA"

    if s[0:2] == ">'" and s[-1:] == "~":
        if checkA(s[2:-1]):
            return "A"
        else:
            return "NA"

    if s[0:2] == ">^" and s[-2:] == "~~":
        if checkB(s[2:-2]):
            return "B"
        else:
            return "NA"

    return "NA"

N = int(input())

for i in range(N):
    s = input()
    print(check(s))

Python: # AOJ 0139 Snakes
# Python3 2018.6.19 bal4u

import re

pa = ">'(=+)#(=+)~$"	# A >'======#======~
pb = ">\^(Q=)+~~$"		# B >^Q=Q=Q=Q=Q=Q=Q=Q=Q=~~

for i in range(int(input())):
	s = input()
	r = re.match(pa, s)

	if r and r.group(1) == r.group(2): print('A')
	elif re.match(pb, s): print('B')
	else: print('NA')

Python: class Ex(Exception):
    pass
n=int(input())
for i in range(n):
    try:
        snk=input()
        l=len(snk)
        head=snk[:2]
        body=snk[2:l-1]
        tail=snk[l-1]
        if tail!='~' or snk[0]!='>':
            print("NA")
            continue
        spl_a=body.split('#')
        if len(spl_a)==2 and spl_a[0]==spl_a[1]:
            for char in spl_a[0]:
                if char!='=':
                    print("NA")
                    raise Ex()
            if head[1]=='\'':
                print('A')
                raise Ex()
            else:
                print("NA")
                raise Ex()
        bars=body.split('=')
        for j in range(len(bars)-1):
            if bars[j]!='Q':
                print("NA")
                raise Ex()
        if head[1]=='^'and bars[len(bars)-1]=='~':print('B')
        else:print("NA")
    except Ex:pass

Python: import re

A = ">'(=+)#\\1~$"
B = ">\^(Q=)+~~$" 

for i in range(int(input())):
    s = input()
    r = re.match(A, s)
    if re.match(A, s): print('A')
    elif re.match(B, s): print('B')
    else: print('NA')

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:
			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)%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)

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

Python: # Aizu Problem 00139: Snakes
#
import sys, math, os, copy

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def is_A_snake(snake):
    if snake[1] != "'":
        return False
    snake = snake[2:-1]
    segments = snake.split('#')
    if len(segments) != 2:
        return False
    L = len(segments[0])
    target = '=' * L
    for segment in segments:
        if segment != target:
            return False
    return True


def is_B_snake(snake):
    if snake[1] != "^" or snake[-2] != '~' or snake[2] != 'Q' or snake[-3] != '=':
        return False
    snake = snake[2:-2]
    L = len(snake)
    return snake == "Q=" * (L // 2)


n = int(input())
for _ in range(n):
    snake = input().strip()
    if snake[0] != '>' or snake[-1] != '~':
        print("NA")
    else:
        if '#' in snake:
            print("A" if is_A_snake(snake) else "NA")
        else:
            print("B" if is_B_snake(snake) else "NA")

Python: class Ex(Exception):
    pass
n=int(input())
for i in range(n):
    try:
        snk=input()
        l=len(snk)
        head=snk[:2]
        body=snk[2:l-1]
        tail=snk[l-1]
        if tail!='~' or snk[0]!='>':
            print("NA")
            continue
        spl_a=body.split('#')
        if len(spl_a)==2 and spl_a[0]==spl_a[1]:
            for char in spl_a[0]:
                if char!='=':
                    print("NA")
                    raise Ex()
            if head[1]=='\'':
                print('A')
                raise Ex()
            else:
                print("NA")
                raise Ex()
        bars=body.split('=')
        for j in range(len(bars)-1):
            if bars[j]!='Q':
                print("NA")
                raise Ex()
        if head[1]=='^':print('B')
        else:print("NA")
    except Ex:pass

Python: #!/usr/bin/env python3
import re

ans = ['A', 'B', 'NA']
for _ in range(int(input())):
    s = input()
    l = max([1, (len(s) - 4) // 2])
    p = [r">'"+("="*l)+"#"+("="*l)+"~", r'>\^(Q=)+~~', r'\S']
    for i in range(3):
        if re.match(p[i], s):
            print(ans[i])
            break