Read problems statements in mandarin chinese, russian and vietnamese as well. 

There are two players A, B playing a game. Player A has a string s with him, and player B has string t with him. Both s and t consist only of lower case English letters and are of equal length. A makes the first move, then B, then A, and so on alternatively. Before the start of the game, the players know the content of both the strings s and t.

These players are building one other string w during the game. Initially, the string w is empty. In each move, a player removes any one character from their respective string and adds this character anywhere (at any position) in the string w (Note that w is a string at all times, and you can insert between characters or at the ends. It is not an empty array where you can add a character at any index. Please see the Explanations for further clarification). If at any stage of the game, the string w is of length greater than 1 and is a palindrome, then the player who made the last move wins. 

If even after the game finishes (ie. when both s and t have become empty strings), no one is able to make the string w a palindrome, then player B wins.

Given the strings s, and t, find out which of A, B will win the game, if both play optimally.

------ Input ------ 

The first line of the input contains an integer T, corresponding to the number of test cases. The description of each testcase follows.
The first line of each testcase will contain the string s.
The second line of each testcase will contain the string t.

------ Output ------ 

For each test case, output "A" or "B" (without quotes) corresponding to the situation, in a new line.

------ Constraints ------ 

$Subtask 1 (20 points) : 1 ≤ T ≤ 500, All characters of string s are equal, All characters of string t are equal. 1 ≤ |s| = |t| ≤ 500$
$Subtask 2 (80 points) : 1 ≤ T ≤ 500, 1 ≤ |s| = |t| ≤ 500 $

----- Sample Input 1 ------ 
3

ab

ab

aba

cde

ab

cd
----- Sample Output 1 ------ 
B

A

B
----- explanation 1 ------ 
Testcase 1: If A adds 'a' to w in the first move, B can add 'a' and make the string w = "aa", which is a palindrome, and hence win. Similarly, you can show that no matter what A plays, B can win. Hence the answer is B.

Testcase 2: Player A moves with 'a', player B can put any of the character 'c', 'd' or 'e', Now Player A can create a palindrome by adding 'a'.

Testcase 3: None of the players will be able to make a palindrome of length > 1. So B will win.

Difficulty: MEDIUM_HARD
Tags: String algorithms

Data structures

Game theory
Skill types: Data structures

Starter code:

Solutions:
T = int(input())
for i in range(T):
	s = input()
	t = input()
	u1 = set(s) - set(t)
	u2 = set(t) - set(s)
	for j in u1:
		if s.count(j) > 1:
			x = 1
			break
		else:
			x = 0
	if not u1:
		print('B')
	elif u1 and (not u2):
		print('A')
	elif x == 1:
		print('A')
	else:
		print('B')


T = int(input())
for i in range(T):
	A = input()
	B = input()
	uniqueA = set(A) - set(B)
	uniqueB = set(B) - set(A)
	for i in uniqueA:
		if A.count(i) > 1:
			x = 1
			break
		else:
			x = 0
	if not uniqueA:
		print('B')
	elif uniqueA and (not uniqueB):
		print('A')
	elif x == 1:
		print('A')
	else:
		print('B')


from collections import Counter
T = int(input())
for i in range(T):
	str_s = input()
	str_t = input()
	s_count = Counter(str_s)
	t_count = Counter(str_t)
	s_unique = set(str_s) - set(str_t)
	t_unique = set(str_t) - set(str_s)
	if not s_unique:
		win = 'B'
	elif s_unique and (not t_unique):
		win = 'A'
	elif any([s_count[j] > 1 for j in s_unique]):
		win = 'A'
	else:
		win = 'B'
	print(win)


from collections import Counter
for _ in range(int(input())):
	s = input()
	p = input()
	s1 = Counter(s)
	p1 = Counter(p)
	s_unique = set(s) - set(p)
	p_unique = set(p) - set(s)
	if not s_unique:
		win = 'B'
	elif s_unique and (not p_unique):
		win = 'A'
	elif any([s1[i] > 1 for i in s_unique]):
		win = 'A'
	else:
		win = 'B'
	print(win)


from collections import Counter
for _ in range(int(input())):
	s = input()
	t = input()
	sc = Counter(s)
	tc = Counter(t)
	s_unique = set(s) - set(t)
	t_unique = set(t) - set(s)
	if not s_unique:
		win = 'B'
	elif s_unique and (not t_unique):
		win = 'A'
	elif any([sc[e] > 1 for e in s_unique]):
		win = 'A'
	else:
		win = 'B'
	print(win)


from collections import Counter
for _ in range(int(input())):
	s = input()
	t = input()
	cs = Counter(s)
	ct = Counter(t)
	if any((cs[i] > 1 and ct[i] == 0 for i in cs)) or (all((cs[i] for i in ct)) and any((ct[i] == 0 for i in cs))):
		print('A')
		continue
	print('B')


t = int(input())
for i in range(t):
	a = [str(x) for x in input()]
	b = [str(x) for x in input()]
	a_count = set(a)
	b_count = set(b)
	flag = 0
	for j in a_count:
		if a.count(j) > 1 and j not in b:
			print('A')
			break
	else:
		if len(a_count - b_count) > 0:
			if len(b_count - a_count) > 0:
				print('B')
			else:
				print('A')
		else:
			print('B')


for _ in range(int(input())):
	a = []
	b = []
	for i in range(26):
		a.append(0)
		b.append(0)
	sa = input()
	sb = input()
	for i in range(len(sa)):
		a[ord(sa[i]) - 97] += 1
		b[ord(sb[i]) - 97] += 1
	f = 0
	s = 0
	for i in range(26):
		if b[i] == 0 and a[i] >= 2:
			f = 1
			break
		elif b[i] == 0 and a[i] == 1:
			s = 1
	if f == 1:
		print('A')
	elif s == 1:
		z = 0
		for i in range(26):
			if b[i] > 0 and a[i] == 0:
				z = 1
				break
		if z == 1:
			print('B')
		else:
			print('A')
	else:
		print('B')


from string import ascii_lowercase
T = int(input())
for _ in range(T):
	a = input()
	b = input()
	A = {}
	B = {}
	for key in a:
		A[key] = A.get(key, 0) + 1
	for key in b:
		B[key] = B.get(key, 0) + 1
	winner = ''
	for key in ascii_lowercase:
		if A.get(key, 0) > 1 and B.get(key, 0) == 0:
			winner = 'A'
			break
		if A.get(key, 0) == 1 and B.get(key, 0) == 0:
			winner = 'C'
	if winner == '':
		winner = 'B'
	if winner == 'C':
		winner = 'A'
		for key in B.keys():
			if A.get(key, 0) == 0:
				winner = 'B'
				break
	print(winner)


t = int(input())
for j in range(t):
	(a, b) = (input(), input())
	(sa, sb) = (set(a), set(b))
	for i in sa:
		if a.count(i) > 1 and i not in sb:
			print('A')
			break
	else:
		if len(sa - sb) > 0:
			if len(sb - sa) > 0:
				print('B')
			else:
				print('A')
		else:
			print('B')


T = int(input())
for testCase in range(T):
	s = list(input())
	t = list(input())
	S = set(s)
	T = set(t)
	if S == T:
		winner = 'B'
	elif S < T:
		winner = 'B'
	elif T < S:
		winner = 'A'
	else:
		S_count = {}
		for i in S - T:
			if s.count(i) >= 2 and i not in T:
				winner = 'A'
				break
		else:
			winner = 'B'
	print(winner)


