Sir Monocarp Hamilton is planning to paint his wall. The wall can be represented as a grid, consisting of $2$ rows and $m$ columns. Initially, the wall is completely white.

Monocarp wants to paint a black picture on the wall. In particular, he wants cell $(i, j)$ (the $j$-th cell in the $i$-th row) to be colored black, if $c_{i, j} =$ 'B', and to be left white, if $c_{i, j} =$ 'W'. Additionally, he wants each column to have at least one black cell, so, for each $j$, the following constraint is satisfied: $c_{1, j}$, $c_{2, j}$ or both of them will be equal to 'B'.

In order for the picture to turn out smooth, Monocarp wants to place down a paint brush in some cell $(x_1, y_1)$ and move it along the path $(x_1, y_1), (x_2, y_2), \dots, (x_k, y_k)$ so that:

for each $i$, $(x_i, y_i)$ and $(x_{i+1}, y_{i+1})$ share a common side;

all black cells appear in the path exactly once;

white cells don't appear in the path.

Determine if Monocarp can paint the wall.


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases.

The first line of each testcase contains a single integer $m$ ($1 \le m \le 2 \cdot 10^5$) — the number of columns in the wall.

The $i$-th of the next two lines contains a string $c_i$, consisting of $m$ characters, where each character is either 'B' or 'W'. $c_{i, j}$ is 'B', if the cell $(i, j)$ should be colored black, and 'W', if the cell $(i, j)$ should be left white.

Additionally, for each $j$, the following constraint is satisfied: $c_{1, j}$, $c_{2, j}$ or both of them are equal to 'B'.

The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.


-----Output-----

For each testcase, print "YES" if Monocarp can paint a wall. Otherwise, print "NO".


-----Examples-----

Input
6
3
WBB
BBW
1
B
B
5
BWBWB
BBBBB
2
BW
WB
5
BBBBW
BWBBB
6
BWBBWB
BBBBBB
Output
YES
YES
NO
NO
NO
YES


-----Note-----

In the first testcase, Monocarp can follow a path $(2, 1)$, $(2, 2)$, $(1, 2)$, $(1, 3)$ with his brush. All black cells appear in the path exactly once, no white cells appear in the path.

In the second testcase, Monocarp can follow a path $(1, 1)$, $(2, 1)$.

In the third testcase:

the path $(1, 1)$, $(2, 1)$, $(2, 2)$, $(2, 3)$, $(1, 3)$, $(2, 4)$, $(2, 5)$, $(1, 5)$ doesn't suffice because a pair of cells $(1, 3)$ and $(2, 4)$ doesn't share a common side;

the path $(1, 1)$, $(2, 1)$, $(2, 2)$, $(2, 3)$, $(1, 3)$, $(2, 3)$, $(2, 4)$, $(2, 5)$, $(1, 5)$ doesn't suffice because cell $(2, 3)$ is visited twice;

the path $(1, 1)$, $(2, 1)$, $(2, 2)$, $(2, 3)$, $(2, 4)$, $(2, 5)$, $(1, 5)$ doesn't suffice because a black cell $(1, 3)$ doesn't appear in the path;

the path $(1, 1)$, $(2, 1)$, $(2, 2)$, $(2, 3)$, $(2, 4)$, $(2, 5)$, $(1, 5)$, $(1, 4)$, $(1, 3)$ doesn't suffice because a white cell $(1, 4)$ appears in the path.

Source: codeforces | Difficulty: EASY
Tags: Dynamic programming

Implementation


Solutions:
import sys
input = sys.stdin.readline

def sol(n, a, b):
	wall = [a, b]
	ind = 0
	check = True
	for i in range(n):
		if wall[ind][i] != 'B':
			check = False
			break
		elif wall[ind ^ 1][i] == 'B':
			ind ^= 1
	if check:
		return 'YES'
	ind = 1
	check = True
	for i in range(n):
		if wall[ind][i] != 'B':
			check = False
			break
		elif wall[ind ^ 1][i] == 'B':
			ind ^= 1
	if check:
		return 'YES'
	else:
		return 'NO'
t = int(input())
for case in range(t):
	n = int(input())
	a = input()
	b = input()
	print(sol(n, a, b))


def solve():
	n = int(input())
	s1 = input()
	s2 = input()
	k1 = k2 = 1
	for i in range(n):
		if s1[i] == s2[i] == 'B':
			(k1, k2) = (k2, k1)
		elif s1[i] == 'B':
			k2 = 0
		else:
			k1 = 0
	if k1 or k2:
		return 'YES'
	return 'NO'
import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
for i in range(t):
	print(solve())


import sys
sys.setrecursionlimit(200019)

def trace_path(row1, row2):
	if not len(row1) or not len(row2):
		return True
	for i in range(len(row1)):
		if row1[i] == 'B':
			if row2[i] == 'B':
				(row1, row2) = (row2, row1)
			else:
				pass
		else:
			return False
	return True
for _ in range(int(input())):
	input()
	row1 = input()
	row2 = input()
	if trace_path(row1, row2) or trace_path(row2, row1):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	s1 = input()
	s2 = input()
	if s1[0] == 'W' and s2[0] == 'W':
		print('NO')
		continue
	if s1[0] == 'B':
		cond = True
		flag = True
		for i in range(n):
			if cond:
				if s1[i] == 'B':
					if s2[i] == 'B':
						cond = not cond
				else:
					flag = False
					break
			elif s2[i] == 'B':
				if s1[i] == 'B':
					cond = not cond
			else:
				flag = False
				break
		if flag:
			print('YES')
			continue
	if s2[0] == 'B':
		cond = False
		flag = True
		for i in range(n):
			if cond:
				if s1[i] == 'B':
					if s2[i] == 'B':
						cond = not cond
				else:
					flag = False
					break
			elif s2[i] == 'B':
				if s1[i] == 'B':
					cond = not cond
			else:
				flag = False
				break
		if flag:
			print('YES')
			continue
	print('NO')


def fun(n, s1, s2):
	if n == 1:
		if s1 == 'B' or s2 == 'B':
			print('YES')
			return
		else:
			print('NO')
			return
	t = 0
	i = 0
	while i < n:
		if t == 1:
			if s1[i] == 'B':
				if s2[i] == 'B':
					t = 0
			else:
				break
		elif s2[i] == 'B':
			if s1[i] == 'B':
				t = 1
		else:
			break
		i = i + 1
	if i == n:
		print('YES')
		return
	t = 1
	i = 0
	while i < n:
		if t == 1:
			if s1[i] == 'B':
				if s2[i] == 'B':
					t = 0
			else:
				break
		elif s2[i] == 'B':
			if s1[i] == 'B':
				t = 1
		else:
			break
		i = i + 1
	if i == n:
		print('YES')
		return
	print('NO')
	return
k = int(input())
for j in range(k):
	n = int(input())
	s1 = input()
	s2 = input()
	fun(n, s1, s2)


t = int(input())

