Two sisters, A and B, play the piano every day. During the day, they can play in any order. That is, A might play first and then B, or it could be B first and then A. But each one of them plays the piano exactly once per day. They maintain a common log, in which they write their name whenever they play.
You are given the entries of the log, but you're not sure if it has been tampered with or not. Your task is to figure out whether these entries could be valid or not.

-----Input-----
- The first line of the input contains an integer $T$ denoting the number of test cases. The description of the test cases follows.
- The first line of each test case contains a string $s$ denoting the entries of the log.

-----Output-----
- For each test case, output yes or no according to the answer to the problem.

-----Constraints-----
- $1 \le T \le 500$
- $2 \le |s| \le 100$
- $|s|$ is even
- Each character of $s$ is either 'A' or 'B'

-----Example Input-----
4
AB
ABBA
ABAABB
AA

-----Example Output-----
yes
yes
no
no

-----Explanation-----
Testcase 1: There is only one day, and both A and B have played exactly once. So this is a valid log. Hence 'yes'.
Testcase 2: On the first day, A has played before B, and on the second day, B has played first. Hence, this is also a valid log.
Testcase 3: On the first day, A played before B, but on the second day, A seems to have played twice. This cannot happen, and hence this is 'no'.

Source: codechef | Difficulty: EASY
Tags: Constructive algorithms

Ad-hoc


Solutions:
for _ in range(int(input())):
	s = input()
	n = len(s)
	a = ['AB', 'BA']
	if n % 2 == 0 and all((s[i:i + 2] in a for i in range(0, len(s) - 1, 2))):
		print('yes')
	else:
		print('no')


for _ in range(int(input())):
	a = list(input())
	c = ''
	for i in range(0, len(a), 2):
		if a[i] == a[i + 1]:
			print('no')
			break
	else:
		print('yes')


T = int(input())
l1 = []
for j in range(T):
	s = input()
	flag = False
	for i in range(0, len(s) - 1, 2):
		s1 = s[i:i + 2]
		if s1[0] == s1[1]:
			print('no')
			flag = True
			break
	if flag == False:
		print('yes')


T = int(input())
for i in range(T):
	s = input()
	count = 0
	for i in range(0, len(s), 2):
		if s[i] == s[i + 1]:
			print('no')
			break
	else:
		print('yes')


t = int(input())
for j in range(t):
	s = input()
	a = 0
	for i in range(0, len(s) - 1, 2):
		if s[i] == 'A' and s[i + 1] == 'B' or (s[i] == 'B' and s[i + 1] == 'A'):
			a = a + 1
	if a == len(s) / 2:
		print('yes')
	else:
		print('no')