for t in range(int(input())):
	a = input()
	b = input()
	fa = [0] * 26
	fb = [0] * 26
	for e in a:
		fa[ord(e) - ord('a')] += 1
	for e in b:
		fb[ord(e) - ord('a')] += 1
	flagA = 0
	for i in range(26):
		if fa[i] > 1 and fb[i] == 0:
			flagA = 1
			break
	flagA1 = 0
	for i in range(26):
		if fa[i] == 1 and fb[i] == 0:
			flagA1 = 1
			break
	flagB = 0
	for i in range(26):
		if fb[i] > 0 and fa[i] == 0:
			flagB = 1
			break
	if flagA == 1 or (flagA1 == 1 and flagB == 0):
		print('A')
	else:
		print('B')


for test in range(int(input())):
	(a, b) = (input(), input())
	(aa, bb) = (set(a), set(b))
	for i in aa:
		if a.count(i) > 1 and i not in bb:
			print('A')
			break
	else:
		if len(aa - bb) > 0:
			if len(bb - aa) > 0:
				print('B')
			else:
				print('A')
		else:
			print('B')


for x in range(int(input())):
	s = input()
	t = input()
	sd = {}
	td = {}
	flag = 1
	for i in s:
		sd[i] = 0
	for i in s:
		sd[i] += 1
	for i in t:
		td[i] = 0
	for i in t:
		td[i] += 1
	for i in sd:
		if sd[i] > 1 and i not in td:
			flag = 0
			print('A')
			break
	if flag and all((i in s for i in td)) and any((j not in t for j in sd)):
		flag = 0
		print('A')
	if flag:
		print('B')


from collections import Counter

def containsall(cx, cy):
	for x in cx:
		if x not in cy:
			return False
	return True

def twice(cx, cy):
	for x in cx:
		if x not in cy and cx[x] > 1:
			return True
	return False

def lol(cx, cy):
	if containsall(cx, cy):
		return False
	if containsall(cy, cx) or twice(cx, cy):
		return True
for _ in range(int(input())):
	a = input()
	b = input()
	ca = Counter(a)
	cb = Counter(b)
	print('A' if lol(ca, cb) else 'B')


for case in range(int(input())):
	(S, T) = (input(), input())
	(setS, setT) = (set(S), set(T))
	for ch in setS:
		if S.count(ch) > 1 and ch not in setT:
			print('A')
			break
	else:
		if len(setS - setT) > 0:
			if len(setT - setS) > 0:
				print('B')
			else:
				print('A')
		else:
			print('B')


def palin(s, t):
	f = [0 for i in range(0, 26)]
	for i in range(len(s)):
		f[ord(s[i]) - ord('a')] += 1
		f[ord(t[i]) - ord('a')] += 1
	out = True
	for i in range(0, 26):
		if f[i] % 2 != 0 and f[i] != 0:
			out = False
			break
	return out
q = int(input())
while q > 0:
	s = input()
	t = input()
	a = set(s)
	b = set(t)
	out = 'B'
	if a != b and b.issubset(a):
		out = 'A'
	fa = [0 for i in range(0, 26)]
	for i in range(len(s)):
		fa[ord(s[i]) - ord('a')] += 1
	fb = [0 for i in range(0, 26)]
	for i in range(len(t)):
		fb[ord(t[i]) - ord('a')] += 1
	for i in range(0, 26):
		if fa[i] > 1 and fb[i] == 0:
			out = 'A'
			break
	print(out)
	q -= 1


T = int(input())
for i in range(T):
	w = []
	unis = []
	unit = []
	srepeat = []
	com = []
	s = str(input())
	t = str(input())
	s = list(s)
	t = list(t)
	for i in range(len(s)):
		if s[i] not in t:
			unis.append(s[i])
		else:
			com.append(s[i])
		if t[i] not in s:
			unit.append(t[i])
	for i in range(len(s)):
		elem = s[i]
		c = s.count(elem)
		if c > 1 and elem not in srepeat and (elem not in com):
			srepeat.append(elem)
	if len(unis) == 0:
		print('B')
	elif len(unis) > 0 and len(unit) == 0:
		print('A')
	elif len(com) == 0 and len(srepeat) == 0:
		print('B')
	elif len(srepeat) > 0:
		print('A')
	else:
		print('B')


def isIn(s, x):
	for k in range(0, len(s)):
		if s[k] == x:
			return True
	else:
		return False

def moreThanOne(s, x):
	counter = 0
	for k in range(0, len(s)):
		if s[k] == x:
			counter += 1
	if counter > 1:
		return True
	else:
		return False

def Palingame(s1, s2):
	global setS1
	setS1 = set([''])
	global setS2
	setS2 = set([''])
	l1 = len(s1)
	l2 = len(s2)
	for i in range(0, l1):
		setS1.add(s1[i])
	for j in range(0, l2):
		setS2.add(s2[j])
	if setS1 >= setS2:
		if len(setS1 - setS2) > 0:
			return 'A'
		else:
			return 'B'
	for i in range(0, len(s1)):
		if moreThanOne(s1, s1[i]):
			z = s1[i]
			if isIn(s2, z) == False:
				return 'A'
	else:
		return 'B'
t = int(input())
for i in range(0, t):
	Set1 = input('')
	Set2 = input('')
	print(Palingame(Set1, Set2))


tc = int(input())
while tc:
	a = input()
	b = input()
	abcd = 'abcdefghijklmnopqrstuvwxyz'
	al = []
	bl = []
	flag = 0
	rcnt = 0
	l = len(b)
	for x in abcd:
		acnt = a.count(x)
		bcnt = b.count(x)
		if acnt > 0:
			if bcnt == 0:
				if acnt > 1:
					flag = 1
					break
				else:
					flag = 2
			else:
				rcnt += bcnt
	if flag == 1 or (rcnt == l and flag == 2):
		print('A')
	else:
		print('B')
	tc -= 1


from collections import Counter
testCase = int(input())
while testCase:
	A = [x for x in input()]
	B = [x for x in input()]
	p = Counter(A)
	q = Counter(B)
	s = set(A)
	t = set(B)
	l = len(A)
	flag = 0
	if len(s - t) == 0 and len(t - s) == 0:
		print('B')
		flag = 1
	elif len(s - t) > 0 and len(t - s) <= 0:
		print('A')
		flag = 1
	elif len(t - s) > 0 and len(s - t) <= 0:
		print('B')
		flag = 1
	elif len(s - t) > 0 and len(t - s) > 0:
		for i in p.keys():
			if p[i] >= 2 and i not in B:
				flag = 1
				print('A')
				break
	if flag == 0:
		print('B')
	testCase -= 1


def ispalindrome(check):
	if check == check[::-1]:
		return True
	else:
		return False