def check(wall, n, i, j):
	seen = False
	while j < n:
		if wall[i][j] != 'B':
			return False
		if seen:
			j += 1
			seen = False
		elif wall[1 - i][j] == 'B':
			i = 1 - i
			seen = True
		else:
			j += 1
			seen = False
	return True
for i in range(t):
	n = int(input())
	wall = []
	wall.append(input())
	wall.append(input())
	print('YES' if check(wall, n, 0, 0) or check(wall, n, 1, 0) else 'NO')


def func():
	for _ in range(int(input())):
		x = int(input())
		a = input()
		b = input()
		f = 1
		f1 = 1
		for i in range(x):
			if a[i] == b[i]:
				(f, f1) = (f1, f)
			elif a[i] == 'B':
				f1 = 0
			else:
				f = 0
		if f or f1:
			print('YES')
		else:
			print('NO')
func()


import sys
input = sys.stdin.readline

def readList():
	return list(map(int, input().split()))

def readInt():
	return int(input())

def readInts():
	return map(int, input().split())

def readStr():
	return input().strip()

def solve():
	m = readInt()
	colors = [readStr() for _ in range(2)]
	prev = None
	for i in range(m):
		if colors[0][i] == colors[1][i]:
			continue
		if not prev:
			prev = (0, i) if colors[0][i] == 'B' else (1, i)
		else:
			r = 0 if colors[0][i] == 'B' else 1
			if r == prev[0] and (i - prev[1]) % 2 == 0:
				return 'NO'
			if r != prev[0] and (i - prev[1]) % 2:
				return 'NO'
			prev = (r, i)
	return 'YES'
for _ in range(int(input())):
	print(solve())


t = int(input())
for i in range(t):
	m = int(input())
	r1 = input()
	r2 = input()

	def checker(m, r1, r2):
		r = 0
		c = 0
		if r1[0] == 'B' and r2[0] == 'W':
			r = 1
			c = 0
		if r1[0] == 'W' and r2[0] == 'B':
			r = 2
			c = 0
		for i in range(1, m):
			if r == 0 and c == 0:
				if r1[i] == 'B' and r2[i] == 'W':
					r = 1
					c = i
				if r1[i] == 'W' and r2[i] == 'B':
					r = 2
					c = i
			else:
				if r1[i] == 'B' and r2[i] == 'B':
					if r == 2:
						r = 1
					else:
						r = 2
					c = i
				if r1[i] == 'W' and r2[i] == 'B':
					if r == 1:
						return False
					c = i
				if r1[i] == 'B' and r2[i] == 'W':
					if r == 2:
						return False
					c = i
		return True
	if m == 1:
		print('YES')
	elif checker(m, r1, r2) == True:
		print('YES')
	else:
		print('NO')


t = int(input())

def solve():
	n = int(input())
	(a, b) = (input(), input())
	(chk1, chk2) = (True, True)
	for i in range(n):
		if a[i] == b[i]:
			(chk1, chk2) = (chk2, chk1)
		elif a[i] == 'B':
			chk2 = False
		else:
			chk1 = False
	if chk1 or chk2:
		print('YES')
	else:
		print('NO')
for _ in range(t):
	solve()


for _ in range(int(input())):
	n = int(input())
	s1 = input()
	s2 = input()
	(check1, check2) = (True, True)
	for i in range(n):
		if s1[i] == s2[i]:
			(check1, check2) = (check2, check1)
		elif s1[i] == 'W':
			check1 = False
		else:
			check2 = False
	if check1 == False and check2 == False:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	r1 = input()
	r2 = input()
	pos = -1
	for i in range(n):
		if r1[i] != r2[i]:
			pos = i
			break
	ans = 1
	up = 0
	if r1[pos] == 'B':
		up = 1
	for i in range(pos + 1, n):
		if up == 1:
			if r1[i] != 'B':
				ans = 0
				break
			if r2[i] == 'B':
				up = 0
				continue
		else:
			if r2[i] != 'B':
				ans = 0
				break
			if r1[i] == 'B':
				up = 1
	if ans == 1:
		print('YES')
	else:
		print('NO')


def main():
	for _ in range(int(input())):
		n = int(input())
		x = input()
		y = input()
		print(a(x, y, n))

def a(x, y, n):
	head = True
	top = True
	for i in range(n):
		cur = x[i] + y[i]
		if cur == 'BB':
			top = not top
		elif head:
			top = cur == 'BW'
			head = False
		elif top and cur == 'WB' or (not top and cur == 'BW'):
			return 'NO'
	return 'YES'
main()


T = int(input())
for _ in range(T):
	n = int(input())
	str1 = input()
	str2 = input()
	bool_ans = True
	bool1 = False
	bool2 = False
	if str1[0] == 'B':
		bool1 = True
	if str2[0] == 'B':
		bool2 = True
	for j in range(1, n):
		if bool1 and bool2:
			if str1[j] == 'B':
				if str2[j] != 'B':
					bool2 = False
			else:
				bool1 = False
				if str2[j] != 'B':
					bool2 = False
		elif bool1 and (not bool2):
			if str1[j] != 'B':
				bool_ans = False
				break
			elif str2[j] == 'B':
				bool1 = False
				bool2 = True
			else:
				bool1 = True
		elif not bool1 and bool2:
			if str2[j] != 'B':
				bool_ans = False
				break
			elif str1[j] == 'B':
				bool1 = True
				bool2 = False
			else:
				bool2 = True
		else:
			bool_ans = False
			break
	if bool_ans:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = []
	for i in range(2):
		arr.append(input())
	pos = -1
	for i in range(n):
		if arr[0][i] != arr[1][i]:
			pos = i
			break
	if pos == -1:
		print('YES')
		continue
	isPossible = True
	curr = 0 if arr[0][pos] == 'B' else 1
	for i in range(pos + 1, n):
		if arr[curr][i] == 'W':
			isPossible = False
			break
		if arr[curr ^ 1][i] == 'B':
			curr = curr ^ 1
	if not isPossible:
		print('NO')
		continue
	for i in range(pos - 1, -1, -1):
		if arr[curr][i] == 'W':
			isPossible = False
			break
		if arr[curr ^ 1][i] == 'B':
			curr = curr ^ 1
	if not isPossible:
		print('NO')
	else:
		print('YES')


def sol(l, t):
	r1 = True
	r2 = True
	for i in range(t):
		if l[0][i] == 'B' and l[1][i] == 'B':
			(r1, r2) = (r2, r1)
		elif l[0][i] == 'B':
			r2 = False
		elif l[1][i] == 'B':
			r1 = False
		else:
			return 'NO'
	if r1 or r2:
		return 'YES'
	return 'NO'
n = int(input())
for i in range(n):
	t = input()
	row1 = input()
	row2 = input()
	l = [list(row1), list(row2)]
	print(sol(l, int(t)))


x = int(input())

