There are two towers consisting of blocks of two colors: red and blue. Both towers are represented by strings of characters B and/or R denoting the order of blocks in them from the bottom to the top, where B corresponds to a blue block, and R corresponds to a red block.

These two towers are represented by strings BRBB and RBR.

You can perform the following operation any number of times: choose a tower with at least two blocks, and move its top block to the top of the other tower.

The pair of towers is beautiful if no pair of touching blocks has the same color; i. e. no red block stands on top of another red block, and no blue block stands on top of another blue block.

You have to check if it is possible to perform any number of operations (possibly zero) to make the pair of towers beautiful.


-----Input-----

The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.

Each test case consists of three lines:

the first line contains two integers $n$ and $m$ ($1 \le n, m \le 20$) — the number of blocks in the first tower and the number of blocks in the second tower, respectively;

the second line contains $s$ — a string of exactly $n$ characters B and/or R, denoting the first tower;

the third line contains $t$ — a string of exactly $m$ characters B and/or R, denoting the second tower.


-----Output-----

For each test case, print YES if it is possible to perform several (possibly zero) operations in such a way that the pair of towers becomes beautiful; otherwise print NO.

You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).


-----Examples-----

Input
4
4 3
BRBB
RBR
4 7
BRBR
RRBRBRB
3 4
RBR
BRBR
5 4
BRBRR
BRBR
Output
YES
YES
YES
NO


-----Note-----

In the first test case, you can move the top block from the first tower to the second tower (see the third picture).

In the second test case, you can move the top block from the second tower to the first tower $6$ times.

In the third test case, the pair of towers is already beautiful.

Source: codeforces | Difficulty: EASY
Tags: String algorithms

Implementation

Complete search


Solutions:
for _ in range(int(input())):
	C = 0
	input().split()
	a = str(input())
	b = str(input())
	a = a + b[::-1]
	for i in range(len(a) - 1):
		if a[i] == a[i + 1]:
			C += 1
	print('YNEOS'[C > 1::2])


from math import *
for z in range(int(input())):
	(n, m) = map(int, input().split())
	s = input()
	s += input()[::-1]
	if s.count('RR') + s.count('BB') > 1 or s.count('RRR') > 0 or s.count('BBB') > 0:
		print('NO')
	else:
		print('YES')


t = int(input().strip())

def PyIsStupid():
	(n, m) = map(int, input().strip().split())
	a = input()
	b = input()
	b = b[::-1]
	a += b
	c = 0
	for i in range(1, len(a)):
		if a[i] == a[i - 1]:
			c += 1
	if c <= 1:
		print('YES')
	else:
		print('NO')
for _ in range(t):
	PyIsStupid()


import sys
input = iter(sys.stdin.read().splitlines()).__next__
TC = int(input())
for tc in range(TC):
	(N, M) = map(int, input().split())
	S = input().strip()
	T = input().strip()
	ST = S + T[::-1]
	res = [i for i in range(1, len(ST)) if ST[i] == ST[i - 1]]
	print('YES' if len(res) <= 1 else 'NO')


for _ in range(int(input())):
	(A, B) = map(int, input().split())
	A = input()
	B = input()
	C = A + B[::-1]
	last = None
	ans = 0
	for c in C:
		if c == last:
			ans += 1
		last = c
	if ans <= 1:
		print('YES')
	else:
		print('NO')


import sys

def solution():
	input()
	first = input()
	second = input()
	full = first + second[::-1]
	cnt = 0
	for idx in range(len(full) - 1):
		if full[idx] == full[idx + 1]:
			cnt += 1
	if cnt > 1:
		print('NO')
	else:
		print('YES')
t = int(input())
for _ in range(t):
	solution()


range_ = int(input())
for i in range(range_):
	(k, l) = input().split()
	first = input()
	second = input()[::-1]
	full = first + second
	cnt = -1
	prev = full[0]
	for el in full:
		if el == prev:
			cnt += 1
		prev = el
	if cnt < 2:
		print('YES')
	else:
		print('NO')


t = int(input())

def sol():
	num = list(map(int, input().split()))
	b1 = input()
	b2 = input()
	total = b1 + b2[::-1]
	a = []
	prev = total[0]
	c = 1
	for i in range(1, len(total)):
		if total[i] == prev:
			c += 1
		else:
			prev = total[i]
			if c > 1:
				a.append(c)
			c = 1
	if c > 1:
		a.append(c)
	if a == [2] or a == []:
		print('YES')
	else:
		print('NO')
for i in range(t):
	sol()


def testcase():
	(n, m) = map(int, input().split())
	a = list(input())
	b = list(input())
	c = a + list(reversed(b))
	same_cnt = 0
	for i in range(n + m - 1):
		if c[i] == c[i + 1]:
			same_cnt += 1
			if same_cnt > 1:
				print('NO')
				return
	print('YES')
t = int(input())
for _ in range(t):
	testcase()


import os
import math
import collections
import bisect
import functools
import itertools
import heapq

def solve(a, b, m, n):
	(v1, v2) = (0, 0)
	for i in range(m - 1):
		if a[i] == a[i + 1]:
			v1 += 1
	for i in range(n - 1):
		if b[i] == b[i + 1]:
			v2 += 1
	if v1 + v2 > 1:
		return 'NO'
	if v1 + v2 == 0:
		return 'YES'
	return 'YES' if a[-1] != b[-1] else 'NO'
