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: # -*- coding: utf-8 -*-

import sys
import os
import math

N = int(input())

def is_A(s):
    if s[0:2] != ">'":
        return False

    if s[-1] != '~':
        return False


    body = s[2:-1]

    if len(body) % 2 == 0:
        return False

    lst = body.split('#')
    if len(lst) != 2:
        return False

    if lst[0] == lst[1] and len(lst[0]) == lst[0].count('='):
        return True
    else:
        return False

def is_B(s):
    if s[0:2] != ">^":
        return False

    if s[-2:] != '~~':
        return False

    body = s[2:-2]

    if len(body) % 2 != 0:
        return False
    elif body.count('Q=') == len(body) / 2:
        return True
    else:
        return False

for s in sys.stdin:
    s = s.strip()

    if is_A(s):
        print('A')
    elif is_B(s):
        print('B')
    else:
        print('NA')

Python: import re

n=int(input())

def A(snake):
    r=re.match("^>\'(\=+)\#(\=+)\~$",snake)
    if r:
        if len(r.group(1))==len(r.group(2)):
            return True
    return False
def B(snake):
    r=re.match("^\>\^(Q=)+\~\~$",snake)
    if r:
        return True
    return False

for i in range(n):
    S=input()
    if A(S):
        print("A")
    elif B(S):
        print("B")
    else:
        print("NA")

Python: n = int(input())
for i in range(n):
    s = input()
    if s[:2] == ">'" and s[-1] == '~' and len(s) % 2 == 0:
        temp = ">'" + '=' * (s.count('=')//2) + '#' + '=' * (s.count('=')//2) + '~'
        if s == temp:
            print('A')
            continue
    elif s[:2] == ">^" and s[-2:] == '~~' and len(s) % 2 == 0:
        temp = ">^" + 'Q=' * ((len(s)-4)//2) + '~~'
        if s == temp:
            print('B')
            continue
    print('NA')

Python: import re

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

Python: def checkA(s):
  if s[:2] == ">'" and s[-1] == "~":
    body = s[2:-1]
    if len(body) >= 3 and body.count("#") == 1 and body.index("#") == len(body) // 2:
      return True
    else:
      return False
  else:
    return False

def checkB(s):
  if s[:2] == ">^" and s[-2:] == "~~":
    body = s[2:-2]
    if len(body) > 0 and len(body) % 2 == 0:
      for i in range(0, len(body), 2):
        if body[i:i + 2] != "Q=":
          return False
      else:
        return True
    else:
      return False
  else:
    return False

n = int(input())
for _ in range(n):
  s = input()
  if len(s) <= 5:
    print("NA")
  else:
    if checkA(s):
      print("A")
    elif checkB(s):
      print("B")
    else:
      print("NA")

Python: for _ in [0]*int(input()):
    s=input()
    if s[1]=='\'':
        l=s.find('#')
        r=s.rfind('~')
        print('A' if l-1==r-l else 'NA')
    else:
        r=s.rfind('~~')
        print('B' if s[1:r].count('Q')==s[1:r].count('=') else 'NA')

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

Python: # AOJ 0139 Snakes
# Python3 2018.6.19 bal4u

import re

# A >'======#======~
# B >^Q=Q=Q=Q=Q=Q=Q=Q=Q=~~
pa = ">'(=*)#(=*)~$"
pb = ">\\^(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: # -*- coding: utf-8 -*-

import sys
import os

N = int(input())

def is_A(s):
    if s[0:2] != ">'":
        return False

    if s[-1] != '~':
        return False

    body = s[2:-1]

    if len(body) % 2 == 0:
        return False

    # body = '==#=='
    center_index = len(body) // 2
    if body[center_index] != '#':
        return False

    if body.count('#') != 1:
        return False

    lst = body.split('#')
    if len(lst) != 2:
        return False

    if lst[0] == lst[1] and len(lst[0]) == lst[0].count('='):
        return True
    else:
        return False

def is_B(s):
    if s[0:2] != ">^":
        return False

    if s[-2:] != '~~':
        return False

    body = s[2:-2]

    if len(body) % 2 != 0:
        return False
    elif body.count('Q=') > 0 and body.count('Q=') == len(body) // 2:
        return True
    else:
        return False

for s in sys.stdin:
    s = s.strip()

    if is_A(s):
        print('A')
    elif is_B(s):
        print('B')
    else:
        print('NA')

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

Python: # -*- coding: utf-8 -*-

import sys
import os
import math

N = int(input())

def is_A(s):
    if s[0:2] != ">'":
        return False

    if s[-1] != '~':
        return False


    body = s[2:-1]
    lst = body.split('#')
    if len(lst) != 2:
        return False

    if lst[0] == lst[1] and len(lst[0]) == lst[0].count('='):
        return True
    else:
        return False

def is_B(s):
    if s[0:2] != ">^":
        return False

    if s[-2:] != '~~':
        return False

    body = s[2:-2]

    if body.count('Q=') == len(body) // 2:
        return True
    else:
        return False

for s in sys.stdin:
    s = s.strip()
    
    if is_A(s):
        print('A')
    elif is_B(s):
        print('B')
    else:
        print('NA')

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

Python: import re

def A(s):
  if s.count('#') != 1 or s[-1] != '~':
    return False
  a, b = s[:-1].split('#')
  return a.count('=') == b.count('=') == len(a)
def B(s):
  return re.match('^(Q=)+~~$', s)

N = input()
for i in range(N):
  s = raw_input()
  a = 'NA'
  if s.startswith(">'"):
    if A(s[2:]):
      a = 'A'
  if s.startswith('>^'):
    if B(s[2:]):
      a = 'B'
  print(a)