def tester():
	n = int(input())
	row1 = list(input())
	row2 = list(input())

	def flip(bit):
		return abs(bit - 1)

	def path_exists(row1, row2, n):
		if n == 1:
			return True
		prev = None
		curr = None
		i = 0
		while row1[i] == row2[i]:
			i += 1
			if i == n - 1:
				return True
		if row1[i] == 'B' and row2[i] == 'W':
			prev = 0
		if row1[i] == 'W' and row2[i] == 'B':
			prev = 1
		while i < n:
			if row1[i] == 'B' and row2[i] == 'W':
				curr = 0
			if row1[i] == 'W' and row2[i] == 'B':
				curr = 1
			if curr != prev:
				return False
			if row1[i] == row2[i]:
				count = 0
				while row1[i] == row2[i]:
					if i == n - 1:
						return True
					count += 1
					i += 1
				if count % 2 == 1:
					prev = flip(curr)
				else:
					prev = curr
			else:
				i += 1
		return True
	if path_exists(row1, row2, n):
		print('YES')
	else:
		print('NO')
for i in range(x):
	tester()


I = input
for _ in [0] * int(I()):
	I()
	s = {0}
	i = 0
	for x in zip(I(), I()):
		if x[0] == x[1]:
			i ^= 1
		else:
			s |= {x[i]}
	print('YNEOS'[len(s) > 2::2])


for time in range(int(input())):
	n = int(input())
	r = [0]
	r.append(input())
	r.append(input())
	s = 0
	won = True
	for j in range(n):
		if s == 0:
			if r[1][j] == 'W':
				s = 2
			if r[2][j] == 'W':
				s = 1
			continue
		if r[s][j] == 'W':
			won = False
			print('NO')
			break
		if r[3 - s][j] == 'B':
			s = 3 - s
	if won:
		print('YES')


t = int(input())
for i in range(t):
	x = int(input())
	a = input()
	b = input()
	key = 0
	t = 0
	for y in range(len(a)):
		if t < 0:
			if a[y] == 'B' and b[y] == 'B':
				t = 1
			elif a[y] == 'B' and b[y] != 'B':
				t = -1
			else:
				key = 1
				break
		elif t > 0:
			if a[y] == 'B' and b[y] == 'B':
				t = -1
			elif a[y] != 'B' and b[y] == 'B':
				t = 1
			else:
				key = 1
				break
		if t == 0:
			if a[y] == 'B' and b[y] != 'B':
				t = -1
			if a[y] != 'B' and b[y] == 'B':
				t = 1
	if key == 1:
		print('NO')
	else:
		print('YES')


tc = int(input())
for i in range(tc):
	l = int(input())
	r0 = input()
	r1 = input()
	col = 0
	row = None
	firstMismatch = False
	gone = True
	for k in range(l):
		if not firstMismatch:
			if r0[k] != r1[k]:
				firstMismatch = True
				if r0[k] == 'B':
					row = 0
					k += 1
				else:
					row = 1
					k += 1
			else:
				k += 1
		elif r0[k] == r1[k]:
			row = (row + 1) % 2
			k += 1
		elif r0[k] == 'B' and row == 0:
			k += 1
		elif r1[k] == 'B' and row == 1:
			k += 1
		else:
			gone = False
			break
	if gone:
		print('YES')
	else:
		print('NO')


def func():
	m = int(input())
	stena = []
	for i in range(2):
		stena.append(input())
	for k in range(2):
		row = k
		i = 0
		if stena[row][0] == 'W':
			continue
		fl = True
		while i < m - 1:
			if stena[row][i + 1] == 'W' and stena[1 - row][i] == 'W':
				fl = False
				break
			if stena[1 - row][i] == 'B':
				row = (row + 1) % 2
				if stena[row][i + 1] == 'W':
					fl = False
					break
			if stena[row][i + 1] == 'B':
				i += 1
		if fl:
			print('YES')
			return
	print('NO')
t = int(input())
for o in range(t):
	func()


def is_paintable(wall):
	pos = [len(wall[0]) - 1, len(wall[0]) - 1]
	for i in range(len(wall[0])):
		if wall[0][i] == 'B' and wall[1][i] == 'B':
			continue
		elif wall[0][i] == 'B':
			pos = [0, i]
			break
		else:
			pos = [1, i]
			break
	for i in range(pos[1] + 1, len(wall[0])):
		if wall[0][i] == 'B' and wall[1][i] == 'B':
			if pos[0] == 0:
				pos[0] = 1
			else:
				pos[0] = 0
		elif wall[pos[0]][i] == 'B':
			pos[1] = i
		else:
			return 'NO'
	return 'YES'
t = int(input())
for i in range(t):
	m = int(input())
	wall = [input() for j in range(2)]
	print(is_paintable(wall))


def search_end(cur, mat, cnt):
	(x, y) = cur
	while x < m:
		if mat[y][x] == 'W':
			return False
		else:
			cnt -= 1
		if mat[1 - y][x] != 'W':
			y = 1 - y
			cnt -= 1
		x += 1
	if not cnt:
		return True
	return False
for _ in range(int(input())):
	m = int(input())
	mat = [list(input()) for _ in range(2)]
	x = search_end((0, 0), mat, mat[0].count('B') + mat[1].count('B'))
	y = search_end((0, 1), mat, mat[0].count('B') + mat[1].count('B'))
	print('YES' if x or y else 'NO')


t = int(input())
for z in range(t):
	m = int(input())
	r1 = input()
	r2 = input()
	ans = 'YES'
	b1 = False
	b2 = False
	if r1[0] == 'B':
		b1 = True
	if r2[0] == 'B':
		b2 = True
	for i in range(1, m):
		if b1 and b2:
			if r1[i] == 'W':
				b1 = False
			if r2[i] == 'W':
				b2 = False
		elif b1 and (not b2):
			if r1[i] == 'W':
				b1 = False
			elif r2[i] == 'B':
				b1 = False
				b2 = True
		elif b2 and (not b1):
			if r2[i] == 'W':
				b2 = False
			elif r1[i] == 'B':
				b2 = False
				b1 = True
		if b2 == b1 == False:
			ans = 'NO'
			break
	print(ans)


t = int(input())

def run():
	num = int(input())
	string1 = input()
	string2 = input()
	sub_ing = []
	tp = 3
	count_2B = 0
	befor_2B = 3
	start = 1
	for i in range(num):
		mid = 0
		if string1[i] == 'B':
			mid += 1
		if string2[i] == 'B':
			mid += 2
		if mid & tp == 0:
			print('NO')
			return 0
		if mid == 3:
			if start:
				continue
			count_2B += 1
		else:
			start = 0
			if count_2B & 1 == 1 and befor_2B & mid:
				print('NO')
				return 0
			elif count_2B & 1 == 0 and befor_2B & mid == 0:
				print('NO')
				return 0
			count_2B = 0
			befor_2B = mid
		tp = mid
	print('YES')
while t:
	t -= 1
	run()