t = int(input())
for _ in range(t):
	(m, n) = map(int, input().split())
	a = [c for c in input()]
	b = [c for c in input()]
	print(solve(a, b, m, n))


t = int(input())
for i in range(t):
	n = input()
	s = input()
	k = input()
	s += k[::-1]
	while 'RR' in s or 'BB' in s:
		s = s.replace('RR', 'R R')
		s = s.replace('BB', 'B B')
	if s.count(' ') <= 1:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	(l1, l2) = map(int, input().split())
	a1 = input()
	a2 = input()
	a = a1 + a2[::-1]
	cou = 0
	for i in range(1, l1 + l2):
		if a[i] == a[i - 1]:
			if cou == 1:
				cou += 1
				print('NO')
				break
			else:
				cou += 1
	else:
		print('YES')


for wwq in range(int(input())):
	(a, b) = map(int, input().split())
	x = input()
	y = input()[::-1]
	c = x + y
	c = list(c)
	t = len(c) - 1
	z = 'YES'
	asd = 0
	for j in range(t):
		if c[j] == c[j + 1]:
			asd += 1
	if asd > 1:
		z = 'NO'
	if a == 1 and b == 1:
		z = 'YES'
	print(z)


t = int(input())
for _ in range(t):
	x = input()
	s = input()
	t = input()
	s += t[::-1]
	x = 0
	for i in range(1, len(s)):
		if s[i] == s[i - 1]:
			x += 1
	if x < 2:
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	(n, m) = map(int, input().split())
	s = input()
	t = input()
	st = s + t[::-1]
	c = 0
	for i in range(n + m - 1):
		if st[i] == st[i + 1]:
			c += 1
	if c < 2:
		print('YES')
	else:
		print('NO')


def getcnt(s):
	prev = '.'
	cnt = 0
	for i in s:
		if i == prev:
			cnt += 1
		prev = i
	return cnt
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	s1 = input()
	s2 = input()
	if s1[-1] == s2[-1]:
		if getcnt(s1) == 0 and getcnt(s2) == 0:
			print('YES')
		else:
			print('NO')
		continue
	first = getcnt(s1)
	second = getcnt(s2)
	if first > 1 or second > 1:
		print('NO')
		continue
	if first == 1 and second == 1:
		print('NO')
		continue
	print('YES')


import sys
import math
import itertools

def solution():
	(n, m) = map(int, input().split())
	s = input()
	t = input()
	k = 0
	for i in range(1, n):
		if s[i] == s[i - 1]:
			k += 1
	if s[-1] == t[-1]:
		k += 1
	for i in range(1, m):
		if t[i] == t[i - 1]:
			k += 1
	if k <= 1:
		print('YES')
		return
	print('NO')
	return
t = int(input())
for i in range(t):
	solution()


def fun(n, m, s, t):
	u = s + t[::-1]
	repetitions = 0
	for i in range(n + m - 1):
		if u[i] == u[i + 1]:
			repetitions += 1
			if repetitions > 1:
				return 'NO'
	return 'YES'
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	s = input()
	t = input()
	print(fun(n, m, s, t))


for i in range(int(input())):
	(n, m) = map(int, input().split())
	a = input()
	b = input()
	c = a + b[::-1]
	j = 1
	pre = c[0]
	while j < len(c) and pre != c[j]:
		pre = c[j]
		j += 1
	if j < len(c):
		if c[j] == 'R':
			pre = 'B'
		else:
			pre = 'R'
		while j < len(c) and pre != c[j]:
			pre = c[j]
			j += 1
	if j != len(c):
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(t1, t2) = map(int, input().split())
	s1 = input()
	s2 = input()
	p1 = ''
	flag = False
	for i in range(t1):
		if p1 == s1[i]:
			if flag == True:
				print('NO')
				break
			else:
				flag = True
		p1 = s1[i]
	else:
		p2 = ''
		for i in range(t2):
			if p2 == s2[i]:
				if flag == True:
					print('NO')
					break
				else:
					flag = True
			p2 = s2[i]
		else:
			if flag == True and s1[-1] == s2[-1]:
				print('NO')
			else:
				print('YES')


n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	t1 = list(input())
	t2 = list(input())
	t2.reverse()
	for j in t2:
		t1.append(j)
	cnt = 0
	tp1 = t1[0]
	for j in range(1, a + b):
		if t1[j] == tp1:
			cnt += 1
		tp1 = t1[j]
	if cnt <= 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	a = list(map(int, input().split()))
	b = input()
	c = input()
	q = c[::-1]
	d = b + q
	ct = 0
	for x in range(a[0] + a[1] - 1):
		if d[x] == d[x + 1]:
			ct += 1
		elif ct > 1:
			print('NO')
			break
	else:
		if ct > 1:
			print('NO')
		else:
			print('YES')