t = int(input())
for i in range(t):
	s = input()
	t = input()
	first = len(s)
	second = len(t)
	w = ''
	palin = 'n'
	tletters = []
	sas = 'y'
	subset = ''
	checker = []
	if set(s) == set(t):
		print('B')
		continue
	for a in s:
		if not a in t and s.count(a) > 1:
			print('A')
			palin = 'y'
			break
	if palin == 'y':
		continue
	q = set(t)
	subsetcheck = 'y'
	for letter in q:
		if letter not in s:
			subsetcheck = 'n'
	if subsetcheck == 'y':
		palin = 'y'
		print('A')
		continue
	if palin == 'n':
		print('B')
		continue
	if minus == 0:
		for a in s:
			if a not in t and s.count(a) > 1:
				print('A')
				palin = 'y'
				minus += 1
				break
			if a not in t:
				w += a
				minus += 1
				x = s.replace(a, '')
				s = x
		if palin == 'y':
			continue
		if minus == 0:
			print('B')
			continue
	loop = 0
	while palin == 'n' or len(t) != 0 or len(s) != 0:
		if len(t) == 0 or len(s) == 0:
			print('B')
			break
		for a in t:
			checker = 0
			if a not in s:
				checker = 1
				temp = a
			if checker != 1:
				temp = t[0]
			for letter in range(len(w) + 1):
				aux = w[0:letter] + a + w[letter:]
				if ispalindrome(aux) == True and len(aux) > 1:
					print('B')
					palin = 'y'
					break
			if palin == 'y':
				break
		if palin == 'y':
			break
		if palin == 'n':
			w += temp
			x = t.replace(temp, '')
			t = x
		for a in s:
			checker = 0
			if a not in t:
				checker = 1
				temp = a
			if checker != 1:
				temp = s[0]
			for letter in range(len(w) + 1):
				aux = w[0:letter] + a + w[letter:]
				if ispalindrome(aux) == True and len(aux) > 1:
					print('A')
					palin = 'y'
					break
			if palin == 'y':
				break
		if palin == 'y':
			break
		if palin == 'n':
			w += temp
			x = s.replace(temp, '')
			s = x
	if palin == 'y':
		continue


import sys
n = int(input().strip())
for i in range(0, n):
	a = list(input().strip())
	b = list(input().strip())
	c = len(a)
	ct = ctr = cnt = breaker1 = u = 0
	for k in range(0, c):
		if a[k] == b[k]:
			u = u + 1
	if u == c:
		print('B')
	else:
		if c == 1:
			print('B')
		else:
			for j in range(0, c):
				for m in range(j + 1, c):
					if a[j] == a[m]:
						cnt = cnt + 1
						for p in range(0, c):
							if a[j] == b[p]:
								cnt = cnt - 1
								break
						if cnt != 0:
							breaker1 = 1
							break
				if breaker1 == 1:
					print('A')
					break
		if breaker1 == 0 and c != 1 and (u != c):
			for j in range(0, c):
				for m in range(0, c):
					if b[j] == a[m]:
						ctr = ctr + 1
						break
				for l in range(0, c):
					if a[j] == b[l]:
						ct = ct + 1
						break
			if ctr == c and ct != c:
				print('A')
			else:
				print('B')


t = int(input())
for _ in range(t):
	apos = 0
	allb = 0
	a = list(input())
	b = list(input())
	for c in b:
		if c not in a:
			allb = 1
	for c in a:
		if c not in b:
			if a.count(c) > 1 or allb == 0:
				apos += 1
				break
	if apos != 0:
		print('A')
	else:
		print('B')


for _ in range(int(input())):
	s1 = input().strip()
	s2 = input().strip()
	flag = 0
	if set(s1) == set(s2):
		flag = 2
	for ele in s1:
		if s1.count(ele) > 1 and ele not in s2:
			flag = 1
			break
	if not flag:
		cnt = 0
		for ele in s2:
			if ele in s1:
				cnt += 1
		if cnt == len(s2):
			flag = 1
	if flag == 1:
		print('A')
	else:
		print('B')


import math
for ii in range(int(input())):
	a = input()
	b = input()
	c = [0] * 26
	d = [0] * 26
	k = 0
	p = 0
	t = 0
	for i in range(len(a)):
		c[ord(a[i]) - 97] = c[ord(a[i]) - 97] + 1
		d[ord(b[i]) - 97] = d[ord(b[i]) - 97] + 1
	for i in range(26):
		if c[i] >= 2 and d[i] == 0:
			k = 1
			break
	if k == 1:
		print('A')
	else:
		for i in range(26):
			if c[i] > 0 and d[i] == 0:
				t = 1
		if t == 0:
			print('B')
		else:
			for i in range(26):
				if c[i] > 0 and d[i] == 0:
					i = 27
					for j in range(26):
						if d[j] > 0 and c[j] == 0:
							p = 1
							break
				if p == 1:
					break
			if p == 0:
				print('A')
			else:
				print('B')


t = int(input())
while t > 0:
	t -= 1
	flag = 0
	s = input()
	p = input()
	s1 = set(s)
	s2 = set(p)
	if s2 < s1:
		print('A')
	elif s1 <= s2:
		print('B')
	else:
		for i in s1 - s2:
			if s.count(i) > 1:
				print('A')
				flag = 1
				break
		if flag == 0:
			print('B')


def palingam(s, t):
	res = 'B'
	cs = [0 for i in range(26)]
	ct = [0 for i in range(26)]
	r = [0, 0, 0, 0]
	for i in range(len(s)):
		cs[ord(s[i]) - ord('a')] += 1
		ct[ord(t[i]) - ord('a')] += 1
	i = 0
	while i < 26 and r[0] != 1:
		if cs[i] > 1 and ct[i] == 0:
			r[0] += 1
		elif cs[i] == 1 and ct[i] == 0:
			r[1] += 1
		elif ct[i] > 1 and cs[i] == 0:
			r[2] += 1
		elif ct[i] == 1 and cs[i] == 0:
			r[3] += 1
		i += 1
	if r[0] or (r[2] == 0 and r[1] == 1 and (r[3] == 0)):
		res = 'A'
	return res
for i in range(int(input().strip())):
	s = input().strip()
	t = input().strip()
	print(palingam(s, t))


t = int(input())
for i in range(t):
	s = input()
	t = input()
	a = set(s)
	b = set(t)
	if a.intersection(b) is None and len(a) == len(s) and (len(b) == len(t)):
		print('B')
	elif a.issubset(b):
		print('B')
	elif b.issubset(a):
		print('A')
	elif a == b:
		print('B')
	else:
		tag = 0
		mamta = {}
		for i in s:
			if t.count(i) == 0:
				try:
					mamta[i] += 1
					if mamta[i] >= 2:
						tag = 1
						break
				except:
					mamta[i] = 1
		if tag == 1:
			print('A')
		else:
			print('B')


from collections import Counter as cnt
for _ in range(int(input())):
	s = input()
	t = input()
	s_set = set(s)
	t_set = set(t)
	s_dict = cnt(s)
	for (k, v) in s_dict.items():
		if v >= 2:
			if k not in t_set:
				print('A')
				break
	else:
		if s_set == t_set:
			print('B')
		elif t_set.issubset(s_set):
			print('A')
		elif s_set.issubset(t_set):
			print('B')
		elif not t_set.issubset(s_set) and (not s_set.issubset(t_set)):
			print('B')


def palingam(s, t):
	s = list(s)
	t = list(t)
	(w, u, ad, bd) = ([], [], [], [])
	(c, cnt) = ([], [])
	S = set(s)
	T = set(t)
	w = [[x, s.count(x)] for x in set(s)]
	for i in w:
		if i[1] > 1:
			c.append(i[0])
	n = 0
	for j in t:
		if j not in c:
			pass
		else:
			c.remove(j)
	if len(c) > 0:
		return 'A'
	else:
		buniquelen = len(T - S)
		if buniquelen != 0:
			return 'B'
		else:
			auniquelen = len(S - T)
			if auniquelen != 0:
				return 'A'
			else:
				return 'B'
T = int(float(input()))
for _ in range(T):
	s = input()
	t = input()
	print(palingam(s, t))


test = int(input())
for z in range(test):
	A = input()
	B = input()
	common = list(set(A).intersection(B))
	uncommon = [A[i] for i in range(len(A)) if A[i] not in B]
	uncommon_set = list(set(uncommon))
	uncommon_B = [x for x in B if x not in A]
	if uncommon == []:
		t = 1
	elif uncommon_B == []:
		t = 0
	else:
		for i in range(len(uncommon_set)):
			t = 1
			if uncommon.count(uncommon_set[i]) > 1:
				t = 0
				break
	if t == 1:
		print('B')
	else:
		print('A')