t = int(input())
for i in range(0, t):
	n = int(input())
	r1 = input()
	r2 = input()
	up = False
	down = False
	for j in range(0, n):
		if r1[j] == 'W' and r2[j] == 'W':
			print('NO')
			break
		if j != 0:
			if r1[j] == 'B':
				if r2[j] == 'W':
					if up == False:
						print('NO')
						break
					else:
						up = True
						down = False
				elif down and up:
					if j + 1 < n - 1:
						if r1[j + 1] == 'B' and r2[j + 1] == 'B':
							up = True
							down = True
						elif r1[j + 1] == 'W' and r2[j + 1] == 'B':
							up = False
							down = True
						elif r1[j + 1] == 'B' and r2[j + 1] == 'W':
							up = True
							down = False
				elif down:
					up = True
					down = False
				elif up:
					up = False
					down = True
			elif down == False:
				print('NO')
				break
			else:
				up = False
				down = True
		else:
			if r1[j] == 'B':
				up = True
			if r2[j] == 'B':
				down = True
		if j == n - 1:
			print('YES')


def switch(x):
	if x == 1:
		return 2
	else:
		return 1
t = int(input())
for i in range(t):
	m = int(input())
	r1 = input()
	r2 = input()
	r = [0, r1, r2]
	flag = 0
	if r1[0] == 'B':
		(x, y) = (1, 1)
		for j in range(m - 1):
			if r[switch(x)][y - 1] == 'B':
				x = switch(x)
			if r[x][y] == 'B':
				y += 1
			else:
				flag = flag + 1
				break
	else:
		flag = flag + 1
	if r2[0] == 'B':
		(x, y) = (2, 1)
		for j in range(m - 1):
			if r[switch(x)][y - 1] == 'B':
				x = switch(x)
			if r[x][y] == 'B':
				y += 1
			else:
				flag = flag + 1
				break
	else:
		flag += 1
	if flag == 2:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	m = int(input())
	s1 = input()
	s2 = input()
	bl = 0
	chot = 0
	j = 0
	while bl == 0 and j <= m - 1:
		if s1[j] == 'B' and s2[j] == 'B' and (chot != 0):
			if chot == 1:
				chot = 2
			else:
				chot = 1
		if s1[j] == 'W':
			if chot == 1:
				bl = 1
			else:
				chot = 2
		if s2[j] == 'W':
			if chot == 2:
				bl = 1
			else:
				chot = 1
		j += 1
	if bl == 0:
		print('YES')
	else:
		print('NO')


def solve():
	n = int(input())
	s = [input(), input()]
	for t in range(2):
		x = t
		ok = 1
		for i in range(n):
			if s[x][i] != 'B':
				ok = 0
			if s[not x][i] == 'B':
				x ^= 1
		if ok:
			print('YES')
			return
	print('NO')
t = int(input())
for _ in range(t):
	solve()


def ss(n, dp, i, j):
	d = [['w'] * n, ['w'] * n]
	while j < n:
		if dp[i][j] == 'b':
			d[i][j] = 'b'
		else:
			break
		if dp[i ^ 1][j] == 'b' and d[i ^ 1][j] == 'w':
			i = i ^ 1
		else:
			j += 1
	if d == dp:
		return 1
	return 0
for _ in range(int(input())):
	n = int(input())
	dp = []
	a = list(input().lower())
	dp.append(a)
	b = list(input().lower())
	dp.append(b)
	j = 0
	i = 0
	res = 0
	while j < n:
		if dp[0][j] == 'b' or dp[1][j] == 'b':
			break
		j += 1
	if j < n:
		if dp[0][j] == 'b':
			res = ss(n, dp, 0, j)
		if res == 0 and dp[1][j] == 'b':
			res = ss(n, dp, 1, j)
	if res == 1:
		print('YES')
	else:
		print('NO')


for _ in ' ' * int(input()):
	n = int(input())
	x = input()
	y = input()
	a = -1
	for i in range(n):
		if a != -1:
			if x[i] == y[i]:
				if x[i] == 'B':
					a = 1 - a
			elif a == 0 and x[i] == 'W' or (a == 1 and y[i] == 'W'):
				print('NO')
				break
		elif x[i] != y[i]:
			if x[i] == 'B':
				a = 0
			else:
				a = 1
	else:
		print('YES')


def checkPath(row, c1, c2, n):
	for i in range(n - 1):
		if (c1[i + 1] == 'B') & (c2[i + 1] == 'B') & (row == 'UP'):
			row = 'Down'
		elif (c1[i + 1] == 'B') & (c2[i + 1] == 'B') & (row == 'Down'):
			row = 'UP'
		elif (row == 'UP') & (c1[i + 1] == 'W'):
			return 'NO'
		elif (row == 'Down') & (c2[i + 1] == 'W'):
			return 'NO'
	return 'YES'
t = int(input())
result = []
for i in range(t):
	n = int(input())
	c1 = input()
	c2 = input()
	if (c1[0] == 'B') & (c2[0] == 'B'):
		if (checkPath('Down', c1, c2, n) == 'YES') | (checkPath('UP', c1, c2, n) == 'YES'):
			result.append('YES')
		else:
			result.append('NO')
	elif (c1[0] == 'W') & (c2[0] == 'B'):
		result.append(checkPath('Down', c1, c2, n))
	else:
		result.append(checkPath('UP', c1, c2, n))
for i in result:
	print(i)


iter = int(input())
for _ in range(iter):
	n = int(input())
	top = input()
	bot = input()
	level = [top, bot]
	l = 0
	firstw = 0
	ans = 1
	i = 0
	while i < n - 1:
		if firstw:
			if level[l][i + 1] == 'B':
				if level[l ^ 1][i + 1] == 'B':
					l = l ^ 1
			else:
				ans = 0
		elif level[0][i] == 'W':
			firstw = 1
			l = 1
			i -= 1
		elif level[1][i] == 'W':
			firstw = 1
			l = 0
			i -= 1
		i += 1
	if ans:
		print('YES')
	else:
		print('NO')


import math
for i in range(int(input())):
	n = int(input())
	s1 = input()
	s2 = input()
	d = 1
	if s1[0] == 'B':
		c = 0
		x = 0
		for i in range(1, n):
			if x == 0:
				if s1[i] == 'B' and s2[i] == 'W':
					x = 0
				elif s1[i] == 'B' and s2[i] == 'B':
					x = 1
				else:
					c = c + 1
			elif s1[i] == 'W' and s2[i] == 'B':
				x = 1
			elif s1[i] == 'B' and s2[i] == 'B':
				x = 0
			else:
				c = c + 1
		d = c
	if s2[0] == 'B':
		c = 0
		x = 1
		for i in range(1, n):
			if x == 0:
				if s1[i] == 'B' and s2[i] == 'W':
					x = 0
				elif s1[i] == 'B' and s2[i] == 'B':
					x = 1
				else:
					c = c + 1
			elif s1[i] == 'W' and s2[i] == 'B':
				x = 1
			elif s1[i] == 'B' and s2[i] == 'B':
				x = 0
			else:
				c = c + 1
	if c == 0 or d == 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	wall = []
	wall.append(input())
	wall.append(input())
	state = None
	res = True
	for i in range(n):
		if wall[0][i] == 'B' and wall[1][i] == 'B':
			if state != None:
				state = 0 if state == 1 else 1
		if wall[0][i] == 'B' and wall[1][i] == 'W':
			if state == None or state == 0:
				state = 0
				continue
			else:
				res = False
				break
		if wall[0][i] == 'W' and wall[1][i] == 'B':
			if state == None or state == 1:
				state = 1
				continue
			else:
				res = False
				break
	print('YES') if res else print('NO')