iter = int(input())
for _ in range(iter):
	(_, _) = list(map(int, input().split(' ')))
	s = input()
	s1 = input()
	f = -1
	f1 = -1
	flag2 = 0
	for i in range(len(s) - 1):
		if s[i] == s[i + 1]:
			if f == -1:
				f = i
			else:
				flag2 = 1
	for i in range(len(s1) - 1):
		if s1[i] == s1[i + 1]:
			if f1 == -1:
				f1 = i
			else:
				flag2 = 1
	if flag2 == 1:
		print('NO')
	elif f == -1 and f1 == -1:
		print('YES')
	elif f == -1 or f1 == -1:
		flag = 0
		if f != -1:
			while len(s) > f + 1:
				if s[-1] == s1[-1]:
					flag = 1
					break
				s1 += s[-1]
				s = s[:-1]
		else:
			while len(s1) > f1 + 1:
				if s[-1] == s1[-1]:
					flag = 1
					break
				s += s1[-1]
				s1 = s1[:-1]
		if flag == 1:
			print('NO')
		else:
			print('YES')
	else:
		print('NO')


def check(left, right):
	total_same = 0
	for i in range(1, len(left)):
		if left[i] == left[i - 1]:
			total_same += 1
	for i in range(1, len(right)):
		if right[i] == right[i - 1]:
			total_same += 1
	if total_same == 0:
		return True
	if total_same > 1 or left[-1] == right[-1]:
		return False
	return True
k = int(input())
res = []
while k > 0:
	_ = input()
	left = input()
	right = input()
	if check(left, right):
		res.append('YES')
	else:
		res.append('NO')
	k -= 1
for x in res:
	print(x)


def main():
	t = int(input())
	result = []
	for _ in range(t):
		(n, m) = list(map(int, input().split()))
		s1 = input()
		s1_count = 0
		s2 = input()
		s2_count = 0
		for i in range(1, n):
			if s1[i] == s1[i - 1]:
				s1_count += 1
		for j in range(1, m):
			if s2[j] == s2[j - 1]:
				s2_count += 1
		if s1_count > 1 or s2_count > 1 or (s1_count == 1 and s2_count == 1):
			result.append('NO')
		elif s1[-1] == s2[-1] and (s1_count > 0 or s2_count > 0):
			result.append('NO')
		else:
			result.append('YES')
	print(*result, sep='\n')
main()


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = input()
	b = input()
	f = True
	one = 0
	for i in range(n - 1, 0, -1):
		if a[i] == a[i - 1]:
			if one > 0 or a[-1] == b[-1]:
				f = False
				break
			else:
				one = 1
	if not f:
		print('NO')
	else:
		for i in range(m - 1, 0, -1):
			if b[i] == b[i - 1]:
				if one > 0 or a[-1] == b[-1]:
					f = False
					break
				else:
					one += 1
		if f:
			print('YES')
		else:
			print('NO')


t = int(input())
A = []
B = []
for i in range(t):
	zzz = [int(p) for p in input().split()]
	a = str(input())
	b = str(input())
	A.append(a)
	B.append(b)
for i in range(t):
	bruh = A[i] + B[i][::-1]
	cek = 1
	lah = True
	count = 0
	for j in range(1, len(bruh)):
		if bruh[j - 1] == bruh[j]:
			cek += 1
		else:
			cek = 1
		if cek == 2:
			count += 1
		if cek > 2:
			lah = False
			break
		if count == 2:
			lah = False
			break
	if lah:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n1, n2) = map(int, input().split())
	t1 = input()
	t2 = input()
	tower = t1 + t2[::-1]
	prev = tower[0]
	bad = 0
	for i in range(1, n1 + n2):
		if tower[i] == prev:
			bad += 1
		prev = tower[i]
	if bad < 2:
		print('YES')
	else:
		print('NO')


def slove():
	for _ in range(int(input())):
		(m, n) = map(int, input().split())
		s_1 = input()
		s_2 = input()
		s = s_1 + s_2[::-1]
		ans_1 = ' '
		ans_2 = ' '
		i = -1
		j = n + m
		while j - i > 1:
			if s[i + 1] != ans_1[-1]:
				ans_1 += s[i + 1]
				i += 1
			elif s[j - 1] != ans_2[-1]:
				ans_2 += s[j - 1]
				j -= 1
			else:
				break
		if j - i > 1:
			print('NO')
		else:
			print('YES')
slove()


import sys, math, cmath, time
from bisect import bisect_left
start_time = time.time()

def solve():
	(n, m) = map(int, input().split())
	s = input().strip()
	s1 = input().strip()
	c = 0
	for i in range(n - 1):
		if s[i + 1] == s[i]:
			c += 1
	if c >= 2:
		print('NO')
		return
	c1 = 0
	for i in range(m - 1):
		if s1[i + 1] == s1[i]:
			c1 += 1
	if c1 >= 2:
		print('NO')
		return
	if c == 1 and c1 == 1:
		print('NO')
		return
	if c == 0 and c1 == 0:
		print('YES')
		return
	if s1[-1] != s[-1]:
		print('YES')
	else:
		print('NO')

def main():
	testcases = 1
	testcases = inp()
	for each_testcase in range(testcases):
		solve()

def inp():
	return int(input())

def inps():
	return input().split()
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


def solveA():
	(n, m) = map(int, input().split())
	s = input()
	t = input()
	combined = s + t[::-1]
	sameCount = 0
	for i in range(len(combined) - 1):
		if combined[i] == combined[i + 1]:
			sameCount += 1
	if sameCount > 1:
		print('NO')
	else:
		print('YES')