def answer2(frank, ursula):
	if frank == [0] * 26:
		return 'B'
	for richard in range(26):
		if ursula[richard] > 0 and frank[richard] == 0:
			return 'B'
	return 'A'

def answer(ben, emma):
	gunther = False
	for carol in range(26):
		if ben[carol] > 1 and emma[carol] == 0:
			return 'A'
		elif not gunther and ben[carol] == 1 and (emma[carol] == 0):
			gunther = True
			ben[carol] = 0
	if gunther:
		return answer2(ben, emma)
	else:
		return 'B'
monica = int(input())
for chandler in range(monica):
	ross = list(input())
	rachel = list(input())
	joey = [0] * 26
	phoebe = [0] * 26
	mike = len(ross)
	for barry in range(mike):
		joey[ord(ross[barry]) - 97] += 1
		phoebe[ord(rachel[barry]) - 97] += 1
	print(answer(joey, phoebe))


t = int(input())
for pq in range(t):
	a = list(input())
	b = list(input())
	for x in a:
		if a.count(x) > 1 and x not in b:
			print('A')
			break
		if x not in b:
			for y in b:
				if y not in a:
					break
			else:
				print('A')
				break
	else:
		print('B')


def mak_pal(x, y, b):
	if len(b) >= 1:
		y = [i for (i, v) in y]
		for (a, v) in x:
			for i in range(len(b) + 1):
				if a not in y:
					t = b[:i] + a + b[i:]
					if t == t[-1::-1]:
						return True
			for i in range(len(b) + 1):
				t = b[:i] + a + b[i:]
				if t == t[-1::-1]:
					return True
	return False
test = int(input())
for t in range(test):
	s = input()
	t = input()
	win = 'B'
	A = {}
	B = {}
	w = ''
	for i in s:
		try:
			A[i] += 1
		except:
			A[i] = 1
	for j in t:
		try:
			B[j] += 1
		except:
			B[j] = 1
	for r in range(min(len(s), len(t))):
		t = sorted(A.items(), key=lambda x: x[1], reverse=True)
		r = sorted(B.items(), key=lambda x: x[1], reverse=True)
		if mak_pal(t, r, w):
			win = 'A'
			break
		else:
			for (i, j) in enumerate(t):
				k = j[0]
				if k not in [temp for (temp, v) in r]:
					w = w + k
					if t[i][1] == 1:
						A.__delitem__(t[i][0])
						f = 1
					else:
						A[t[i][0]] += -1
					break
			else:
				w = w + t[0][0]
				if t[0][1] == 1:
					A.__delitem__(t[0][0])
					f = 1
				else:
					A[t[0][0]] += -1
		if mak_pal(r, t, w):
			win = 'B'
			break
		else:
			for (i, j) in enumerate(r):
				k = j[0]
				if k not in [temp for (temp, v) in t]:
					w = w + k
					if r[i][1] == 1:
						B.__delitem__(r[i][0])
						f = 1
					else:
						B[r[i][0]] += -1
					break
			else:
				w = w + t[0][0]
				if r[0][1] == 1:
					B.__delitem__(r[0][0])
					f = 1
				else:
					B[r[0][0]] += -1
	print(win)


for _ in range(int(input())):
	a = input()
	b = input()
	sa = set(a)
	sb = set(b)
	if sa & sb == sa:
		print('B')
	elif sa & sb == sb:
		print('A')
	else:
		l = [x for x in a if x not in b]
		if len(l) > len(set(l)):
			print('A')
		else:
			print('B')


N = int(input())
for i in range(N):
	cardA = [0] * 256
	cardB = [0] * 256
	s = input()
	for c in s:
		cardA[ord(c)] += 1
	t = input()
	for c in t:
		cardB[ord(c)] += 1
	A = False
	B_inclu = True
	A_strict = False
	for j in range(256):
		if cardA[j] > 1 and cardB[j] == 0:
			A = True
		if cardA[j] > 0 and cardB[j] == 0:
			A_strict = True
		if cardB[j] > 0 and cardA[j] == 0:
			B_inclu = False
	if A or (B_inclu and A_strict):
		print('A')
	else:
		print('B')


t = int(input())
while t:
	a = input()
	b = input()
	count = 0
	letterA = set(a)
	letterB = set(b)
	play = False
	if letterB < letterA:
		play = True
	for i in a:
		if i not in b:
			count = 0
			for j in a:
				if i == j:
					count += 1
			if count > 1:
				break
			elif count == 1 and play:
				break
	if count > 1 or (count > 0 and play):
		print('A')
	else:
		print('B')
	t -= 1


t = int(input())
while t > 0:
	a = input()
	b = input()
	dffs = [i for i in b if i not in a]
	a = [i for i in a if i not in b]
	if len(a) == 1 and len(b) != 1 and (len(dffs) == 0):
		print('A')
	elif len(a) == 1 and len(b) == 1:
		print('B')
	else:
		c = []
		flag = 0
		for i in a:
			if i in c:
				print('A')
				flag = 1
				break
			else:
				c.append(i)
		if flag == 0:
			print('B')
	t -= 1


for z in range(int(input())):
	a = list(input())
	b = list(input())
	over = 0
	new_list = set(a) - set(b)
	for xyz in new_list:
		if a.count(xyz) > 1:
			over = 1
			break
	if set(a) <= set(b):
		op = 'B'
	elif set(b) < set(a):
		op = 'A'
	elif over == 1:
		op = 'A'
	else:
		op = 'B'
	print(op)


fir1 = []
fir2 = []
for k in range(26):
	fir1.append(0)
	fir2.append(0)
for _ in range(int(input())):
	fir = list(input())
	sec = list(input())
	emp = []
	ans = 0
	for j in range(26):
		fir1[j] = 0
		fir2[j] = 0
	for j in range(len(fir)):
		fir1[ord(fir[j]) - ord('a')] += 1
		fir2[ord(sec[j]) - ord('a')] += 1
	for i in range(26):
		emp.append([fir1[i], fir2[i]])
	firstst = 0
	secondst = 0
	emp.sort(reverse=True)
	for k in range(26):
		if emp[k][0] != 0 and emp[k][1] == 0:
			firstst += 1
		if emp[k][0] == 0 and emp[k][1] != 0:
			secondst += 1
	mainst = 1
	for j in range(26):
		if emp[j][0] != 0 and emp[j][1] == 0:
			if emp[j][0] == 1:
				mainst = 23
				if secondst == 0:
					mainst = 0
				break
			else:
				mainst = 0
				break
		if mainst != 1:
			break
	if mainst == 1 or mainst == 23:
		print('B')
	else:
		print('A')


def countLetters(word):
	letterdict = {}
	for letter in word:
		letterdict[letter] = 0
	for letter in word:
		letterdict[letter] += 1
	return letterdict

def intersect(a, b):
	return list(set(a) & set(b))

def isBdifA(a1, a2):
	l = []
	l = list(set(a1) - set(a2))
	l2 = list(set(a2) - set(a1))
	if len(l) > 0 and len(intersect(a1, a2)) > 0 and (len(l2) == 0):
		return 1
	return 0
T = int(input())
for i in range(0, T):
	flag = 0
	s = input()
	t = input()
	freqA = countLetters(s)
	freqB = countLetters(t)
	for j in freqA.keys():
		if freqA[j] >= 2:
			if j not in freqB.keys():
				print('A')
				flag = 1
		if flag == 1:
			break
	if flag == 0:
		if isBdifA(freqA.keys(), freqB.keys()) == 1:
			print('A')
		else:
			print('B')