from collections import defaultdict

def f1(a, b):
	if a == 'B' and b == 'B':
		return 3
	elif a == 'B':
		return 1
	else:
		return 2
for _ in range(int(input())):
	ln = int(input())
	a = input()
	b = input()
	st = [a, b]
	pre = 3
	bval = True
	for i in range(ln):
		b = f1(st[0][i], st[1][i])
		if b == 3:
			if pre == 3:
				continue
			else:
				pre = 2 if pre == 1 else 1
		elif pre != b and pre != 3:
			bval = not bval
			print('NO')
			break
		else:
			pre = b
	if bval:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	m = []
	m.append(list(input()))
	m.append(list(input()))
	last = -1
	db = 0
	f = True
	for c in range(n):
		if m[0][c] == 'B' and m[1][c] == 'B':
			db += 1
		elif m[0][c] == 'B':
			if db % 2 == 0 and (last == 0 or last == -1):
				last = 0
			elif db % 2 == 1 and (last == 1 or last == -1):
				last = 0
			else:
				f = False
				break
			db = 0
		elif m[1][c] == 'B':
			if db % 2 == 0 and (last == 1 or last == -1):
				last = 1
			elif db % 2 == 1 and (last == 0 or last == -1):
				last = 1
			else:
				f = False
				break
			db = 0
		else:
			f = False
			break
	if f:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	s2 = input()
	l1 = list(s)
	l2 = list(s2)
	c = 0
	vis1 = 0
	vis2 = 0
	for i in range(n - 1):
		a = l1[i]
		b = l2[i]
		if a == b and a == 'B':
			if vis1 == 1:
				if l2[i + 1] == 'W':
					c = c + 1
					break
				else:
					vis2 = 1
					vis1 = 0
			elif vis2 == 1:
				if l1[i + 1] == 'W':
					c = c + 1
					break
				else:
					vis1 = 1
					vis2 = 0
			else:
				pass
		elif a == 'B':
			if l1[i + 1] == 'W':
				c = c + 1
				break
			else:
				vis1 = 1
				vis2 = 0
		elif l2[i + 1] == 'W':
			c = c + 1
			break
		else:
			vis2 = 1
			vis1 = 0
	if c == 0:
		print('YES')
	else:
		print('NO')


def solve(state, a):
	for i in range(1, len(a)):
		if a[i] == state or (i == len(a) - 1 and a[i] == 2):
			continue
		elif a[i] == 2:
			state *= -1
		else:
			return False
	return True
t = int(input())
for i in range(t):
	m = int(input())
	s1 = input()
	s2 = input()
	if m == 1:
		print('YES')
	else:
		l = [s1, s2]
		count = [1] * m
		for i in range(m):
			if l[0][i] == 'B' and l[1][i] == 'B':
				count[i] = 2
			else:
				count[i] = 1 if l[0][i] == 'B' else -1
		if count[0] == 1:
			if solve(1, count):
				print('YES')
			else:
				print('NO')
		elif count[0] == -1:
			if solve(-1, count):
				print('YES')
			else:
				print('NO')
		elif solve(-1, count) or solve(1, count):
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	s = [input() for i in range(2)]
	pos = -1
	for i in range(n):
		if s[0][i] != s[1][i]:
			pos = i
	if pos == -1:
		print('YES')
		continue
	ok = True
	cur = 0 if s[0][pos] == 'B' else 1
	for i in range(pos + 1, n):
		if s[cur][i] == 'W':
			ok = False
		if s[cur ^ 1][i] == 'B':
			cur ^= 1
	cur = 0 if s[0][pos] == 'B' else 1
	for i in range(pos - 1, -1, -1):
		if s[cur][i] == 'W':
			ok = False
		if s[cur ^ 1][i] == 'B':
			cur ^= 1
	print('YES' if ok else 'NO')


n = int(input())
for i in range(n):
	a = int(input())
	c1 = str(input())
	c2 = str(input())
	ct = 0
	prev = -1
	x = True
	for j in range(len(c1)):
		if (c1[j], c2[j]) == ('B', 'B'):
			ct += 1
		elif ct % 2 == 0:
			if prev != -1:
				if (c1[j], c2[j]) != (c1[prev], c2[prev]):
					x = False
					break
				else:
					ct = 0
					prev = j
			else:
				ct = 0
				prev = j
		elif prev != -1:
			if (c1[j], c2[j]) == (c1[prev], c2[prev]):
				x = False
				break
			else:
				ct = 0
				prev = j
		else:
			ct = 0
			prev = j
	if x == True:
		print('YES')
	else:
		print('NO')


def paint(wall, pos, prev):
	n = len(wall[0])
	for i in range(n * 2):
		if pos[0] == 0:
			if prev != 1 and wall[1][pos[1]] == 'B':
				prev = 0
				pos[0] = 1
			elif pos[1] + 1 == n:
				return True
			elif wall[0][pos[1] + 1] == 'B':
				prev = 0
				pos[1] = pos[1] + 1
			else:
				return False
		elif prev != 0 and wall[0][pos[1]] == 'B':
			prev = 1
			pos[0] = 0
		elif pos[1] + 1 == n:
			return True
		elif wall[1][pos[1] + 1] == 'B':
			prev = 1
			pos[1] = pos[1] + 1
		else:
			return False
	return True

def is_paintable(wall):
	if wall[0][0] == 'B' and wall[1][0] != 'B':
		ans = paint(wall, pos=[0, 0], prev=0)
	elif wall[1][0] == 'B' and wall[0][0] != 'B':
		ans = paint(wall, pos=[1, 0], prev=1)
	else:
		ans = paint(wall, pos=[0, 0], prev=0)
		if ans == True:
			return 'YES'
		ans = paint(wall, pos=[1, 0], prev=1)
	return 'YES' if ans == True else 'NO'
t = int(input())
for i in range(t):
	m = int(input())
	wall = [input() for j in range(2)]
	print(is_paintable(wall))