t = int(input())
for tc in range(t):
	solveA()


from collections import Counter
import sys
input = lambda : sys.stdin.readline().rstrip()
T = int(input())
res = []
for _ in range(T):
	(N, M) = map(int, input().split())
	a = list(input().rstrip())
	b = list(input().rstrip())
	b.reverse()
	a = a + b
	cnt = 0
	for i in range(N + M - 1):
		if a[i] == a[i + 1]:
			cnt += 1
	if cnt > 1:
		res.append('NO')
	else:
		res.append('YES')
print('\n'.join(res))


def consistent(v):
	p = 'Z'
	for x in range(len(v)):
		if v[x] == p:
			return False
		else:
			p = v[x]
	return True
t = int(input())
for _ in range(t):
	(n, m) = [int(x) for x in input().split()]
	s1 = input()
	s2 = input()[::-1]
	f = s1 + s2
	p = 'Z'
	for x in range(len(f)):
		if f[x] == p:
			s1 = f[0:x]
			s2 = f[x:]
			break
		else:
			p = f[x]
	if len(s1) >= 1 and len(s1) >= 1 and consistent(s1) and consistent(s2):
		print('YES')
	else:
		print('NO')


def solve():
	(n, m) = map(int, input().split())
	a = input()
	b = input()
	a = a[::-1]
	b = b[::-1]
	h = a
	k = b
	if 'BB' in a or 'RR' in a:
		w = True
		for i in range(n):
			k = a[0] + k
			a = a[1:]
			if 'BB' in a or 'RR' in a or 'BB' in k or ('RR' in k):
				continue
			else:
				w = False
				break
		if w:
			print('NO')
			return
	if 'BB' in b or 'RR' in b:
		w = True
		for i in range(m):
			h = b[0] + h
			b = b[1:]
			if 'BB' in b or 'RR' in b or 'BB' in h or ('RR' in h):
				continue
			else:
				w = False
				break
		if w:
			print('NO')
			return
	print('YES')
for _ in range(int(input())):
	solve()


t = int(input())
for i in range(t):
	l = [int(x) for x in input().split()]
	s1 = input()
	s2 = input()
	while 'BB' in s1 or 'RR' in s1:
		ch = s1[-1]
		if ch != s2[-1]:
			s2 += ch
			s1 = s1[0:len(s1) - 1]
		else:
			break
	while 'BB' in s2 or 'RR' in s2:
		ch = s2[-1]
		if ch != s1[-1]:
			s1 += ch
			s2 = s2[0:len(s2) - 1]
		else:
			break
	if 'BB' in s1 or 'RR' in s1 or 'BB' in s2 or ('RR' in s2):
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	(n1, n2) = list(map(int, input().split()))
	s1 = input()
	t1 = list(s1[:len(s1)])
	s2 = input()
	t2 = list(s2[:len(s2)])
	t2.reverse()
	l = t1 + t2
	count = 0
	for i in range(n1 + n2 - 1):
		if l[i] != l[i + 1]:
			continue
		else:
			count += 1
		if count > 1:
			print('NO')
			break
	if count <= 1:
		print('YES')


for j in range(int(input())):
	(a, b) = map(int, input().split())
	s1 = input()
	s2 = input()
	sf = s1 + s2[::-1]
	initial_a = sf[0]
	ct = 0
	for i in range(1, a + b):
		if sf[i] == initial_a:
			ct += 1
		initial_a = sf[i]
	if ct <= 1:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	t -= 1
	(n1, n2) = list(map(int, input().split(' ')))
	tower = [*input()] + list(reversed([*input()]))
	count = 0
	for i in range(1, n1 + n2):
		if tower[i] == tower[i - 1]:
			count += 1
	if count > 1:
		print('NO')
	else:
		print('YES')


t = int(input())
l = []
for j in range(t):
	(n, m) = map(int, input().split())
	r1 = input()
	r2 = input()
	l.append([r1, r2])

def check(s):
	c = 0
	samp = s[0]
	flag = 1
	for i in s:
		if c % 2 == 0:
			if i != samp:
				flag = 0
				break
		elif i == samp:
			flag = 0
			break
		c += 1
	if flag:
		return True
	return False
for ty in l:
	s1 = ty[0]
	s2 = ty[1]
	appli = 0
	if check(s1) and check(s2):
		print('YES')
		appli = 1
	a = ''
	b = ''
	if check(s1) == False:
		a = s1
		b = s2
	else:
		a = s2
		b = s1
	f = 0
	if appli == 0:
		while len(a) > 1:
			b += a[-1]
			a = a[:len(a) - 1]
			if check(a) and check(b):
				f = 1
				print('YES')
				break
		if f == 0:
			print('NO')


loops = int(input())
for z in range(loops):
	a = input().split(' ')
	tower1 = input()
	tower2 = input()
	sexpoints1 = 0
	sexpoints2 = 0
	for i in range(len(tower1) - 1):
		if tower1[i] == tower1[i + 1]:
			sexpoints1 += 1
	for i in range(len(tower2) - 1):
		if tower2[i] == tower2[i + 1]:
			sexpoints2 += 1
	if sexpoints1 + sexpoints2 > 1:
		print('NO')
	elif sexpoints1 + sexpoints2 == 0:
		print('YES')
	elif tower1[-1] == tower2[-1]:
		print('NO')
	else:
		print('YES')