for t in range(int(input())):
	stra = input()
	strb = input()
	(a, b) = ([0] * 26, [0] * 26)
	for i in range(0, len(stra)):
		a[ord(stra[i]) - 97] += 1
		b[ord(strb[i]) - 97] += 1
	flag = 0
	for i in range(0, 26):
		if a[i] > 1 and b[i] == 0:
			flag = 1
			break
		if flag != 2 and b[i] == 0 and (a[i] != 0):
			for j in range(0, 26):
				if i != j and b[j] != 0 and (a[j] == 0):
					flag = 2
					break
			if flag != 2:
				flag = 1
				break
	if flag == 1:
		print('A')
	else:
		print('B')


from sys import stdin
t = int(stdin.readline().strip())
while t > 0:
	t -= 1
	a = stdin.readline().strip()
	b = stdin.readline().strip()
	arr = []
	A = a
	for i in a:
		c = a.replace(i, '', 1)
		if i in c:
			arr.append(i)
			a = a.replace(i, '')
	f = False
	for i in arr:
		if i not in b:
			f = True
			break
	if f == True:
		print('A')
	else:
		(c1, c2) = (False, False)
		for i in A:
			if i not in b:
				c1 = True
		for i in b:
			if i not in A:
				c2 = True
		if c1 and (not c2):
			print('A')
		else:
			print('B')


t = int(input())
for _ in range(0, t):
	a = list(input())
	b = list(input())
	winner = 'B'
	for c in a:
		if a.count(c) > 1 and c not in b:
			winner = 'A'
			break
	if winner == 'B' and set(b) < set(a):
		winner = 'A'
	print(winner)


import random
debug = False
test = False
if test:
	T = 1
else:
	T = int(input())
for t in range(T):
	if test:
		l = 10
		r = 26
		s = ''
		t = ''
		for _ in range(l):
			s += chr(ord('a') + random.randrange(r))
			t += chr(ord('a') + random.randrange(r))
		if debug:
			print(*s)
			print(*t)
	else:
		s = input()
		t = input()
		if debug:
			print(*s)
			print(*t)
	playerACantPlay = []
	playerACanPlay = []
	playerACanReallyPlay = []
	for c in s:
		if c in t:
			playerACantPlay.append(c)
		else:
			playerACanPlay.append(c)
	if len(playerACanPlay) == 0:
		if debug:
			print('What ever the letter A plays, B will play the same letter:', *playerACantPlay)
		print('B')
		continue
	countALetters = []
	for c in playerACanPlay:
		countALetters.append(s.count(c))
	if max(countALetters) > 1:
		if debug:
			print('Player A can play a letter twice and B does not have this letter:', *[c for (i, c) in enumerate(playerACanPlay) if countALetters[i] > 1])
		print('A')
		continue
	for i in range(len(countALetters)):
		if countALetters[i] == 1:
			playerACanReallyPlay.append(playerACanPlay[i])
	if debug:
		print('Letter A owns once but not B:', *playerACanReallyPlay)
	playerBCanPlay = []
	for c in t:
		if c in s:
			pass
		else:
			playerBCanPlay.append(c)
	if len(playerBCanPlay) == 0:
		if debug:
			print('What ever B plays, A will play the same and create a palindrom')
		print('A')
		continue
	if debug:
		print('Letter B owns but not A:', *playerBCanPlay)
	print('B')


n = int(input())
for i in range(0, n, 1):
	flag = 0
	t = 0
	z = 0
	a = input()
	b = input()
	e = []
	f = []
	g = []
	u = []
	h = []
	x = ''.join(sorted(a))
	y = ''.join(sorted(b))
	for j in range(0, len(x) - 1, 1):
		if x[j] == x[j + 1]:
			flag = 1
			u.append(x[j])
	e = list(set(x) & set(y))
	if len(e) > 0:
		f = list(set(x) - set(e))
		g = list(set(y) - set(e))
		h = list(set(u) - set(e))
		if len(f) > 0 and len(g) == 0:
			print('A')
		elif len(f) > 0 and len(g) > 0:
			if len(h) > 0:
				print('A')
			else:
				print('B')
		elif len(f) == 0:
			print('B')
	elif flag == 1:
		print('A')
	else:
		print('B')


def solve(s, t):
	s = list(s)
	t = list(t)
	s.sort()
	t.sort()
	if len(s) == 1:
		return 'B'
	if s == t:
		return 'B'
	set_s = set(s)
	set_t = set(t)
	intersect = set_s & set_t
	if len(intersect) > 0:
		if set_s == set_t:
			return 'B'
		for s_idx in range(1, len(s)):
			if s[s_idx] == s[s_idx - 1] and s[s_idx] not in set_t:
				return 'A'
		if not set_s.issubset(intersect) and set_t.issubset(intersect):
			return 'A'
	elif len(s) != len(set_s):
		return 'A'
	return 'B'
T = int(input())
for _ in range(T):
	s = input()
	t = input()
	print(solve(s, t))


from collections import defaultdict
for test in range(int(input())):
	s = input()
	t = input()
	win = 'B'
	s_count = defaultdict(int)
	if set(s) > set(t):
		win = 'A'
	elif len(set(t)) == 1 and list(set(t))[0] in s and (len(set(s)) > 1):
		win = 'A'
	else:
		for i in s:
			s_count[i] = s_count[i] + 1
			if s_count[i] > 1 and i not in t:
				win = 'A'
				break
	print(win)


tc = int(input())
for x in range(tc):
	s = input()
	t = input()
	c = 0
	str1 = ''
	str2 = ''
	str3 = ''
	str4 = ''
	str5 = ''
	for i in s:
		c = s.count(i)
		if c > 1:
			str1 += i
	for i in s:
		if i not in t:
			str4 += i
	for i in t:
		if i not in s:
			str5 += i
	for i in str1:
		if i not in t:
			str2 += i
	for i in s:
		if i in t:
			str3 += i
	if str3 == str1 and str3 == str2:
		print('B')
	elif len(str2) > 0:
		print('A')
	elif len(str3) < len(s) and len(str5) == 0:
		print('A')
	else:
		print('B')


t = int(input())
for i in range(t):
	s1 = input()
	s2 = input()
	freq1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
	freq2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
	max1 = 0
	pos = -1
	s = 0
	ss1 = 0
	ss2 = 0
	max2 = 0
	max3 = 0
	pos2 = -1
	for j in range(len(s1)):
		c = ord(s1[j]) - ord('a')
		b = ord(s2[j]) - ord('a')
		freq1[c] = freq1[c] + 1
		freq2[b] = freq2[b] + 1
	for j in range(26):
		if freq1[j] > 0 and freq2[j] > 0:
			s += 1
		if freq1[j] > 0:
			ss1 += 1
		if freq2[j] > 0:
			ss2 += 1
		if freq2[j] > max2:
			max2 = freq2[j]
			pos2 = j
		if freq1[j] > max1:
			max1 = freq1[j]
		if freq1[j] > max3 and freq2[j] == 0:
			max3 = freq1[j]
	if max3 > 1:
		print('A')
	elif ss2 == s and ss1 > s:
		print('A')
	else:
		print('B')


def fun():
	a = int(input())
	for r in range(a):
		s = list(input().strip())
		t = list(input().strip())
		if len(set(s) - set(t)) == 0:
			print('B')
			continue
		if len(set(t) - set(s)) == 0:
			print('A')
			continue
		for k in s:
			if k in s[s.index(k) + 1:] and k not in t:
				print('A')
				break
		else:
			print('B')
fun()