t = int(input())
for _ in range(t):
	m = int(input())
	wall = []
	wall.append(input())
	wall.append(input())
	check = False
	visited = [[False] * m for i in range(2)]
	if wall[0][0] == 'B':
		i = 0
		j = 0
		while j < m - 1:
			visited[i][j] = True
			if wall[1 - i][j] == 'B' and (not visited[1 - i][j]):
				i = 1 - i
			elif wall[i][j + 1] == 'B':
				j += 1
			else:
				break
		if j == m - 1:
			check = True
	visited = [[False] * m for i in range(2)]
	if wall[1][0] == 'B' and (not check):
		i = 1
		j = 0
		while j < m - 1:
			visited[i][j] = True
			if wall[1 - i][j] == 'B' and (not visited[1 - i][j]):
				i = 1 - i
			elif wall[i][j + 1] == 'B':
				j += 1
			else:
				break
		if j == m - 1:
			check = True
	if check:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	a = int(input())
	b = input()
	c = input()
	d = 0
	e = 0
	g = 0
	for i in range(0, a):
		if b[i] == 'B' and c[i] == 'B':
			e += 1
		else:
			if e % 2 == 1 and i != 1:
				if c[i] == 'W' and g == 1 or (b[i] == 'W' and g == 2):
					d = 1
					e = 0
					break
			elif e % 2 == 0:
				if c[i] == 'W' and g == 2 or (b[i] == 'W' and g == 1):
					d = 1
					e = 0
					break
			if b[i] == 'B':
				g = 1
			else:
				g = 2
			e = 0
	if d == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in [0] * t:
	n = int(input())
	(s, t) = (input(), input())
	cr = 0
	cri = 0
	index = 1
	tr = 1
	for i in range(n):
		if s[i] == 'W':
			cri = i
			index = i + 1
			cr = 0
			break
		if t[i] == 'W':
			cri = i
			index = i + 1
			cr = 1
			break
	while index <= n - 1:
		if cr == 0:
			if (index - cri) % 2 == 0:
				if s[index] == 'W':
					tr = 0
					break
				if t[index] == 'W':
					cri = index
					cr = 1
			else:
				if s[index] == 'W':
					cri = index
					cr = 0
				if t[index] == 'W':
					tr = 0
					break
		elif (index - cri) % 2 == 0:
			if t[index] == 'W':
				tr = 0
				break
			if s[index] == 'W':
				cri = index
				cr = 0
		else:
			if t[index] == 'W':
				cri = index
				cr = 1
			if s[index] == 'W':
				tr = 0
				break
		index += 1
	if tr == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = []
	a.append(list(input()))
	a.append(list(input()))
	row = 0
	first = 0
	ans = 1
	for j in range(n):
		flag = 0
		if a[0][j] == 'B':
			flag += 1
		if a[1][j] == 'B':
			flag += 2
		if flag == 3:
			row += 1
		elif flag != 3 and row == 0:
			if first != 0 and first != flag:
				ans = 0
				break
			first = flag
		elif flag != 3 and row != 0:
			if row % 2 == 1 and first == flag:
				ans = 0
				break
			elif row % 2 == 0 and first != flag and (first != 0):
				ans = 0
				break
			row = 0
			first = flag
	if ans == 0:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a1 = list(input())
	a2 = list(input())
	if n == 1:
		print('YES')
	else:
		c = 0
		i = 1
		while i < n:
			if a1[i] == 'W' and a2[i] == 'B' and (a2[i - 1] == 'W') and (a1[i - 1] == 'B'):
				c = 1
				break
			elif a1[i] == 'B' and a2[i] == 'W' and (a2[i - 1] == 'B') and (a1[i - 1] == 'W'):
				c = 1
				break
			elif a1[i] == 'B' and a2[i] == 'B' and (i == n - 1):
				break
			elif a1[i] == 'B' and a2[i] == 'B' and (a1[i - 1] != 'B' or a2[i - 1] != 'B'):
				j = i + 1
				c1 = 1
				while a1[j] == 'B' and a2[j] == 'B' and (j != n - 1):
					c1 += 1
					j += 1
				if a1[j] == 'B' and a2[j] == 'B' and (j == n - 1):
					break
				if c1 % 2 == 0 and a1[j] != a1[i - 1] or (c1 % 2 == 1 and a1[j] == a1[i - 1]):
					c = 1
					break
				i += c1
			else:
				i += 1
		if c == 1:
			print('NO')
		else:
			print('YES')


def start1(a, b):
	for i in range(len(a)):
		if a[i] == b[i] == 'W':
			return True
	return False

def start2(a, b):
	for i in range(len(a) - 1):
		if a[i] == b[i + 1] == 'W':
			return True
	return False

def start3(la, lb):
	a = 0
	b = 0
	c = 0
	d = 0
	for i in range(len(la)):
		c = a
		d = b
		if la[i] == 'W':
			a = i + 1
			if c == d:
				continue
			elif c > d and (a - c) % 2 == 0:
				return True
			elif c < d and (a - d) % 2 == 1:
				return True
		elif lb[i] == 'W':
			b = i + 1
			if c == d:
				continue
			elif c > d and (b - c) % 2 == 1:
				return True
			elif c < d and (b - d) % 2 == 0:
				return True
	return False
for i in range(int(input())):
	x = int(input())
	la = input()
	lb = input()
	if start1(la, lb):
		print('NO')
		continue
	if start2(la, lb):
		print('NO')
		continue
	if start2(lb, la):
		print('NO')
		continue
	if start3(la, lb):
		print('NO')
		continue
	print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	wall = [input(), input()]
	answer = 'YES'
	a = n
	for i in range(n):
		if wall[0][i] == 'B' and wall[1][i] == 'B':
			pass
		elif wall[0][i] == 'B':
			a = i
			now = 0
			break
		elif wall[1][i] == 'B':
			a = i
			now = 1
			break
		else:
			answer = 'NO'
			break
	if answer == 'NO':
		print(answer)
		continue
	start = -1
	for i in range(a, n):
		if wall[0][i] == 'B' and wall[1][i] == 'B':
			if start == -1:
				start = i
				count = 1
			else:
				count += 1
		elif start != -1:
			if wall[(now + count) % 2][i] == 'B':
				now = (now + count) % 2
				start = -1
			else:
				answer = 'NO'
				break
		if not (wall[0][i] == 'B' and wall[1][i] == 'B') and i != n - 1 and (wall[now][i + 1] != 'B'):
			answer = 'NO'
			break
	print(answer)


t = int(input())
for _ in range(t):
	m = int(input())
	a = [[i for i in input()]]
	a.append([i for i in input()])
	coord = [0, 0]
	f = 0
	for i in range(m - 1):
		if a[0][i] == 'B' and a[0][i + 1] == 'W':
			coord = [0, i]
			break
		elif a[1][i] == 'B' and a[1][i + 1] == 'W':
			coord = [1, i]
			break
		elif a[0][i] == 'B' and a[1][i] == 'W':
			coord = [0, i]
			break
		elif a[1][i] == 'B' and a[0][i] == 'W':
			coord = [1, i]
			break
	else:
		coord = [1, m - 1]
	for i in range(coord[1], m - 1):
		coord[1] = i
		check = 1 - coord[0]
		if a[check][i] == 'B':
			a[coord[0]][i] = 'W'
			coord[0] = check
		if a[coord[0]][coord[1] + 1] == 'W':
			for j in range(coord[1] + 1, m):
				if a[0][j] == 'B' or a[1][j] == 'B':
					print('NO')
					f = 1
					break
			else:
				print('YES')
				break
		if f:
			break
	else:
		print('YES')