def take():
	(n, m) = map(int, input().split())
	first = input()
	second = input()
	res = first + second[::-1]
	counter = 1
	cur_color = res[0]
	pairs = 0
	for i in range(1, n + m):
		if res[i] == cur_color:
			pairs += 1
		cur_color = res[i]
	if pairs > 1:
		print('NO')
	else:
		print('YES')
n = int(input())
for i in range(n):
	take()


n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	(s1, s2) = (input(), input())
	s1 = s1 + s2[::-1]
	x = s1.count('BB') + s1.count('RR')
	x += s1.count('BBB') + s1.count('RRR')
	if x == 0 or x == 1:
		print('YES')
	else:
		print('NO')


import sys

def read(func=int):
	return func(sys.stdin.readline().strip())

def read_array(func=int):
	return list(map(func, read(str).split()))
k = read()
for _ in range(k):
	(n, m) = read_array()
	s = read(str)
	t = read(str)
	st = s + t[::-1]
	ds = 0
	for i in range(1, n + m):
		if st[i] == st[i - 1]:
			ds += 1
	if ds < 2:
		print('YES')
	else:
		print('NO')


def solve():
	t = 1
	t = int(input())
	while t:
		t -= 1
		(n, m) = [int(x) for x in input().split()]
		a = input()
		b = input()
		nonSame = 0
		for i in range(n - 1):
			if a[i] == a[i + 1]:
				nonSame += 1
		for i in range(m - 1):
			if b[i] == b[i + 1]:
				nonSame += 1
		print('YES' if nonSame == 0 or (nonSame == 1 and a[n - 1] != b[m - 1]) else 'NO')
solve()


n = int(input())
for i in range(n):
	(n0, m0) = map(int, input().split())
	t1 = input()
	t2 = input()
	count = 0
	for i in range(n0 - 1):
		if t1[i] == t1[i + 1]:
			count += 1
		if count > 1:
			break
	if t2[m0 - 1] == t1[n0 - 1]:
		count += 1
	for i in range(m0 - 1):
		if count > 1:
			break
		if t2[i] == t2[i + 1]:
			count += 1
	if count <= 1:
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(n):
	(p, q) = map(int, input().split())
	s1 = input()
	s2 = input()
	w = 0
	e = 0
	for i in range(p - 1):
		if s1[i] == s1[i + 1]:
			w = w + 1
	for i in range(q - 1):
		if s2[i] == s2[i + 1]:
			e = e + 1
	if e > 1 or w > 1:
		print('NO')
	elif w == 1 and e == 1:
		print('NO')
	elif s1[p - 1] == s2[q - 1] and (w == 1 or e == 1):
		print('NO')
	else:
		print('YES')


import sys
input = sys.stdin.readline

def sol(s1, s2, m, n):
	i = 1
	for i in range(1, m):
		if s1[i] == s1[i - 1]:
			break
		i += 1
	j = 1
	for j in range(1, n):
		if s2[j] == s2[j - 1]:
			break
		j += 1
	if i != m and j != n:
		return 'NO'
	if i != m:
		s1 += s2[-1]
		for k in range(m - 1, i - 1, -1):
			if s1[k] == s1[k + 1]:
				return 'NO'
	elif j != n:
		s2 += s1[-1]
		for k in range(n - 1, j - 1, -1):
			if s2[k] == s2[k + 1]:
				return 'NO'
	return 'YES'