t = int(input())
for test in range(t):
	s = input()
	t = input()
	ans = 'B'
	Ds = {}
	Dt = {}
	for i in s:
		if i in Ds:
			Ds[i] += 1
		else:
			Ds[i] = 1
	for i in t:
		if i in Dt:
			Dt[i] += 1
		else:
			Dt[i] = 1
	setS = set(s)
	setT = set(t)
	s_t = setS - setT
	t_s = setT - setS
	if len(s_t) != 0:
		if len(t_s) == 0:
			ans = 'A'
		else:
			for i in s_t:
				if Ds[i] > 1:
					ans = 'A'
					break
	print(ans)


t = int(input())
while t > 0:
	s1 = input()
	s2 = input()
	l1 = len(s1)
	flag = 0
	temp = 0
	flag2 = 0
	count = 0
	count1 = 0
	count2 = 0
	flag3 = 0
	flag1 = 0
	cnf = 0
	check1 = list(set(s1))
	check2 = list(set(s2))
	for item in check1:
		if item in s2:
			count1 += 1
	for item in check2:
		if item in s1:
			count2 += 1
	if count1 == len(check1):
		flag1 = 1
	if count2 == len(check2):
		flag2 = 1
	for i in range(l1):
		temp = s1.count(s1[i])
		if temp >= 2:
			flag = 1
			break
	for i in range(l1):
		temp = s1.count(s1[i])
		if temp >= 2:
			if s1[i] not in s2:
				cnf = 1
				break
	if l1 == 1:
		print('B')
	elif s1 == s2:
		print('B')
	else:
		p = set(s1) & set(s2)
		if p:
			if cnf == 1:
				print('A')
			elif flag1 == 1:
				print('B')
			elif flag2 == 1:
				print('A')
			else:
				print('B')
		elif flag == 1:
			print('A')
		else:
			print('B')
	t -= 1


from collections import Counter
import sys
sys.setrecursionlimit(1000000000)