nCases = int(input())
for _ in range(nCases):
	m = int(input())
	F_1 = list(input())
	F_2 = list(input())
	pin = [F_1[0], F_2[0]]
	check = False
	if pin[0] == pin[1] == 'B':
		check = 0
	elif pin[0] == 'B':
		check = 1
	elif pin[1] == 'B':
		check = 2
	for i in range(1, m):
		if check == 0:
			if F_1[i] == F_2[i] == 'B':
				pass
			elif F_1[i] == 'B':
				check = 1
			elif F_2[i] == 'B':
				check = 2
		elif F_1[i] == F_2[i] == 'B':
			check = 3 - check
		elif F_1[i] == pin[0] == 'B' and check == 1 or (F_2[i] == pin[1] == 'B' and check == 2):
			pass
		else:
			check = -1
			break
		pin = [F_1[i], F_2[i]]
	if check >= 0:
		print('YES')
	else:
		print('NO')


def find(m):
	s1 = input()
	s2 = input()
	flag = True
	array = [-1, -1]
	if s1[0] == 'B':
		array[0] = 0
	if s2[0] == 'B':
		array[1] = 1
	for i in range(1, m):
		b = array.copy()
		if (s1[i] == 'B' and array[0] == -1) and (s2[i] == 'B' and array[1] == -1):
			flag = False
			break
		if s1[i] == 'B' and array[0] == -1 and (s2[i] == 'W') or (s2[i] == 'B' and array[1] == -1 and (s1[i] == 'W')):
			flag = False
			break
		if s1[i] == 'B' and array[0] == -1:
			array[0] = 0
			array[1] = -1
		if s2[i] == 'B' and b[1] == -1:
			array[1] = 1
			array[0] = -1
		if s1[i] != 'B':
			array[0] = -1
		if s2[i] != 'B':
			array[1] = -1
	if flag:
		print('YES')
	else:
		print('NO')
t = int(input())
for i in range(0, t):
	m = int(input())
	find(m)


import os
import math
import collections
import bisect
import functools
import itertools
import heapq
import sys
sys.setrecursionlimit(100000000)

def solve(a, n):
	target = a[0].count('B') + a[1].count('B')

	def dfs(x, y, arr):
		while y < n - 1:
			(i, j) = (1 - x, y)
			if 0 <= i < 2 and 0 <= j < n and (arr[i][j] == 'B'):
				arr[i][j] = 'W'
				(x, y) = (i, j)
				continue
			(i, j) = (x, y + 1)
			if 0 <= i < 2 and 0 <= j < n and (a[i][j] == 'B'):
				arr[i][j] = 'W'
				(x, y) = (i, j)
				continue
			return False
		return True
	if a[0][0] == 'B':
		a1 = [a[0][:], a[1][:]]
		a1[0][0] = 'W'
		if dfs(0, 0, a1):
			return True
	if a[1][0] == 'B':
		a1 = [a[0][:], a[1][:]]
		a1[1][0] = 'W'
		if dfs(1, 0, a1):
			return True
	return False
directs = [[-1, 0], [0, -1], [1, 0], [0, 1]]
t = int(input())
for _ in range(t):
	n = int(input())
	a = [[c for c in input()], [c for c in input()]]
	ok = solve(a, n)
	if ok:
		print('YES')
	else:
		print('NO')


from math import *
from collections import deque
from functools import lru_cache
from collections import defaultdict, Counter
from bisect import *
MOD = 10 ** 9 + 7
import sys

def solution():
	m = int(input())
	arr = []
	arr.append(list(input()))
	arr.append(list(input()))

	def dfs(i, j):
		if j >= len(arr[0]):
			return True
		if arr[i][j] == 'W':
			return False
		nbr_i = 1 - i
		if arr[nbr_i][j] == 'B':
			arr[i][j] = '#'
			ans = dfs(nbr_i, j)
			arr[i][j] = 'B'
			return ans
		ans = dfs(i, j + 1)
		return ans
	res = False
	if arr[0][0] == 'B':
		res = res or dfs(0, 0)
	if not res and arr[1][0] == 'B':
		res = res or dfs(1, 0)
	print(['NO', 'YES'][res])

def main():
	t = 1
	t = int(input())
	for _ in range(t):
		solution()
import sys
import threading
sys.setrecursionlimit(10 ** 6)
threading.stack_size(1 << 27)
thread = threading.Thread(target=main)
thread.start()
thread.join()


for _ in range(int(input())):
	n = int(input())
	rowone = input()
	rowtwo = input()
	s = []
	for i in range(n):
		x = 0
		if len(s) != 0:
			x = s[-1]
		y = 0
		if rowone[i] == 'B':
			y = 1
		if rowtwo[i] == 'B' and rowone[i] == 'W':
			y = 2
		if rowtwo[i] == 'B' and rowone[i] == 'B':
			y = 3
		if x != 0:
			if x == 3 and y == 3:
				s.pop()
			else:
				s.append(y)
		else:
			s.append(y)
	check = 1
	prev = 0
	if len(s) != 0:
		prev = s[-1]
		s.pop()
	while len(s) != 0:
		curr = s[-1]
		s.pop()
		if prev == 3:
			prev = curr
		if curr == 3:
			if prev == 1:
				prev = 2
			else:
				prev = 1
		elif prev != curr:
			check = 0
			break
	if check == 1:
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	m = int(input())
	s0 = list(input())
	s1 = list(input())
	i = 0
	ans = 'YES'
	for at in range(2):
		time = at
		i = 0
		ans = 'YES'
		while i < m:
			s0i = s0[i]
			s1i = s1[i]
			if i > 1:
				s0i_1 = s0[i - 1]
				s1i_1 = s1[i - 1]
			else:
				s0i_1 = 'B'
				s1i_1 = 'B'
			if i == m - 1:
				s0i1 = 'B'
				s1i1 = 'B'
			else:
				s0i1 = s0[i + 1]
				s1i1 = s1[i + 1]
			if s0i == 'B' and s1i == 'B':
				atin = at
				if atin == 0:
					at = 1
				else:
					at = 0
				if at == 0:
					if s0i1 == 'B':
						i += 1
					else:
						ans = 'NO'
						break
				elif at == 1:
					if s1i1 == 'B':
						i += 1
					else:
						ans = 'NO'
						break
			elif s0i == 'B':
				if s0i_1 == 'B' and s0i1 == 'B':
					i += 1
					at = 0
				else:
					ans = 'NO'
					break
			elif s1i_1 == 'B' and s1i1 == 'B':
				i += 1
				at = 1
			else:
				ans = 'NO'
				break
		if ans == 'YES' and time == 0:
			break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr1 = input()
	c1 = list(map(str, arr1))
	arr2 = input()
	c2 = list(map(str, arr2))
	ans = 'YES'
	j = 0
	if n == 1:
		print('YES')
	else:
		while j < n - 1:
			if c1[j] == 'W':
				count = 1
				while count < n - j and c1[j + count] == 'B' and (c2[j + count] == 'B'):
					count += 1
				if count % 2 == 1 and count < n - j and (c1[j + count] == 'B'):
					ans = 'NO'
					break
				elif count % 2 == 0 and count < n - j and (c1[j + count] == 'W'):
					ans = 'NO'
					break
				j = j + count
			elif c2[j] == 'B':
				j = j + 1
			else:
				count = 1
				while count < n - j and c1[j + count] == 'B' and (c2[j + count] == 'B'):
					count += 1
				if count % 2 == 1 and count < n - j and (c1[j + count] == 'W'):
					ans = 'NO'
					break
				elif count % 2 == 0 and count < n - j and (c1[j + count] == 'B'):
					ans = 'NO'
					break
				j = j + count
		print(ans)