t = int(input())
for i in range(t):
	s = input()
	x = 0
	y = 2
	count = 0
	for i in range(len(s) // 2):
		a = s[x:y]
		if 'A' in a and 'B' in a:
			count = count + 1
		else:
			count = count
		x = x + 2
		y = y + 2
	if count == len(s) // 2:
		print('yes')
	else:
		print('no')


n = int(input())
for i in range(n):
	a = input()
	for j in range(0, len(a), 2):
		if a[j] == a[j + 1]:
			print('no')
			break
	else:
		print('yes')


t = int(input())
while t:
	P = input()
	s = 1
	for i in range(0, len(P), 2):
		if P[i] == P[i + 1]:
			s = 0
			break
		else:
			s = 1
	if s == 0:
		print('no')
	else:
		print('yes')
	t = t - 1


n = int(input())
for i in range(n):
	ser = str(input())
	for j in range(0, len(ser), 2):
		if ser[j] == 'A' and ser[j + 1] == 'B' or (ser[j] == 'B' and ser[j + 1] == 'A'):
			y = 'yes'
		else:
			y = 'no'
			break
	print(y)


n = int(input())
l = []
for i in range(0, n):
	s = input()
	for j in range(0, len(s), 2):
		if s[j] == 'A' and s[j + 1] == 'B' or (s[j] == 'B' and s[j + 1] == 'A'):
			count = 1
		else:
			l.append('no')
			count = 0
			break
	if count == 1:
		l.append('yes')
for i in l:
	print(i)


from textwrap import wrap
T = int(input())
for _ in range(T):
	log = input()
	log_chk = ['AB' in i or 'BA' in i for i in wrap(log, 2)]
	print('yes') if all(log_chk) else print('no')


for _ in range(int(input())):
	a = input()
	n = len(a)
	c = 0
	for i in range(0, n - 1, 2):
		if a[i] == 'A' and a[i + 1] == 'B' or (a[i] == 'B' and a[i + 1] == 'A'):
			c += 1
	if c == n // 2:
		print('yes')
	else:
		print('no')


def piano(s):
	acount = s.count('A')
	bcount = s.count('B')
	if acount != bcount:
		return False
	for i in range(0, len(s) - 1, 2):
		if s[i] == s[i + 1]:
			return False
	return True
for _ in range(int(input())):
	s = list(input())
	if piano(s):
		print('yes')
	else:
		print('no')


t = int(input())
for i in range(t):
	a = str(input())
	for i in range(0, len(a), 2):
		if a[i] == a[i + 1]:
			print('no')
			break
	else:
		print('yes')


t = int(input())
for i in range(t):
	x = input()
	f = 0
	for i in range(0, len(x), 2):
		if x[i] == 'A' and x[i + 1] == 'A' or (x[i] == 'B' and x[i + 1] == 'B'):
			f = 1
	if f == 1:
		print('no')
	else:
		print('yes')


def checkValidity(s):
	count = 0
	previous = ''
	for x in s:
		if count == 0:
			previous = x
			count += 1
		elif count == 1:
			count = 0
			if previous == x:
				return 'no'
	return 'yes'
t = int(input())
for _ in range(t):
	s = input()
	print(checkValidity(s))


goals = int(input())
for distractions in range(goals):
	life = input()
	for aim in range(0, len(life), 2):
		if life[aim] == life[aim + 1]:
			print('no')
			break
	else:
		print('yes')


for t in range(int(input())):
	log = input()
	reu = 'yes'
	for i in range(0, len(log) - 1, 2):
		if log[i] == log[i + 1]:
			reu = 'no'
	print(reu)


t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	for i in range(0, n, 2):
		if s[i] == s[i + 1]:
			print('no')
			break
		elif i == n - 2:
			print('yes')
		else:
			continue


n = int(input())
for i in range(n):
	str1 = input().strip()
	i = 0
	while i < len(str1) - 1:
		if str1[i] == 'A' and str1[i + 1] == 'B' or (str1[i] == 'B' and str1[i + 1] == 'A'):
			i += 2
			c = 0
		else:
			c = 1
			break
	if c != 0:
		print('no')
	else:
		print('yes')


for _ in range(int(input())):
	s = input()
	if len(s) & 1 == 1:
		print('no')
	else:
		for i in range(0, len(s), 2):
			x = s[i:i + 2]
			if x == 'AA' or x == 'BB':
				print('no')
				break
		else:
			print('yes')


n = int(input())
for i in range(n):
	entry = input()
	flag = 0
	p = 0
	q = 1
	length = len(entry) // 2
	for j in range(length):
		if entry[p] == entry[q]:
			flag = 1
			break
		p += 2
		q += 2
	if flag == 1:
		print('no')
	else:
		print('yes')


n = int(input())
for _ in range(n):
	text = input()
	flag = False
	for i in range(0, len(text) - 1, 2):
		text2 = text[i:i + 2]
		if text2[0] == text2[1]:
			print('no')
			flag = True
			break
	if flag == False:
		print('yes')


for _ in range(int(input())):
	s = input()
	l = []
	for i in range(0, len(s) - 1, 2):
		l.append([s[i], s[i + 1]])
	flag = 0
	for j in l:
		if j == ['A', 'A'] or j == ['B', 'B']:
			flag = 1
			break
	if flag == 0:
		print('yes')
	else:
		print('no')


n = int(input())
for i in range(0, n):
	c = 0
	s = input()
	for j in range(0, len(s), 2):
		if s[j] == s[j + 1]:
			c = c + 1
	if c > 0:
		print('no')
	else:
		print('yes')


def is_valid(s):
	res = True
	for i in range(0, len(s), 2):
		if s[i] == s[i + 1]:
			res = False
			break
	return res
for i in range(int(input())):
	string = input()
	if is_valid(string):
		print('yes')
	else:
		print('no')


t = int(input())
for i in range(0, t):
	ch = input()
	flag = True
	for i in range(0, len(ch), 2):
		str = ch[i] + ch[i + 1]
		if str[0] == str[1]:
			flag = False
			break
	if flag == False:
		print('no')
	else:
		print('yes')


t = int(input())
while t:
	log = input()
	size = len(log)
	for i in range(0, size, 2):
		if log[i] == log[i + 1]:
			print('no')
			break
	else:
		print('yes')
	t -= 1


def problem(log, n):
	for i in range(n // 2):
		l1 = log[2 * i]
		l2 = log[2 * i + 1]
		if l1 == l2:
			return False
	return True
for _ in range(int(input())):
	log = input()
	res = problem(log, len(log))
	if res:
		print('yes')
	else:
		print('no')


def string(s):
	x = 0
	for i in range(0, len(s) - 1, +2):
		if s[i] == s[i + 1]:
			x = 0
			return 'no'
		else:
			x = 1
	if x == 1:
		return 'yes'
for _ in range(int(input())):
	s = input()
	print(string(s))


a = []
for i in range(int(input())):
	a.append(str(input()))
for i in a:
	b = 'yes'
	for j in range(0, len(i), 2):
		if i[j] == i[j + 1]:
			b = 'no'
	print(b)


for _ in range(int(input())):
	s1 = input()
	f = 0
	for i in range(0, len(s1), 2):
		if s1[i] == s1[i + 1]:
			f = 1
	print('yes') if f == 0 else print('no')


for i in range(int(input())):
	s = input()
	flag = 0
	for j in range(0, len(s), 2):
		if not (s[j:j + 2] == 'AB' or s[j:j + 2] == 'BA'):
			print('no')
			flag = 1
			break
	if flag == 0:
		print('yes')


def solve():
	for _ in range(int(input())):
		s = str(input())
		r = 'yes'
		for i in range(0, len(s), 2):
			if s[i] == s[i + 1]:
				r = 'no'
				break
		print(r)

def __starting_point():
	solve()
__starting_point()


for _ in range(int(input())):
	arr = input()
	flag = 0
	for i in range(0, len(arr), 2):
		if arr[i:i + 2] == 'AA' or arr[i:i + 2] == 'BB':
			flag = 1
			break
	if flag == 1:
		print('no')
	else:
		print('yes')


T = int(input())
for t in range(T):
	n = input()
	for i in range(0, len(n), 2):
		if n[i] == n[i + 1]:
			print('no')
			break
	else:
		print('yes')


for i in range(int(input())):
	ls = input()
	for i in range(0, len(ls), 2):
		if ls[i] == ls[i + 1]:
			print('no')
			break
	else:
		print('yes')


for _ in range(int(input())):
	s = input()
	l = len(s)
	o = 'yes'
	for i in range(0, l, 2):
		if s[i] == s[i + 1]:
			o = 'no'
	print(o)


t = int(input())
for _ in range(t):
	s = input()
	list_of_strings = []
	count = 0
	for i in range(0, len(s), 2):
		list_of_strings.append(s[i:i + 2])
	for i in list_of_strings:
		if i == 'AB':
			count += 1
		elif i == 'BA':
			count += 1
	k = len(list_of_strings)
	if count == k:
		print('yes')
	else:
		print('no')


for _ in range(int(input())):
	string = input()
	s = True
	for i in range(1, len(string), 2):
		if string[i] == string[i - 1]:
			s = False
			break
		else:
			s = True
	if s == True:
		print('yes')
	else:
		print('no')


for _ in range(int(input())):
	s = input()
	temp = 1
	if s.count('A') != s.count('B'):
		temp = 0
	else:
		for i in range(2, len(s) + 1, 2):
			if s[:i].count('A') != s[:i].count('B'):
				temp = 0
				break
	if temp == 1:
		print('yes')
	else:
		print('no')


for _ in range(int(input())):
	s = input()
	l = []
	st = ''
	for i in range(0, len(s), 2):
		l.append(s[i] + s[i + 1])
	if 'AA' in l or 'BB' in l:
		print('no')
	else:
		print('yes')


for i in range(int(input())):
	a = input()
	res = 'yes'
	for couple in range(0, len(a), 2):
		if a[couple:couple + 2] != 'AB' and a[couple:couple + 2] != 'BA':
			res = 'no'
			break
	print(res)


for i in range(int(input())):
	s = input()
	flag = 'yes'
	c = 0
	for i in range(len(s)):
		if c == 2:
			c = 0
		c = c + 1
		if c != 2 and s[i] == s[i + 1]:
			flag = 'no'
			break
	print(flag)


T = int(input())
for i in range(T):
	data = input()
	length = len(data)
	for k in range(0, length, 2):
		if data[k] == data[k + 1]:
			print('no')
			break
	else:
		print('yes')


for _ in range(int(input())):
	s = input()
	flag = 0
	for i in range(0, len(s), 2):
		if s[i] == s[i + 1]:
			flag = 1
			break
	if flag == 0:
		print('yes')
	else:
		print('no')


test_num = int(input())
for k in range(0, test_num):
	log = input()
	if len(log) % 2 == 1:
		print('no')
		continue
	log_days = [log[i:i + 2] for i in range(0, len(log), 2)]
	valid = True
	for day in log_days:
		if day[0] == day[1]:
			valid = False
			break
	if valid:
		print('yes')
	else:
		print('no')


t = int(input())
while t > 0:
	s = input()
	i = fl = 0
	while i < len(s):
		if s[i] == s[i + 1]:
			fl = 1
			break
		i += 2
	if fl == 0:
		print('yes')
	elif fl == 1:
		print('no')
	t -= 1


for i in range(int(input())):
	s = list(input())
	(a, b, l) = ('yes', 'no', [])
	for i in range(0, len(s), 2):
		q = s[i] + s[i + 1]
		if q == 'AB' or q == 'BA':
			l.append(a)
		else:
			l.append(b)
	if b in l:
		print(b)
	else:
		print(a)


t = int(input())
for _ in range(t):
	s = input()
	c = 0
	for i in range(0, len(s) - 1, 2):
		if s[i] == 'B' and s[i + 1] == 'A' or (s[i] == 'A' and s[i + 1] == 'B'):
			pass
		else:
			c = 1
			break
	if c == 1:
		print('no')
	else:
		print('yes')


t = int(input())
for i in range(t):
	s = input()
	lst = []
	j = 0
	while j < len(s):
		lst.append(s[j:j + 2])
		j = j + 2
	flag = True
	for l in lst:
		if l[0] == l[1]:
			flag = False
			break
		elif l[0] != l[1]:
			flag = True
	if flag == True:
		print('yes')
	elif flag == False:
		print('no')


T = int(input())
for i in range(0, T):
	s = input()
	x = True
	for j in range(0, len(s) - 1, 2):
		if s[j] == 'A' and s[j + 1] == 'B' or (s[j] == 'B' and s[j + 1] == 'A'):
			continue
		else:
			x = False
			break
	if x == True:
		print('yes')
	else:
		print('no')


t = int(input())
for i in range(t):
	s = input()
	flag = 1
	for j in range(0, len(s), 2):
		k = s[j:j + 2]
		if k.count('A') == k.count('B'):
			continue
		else:
			flag = 0
	if flag == 1:
		print('yes')
	else:
		print('no')


t = int(input())
for i in range(t):
	st1 = input()
	s = len(st1) - 1
	c = 0
	for i in range(s):
		if i % 2 == 0:
			if st1[i] == st1[i + 1]:
				print('no')
				break
	else:
		print('yes')


try:
	t = int(input())
	for r in range(t):
		s = input()
		c = 0
		for i in range(0, len(s), 2):
			if s[i] == s[i + 1]:
				c += 1
		if c == 0:
			print('yes')
		else:
			print('no')
except:
	pass


n = int(input())
while n != 0:
	i = 0
	flag = True
	string = input()
	while i <= len(string) - 2:
		if string[i] == string[i + 1]:
			print('no')
			flag = False
			break
		else:
			i += 2
	if flag == True:
		print('yes')
	n -= 1


t = int(input())
for ts in range(t):
	n = input()
	c = 0
	for i in range(0, len(n) - 1, 2):
		if n[i] == 'A' and n[i + 1] == 'B' or (n[i] == 'B' and n[i + 1] == 'A'):
			c += 1
	if c == len(n) // 2:
		print('yes')
	else:
		print('no')


tc = int(input())
while tc > 0:
	tc -= 1
	s = input()
	ok = True
	for i in range(int(len(s) / 2)):
		if s[2 * i] != s[2 * i + 1]:
			continue
		else:
			ok = False
			print('no')
			break
	if ok == True:
		print('yes')


Test = int(input())
for Cases in range(Test):
	OrderofPlay = input()
	OrderofPlayLen = len(OrderofPlay)
	for Order in range(0, OrderofPlayLen, 2):
		if OrderofPlay[Order] == OrderofPlay[Order + 1]:
			print('no')
			break
	else:
		print('yes')


for _ in range(int(input())):
	s = list(input())
	j = 0
	flag = True
	while j < len(s) - 1:
		if s[j] + s[j + 1] == 'AA' or s[j] + s[j + 1] == 'BB':
			flag = False
			break
		j += 2
	if flag == False:
		print('no')
	else:
		print('yes')