t = int(input())
for case in range(t):
	(m, n) = list(map(int, input().split()))
	s1 = input()[:-1]
	s2 = input()[:-1]
	print(sol(s1, s2, m, n))


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	l = input()
	m = input()
	if a > 2 and b > 2:
		c = m + l[::-1]
		e = l + m[::-1]
		d = f = 0
		for i in range(a + b - 1):
			if c[i] == c[i + 1]:
				d += 1
			if e[i] == e[i + 1]:
				f += 1
	elif b < 3:
		e = l + m[::-1]
		d = f = 0
		for i in range(a + b - 1):
			if e[i] == e[i + 1]:
				f += 1
				d = f
	elif a < 3:
		c = m + l[::-1]
		d = f = 0
		for i in range(a + b - 1):
			if c[i] == c[i + 1]:
				d += 1
				f = d
	if d <= 1 or f <= 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = input()
	b = input()
	c1 = 0
	c2 = 0
	for i in range(n - 1):
		if a[i] == a[i + 1]:
			c1 += 1
	for i in range(m - 1):
		if b[i] == b[i + 1]:
			c2 += 1
	if c1 == 0 and c2 == 0:
		print('YES')
	elif c1 == 1 and c2 == 0:
		if a[-1] != b[-1]:
			print('YES')
		else:
			print('NO')
	elif c1 == 0 and c2 == 1:
		if a[-1] != b[-1]:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for i in range(int(input())):
	map(int, input().split())
	a = input()
	b = input()
	c = 0
	d = 0
	e = 0
	g = 0
	if a.count('BB') == 0 and a.count('RR') == 0 and (b.count('BB') == 0) and (b.count('RR') == 0):
		print('YES')
	elif a[-1] == b[-1]:
		print('NO')
	elif a.count('BB') + a.count('RR') + b.count('BB') + b.count('RR') > 1:
		print('NO')
	elif a.count('BBB') == 0 and a.count('RRR') == 0 and (b.count('BBB') == 0) and (b.count('RRR') == 0):
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	x = input()
	y = input()
	tower = x + y[::-1]
	f = 'a'
	count = 0
	for letter in tower:
		if letter == f:
			count += 1
		else:
			f = letter
		if count > 1:
			break
	if count > 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	sn = input()
	sm = input()
	f1 = f2 = True
	c1 = c2 = 0
	for i in range(1, n):
		if sn[i] == sn[i - 1]:
			c1 += 1
			f1 = False
	for i in range(1, m):
		if sm[i] == sm[i - 1]:
			c2 += 1
			f2 = False
	if f1 or f2:
		if f1 and f2:
			print('YES')
		elif f1 is True and f2 is False:
			if sn[-1] != sm[-1] and c2 == 1:
				print('YES')
			else:
				print('NO')
		elif sn[-1] != sm[-1] and c1 == 1:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for _ in range(int(input())):
	(n, m) = input().split()
	s1 = input()
	s2 = input()
	n = int(n)
	m = int(m)
	(sn, sm) = (0, 0)
	for i in range(1, m):
		if s2[i] == s2[i - 1]:
			sm += 1
	for i in range(1, n):
		if s1[i] == s1[i - 1]:
			sn += 1
	if (sn == 1 or sm == 1) and s2[-1] == s1[-1]:
		print('NO')
	elif sn == 1 and sm == 1 or (sn > 1 or sm > 1):
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	L = [s for s in input()]
	R = [s for s in input()]
	l = L

	def checkPair(s):
		for i in range(len(s)):
			if i == 0:
				continue
			if s[i - 1] == s[i]:
				return False
		return True
	r1 = []
	r2 = []
	while L:
		if not r1:
			r1.append(L[0])
			L = L[1:]
		elif r1 and r1[-1] != L[0]:
			r1.append(L[0])
			L = L[1:]
		else:
			break
	if L:
		temp = R + L[::-1]
		print('YES' if checkPair(temp) else 'NO')
	else:
		while R:
			if not r2:
				r2.append(R[0])
				R = R[1:]
			elif r2 and r2[-1] != R[0]:
				r2.append(R[0])
				R = R[1:]
			else:
				break
		if R:
			temp = l + R[::-1]
			print('YES' if checkPair(temp) else 'NO')
		else:
			print('YES')


t = int(input())
for _ in range(t):
	(n, m) = input().split(' ')
	(n, m) = (int(n), int(m))
	s = input()
	t = input()
	s += t[::-1]
	flag = 0
	last = ''
	ans = 1
	for i in s:
		if last == i:
			if flag == 1:
				ans = 0
				break
			flag = 1
		last = i
	if ans == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, m) = [int(x) for x in input().split()]
	first_tower = input()
	second_tower = input()
	counter = 0
	for i in range(1, n):
		if first_tower[i - 1] == first_tower[i]:
			counter += 1
	for i in range(1, m):
		if second_tower[i - 1] == second_tower[i]:
			counter += 1
	if counter == 0:
		print('YES')
	elif counter == 1 and first_tower[-1] != second_tower[-1]:
		print('YES')
	else:
		print('NO')


tc = int(input())
for _ in range(tc):
	(n1, n2) = map(int, input().split())
	t1 = list(input())
	t2 = list(input())
	b1 = True
	b2 = True
	c1 = 0
	c2 = 0
	for i in range(n1 - 1):
		if t1[i] == t1[i + 1]:
			b1 = False
			c1 = i + 1
			break
	for i in range(n2 - 1):
		if t2[i] == t2[i + 1]:
			b2 = False
			c2 = i + 1
			break
	gorb = True
	if b1 == False and b2 == False:
		print('NO')
	elif b1 == True and b2 == True:
		print('YES')
	elif b1 == True and b2 == False:
		for i in range(n2 - 1, c2 - 1, -1):
			top = t2.pop()
			if top == t1[-1]:
				print('NO')
				gorb = False
				break
			else:
				t1.append(top)
		if gorb:
			print('YES')
	elif b1 == False and b2 == True:
		for i in range(n1 - 1, c1 - 1, -1):
			top = t1.pop()
			if top == t2[-1]:
				print('NO')
				gorb = False
				break
			else:
				t2.append(top)
		if gorb:
			print('YES')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	s = input()
	t = input()
	a = ''
	for i in t:
		a = i + a
	c = s + a
	b = 0
	for j in range(n + m - 1):
		if c[j] == c[j + 1]:
			b += 1
	if b > 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for y in range(t):
	(n, m) = map(int, input().split())
	first = input()
	second = input()
	x = first[0]
	flag = 0
	res = first + second[::-1]
	for i in range(1, len(res)):
		if res[i] == res[i - 1]:
			flag += 1
			if flag == 2:
				print('NO')
				break
	if flag != 2:
		print('YES')


import math