def isPalindrome(word):
	for i in range(len(word) // 2):
		if word[i] != word[-1 - i]:
			return False
	return True

def myPlayerWins(myCount, yourCount, word):
	mySize = sum(myCount.values())
	yourSize = sum(yourCount.values())
	cutoff = 5
	if len(word) >= 5:
		if mySize == yourSize:
			return False
		else:
			return True

	def getSubproblems():
		for c in myCount.keys():
			for i in range(len(word) + 1):
				if word == [] or (i == 0 and word[i][0] != c) or (i == len(word) and word[i - 1][0] != c) or (word[i - 1][0] != c and word[i][0] != c):
					newWord = word[:i] + [(c, 1)] + word[i:]
					yield (c, newWord)
			for (i, pair) in enumerate(word):
				if pair[0] == c:
					newWord = word[:i] + [(pair[0], pair[1] + 1)] + word[i + 1:]
					yield (c, newWord)
				else:
					for j in range(1, pair[1]):
						newWord = word[:i] + [(pair[0], j), (c, 1), (pair[0], pair[1] - j)] + word[i + 1:]
						yield (c, newWord)
	if len(myCount) == 0:
		return False
	q = []
	for (c, newWord) in getSubproblems():
		if (len(newWord) > 1 or (len(newWord) == 1 and newWord[0][1] > 1)) and isPalindrome(newWord):
			return True
		q.append((c, newWord))
	for (c, newWord) in q:
		newMyCount = myCount.copy()
		newMyCount[c] -= 1
		if newMyCount[c] == 0:
			del newMyCount[c]
		if not myPlayerWins(yourCount, newMyCount, newWord):
			return True
	return False
T = int(input())
for _ in range(T):
	s = input().strip()
	t = input().strip()
	count1 = Counter(s)
	count2 = Counter(t)
	word = []
	if myPlayerWins(count1, count2, word):
		print('A')
	else:
		print('B')


def cal(s, x):
	count = 0
	for i in s:
		if i == x:
			count += 1
	return count
t = int(input())
while t:
	t -= 1
	t1 = input()
	t2 = input()
	a = set(t1)
	b = set(t2)
	if a != b:
		if a & b == set() and len(t1) > len(a):
			print('A')
		elif a > b:
			print('A')
		else:
			c = a - b
			for y in c:
				if cal(t1, y) > 1:
					print('A')
					break
			else:
				print('B')
	else:
		print('B')


t = int(input())
while t > 0:
	t -= 1
	s1 = input()
	s2 = input()
	se1 = set(s1)
	se2 = set(s2)
	c = False
	n = len(s1)
	l1 = [0] * 26
	l2 = [0] * 26
	for i in range(n):
		l1[ord(s1[i]) - ord('a')] += 1
		l2[ord(s2[i]) - ord('a')] += 1
	for i in se1 - se2:
		if l1[ord(i) - ord('a')] > 1:
			c = True
	if se1 & se2 == se2 and se1 != se2:
		print('A')
	elif c:
		print('A')
	else:
		print('B')


import sys
t = int(sys.stdin.readline())

def any(iter, f):
	ans = False
	for x in iter:
		ans |= f(x)
	return ans
for i in range(t):
	s = sys.stdin.readline().strip()
	r = sys.stdin.readline().strip()
	fs = {x: 0 for x in set(s)}
	fr = {x: 0 for x in set(r)}
	for x in list(s):
		fs[x] += 1
	for x in list(r):
		fr[x] += 1
	if any(set(s) - set(r), lambda x: fs[x] > 1) or (set(r) - set(s) == set([]) and set(s) - set(r) != set([])):
		sys.stdout.write('A\n')
	else:
		sys.stdout.write('B\n')


def func():
	a = input()
	b = input()
	(x, y) = (sorted(a), sorted(b))
	w = []
	x1 = list(set(a))
	y1 = list(set(b))
	x1.sort()
	y1.sort()
	xsy = True
	ysx = True
	for i in x1:
		if i not in y1:
			xsy = False
			break
	for i in y1:
		if i not in x1:
			ysx = False
			break
	if xsy == True and ysx == True:
		print('B')
		return 1
	elif xsy == True and ysx == False:
		print('B')
		return 1
	elif xsy == False and ysx == True:
		print('A')
		return 1
	x_temp = []
	for i in range(len(x) - 1):
		if x[i] == x[i + 1]:
			if x[i] not in x_temp:
				x_temp.append(x[i])
	for i in x_temp:
		if i not in y:
			print('A')
			return 1
	print('B')
t = int(input())
while t > 0:
	t -= 1
	func()


T = int(input())
for i in range(T):
	s = input()
	t = input()
	flag = 0
	win = 0
	s_arr = [0] * 26
	uniq_a_arr = [0] * 26
	uniq_b_arr = [0] * 26
	t_arr = [0] * 26
	uniq_a = 0
	uniq_b = 0
	for c in s:
		s_arr[ord(c) - 97] += 1
		if s_arr[ord(c) - 97] > 1:
			flag = 1
	for c in t:
		t_arr[ord(c) - 97] += 1
	for j in range(26):
		if s_arr[j] != 0 and t_arr[j] == 0:
			uniq_a += 1
			uniq_a_arr[j] = s_arr[j]
			if uniq_a_arr[j] >= 2:
				win = 1
				break
		if t_arr[j] != 0 and s_arr[j] == 0:
			uniq_b += 1
	if uniq_a == 0:
		print('B')
		continue
	elif win == 1:
		print('A')
		continue
	elif uniq_b == 0:
		print('A')
		continue
	else:
		print('B')
		continue
	print(s_arr)


t = int(input())
i = 0
while i < t:
	s = input()
	t1 = input()
	li = list(s)
	l1 = list(set(li))
	li1 = list(t1)
	l2 = list(set(li1))
	flag1 = 0
	for j in li:
		if li.count(j) > 1:
			flag1 = 1
			break
	flag2 = 0
	for j in li1:
		if li1.count(j) > 1:
			flag2 = 1
			break
	li1_c = []
	li1_nc = []
	li2_c = []
	li2_nc = []
	for x in li:
		if li1.count(x) >= 1:
			li1_c.append(x)
		else:
			li1_nc.append(x)
	for x in li1:
		if li.count(x) >= 1:
			li2_c.append(x)
		else:
			li2_nc.append(x)
	lil_c = list(set(li1_c))
	li1_nc = list(set(li1_nc))
	li2_c = list(set(li2_c))
	li2_nc = list(set(li2_nc))
	l1_c = []
	l1_nc = []
	l2_c = []
	l2_nc = []
	for x in li1_c:
		l1_c.append(li.count(x))
	for x in li1_nc:
		l1_nc.append(li.count(x))
	for x in li2_c:
		l2_c.append(li1.count(x))
	for x in li2_nc:
		l2_nc.append(li1.count(x))
	li2 = list(set(li1).intersection(set(li)))
	if len(set(li)) == 1 and len(set(li1)) == 1:
		if (flag1 == 1 and flag2 == 0) and len(li2) == 0 or ((flag1 == 1 and flag2 == 1) and len(li2) == 0):
			print('A')
		elif len(li2) >= 1:
			print('B')
		elif flag1 == 0 and flag2 == 1 or (flag1 == 0 and flag2 == 0):
			print('B')
	else:
		w = ''
		flaga = -1
		flagb = -1
		while len(li1_c) > 0 or len(li1_nc) > 0 or len(li2_c) > 0 or (len(li2_nc) > 0):
			if len(li1_nc) > 0 and len(w) == 0:
				m = max(l1_nc)
				ind = l1_nc.index(m)
				ch = li1_nc[ind]
				l1_nc[ind] -= 1
				if l1_nc[ind] == 0:
					li1_nc.remove(li1_nc[ind])
			elif len(li1_c) > 0 and len(w) == 0:
				if len(w) == 0:
					m = max(l1_c)
					ind = l1_c.index(m)
					ch = li1_c[ind]
					l1_c[ind] -= 1
					if l1_c[ind] == 0:
						li1_c.remove(li1_c[ind])
			elif (len(li1_nc) > 0 or len(li1_c) > 0) and len(w) == 2:
				ch1 = w[0]
				ch2 = w[1]
				if li1_nc.count(ch1) >= 1 or li1_nc.count(ch2) >= 1:
					flaga = 1
					break
				elif li1_c.count(ch1) >= 1 or li1_c.count(ch2) >= 1:
					flaga = 1
					break
				else:
					flagb = 1
					break
			w += ch
			if len(li1_nc) >= 0:
				if len(w) == 1:
					if li2_c.count(ch) >= 1:
						flagb = 1
						break
					else:
						if len(li2_nc) > 0:
							m = max(l2_nc)
							ind = l2_nc.index(m)
							ch = li2_nc[ind]
							l2_nc[ind] -= 1
							if l2_nc[ind] == 0:
								li2_nc.remove(li2_nc[ind])
						else:
							m = max(l2_c)
							ind = l2_c.index(m)
							ch = li2_c[ind]
							l2_c[ind] -= 1
							if l2_c[ind] == 0:
								li2_c.remove(li2_c[ind])
						w += ch
		if flaga == 1:
			print('A')
		else:
			print('B')
	i += 1


def condition(d, d_, val):
	dic = {}
	for k in d:
		flag = True
		try:
			dic[k] += 1
		except:
			dic[k] = 1
		for k1 in d_:
			if k == k1:
				flag = False
				break
		if flag and dic[k] > val:
			return True
	return False

def condition2(d, d_):
	return condition(d, d_, 1)

def condition3(d, d_):
	res = condition(d, d_, 0)
	if not res:
		return False
	for k in d_:
		flag = True
		for k1 in d:
			if k == k1:
				flag = False
				break
		if flag:
			return False
	return True

def solve(s, t):
	if condition2(s, t) or condition3(s, t):
		return 'A'
	return 'B'
from sys import stdin
test = int(stdin.readline().strip())
while test > 0:
	s = stdin.readline().strip()
	t = stdin.readline().strip()
	ans = solve(s, t)
	print(ans)
	test -= 1


t = int(input())
for y in range(t):
	count = [0] * 256
	countb = [0] * 256
	a = input()
	b = input()
	n = len(a)
	for i in a:
		count[ord(i)] += 1
	for i in b:
		countb[ord(i)] += 1
	c = 0
	d = 0
	e = 0
	for i in range(256):
		if int(count[i]) > 1:
			if int(countb[i]) == 0:
				c = 1
				break
			else:
				e += count[i] + countb[i]
		elif int(count[i]) == 1:
			if int(countb[i]) == 0:
				d += 1
			else:
				e += 1 + countb[i]
	if c == 1:
		print('A')
	elif 2 * n == e + d and d > 0:
		print('A')
	else:
		print('B')


T = int(input())
(s, t) = ([], [])
for i in range(T):
	s.append(input())
	t.append(input())
for i in range(T):
	(dnis, dis, dnit, dit) = ({}, {}, {}, {})
	flag = 0
	for st in s[i]:
		if st not in t[i]:
			if st not in dnis.keys():
				dnis[st] = 1
			else:
				print('A')
				dnis[st] += 1
				flag = 1
				break
		elif st not in dis.keys():
			dis[st] = 1
		else:
			dis[st] += 1
	for st in t[i]:
		if st not in s[i]:
			if st not in dnit.keys():
				dnit[st] = 1
			else:
				dnit[st] += 1
		elif st not in dit.keys():
			dit[st] = 1
		else:
			dit[st] += 1
	if flag == 0:
		if list(dnis.keys()) == []:
			print('B')
		elif list(dnit.keys()) == []:
			print('A')
		else:
			print('B')


for _ in range(int(input())):
	s = sorted(input())
	t = sorted(input())
	flag = 0
	if len(s) == 1:
		print('B')
		continue
	if set(s) == set(t):
		print('B')
		continue
	if len(s) - len(set(s)) > 0:
		for i in range(0, len(s) - 1):
			if s[i] == s[i + 1] and s[i] not in t:
				print('A')
				flag = 1
				break
		if flag == 1:
			continue
	if len(set(s) - set(t)) != 0 and len(set(t) - set(s)) == 0:
		print('A')
		continue
	if flag == 0:
		print('B')


t = int(input())
string = 'abcdefghijklmnopqrstuvwxyz'
for _ in range(t):
	flag = 1
	a = input()
	b = input()
	same = []
	diffa = []
	diffb = []
	for char in string:
		if char in a and char in b:
			same.append(char)
	for char in string:
		if char in a and char not in same:
			diffa.append(char)
		if char in b and char not in same:
			diffb.append(char)
	for char in string:
		if a.count(char) > 1 and char not in same:
			flag = 0
			print('A')
			break
	if flag:
		if len(diffa) >= 1 and len(diffb) >= 1:
			print('B')
		elif len(diffa) >= 1 and len(diffb) == 0:
			print('A')
		else:
			print('B')


import string
letters = string.ascii_lowercase
T = int(input())
for t in range(T):
	s = input()
	t = input()
	A_set = set([i for i in s])
	B_set = set([i for i in t])
	if A_set <= B_set:
		print('B')
		continue
	if B_set < A_set:
		print('A')
		continue
	A = dict()
	B = dict()
	for i in letters:
		A[i] = 0
		B[i] = 0
	for i in s:
		A[i] += 1
	for i in t:
		B[i] += 1
	exit_flag = False
	for i in letters:
		if A[i] >= 2 and B[i] == 0:
			print('A')
			exit_flag = True
			break
	if exit_flag:
		continue
	else:
		print('B')


t = int(input())
for i in range(t):
	s1 = list(input())
	s2 = list(input())
	ch1 = 0
	ch2 = 0
	for j in s1:
		if s1.count(j) > 1 and j not in s2:
			ch1 = 1
			break
	if set(s2) < set(s1):
		ch2 = 1
	if ch1 == 1 or ch2 == 1:
		print('A')
	else:
		print('B')


T = int(input())
while T > 0:
	s = input()
	t = input()
	N = len(s)
	D = []
	C = []
	A = []
	M = len(t)
	for i in range(0, N):
		if s[i] not in t:
			if s.count(s[i]) >= 2:
				D.append(s[i])
			else:
				C.append(s[i])
	for i in range(0, M):
		if t[i] not in s:
			A.append(t[i])
	if len(D) != 0:
		print('A')
	elif len(A) == 0 and len(C) != 0 and (s != t):
		print('A')
	else:
		print('B')
	T = T - 1


t = int(input())
for i in range(0, t):
	s = input()
	t = input()
	a = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
	b = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
	for char in s:
		a[char] += 1
	for char in t:
		b[char] += 1
	a_minus_b = []
	b_minus_a = []
	for char in s:
		if b[char] == 0:
			a_minus_b.append(char)
	for char in t:
		if a[char] == 0:
			b_minus_a.append(char)
	if len(a_minus_b) > 0:
		for char in a_minus_b:
			if a[char] > 1:
				print('A')
				break
		else:
			if len(b_minus_a) > 0:
				print('B')
			else:
				print('A')
	else:
		print('B')


t = int(input())
for z in range(0, t):
	s = input()
	t = input()
	s_dict = {}
	t_dict = {}
	for i in s:
		if i not in s_dict:
			s_dict[i] = 1
		else:
			s_dict[i] += 1
	for i in t:
		if i not in t_dict:
			t_dict[i] = 1
		else:
			t_dict[i] += 1
	for i in s_dict:
		if s_dict[i] > 1:
			try:
				if t_dict[i]:
					continue
			except:
				print('A')
				break
	else:
		for i in t_dict:
			try:
				if s_dict[i]:
					continue
			except:
				print('B')
				break
		else:
			for i in s_dict:
				try:
					if t_dict[i]:
						continue
				except:
					print('A')
					break
			else:
				print('B')


test = int(input())
for _ in range(test):
	fa = [0] * 27
	fb = [0] * 27
	s = input()
	t = input()
	f = 0
	ans = False
	if s == t:
		f = 1
		ans = False
	for letter in s:
		index = ord(letter) - 96
		fa[index] += 1
	for letter in t:
		index = ord(letter) - 96
		fb[index] += 1
	if f == 0:
		for letter in s:
			index = ord(letter) - 96
			if fa[index] > 1 and fb[index] == 0:
				ans = True
				f = 1
				break
	if f == 0:
		count_1 = len(t)
		for letter in t:
			if letter in s:
				count_1 -= 1
		count_2 = len(s)
		for letter in s:
			if letter in t:
				count_2 -= 1
		if count_1 == 0 and count_2 > 0:
			ans = True
	if ans == True:
		print('A')
	else:
		print('B')


t = int(input())

def main():
	a = input()
	b = input()
	ah = [0] * 26
	bh = [0] * 26
	asciiBase = ord('a')
	for x in [ord(i) for i in a]:
		ah[x - asciiBase] += 1
	for x in [ord(i) for i in b]:
		bh[x - asciiBase] += 1
	for i in range(26):
		if ah[i] > 1 and bh[i] == 0:
			print('A')
			return
	aset = set(a)
	bset = set(b)
	if len(aset - bset) and (not len(bset - aset)):
		print('A')
		return
	print('B')
while t > 0:
	main()
	t -= 1


def main():
	test_case = int(input())
	for t in range(test_case):
		A = String()
		B = String()
		s = input()
		for c in s:
			A.insert(c)
		A.makeSet()
		s = input()
		for c in s:
			B.insert(c)
		B.makeSet()
		Ax = A - B
		Bx = B - A
		if not bool(Ax):
			print('B')
		elif not bool(Bx):
			print('A')
		elif isCount(Ax):
			print('A')
		else:
			print('B')

class Alphabet:

	def __init__(self, character, number):
		self.alphabet = character
		self.count = number

	def __eq__(self, other):
		return self.alphabet == other.alphabet

	def __hash__(self):
		return ord(self.alphabet)

class String:

	def __init__(self):
		self.count = []
		self.alphabet = []
		for i in range(26):
			self.count.append(0)

	def insert(self, character):
		self.count[ord(character) - ord('a')] += 1

	def makeSet(self):
		string_set = []
		for i in range(26):
			if self.count[i] != 0:
				string_set.append(Alphabet(chr(i + ord('a')), self.count[i]))
		self.alphabet = set(string_set)

	def __sub__(self, other):
		return self.alphabet - other.alphabet

def isCount(set_alphabet):
	for it in set_alphabet:
		if it.count > 1:
			return True
main()


def codechef():
	t = int(input())
	for i in range(0, t):
		s1 = input()
		s2 = input()
		c = 0
		f = 0
		c2 = 0
		for i in range(0, len(s1)):
			if s1.count(s1[i]) > 0 and s2.count(s1[i]) == 0:
				f = 1
			if s1.count(s1[i]) > 1 and s2.count(s1[i]) == 0:
				c = 1
				break
		for j in range(0, len(s2)):
			if s1.count(s2[j]) > 0:
				c2 = c2 + 1
		if c2 == len(s2) and f:
			c = 1
		if c == 1:
			print('A')
		else:
			print('B')
codechef()


t = int(input())
for i in range(t):
	a = [0] * 26
	b = [0] * 26
	flag = 0
	s = input().rstrip('\r')
	s1 = input().rstrip('\r')
	for j in range(len(s)):
		a[ord(s[j]) - 97] += 1
		b[ord(s1[j]) - 97] += 1
	count1 = 0
	count2 = 0
	f = 0
	c = []
	for j in range(26):
		if a[j] > 1 and b[j] == 0:
			count1 += 1
		if a[j] == 1 and b[j] == 0:
			c.append(j)
			count2 += 1
		if b[j] > 0 and a[j] == 0:
			f = 1
	if len(s) == 1:
		print('B')
	elif count1 > 0 or (count2 >= 1 and f == 0):
		print('A')
	else:
		print('B')


from collections import Counter
for t in range(int(input())):
	(A, B) = (input().strip(), input().strip())
	(sa, sb) = (set(A), set(B))
	(ca, cb) = (Counter(A), Counter(B))
	if sa <= sb:
		winner = 'B'
	elif sa > sb:
		winner = 'A'
	else:
		winner = 'B'
		sda = sa - sb
		for c in sda:
			if ca[c] > 1:
				winner = 'A'
				break
	print(winner)


def run(s, t):
	sol = 'B'
	arr = ''.join(t)
	nis = set(s)
	nar = set(t)
	if len(nis) - len(nar) == 1:
		for el in nis:
			if el in nar:
				continue
			else:
				print('A')
				return
	for j in range(len(s) - 1):
		boo = arr.find(s[j]) < 0
		nar = set(t)
		if boo and s[j] == s[j + 1] or (boo and len(nar) == 1 and (s[j + 1] in nar)):
			sol = 'A'
			break
	print(sol)
n = int(input())
for k in range(n):
	a = sorted(input())
	b = sorted(input())
	run(a, b)