t = int(input())
for i in range(0, t):
	m = int(input())
	a1 = input()
	a2 = input()
	bl = True
	j = 0
	while j < m:
		if a1[j] == 'W' or a2[j] == 'W':
			bl2 = True
			for k in range(j + 1, m):
				if a1[k] == 'W' or a2[k] == 'W':
					bl2 = False
					if a1[j] == 'W' and a1[k] == 'W':
						if (k - j) % 2 == 0:
							bl = False
							break
						else:
							j = k
							break
					elif a2[j] == 'W' and a2[k] == 'W':
						if (k - j) % 2 == 0:
							bl = False
							break
						else:
							j = k
							break
					elif a1[j] == 'W' and a2[k] == 'W':
						if (k - j) % 2 != 0:
							bl = False
							break
						else:
							j = k
							break
					elif a2[j] == 'W' and a1[k] == 'W':
						if (k - j) % 2 != 0:
							bl = False
							break
						else:
							j = k
							break
			if bl == False:
				break
			if bl2 == True:
				break
		else:
			j = j + 1
	if bl:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	s1 = input()
	s2 = input()
	a = [0 + int(s1[i] == 'B') for i in range(n)]
	b = [0 + int(s2[i] == 'B') for i in range(n)]
	if a[0] == 1:
		a[0] = 2
	if b[0] == 1:
		b[0] = 2
	flag = True
	if a[0] == 2 or b[0] == 2:
		for i in range(1, n):
			if a[i - 1] == 2 and b[i] == 1 and (a[i] == 1):
				b[i] = 2
			if b[i - 1] == 2 and a[i] == 1 and (b[i] == 1 or b[i] == 2):
				a[i] = 2
			if a[i - 1] == 2 and b[i] == 0 and (a[i] == 1):
				a[i] = 2
			if b[i - 1] == 2 and a[i] == 0 and (b[i] == 1):
				b[i] = 2
	if a[-1] == 2 or b[-1] == 2:
		print('YES')
	else:
		print('NO')


def solve(m, grid):
	dp = [[False] * m for _ in range(2)]
	dp[0][m - 1] = grid[0][m - 1] == 'B'
	dp[1][m - 1] = grid[1][m - 1] == 'B'
	for col in range(m - 2, -1, -1):
		for row in range(2):
			dp[row][col] = grid[row][col] == 'B' and (grid[(row + 1) % 2][col] == 'B' and dp[(row + 1) % 2][col + 1] or (grid[(row + 1) % 2][col] == 'W' and dp[row][col + 1]))
	return 'YES' if dp[0][0] or dp[1][0] else 'NO'
t = int(input())
for _ in range(t):
	m = int(input())
	grid = []
	for row in range(2):
		grid.append(input())
	print(solve(m, grid))


def countB(arr, i):
	return int(arr[0][i] == 'B') + int(arr[1][i] == 'B')
for t in range(int(input())):
	n = int(input())
	arr = []
	for _ in range(2):
		arr.append(input())
	prev = -1
	double = 0
	flag = True
	for i in range(n):
		if countB(arr, i) == 2 and prev != -1:
			double += 1
		if countB(arr, i) == 1:
			if prev == -1:
				if arr[0][i] == 'B':
					prev = 0
				else:
					prev = 1
				continue
			if double % 2 == 0:
				if arr[prev][i] != 'B':
					flag = False
			elif arr[1 - prev][i] != 'B':
				flag = False
			else:
				prev = 1 - prev
			double = 0
	if flag:
		print('YES')
	else:
		print('NO')


from collections import deque
for _ in range(int(input())):
	n = int(input())
	matrix = [input() for _ in range(2)]
	count = 0
	for col in range(n):
		count += int(matrix[0][col] == 'B') + int(matrix[1][col] == 'B')

	def dfs(startCord, count):
		visited = set()
		stk = deque([startCord])
		while stk:
			currentNode = stk.popleft()
			(row, col) = currentNode
			visited.add(currentNode)
			row ^= 1
			if matrix[row][col] == 'B' and (row, col) not in visited:
				stk.append((row, col))
				continue
			row ^= 1
			col += 1
			if col < n and (row, col) not in visited:
				stk.append((row, col))
		return len(visited) == count
	if matrix[0][0] == 'B' and dfs((0, 0), count):
		print('YES')
	elif matrix[1][0] == 'B' and dfs((1, 0), count):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	A = [[0 for __ in range(n)] for _ in range(2)]
	B = [list(input()) for _ in range(2)]
	row = 0
	for i in range(2):
		for j in range(n):
			A[i][j] = int(B[i][j] == 'B')
			B[i][j] = A[i][j]

	def func(row, pred, flag):
		i = 0
		while i < n - 1:
			if pred[row][i]:
				pred[row][i] = 0
				pos = 1 if not row else -1
				if not pred[row][i + 1] and (not pred[row + pos][i]):
					flag = 0
					break
				elif not pred[row][i + 1] and pred[row + pos][i]:
					row ^= 1
					i -= 1
				elif pred[row][i + 1] and pred[row + pos][i]:
					row ^= 1
					i -= 1
			else:
				flag = 0
			i += 1
		return flag
	f1 = func(0, A, 1)
	f2 = func(1, B, 1)
	print(('NO', 'YES')[f1 or f2])


from copy import deepcopy

def solution():

	def paint(i: int) -> bool:
		j = 0
		while j < n:
			if s[i][j] == 'B':
				s[i][j] = 'W'
				if s[i ^ 1][j] == 'B':
					i ^= 1
					continue
				else:
					j += 1
			else:
				return False
		return True
	n = int(input())
	s1 = [list(input()), list(input())]
	s = deepcopy(s1)
	if paint(0):
		print('YES')
		return
	s = deepcopy(s1)
	if paint(1):
		print('YES')
	else:
		print('NO')

def main():
	t = int(input())
	while t:
		solution()
		t -= 1
main()