def solve():
	(n, m) = map(int, input().split())
	s1 = input()
	s2 = input()
	f1 = f2 = True
	c1 = c2 = 0
	for i in range(n - 1):
		if s1[i] == s1[i + 1]:
			c1 += 1
	for i in range(m - 1):
		if s2[i] == s2[i + 1]:
			c2 += 1
	if (c1 > 1 or c2 > 1) or (c1 == 1 and c2 == 1):
		print('NO')
		return
	if (c1 == 1 or c2 == 1) and s1[-1] == s2[-1]:
		print('NO')
		return
	print('YES')
for t in range(int(input())):
	solve()


def TwoTowers(s, t):
	t = t[::-1]
	a = s + t
	count = 0
	for i in range(n + m - 1):
		if a[i] == a[i + 1]:
			count += 1
	if count > 1:
		return 'NO'
	else:
		return 'YES'
T = int(input())
while T > 0:
	(n, m) = map(int, input().split())
	s = input()
	t = input()
	ans = TwoTowers(s, t)
	print(ans)
	T = T - 1


def funk(st):
	res = 0
	while st.count('BB') + st.count('RR'):
		if st.count('BB'):
			st = st.replace('BB', 'B', 1)
		else:
			st = st.replace('RR', 'R', 1)
		res += 1
	return res
t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	st1 = input()[::-1]
	st2 = input()
	st = st2 + st1
	if funk(st) <= 1:
		print('YES')
	else:
		print('NO')


a = int(input())
while a:
	(n, m) = map(int, input().split())
	s = [x for x in str(input())]
	t = [x for x in str(input())]
	f = 0
	t.reverse()
	s = s + t
	cnt = 0
	for i in range(len(s) - 1):
		if s[i] == s[i + 1]:
			cnt += 1
	if cnt > 1:
		print('NO')
	else:
		print('YES')
	a -= 1


T = int(input())
for t in range(0, T):
	(n, m) = map(int, input().split())
	s = input()
	p = input()
	problem1 = 0
	problem2 = 0
	for i in range(0, n - 1):
		if s[i] == s[i + 1]:
			problem1 += 1
	for i in range(0, m - 1):
		if p[i] == p[i + 1]:
			problem2 += 1
	if problem1 == 0 and problem2 == 0:
		print('YES')
	elif problem1 * problem2 != 0:
		print('NO')
	else:
		if problem1 != 0:
			if problem1 == 1 and s[n - 1] != p[m - 1]:
				print('YES')
			else:
				print('NO')
		if problem2 != 0:
			if problem2 == 1 and p[m - 1] != s[n - 1]:
				print('YES')
			else:
				print('NO')


import re
T = int(input())
for t in range(T):
	(N, M) = [int(x) for x in input().split()]
	t1 = input().strip()
	t2 = input().strip()
	tow = t1 + t2[::-1]
	if len(re.findall('(?=BB)', tow)) + len(re.findall('(?=RR)', tow)) > 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(n, m) = input().split()
	n = int(n)
	m = int(m)
	s = input()
	t = input()
	flag1 = False
	flag2 = False
	count1 = 0
	count2 = 0
	for i in range(n - 1):
		if s[i] == s[i + 1]:
			flag1 = True
			count1 += 1
	for i in range(m - 1):
		if t[i] == t[i + 1]:
			flag2 = True
			count2 += 1
	if flag1 and flag2:
		print('NO')
	elif not flag1 and (not flag2):
		print('YES')
	elif s[-1] == t[-1]:
		print('NO')
	elif count1 > 1 or count2 > 1:
		print('NO')
	else:
		print('YES')


t = int(input())
ls = []
for j in range(t):
	l = list(map(int, input().split()))
	n1 = l[0]
	n2 = l[1]
	a1 = str(input())
	a2 = str(input())
	for i in range(n2):
		a1 = a1 + a2[-1 - i]
	ans = 0
	for i in range(n1 + n2 - 1):
		if a1[i] == a1[i + 1]:
			ans = ans + 1
	if ans > 1:
		ls.append('NO')
	else:
		ls.append('YES')
for k in range(t):
	print(ls[k])


from itertools import groupby
for s in ([input(), input() + ''.join(reversed(input()))][1] for _ in range(int(input()))):
	print(['NO', 'YES'][sum((s[i - 1] == s[i] for i in range(1, len(s)))) <= 1])


for t in range(int(input())):
	(n, m) = map(int, input().split())
	s = input()
	t = input()
	st = s + t[::-1]
	y = 0
	v = 0
	if n == 1 and m == 1:
		print('YES')
	else:
		for x in range(0, n + m - 1):
			if st[x] == st[x + 1]:
				if x != n + m - 2 and st[x + 2] == st[x]:
					y = 1
					break
				else:
					v += 1
	if y == 1 or v > 1:
		print('NO')
	elif not (n == 1 and m == 1):
		print('YES')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = input()
	b = input()
	acnt = bcnt = 0
	for i in range(1, n):
		if a[i] == a[i - 1]:
			acnt += 1
	for j in range(1, m):
		if b[j - 1] == b[j]:
			bcnt += 1
	if (acnt > 1 or bcnt > 1) or (acnt == 1 and bcnt == 1):
		print('NO')
	elif (bcnt == 1 or acnt == 1) and a[n - 1] == b[m - 1]:
		print('NO')
	else:
		print('YES')


