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

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 len(lst[0]) > 0 and 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: import re

SNAKE_A = re.compile(r">'(=+)#\1~")
SNAKE_B = re.compile(r">\^(Q=)+~~")


def answer(regex, string, output):
    result = regex.fullmatch(string)

    if result is not None:
        print(output)
    else:
        print("NA")


for _ in range(int(input())):

    snake = input()

    if snake[1] == "'":
        answer(SNAKE_A, snake, "A")
    else:
        answer(SNAKE_B, snake, "B")

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('=')
        if len(bars)==1:
            print("NA")
            continue
        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: for i in range(input()):
  s = raw_input()
  l = (len(s) - 4) / 2
  if l > 0 and s == ">'%s#%s~" % (('=' * l,) * 2):
    print('A')
  elif l > 0 and s == '>^%s~~' % ('Q=' * l):
    print('B')
  else:
    print('NA')

Python: import re

a = ">'(=+)#(=+)~$"
b = ">\^(Q=)+~~$"
n = int(input())

for _ in range(n):
    s = input()
    r = re.match(a, s)
    if r and r.group(1) == r.group(2):
        print('A')
    elif re.match(b, s):
        print('B')
    else:
        print('NA')

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])
    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 and '=#=' in s:
        temp = ">'" + '=' * (s.count('=')//2) + '#' + '=' * (s.count('=')//2) + '~'
        if s == temp:
            print('A')
            continue
    elif s[:2] == ">^" and s[-2:] == '~~' and len(s) % 2 == 0 and 'Q=' in s:
        temp = ">^" + 'Q=' * ((len(s)-4)//2) + '~~'
        if s == temp:
            print('B')
            continue
    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=') == 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()
    a='NA'
    if s[:2]==">'" 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[:2]=='>^' and s[-2:]=='~~':
        s=s[2:-2]
        a=['NA','B'][len(s)==2*s.count('Q=') and len(s)>0]
    print(a)

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 and body.count("=") == len(body) - 1:
      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: # 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('#')
    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")