t = int(input())
for t in range(t):
	(n, m) = list(map(int, input().split()))[:2]
	a = input()
	b = input()
	c = a + b[::-1]
	temp = 0
	for i in range(n + m - 1):
		if c[i] == c[i + 1]:
			temp += 1
	if temp > 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	s = input()
	s1 = input()
	ib = 0
	for i in range(1, n):
		if s[i - 1] == s[i]:
			ib += 1
	for i in range(1, m):
		if s1[i - 1] == s1[i]:
			ib += 1
	if ib == 0:
		print('YES')
	elif ib == 1:
		if s[-1] != s1[-1]:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


t = int(input())
from collections import defaultdict
while t > 0:
	arr = input().split(' ')
	arr = [arr[0], arr[1]]
	t1 = input()
	t1 = list(t1)
	t2 = input()
	t2 = list(t2)
	x = len(t1) - 1
	y = len(t2) - 1
	ans = 'YES'
	(ca, cb) = (0, 0)
	for i in range(len(t1) - 1, 0, -1):
		if t1[i] == t1[i - 1]:
			ca += 1
	for i in range(len(t2) - 1, 0, -1):
		if t2[i] == t2[i - 1]:
			cb += 1
	if (ca > 1 or cb > 1) or (ca == 1 and cb == 1):
		ans = 'NO'
	elif (ca == 1 or cb == 1) and t1[len(t1) - 1] == t2[len(t2) - 1]:
		ans = 'NO'
	print(ans)
	t -= 1


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	s1 = input()
	s2 = input()
	f1 = 0
	f2 = 0
	for i in range(0, n - 1):
		if s1[i] == s1[i + 1]:
			f1 += 1
	for i in range(0, m - 1):
		if s2[i] == s2[i + 1]:
			f2 += 1
	if f1 == 0 and f2 == 0:
		print('YES')
	elif f1 != 0 and f2 != 0 or (f1 > 1 or f2 > 1):
		print('NO')
	else:
		s1 = s1[::-1]
		s2 = s2[::-1]
		if f1 == 1:
			while True:
				if s1[0] != s2[0]:
					s2 = s1[0] + s2
					s1 = s1[1:]
				else:
					break
			f = 0
			for i in range(0, len(s1) - 1):
				if s1[i] == s1[i + 1]:
					f = 1
					break
			if f == 1:
				print('NO')
			else:
				print('YES')
		else:
			while True:
				if s1[0] != s2[0]:
					s1 = s2[0] + s1
					s2 = s2[1:]
				else:
					break
			f = 0
			for i in range(0, len(s2) - 1):
				if s2[i] == s2[i + 1]:
					f = 1
					break
			if f == 1:
				print('NO')
			else:
				print('YES')


num = int(input())
for i in range(num):
	(a, b) = map(int, input().split())
	t = input()
	t2 = input()
	f = t + t2[::-1]
	count = 0
	x = 0
	while count <= 1 and x <= a + b - 2:
		if f[x] == f[x + 1]:
			count += 1
		x += 1
	if count <= 1:
		print('YES')
	else:
		print('NO')


for tests in range(int(input())):
	(n, m) = map(int, input().split())
	a = input()
	b = input()
	a = a + b[::-1]
	ans = 1
	count = 0
	for i in range(len(a) - 1):
		if a[i] == a[i + 1]:
			count += 1
		if count > 1:
			ans = 0
			break
	if ans:
		print('YES')
	else:
		print('NO')


n = int(input())
data = [() for i in range(n)]
for i in range(n):
	input()
	str1 = input()
	str2 = input()
	data[i] = (str1, str2)
for i in range(n):
	(st1, st2) = (data[i][0], data[i][1])
	l1 = len(st1)
	l2 = len(st2)
	if st1.count('RRR') > 0 or st1.count('BBB') > 0 or st2.count('RRR') > 0 or (st2.count('BBB') > 0):
		print('NO')
		continue
	x1 = st1.count('RR')
	x2 = st1.count('BB')
	y1 = st2.count('RR')
	y2 = st2.count('BB')
	if x1 + y1 + x2 + y2 == 0:
		print('YES')
	elif x1 + x2 + y1 + y2 == 1:
		if st1[-1] == st2[-1]:
			print('NO')
		else:
			print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(m, n) = [int(i) for i in input().split(' ')]
	first = input()
	second = input()
	position1 = []
	for i in range(m):
		if i > 0:
			if first[i] == first[i - 1]:
				position1.append(i)
	position2 = []
	for i in range(n):
		if i > 0:
			if second[i] == second[i - 1]:
				position2.append(i)
	if len(position1) != 0 and len(position2) != 0:
		print('NO')
	elif (len(position1) != 0 or len(position2) != 0) and first[-1] == second[-1]:
		print('NO')
	elif len(position1) >= 2 or len(position2) >= 2:
		print('NO')
	else:
		print('YES')


n = int(input())
for _ in range(n):
	_ = list(map(int, input().split()))
	a = input()
	b = input()
	b = b[::-1]
	a = a + b
	temp_count = len([x for x in range(len(a) - 1) if a[x] == a[x + 1]])
	if temp_count > 1:
		print('NO')
	else:
		print('YES')