You are given a hexagonal grid consisting of two rows, each row consisting of $n$ cells. The cells of the first row are labelled $a_1,a_2,\ldots a_n$ and the cells of the second row are labelled $b_1,b_2,\ldots,b_n$.  

For example, for $n=6$:

(Note that the $b_i$ is connected with $a_{i+1}$.)

Your task is to tile this grid with $2\times1$ tiles that look like the following:

As you can see above, there are three possible orientations in which a tile can be placed.

Your goal is to tile the whole grid such that every cell is covered by a tile, and no two tiles occupy the same cell. To add to the woes, certain cells of the hexagonal grid are blackened. No tile must occupy a blackened cell.

Is it possible to tile the grid?

Here's an example. Suppose we want to tile this grid:

Then we can do the tiling as follows:

Input Format

The first line contains a single integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.  

The first line of each test case contains a single integer $n$ denoting the length of the grid. 

The second line contains a binary string of length $n$. The $i^{\cdot t h}$ character describes whether cell $a_i$ is blackened. 

The third line contains a binary string of length $n$. The $i^{\cdot th}$ character describes whether cell $b_i$ is blackened. 

A 0 corresponds to an empty cell and a 1 corresponds to blackened cell.  

Constraints

$1\leq t\leq100$  
$1\leq n\leq10$  

Output Format

For each test case, print YES if there exists at least one way to tile the grid, and NO otherwise.

Sample Input 0
6
6
010000
000010
2
00
00
2
00
10
2
00
01
2
00
11
2
10
00

Sample Output 0
YES
YES
NO
NO
YES
NO

Explanation 0

The first test case in the sample input describes the example given in the problem statement above. 

For the second test case, there are two ways to fill it: either place two diagonal tiles side-by-side or place two horizontal tiles.

Difficulty: MEDIUM_HARD
Tags: Dynamic programming
Skill types: Dynamic programming

Starter code:

Solutions:
def nextIndex(index):
	return [index[0] + index[1], (index[1] + 1) % 2]

def onBoard(index, board):
	return index[0] < len(board[0])
for t in range(int(input())):
	n = int(input())
	line1 = input()
	line2 = input()
	board = [line1, line2]
	index = [0, 0]
	sol = 'YES'
	while index[0] < n:
		if board[index[1]][index[0]] == '1':
			index = nextIndex(index)
			continue
		index1 = nextIndex(index)
		if not onBoard(index1, board):
			sol = 'NO'
			break
		if board[index1[1]][index1[0]] == '0':
			index = nextIndex(index1)
			continue
		index2 = nextIndex(index1)
		if not onBoard(index2, board):
			sol = 'NO'
			break
		if board[index2[1]][index2[0]] == '0':
			index = nextIndex(index2)
			continue
		sol = 'NO'
		break
	print(sol)


import sys
import collections
r = sys.stdin.readline

def recursive(lines, n):
	for i in range(n):
		for j in range(2):
			if lines[j][i] == '0':
				if i < n - 1 and lines[j][i + 1] == '0':
					newlines = [lines[0][:], lines[1][:]]
					newlines[j][i] = '2'
					newlines[j][i + 1] = '2'
					ret = recursive(newlines, n)
					if ret == True:
						return ret
				if j == 0 and lines[j + 1][i] == '0':
					newlines = [lines[0][:], lines[1][:]]
					newlines[j][i] = '2'
					newlines[j + 1][i] = '2'
					ret = recursive(newlines, n)
					if ret == True:
						return ret
				if j == 1 and i < n - 1 and (lines[j - 1][i + 1] == '0'):
					newlines = [lines[0][:], lines[1][:]]
					newlines[j][i] = '2'
					newlines[j - 1][i + 1] = '2'
					ret = recursive(newlines, n)
					if ret == True:
						return ret
				return False
	return True

def solve(lines, n):
	if recursive(lines, n) == False:
		return 'NO'
	else:
		return 'YES'
text = r()
t = int(text)
for i in range(t):
	text = r()
	n = int(text)
	text = r()
	line1 = [ch for ch in text.strip()]
	c1 = collections.Counter(line1)
	text = r()
	line2 = [ch for ch in text.strip()]
	c2 = collections.Counter(line2)
	lists = [line1, line2]
	print(solve(lists, n))


def fill(a):
	for i in range(0, len(a[0]) - 1):
		if a[0][i] == 0 and a[1][i] == 0:
			a[0][i] = 1
			a[1][i] = 1
		elif a[0][i] == 0 and a[1][i] == 1:
			if a[0][i + 1] == 0:
				a[0][i + 1] = 1
			else:
				return False
			a[0][i] = 1
		elif a[0][i] == 1 and a[1][i] == 0:
			if a[0][i + 1] == 0:
				a[0][i + 1] = 1
			elif a[1][i + 1] == 0:
				a[1][i + 1] = 1
			else:
				return False
			a[1][i] = 1
	return a[0][-1] == a[1][-1]
for case in range(int(input())):
	l = int(input())
	print('YES' if fill([list(map(int, input())), list(map(int, input()))]) else 'NO')


def fill(grid, N):
	i = 0
	while i < 2 * N:
		if grid[i % 2][i] is 0:
			break
		i += 1
	if i is 2 * N:
		return True
	ans = False
	if i + 1 < 2 * N and grid[(i + 1) % 2][i + 1] is 0:
		gr = [list(grid[0]), list(grid[1])]
		gr[i % 2][i] = 1
		gr[(i + 1) % 2][i + 1] = 1
		ans = ans or fill(gr, N)
	if i + 2 < 2 * N and grid[(i + 2) % 2][i + 2] is 0:
		gr = [list(grid[0]), list(grid[1])]
		gr[i % 2][i] = 1
		gr[(i + 2) % 2][i + 2] = 1
		ans = ans or fill(gr, N)
	return ans
T = int(input())
while T > 0:
	T -= 1
	N = int(input())
	gr = [0 for i in range(2 * N)]
	grid = [list(gr), list(gr)]
	line = input()
	for i in range(N):
		grid[0][2 * i] = int(line[i])
	line = input()
	for i in range(N):
		grid[1][2 * i + 1] = int(line[i])
	ans = fill(grid, N)
	if ans:
		print('YES')
	else:
		print('NO')


def main():
	for _ in range(int(input())):
		n = int(input())
		A = list(map(int, input()))
		B = list(map(int, input()))
		for i in range(n - 1):
			if A[i] == B[i] == 0:
				A[i] = 2
				B[i] = 2
				if A[i + 1] == B[i + 1] == 0:
					A[i + 1] = 2
					B[i + 1] = 2
			elif A[i] != 0 and B[i] == 0:
				if A[i + 1] == 0:
					A[i + 1] = 2
					B[i] = 2
				elif A[i + 1] != 0 and B[i + 1] == 0:
					B[i] = 2
					B[i + 1] = 2
				elif A[i + 1] == 0:
					A[i + 1] = 2
					B[i] = 2
			elif A[i] == A[i + 1] == 0:
				A[i] = 2
				A[i + 1] = 2
			elif A[i] != 0 and B[i] != 0 and (A[i + 1] == B[i + 1] == 0):
				A[i + 1] = 2
				B[i + 1] = 2
		if n == 1 and A[0] == B[0] == 0:
			print('YES')
		elif 0 in A or 0 in B:
			print('NO')
		else:
			print('YES')
main()


def main():
	for _ in range(int(input())):
		n = int(input())
		A = list(map(int, input()))
		B = list(map(int, input()))
		for i in range(n - 1):
			if A[i] == B[i] == 0:
				A[i] = 2
				B[i] = 2
				if A[i + 1] == B[i + 1] == 0:
					A[i + 1] = 2
					B[i + 1] = 2
			elif A[i] != 0 and B[i] == 0:
				if A[i + 1] == 0:
					A[i + 1] = 2
					B[i] = 2
				elif A[i + 1] != 0 and B[i + 1] == 0:
					B[i] = 2
					B[i + 1] = 2
				elif A[i + 1] == 0:
					A[i + 1] = 2
					B[i] = 2
			elif A[i] == A[i + 1] == 0:
				A[i] = 2
				A[i + 1] = 2
			elif A[i] != 0 and B[i] != 0 and (A[i + 1] == B[i + 1] == 0):
				A[i + 1] = 2
				B[i + 1] = 2
		if n == 1 and A[0] == B[0] == 0:
			print('YES')
		elif 0 in A or 0 in B:
			print('NO')
		else:
			print('YES')
main()


def main():
	ncases = int(input())
	for _ in range(ncases):
		length = int(input())
		blocked = [[digit == '1' for digit in input()] + [True, True]]
		blocked.append([digit == '1' for digit in input()] + [True, True])
		assert all((len(row) == length + 2 for row in blocked))
		print('YES' if cansolve(blocked, 0, 0, {(length, length): True}) else 'NO')

def cansolve(blocked, start1, start2, cache):
	key = (start1, start2)
	if key in cache:
		return cache[key]
	if start1 <= start2:
		if blocked[0][start1]:
			cache[key] = cansolve(blocked, start1 + 1, start2, cache)
		else:
			cache[key] = not blocked[0][start1 + 1] and cansolve(blocked, start1 + 2, start2, cache) or (start1 == start2 and (not blocked[1][start1]) and cansolve(blocked, start1 + 1, start2 + 1, cache))
	elif blocked[1][start2]:
		cache[key] = cansolve(blocked, start1, start2 + 1, cache)
	else:
		cache[key] = not blocked[1][start2 + 1] and cansolve(blocked, start1, start2 + 2, cache) or (start1 == start2 + 1 and (not blocked[0][start1]) and cansolve(blocked, start1 + 1, start2 + 1, cache))
	return cache[key]
main()


T = int(input())
for _ in range(T):
	n = int(input())
	a = [-1] + [int(x) for x in list(input())]
	b = [-1] + [int(x) for x in list(input())]
	f = [[False] * (n + 1) for _ in range(n + 1)]
	f[0][0] = True
	if b[1] == 1:
		f[0][1] = True
	for j in range(2, n + 1):
		if b[j] == 1:
			f[0][j] = f[0][j - 1]
		elif b[j - 1] == 1:
			f[0][j] = False
		else:
			f[0][j] = f[0][j - 2]
	if a[1] == 1:
		f[1][0] = True
	for i in range(2, n + 1):
		if a[i] == 1:
			f[i][0] = f[i - 1][0]
		elif a[i - 1] == 1:
			f[i][0] = False
		else:
			f[i][0] = f[i - 2][0]
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			if a[i] == 1:
				f[i][j] = f[i - 1][j]
				continue
			if b[j] == 1:
				f[i][j] = f[i][j - 1]
				continue
			if i == j:
				if a[i] != 1 and b[j] != 1:
					f[i][j] = f[i][j] or f[i - 1][j - 1]
				if i >= 2 and a[i] != 1 and (a[i - 1] != 1):
					f[i][j] = f[i][j] or f[i - 2][j]
				if j >= 2 and b[j] != 1 and (b[j - 1] != 1):
					f[i][j] = f[i][j] or f[i][j - 2]
			if i == j + 1:
				if a[i] != 1 and b[j] != 1:
					f[i][j] = f[i][j] or f[i - 1][j - 1]
				if i >= 2 and a[i] != 1 and (a[i - 1] != 1):
					f[i][j] = f[i][j] or f[i - 2][j]
				if j >= 2 and b[j] != 1 and (b[j - 1] != 1):
					f[i][j] = f[i][j] or f[i][j - 2]
			else:
				if i >= 2 and a[i] != 1 and (a[i - 1] != 1):
					f[i][j] = f[i][j] or f[i - 2][j]
				if j >= 2 and b[j] != 1 and (b[j - 1] != 1):
					f[i][j] = f[i][j] or f[i][j - 2]
	if f[n][n]:
		print('YES')
	else:
		print('NO')


T = int(input())
for case in range(T):
	N = int(input())
	a1 = list(map(int, list(input().rstrip())))
	a2 = list(map(int, list(input().rstrip())))
	mem = {}

	def solve(i1, i2):
		if i1 == i2 == len(a1):
			return True
		if i1 > len(a1) or i2 > len(a1):
			return False
		if (i1, i2) in mem:
			return mem[i1, i2]
		ans = False
		if i1 < len(a1) and i2 < len(a1) and (a1[i1] == a2[i2]) and (i1 == i2 or i1 == i2 + 1):
			ans |= solve(i1 + 1, i2 + 1)
		if i1 + 1 < len(a1) and a1[i1] == a1[i1 + 1] == 0:
			ans |= solve(i1 + 2, i2)
		if i2 + 1 < len(a1) and a2[i2] == a2[i2 + 1] == 0:
			ans |= solve(i1, i2 + 2)
		if i1 < len(a1) and a1[i1] == 1:
			ans |= solve(i1 + 1, i2)
		if i2 < len(a1) and a2[i2] == 1:
			ans |= solve(i1, i2 + 1)
		mem[i1, i2] = ans
		return ans
	print('YES' if solve(0, 0) else 'NO')


def can_tile(cols):
	while len(cols) > 0 and cols[-1] == (True, True):
		cols.pop()
	if len(cols) == 0:
		return True
	if len(cols) == 1:
		return cols[0] == (False, False)
	[(a, c), (b, d)] = cols[-2:]
	frontier = []
	if not (b or d):
		frontier.append([(a, c), (True, True)])
	if not (c or d):
		frontier.append([(a, True), (b, True)])
	if not (a or b):
		frontier.append([(True, c), (True, d)])
	if d and (not (c or b)):
		frontier.append([(a, True), (True, d)])
	return any((can_tile(cols[:-2] + f) for f in frontier))

def parse_row():
	return [bool(int(x)) for x in input()]
cases = int(input())
for _ in range(cases):
	width = int(input())
	rows = list(zip(parse_row(), parse_row()))
	if can_tile(rows):
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	T = [False for _ in range(2 * n + 1)]
	A = input()
	B = input()
	lis = []
	for i in range(n):
		lis.append(int(A[i]))
		lis.append(int(B[i]))
	T[0] = True
	for i in range(1, 2 * n + 1):
		if lis[i - 1] == 1:
			T[i] = T[i - 1]
			continue
		if i >= 2 and T[i - 2] and (lis[i - 2] == 0):
			T[i] = True
		if i >= 3 and T[i - 3] and (lis[i - 2] == 1) and (lis[i - 3] == 0):
			T[i] = True
	print('YES' if T[2 * n] else 'NO')


def run():
	n = int(input())
	a = input()
	b = input()
	state = [[False] * (n + 1) for i in range(n + 1)]
	for i in range(n + 1):
		for j in range(n + 1):
			if i == 0 and j == 0:
				state[i][j] = True
				continue
			possible = False
			if i >= 1 and j >= 1 and (i == j or i == j + 1) and (a[i - 1] == b[j - 1]):
				possible = state[i - 1][j - 1]
			state[i][j] = possible or (i >= 2 and a[i - 1] == a[i - 2] and state[i - 2][j]) or (j >= 2 and b[j - 1] == b[j - 2] and state[i][j - 2]) or (i >= 1 and a[i - 1] == '1' and state[i - 1][j]) or (j >= 1 and b[j - 1] == '1' and state[i][j - 1])
	print('YES' if state[-1][-1] else 'NO')
for i in range(int(input())):
	run()


def hexagonal_grid(N, A, B):
	T = 2 * N
	C = [0 for _ in range(T)]
	D = []
	for i in range(len(A)):
		D.append(int(A[i]))
		D.append(int(B[i]))
	L = T - 1 - D[::-1].index(0) if 0 in D else 0
	if D[0] != 0:
		C[0] = 1
	else:
		if N > 0:
			C[1] = 1 - D[1]
		if N > 1:
			C[2] = D[1] - D[2]
	for i in range(1, T):
		if D[i] != 0:
			C[i] = C[i - 1]
		elif C[i - 1] != 0:
			if i < T - 1:
				C[i + 1] = 1 - D[i + 1]
			if i < T - 2:
				C[i + 2] = D[i + 1] - D[i + 2]
	return 'YES' if C[L] != 0 else 'NO'

def main():
	T = int(input())
	for _ in range(T):
		N = int(input())
		A = input()
		B = input()
		print(hexagonal_grid(N, A, B))
main()


import sys

def solve(listA, listB):
	if not listA and (not listB):
		return True
	if not listB and listA[0] == 0:
		return False
	if listA[0] == 1 and solve(listB, listA[1:]):
		return True
	if listA[0] == listB[0] and solve(listA[1:], listB[1:]):
		return True
	if len(listA) > 1 and listA[0] == listA[1]:
		if len(listB) > 1 and listB[0] == listB[1] and solve(listA[2:], listB[2:]):
			return True
		if listB[0] == 1:
			return solve(listB[1:], listA[2:])
T = int(sys.stdin.readline())
for _case in range(T):
	_N = sys.stdin.readline()
	listA = [int(X) for X in sys.stdin.readline().strip()]
	listB = [int(X) for X in sys.stdin.readline().strip()]
	print('YES' if solve(listA, listB) else 'NO')


def main():
	t = int(input())
	while t:
		n = int(input())
		upper_line = input()
		lower_line = input()
		pos = 1
		res = 0
		i = 0
		while i < n:
			next_vert = upper_line[i] + lower_line[i]
			if pos == 1:
				if next_vert == '11':
					res = 1
				elif next_vert == '00':
					res = 1
				elif next_vert == '10':
					res = 0
					pos = 2
				else:
					res = 0
					pos = 3
			elif pos == 2:
				if next_vert == '10':
					res = 1
					pos = 1
				elif next_vert == '01':
					res = 1
					pos = 1
				elif next_vert == '11':
					res = 0
					break
				else:
					pos = 2
			elif pos == 3:
				if next_vert == '01':
					res = 1
					pos = 1
				elif next_vert == '10':
					res = 0
					break
				elif next_vert == '00':
					res = 0
					pos = 2
				else:
					res = 0
					break
			i += 1
		if res == 1:
			print('YES')
		else:
			print('NO')
		t -= 1
main()


def back(A):
	if A[0][0] != A[0][1]:
		return None
	A = [list(L) for L in A]
	return A[1:]

def forward(A):
	if A[0][1] != A[1][0] or A[0][0] == '0':
		return (None, 0)
	A = [list(L) for L in A]
	A[0][1] = '1'
	A[1][0] = '1'
	if A[1][1] == '1':
		return (A[2:], 2)
	else:
		return (A[1:], 1)

def flat_t(A):
	if A[0][0] != A[1][0] or A[0][1] == '0':
		return None
	A = [list(L) for L in A]
	A[0][0] = '1'
	A[1][0] = '1'
	return A[1:]

def flat_b(A):
	if A[0][1] != A[1][1] or A[0][0] == '0':
		return None
	A = [list(L) for L in A]
	A[0][1] = '1'
	A[1][1] = '1'
	return A[1:]

def solve(A, N):
	if A is None:
		return False
	if N == 0:
		return True
	if N == 1:
		return A[0][0] == A[0][1]
	else:
		if solve(back(A), N - 1):
			return True
		(L, k) = forward(A)
		if solve(L, N - k):
			return True
		if solve(flat_t(A), N - 1):
			return True
		if solve(flat_b(A), N - 1):
			return True
	return False
for _ in range(int(input())):
	N = int(input())
	A = input()
	B = input()
	print('YES' if solve([[a, b] for (a, b) in zip(A, B)], N) else 'NO')


def fill(row1, row2, i1=0, i2=0):
	while i1 < len(row1) and row1[i1] == 1:
		i1 += 1
	while i2 < len(row2) and row2[i2] == 1:
		i2 += 1
	if i1 >= len(row1) and i2 >= len(row2):
		return True
	if i1 >= len(row1):
		i = i2
		while i < len(row2):
			if row2[i] == 0:
				if i + 1 >= len(row2):
					return False
				if row2[i + 1] != 0:
					return False
				i += 2
			else:
				i += 1
		return True
	if i2 >= len(row2):
		i = i1
		while i < len(row1):
			if row1[i] == 0:
				if i + 1 >= len(row1):
					return False
				if row1[i + 1] != 0:
					return False
				i += 2
			else:
				i += 1
		return True
	if i1 + 1 < len(row1) and row1[i1 + 1] == 0:
		horizontal = fill(row1, row2, i1 + 2, i2)
		if horizontal:
			return True
	if i2 + 1 < len(row2) and row2[i2 + 1] == 0:
		horizontal = fill(row1, row2, i1, i2 + 2)
		if horizontal:
			return True
	if i2 == i1 - 1:
		diagonal1 = fill(row1, row2, i1 + 1, i2 + 1)
		if diagonal1:
			return True
	if i2 == i1:
		diagonal2 = fill(row1, row2, i1 + 1, i2 + 1)
		if diagonal2:
			return True
	return False
t = int(input())
for i in range(t):
	n = int(input())
	row1 = [int(i) for i in list(input())]
	row2 = [int(i) for i in list(input())]
	if fill(row1, row2):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	(A, B) = (input(), input())
	C = [[False for i in range(n + 1)] for _ in range(n + 1)]
	C[n][n] = True
	for i in range(n, -1, -1):
		for j in range(n, -1, -1):
			if i < n and A[i] == '1':
				C[i][j] = C[i + 1][j]
			elif j < n and B[j] == '1':
				C[i][j] = C[i][j + 1]
			else:
				p1 = (i < n and i == j and (A[i] != '1') and (B[j] != '1')) and C[i + 1][j + 1]
				p2 = (i < n and i == j + 1 and (A[i] != '1') and (B[j] != '1')) and C[i + 1][j + 1]
				p3 = A[i] != '1' and A[i + 1] != '1' and C[i + 2][j] if i + 2 <= n else False
				p4 = B[j] != '1' and B[j + 1] != '1' and C[i][j + 2] if j + 2 <= n else False
				C[i][j] = C[i][j] or any([p1, p2, p3, p4])
	if C[0][0]:
		print('YES')
	else:
		print('NO')


def isBoardFillable():
	T = int(input().strip())
	for test in range(T):
		fillBoard()

def fillBoard():
	n = int(input().strip())
	memo = {}
	board = [[-int(i) for i in input()], [-int(i) for i in input()]]
	for x in range(n):
		for y in range(2):
			if oneFits(board, x, y):
				board = fitOne(board, x, y)
				break
			if twoFits(board, x, y):
				board = fitTwo(board, x, y)
				break
			if threeFits(board, x, y):
				board = fitThree(board, x, y)
				break
		if board[0][x] == 0 or board[1][x] == 0:
			print('NO')
			return 0
	print('YES')
	return 1

def oneFits(board, x, y):
	if board[y][x] == 0 and y == 0:
		if board[y + 1][x] == 0:
			return True

def twoFits(board, x, y):
	if board[y][x] == 0 and y == 1 and (x < len(board[y]) - 1):
		if board[y - 1][x + 1] == 0:
			return True

def threeFits(board, x, y):
	if board[y][x] == 0 and x < len(board[y]) - 1:
		if board[y][x + 1] == 0:
			return True

def fitOne(board, x, y):
	board[y][x] = 1
	board[y + 1][x] = 1
	return board

def fitTwo(board, x, y):
	board[y][x] = 1
	board[y - 1][x + 1] = 1
	return board

def fitThree(board, x, y):
	board[y][x] = 1
	board[y][x + 1] = 1
	return board
isBoardFillable()


def dfs(ar, i, j, visited):
	visited[i][j] = True
	c = 1
	if i == 0:
		ds = [[0, -1], [0, 1], [1, -1], [1, 0]]
	else:
		ds = [[0, -1], [0, 1], [-1, 0], [-1, 1]]
	for d in ds:
		ni = i + d[0]
		nj = j + d[1]
		if ni >= 0 and nj >= 0 and (ni < len(ar)) and (nj < len(ar[0])):
			if not visited[ni][nj] and (not ar[ni][nj]):
				c = c + dfs(ar, ni, nj, visited)
	return c
T = int(input().strip())
for t in range(T):
	N = int(input().strip())
	ar = []
	for _ in range(2):
		ar.append([int(i) for i in input().strip()])
	visited = []
	for _ in range(2):
		visited.append([0] * N)
	possible = True
	for i in range(2):
		for j in range(N):
			if not ar[i][j] and (not visited[i][j]):
				c = dfs(ar, i, j, visited)
				if c % 2:
					possible = False
					break
	print('YES' if possible else 'NO')


import sys

def existsWay(lines, n):
	cntOnes = 0
	for i in range(n):
		cntOnes += lines[0][i] + lines[1][i]
	cntZeros = 2 * n - cntOnes
	c = [[0 for i in range(n + 1)] for i in range(2)]
	c[0][0] = c[1][0] = -2
	cnt = countCells(lines, n, 0, 0)
	return 'YES' if cntZeros == cnt else 'NO'

def countCells(lines, n, i, j):
	if j >= n:
		return 0
	if not available(lines, i, j, n):
		if i == 0:
			return countCells(lines, n, 1, j)
		else:
			return countCells(lines, n, 0, j + 1)
	elif i == 0:
		if available(lines, 1, j, n):
			return 2 + countCells(lines, n, i, j + 1)
		elif available(lines, i, j + 1, n):
			return 2 + countCells(lines, n, 1, j + 1)
		else:
			return countCells(lines, n, i, j + 1)
	elif available(lines, 0, j + 1, n):
		return 2 + countCells(lines, n, i, j + 1)
	elif available(lines, i, j + 1, n):
		return 2 + countCells(lines, n, 0, j + 2)
	else:
		return countCells(lines, n, i, j + 1)
	return ''

def available(lines, i, j, n):
	return j < n and lines[i][j] == 0
t = int(sys.stdin.readline().rstrip('\n'))
for i in range(t):
	n = int(sys.stdin.readline().rstrip('\n'))
	line1 = sys.stdin.readline().rstrip('\n')
	line2 = sys.stdin.readline().rstrip('\n')
	lines = []
	lines.append([int(x) for x in list(line1)])
	lines.append([int(x) for x in list(line2)])
	print(existsWay(lines, n))


from collections import deque

def SolveIt(N, iMap):
	isLegal = [[False] * (N + 1) for _ in range(N + 1)]
	isLegal[0][0] = True
	visited = set([])
	toVisit = deque([(0, 0)])
	while len(toVisit) > 0:
		(i0, i1) = toVisit.popleft()
		if (i0, i1) in visited:
			continue
		else:
			visited.add((i0, i1))
		if i0 >= N and i1 >= N:
			break
		if i0 < N and iMap[0][i0] == '1':
			isLegal[i0 + 1][i1] = True
			toVisit.append((i0 + 1, i1))
			continue
		if i1 < N and iMap[1][i1] == '1':
			isLegal[i0][i1 + 1] = True
			toVisit.append((i0, i1 + 1))
			continue
		if i0 < N and i1 < N and all([i0 == i1, iMap[0][i0] == '0', iMap[0][i1] == '0']):
			isLegal[i0 + 1][i1 + 1] = True
			toVisit.append((i0 + 1, i1 + 1))
		if i0 < N and i1 < N and all([i0 == i1 + 1, iMap[0][i0] == '0', iMap[1][i1] == '0']):
			isLegal[i0 + 1][i1 + 1] = True
			toVisit.append((i0 + 1, i1 + 1))
		if i0 < N - 1 and iMap[0][i0] == '0' and (iMap[0][i0 + 1] == '0'):
			isLegal[i0 + 2][i1] = True
			toVisit.append((i0 + 2, i1))
		if i1 < N - 1 and iMap[1][i1] == '0' and (iMap[1][i1 + 1] == '0'):
			isLegal[i0][i1 + 2] = True
			toVisit.append((i0, i1 + 2))
	return isLegal[N][N]
T = int(input())
for _ in range(T):
	N = int(input())
	iMap = [input().strip(), input().strip()]
	print('YES' if SolveIt(N, iMap) else 'NO')


T = int(input())
dp = {}
DBG = False

def pp(s):
	if DBG:
		print(s)

def rec(a, b):
	pp('a: ' + str(a))
	pp('b: ' + str(b))
	_a = tuple(a)
	_b = tuple(b)
	if len(a) == 0 and len(b) == 0:
		return 'YES'
	elif (_a, _b) not in dp:
		results = []
		if a[0] in (1, 2) and b[0] in (1, 2):
			pp('remove black/covered')
			return rec(a[1:], b[1:])
		if a[0] == 0 and b[0] == 0:
			pp('try red')
			results.append(rec(a[1:], b[1:]))
			if 'YES' in results:
				dp[_a, _b] = 'YES'
				return 'YES'
		try:
			if (a[0] == 1 or a[0] == 2) and b[0] == 0 and (a[1] == 0):
				pp('try brown')
				xa = a[1:]
				xa[0] = 2
				results.append(rec(xa, b[1:]))
				if 'YES' in results:
					dp[_a, _b] = 'YES'
					return 'YES'
		except IndexError:
			pass
		try:
			if a[0] == 0 and a[1] == 0:
				pp('try yellow top')
				xa = a.copy()
				xa[0] = xa[1] = 2
				results.append(rec(xa, b.copy()))
				if 'YES' in results:
					dp[_a, _b] = 'YES'
					return 'YES'
		except IndexError:
			pass
		try:
			if b[0] == 0 and b[1] == 0:
				pp('try yellow bot')
				xb = b.copy()
				xb[0] = xb[1] = 2
				results.append(rec(a.copy(), xb))
				if 'YES' in results:
					dp[_a, _b] = 'YES'
					return 'YES'
		except IndexError:
			pass
		dp[_a, _b] = 'NO'
		pp('Returning NO')
	return dp[_a, _b]
for t in range(T):
	DBG = False
	N = int(input())
	a = list(map(int, list(input())))
	b = list(map(int, list(input())))
	dp = {}
	print(rec(a, b))


t = int(input())
for i in range(t):
	n = int(input())
	st = input()
	u = list(map(int, st))
	st = input()
	l = list(map(int, st))
	flag = [0 for j in range(n)]
	count = 0
	ans = 1
	for j in range(n):
		if u[j] == 1:
			count += 1
			if l[j] == 1:
				flag[j] = 1
			if l[j - 1] == 1:
				if count % 2 == 0:
					ans = 0
					break
		if l[j] == 1:
			count += 1
		if flag[j] == 1:
			if count % 2 != 0:
				ans = 0
				break
			count = 0
	if count % 2 != 0:
		ans = 0
	if ans == 1:
		print('YES')
	else:
		print('NO')


from sys import stderr

def placeable(s, i):
	return i < N and s[i] == '0'

def skipblack(s, i):
	while i < N and s[i] != '0':
		i += 1
	return i

def fillable_(i, j):
	i = skipblack(UPPER, i)
	j = skipblack(LOWER, j)
	while i + 1 <= j:
		if UPPER[i] != '0':
			i += 1
		elif i + 1 < N and UPPER[i + 1] == '0':
			i += 2
		else:
			return False
	while j + 2 <= i:
		if LOWER[j] != '0':
			j += 1
		elif LOWER[j + 1] == '0':
			j += 2
		else:
			return False
	assert j <= i <= j + 1
	if i >= N or j >= N:
		i = skipblack(UPPER, i)
		j = skipblack(LOWER, j)
		return i == N and j == N
	elif i == j:
		if not placeable(UPPER, i):
			return fillable(i + 1, j)
		elif not placeable(LOWER, j):
			return placeable(UPPER, i + 1) and fillable(i + 2, j + 1)
		elif fillable(i + 1, j + 1):
			return True
		elif placeable(UPPER, i + 1) and placeable(LOWER, j + 1):
			return fillable(i + 2, j + 2)
	elif i == j + 1:
		if not placeable(LOWER, j):
			return fillable(i, j + 1)
		elif not placeable(UPPER, i):
			return placeable(LOWER, j + 1) and fillable(i + 1, j + 2)
		elif fillable(i + 1, j + 1):
			return True
		elif placeable(UPPER, i + 1) and placeable(LOWER, j + 1):
			return fillable(i + 2, j + 2)
	return False

def fillable(i, j):
	r = MEM.get((i, j))
	if r is None:
		r = fillable_(i, j)
		MEM[i, j] = r
	return r
T = int(input())
for t in range(T):
	N = int(input())
	UPPER = input()
	LOWER = input()
	MEM = {}
	print('YES' if fillable(0, 0) else 'NO')


def fill_grid(gridtop, gridbottom):
	(top, bottom) = (list(gridtop), list(gridbottom))
	if len(top) == 0:
		return False
	if len(top) == 1:
		return top[0] == bottom[0]
	if top[0] == bottom[0]:
		return fill_grid(top[1:], bottom[1:])
	elif bottom[0] == '1':
		if top[1] == '1':
			return False
		else:
			top[0] = top[1] = '1'
			return fill_grid(top[1:], bottom[1:])
	else:
		if bottom[1] == '1':
			case1 = False
		else:
			case1 = fill_grid(top[1:], ['1'] + bottom[2:])
		if top[1] == '1':
			case2 = False
		else:
			case2 = fill_grid(['1'] + top[2:], bottom[1:])
		return case1 or case2
t = int(input())
for _ in range(t):
	_ = input()
	fillable = fill_grid(input().strip(), input().strip())
	if fillable:
		print('YES')
	else:
		print('NO')


def solve(X, Y):
	if len(X) == 1:
		return X[0] == Y[0]
	if X[0] == '0':
		if Y[0] == '0':
			if solve(X[1:], Y[1:]):
				return True
		if X[1] == '0':
			X[0] = X[1] = '1'
			return solve(X, Y)
	elif Y[0] == '0':
		if X[1] == '0':
			X[1] = '1'
			if solve(X[1:], Y[1:]):
				return True
			X[1] = '0'
		if Y[1] == '0':
			Y[0] = Y[1] = '1'
			return solve(X, Y)
	else:
		return solve(X[1:], Y[1:])
	return False
T = int(input())
for _ in range(T):
	N = int(input())
	print('YES' if solve(list(input().strip()), list(input().strip())) else 'NO')


def resolveGrid(firstRow, secondRow, N, lastRight) -> 'null':
	if N == 1:
		if lastRight == 0:
			if firstRow == secondRow:
				print('YES')
			else:
				print('NO')
		elif lastRight == 1:
			if firstRow == secondRow:
				print('NO')
			else:
				print('YES')
		elif lastRight == 2:
			if firstRow == secondRow or (firstRow == '1' and secondRow == '0'):
				print('NO')
			else:
				print('YES')
		return
	else:
		if lastRight == 0:
			if firstRow[0] == secondRow[0]:
				lastRight = 0
			elif firstRow[0] == '0' and secondRow[0] == '1':
				lastRight = 2
			else:
				lastRight = 1
		elif lastRight == 1:
			if firstRow[0] == secondRow[0] == '1':
				print('NO')
				return
			elif firstRow[0] == secondRow[0] == '0':
				lastRight = 1
			else:
				lastRight = 0
		elif lastRight == 2:
			if firstRow[0] == secondRow[0] == '1' or (firstRow[0] == '1' and secondRow[0] == '0'):
				print('NO')
				return
			elif firstRow[0] == secondRow[0] == '0':
				lastRight = 1
			else:
				lastRight = 0
		return resolveGrid(firstRow[1:], secondRow[1:], N - 1, lastRight)
T = int(input())
for times in range(0, T):
	N = int(input())
	firstRow = input()
	secondRow = input()
	resolveGrid(firstRow, secondRow, N, 0)


t = int(input())
for _ in range(t):
	n = int(input())
	data = [1] * (2 * n)
	data[::2] = (int(c) for c in input())
	data[1::2] = (int(c) for c in input())
	data.extend([1, 1])
	prev = 0
	count = 0
	for x in data:
		if prev and x and (count % 2 == 1):
			print('NO')
			break
		count += 1 - x
		prev = x
	else:
		print('YES')


def isPossible(block):
	if block % 2 == 1:
		return False
	else:
		return True

def main():
	t = int(input())
	for case in range(t):
		n = int(input())
		line1 = input() + '1'
		line2 = input() + '1'
		block = 0
		comb = []
		for i in range(n + 1):
			comb.append(line1[i])
			comb.append(line2[i])
		possible = True
		for i in range(len(comb) - 1):
			if comb[i] == '1' and comb[i + 1] == '1':
				if not isPossible(block):
					possible = False
					break
				block = 0
			if comb[i] == '0':
				block += 1
		if possible:
			print('YES')
		else:
			print('NO')
main()


def funA():
	if list1[i] == 0 and list2[i - 1] == 0 and (i != 0):
		list1[i] = 2
		list2[i - 1] = 2
	elif list1[i] == 0 and list2[i] == 0:
		list1[i] = 2
		list2[i] = 2
	elif i != len1 - 1:
		if list1[i] == 0 and list1[i + 1] == 0:
			list1[i] = 2
			list1[i + 1] = 2

def funB():
	if list2[i] == 0 and list1[i] == 0:
		list2[i] = 2
		list1[i] = 2
	elif i != len1 - 1:
		if list2[i] == 0 and list1[i + 1] == 0:
			list2[i] = 2
			list1[i + 1] = 2
		elif list2[i] == 0 and list2[i + 1] == 0:
			list2[i] = 2
			list2[i + 1] = 2
N = int(input())
for j in range(N):
	status = 'X'
	len1 = int(input())
	list1 = list(map(int, input()))
	list2 = list(map(int, input()))
	for i in range(len1):
		if i % 2 == 0:
			if list1[i] == 0:
				funA()
			else:
				funB()
		elif list2[i] == 0:
			funB()
		else:
			funA()
	if 0 in list1 or 0 in list2:
		status = 'NO'
	else:
		status = 'YES'
	print(status)


import collections
T = int(input())
for t in range(T):
	N = int(input())
	A = [int(c) for c in input().strip()]
	B = [int(c) for c in input().strip()]
	G = [[0] * (N + 1) for i in range(N + 1)]
	active = collections.deque()
	active.append((0, 0))
	while len(active):
		(lo, hi) = active.popleft()
		while lo < N and A[lo]:
			lo += 1
		while hi < N and B[hi]:
			hi += 1
		if G[lo][hi]:
			continue
		G[lo][hi] = 1
		if lo + 1 < N and (not A[lo + 1]):
			active.append((lo + 2, hi))
		if hi + 1 < N and (not B[hi + 1]):
			active.append((lo, hi + 2))
		if lo < N and lo == hi:
			active.append((lo + 1, hi + 1))
		if lo < N and lo == hi + 1:
			active.append((lo + 1, hi + 1))
	print('YES' if G[N][N] else 'NO')


t = int(input())

def zero(a, b):
	zeros = 0
	for i in range(len(a)):
		if a[i] == '0':
			zeros += 1
		if b[i] == '0':
			zeros += 1
	return zeros
abset = []

def check(a, b):
	if a == '' or b == '' or ans[0] == 'YES':
		pass
	elif [a, b] in abset:
		pass
	elif zero(a, b) == 0:
		ans[0] = 'YES'
	else:
		abset.append([a, b])
		for i in range(len(a)):
			if a[i] == '1':
				continue
			if i == 0:
				if len(a) > 1 and a[i + 1] == '0':
					check('11' + a[2:], b)
				if b[i] == '0':
					check('1' + a[1:], '1' + b[1:])
			elif i == len(a) - 1:
				if b[i] == '0':
					check(a[:i] + '1', b[:i] + '1')
				if len(b) > 1 and b[i - 1] == '0':
					check(a[:i] + '1', b[:i - 1] + '1' + b[i:])
				if len(a) > 1 and a[i - 1] == '0':
					check(a[:i - 1] + '11', b)
			else:
				if a[i + 1] == '0':
					check(a[:i] + '11' + a[i + 2:], b)
				if b[i] == '0':
					check(a[:i] + '1' + a[i + 1:], b[:i] + '1' + b[i + 1:])
				if len(b) > 1 and b[i - 1] == '0':
					check(a[:i] + '1' + a[i + 1:], b[:i - 1] + '1' + b[i:])
				if len(a) > 1 and a[i - 1] == '0':
					check(a[:i - 1] + '11' + a[i + 1:], b)
		for i in range(len(b)):
			if b[i] == '1':
				continue
			if i == 0:
				if len(b) > 1 and b[i + 1] == '0':
					check(a, '11' + b[2:])
				if a[i] == '0':
					check('1' + a[1:], '1' + b[1:])
				if len(a) > 1 and a[i + 1] == '0':
					check(a[0] + '1' + a[2:], '1' + b[1:])
			elif i == len(b) - 1:
				if a[i] == '0':
					check(a[:i] + '1', b[:i] + '1')
				if len(b) > 1 and b[i - 1] == '0':
					check(a, b[:i - 1] + '11')
			else:
				if a[i + 1] == '0':
					check(a[:i + 1] + '1' + a[i + 2:], b[:i] + '1' + b[i + 1:])
				if b[i + 1] == '0':
					check(a, b[:i] + '11' + b[i + 2:])
				if a[i] == '0':
					check(a[:i] + '1' + a[i + 1:], b[:i] + '1' + b[i + 1:])
				if len(b) > 1 and b[i - 1] == '0':
					check(a, b[:i - 1] + '11' + b[i + 1:])
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	abset = []
	zeros = zero(a, b)
	if zeros % 2 == 1:
		print('NO')
	else:
		ans = ['']
		check(a, b)
		if ans[0] == 'YES':
			print('YES')
		else:
			print('NO')


def canFill(d1, d2, i1, i2, n):
	if i1 == n and i2 == n:
		return True
	elif i1 == n - 1 and i2 == n and (d1[n - 1] == '1') or (i2 == n - 1 and i1 == n and (d2[n - 1] == '1')):
		return True
	elif i1 == n - 1 and i2 == n and (d1[n - 1] == '0') or (i2 == n - 1 and i1 == n and (d2[n - 1] == '0')):
		return False
	elif i1 == i2:
		if d1[i1] == '0' and d2[i2] == '0' or (d1[i1] == '1' and d2[i2] == '1'):
			return canFill(d1, d2, i1 + 1, i2 + 1, n)
		elif d1[i1] == '1' and d2[i2] == '0':
			return canFill(d1, d2, i1 + 1, i2, n)
		else:
			return canFill(d1, d2, i1, i2 + 1, n)
	elif i1 == i2 + 1:
		if d2[i2] == '1':
			return canFill(d1, d2, i1, i2 + 1, n)
		elif d1[i1] == '0' and d2[i2] == '0':
			return canFill(d1, d2, i1 + 1, i2 + 1, n)
		elif d1[i1] == '1' and d2[i2] == '0' and (i2 < n - 1) and (d2[i2 + 1] == '0'):
			return canFill(d1, d2, i1 + 1, i2 + 2, n)
		else:
			return False
	elif i2 == i1 + 1:
		if d1[i1] == '1':
			return canFill(d1, d2, i1 + 1, i2, n)
		elif d1[i1] == '0' and i1 < n - 1 and (d1[i1 + 1] == '0'):
			return canFill(d1, d2, i1 + 2, i2, n)
		else:
			return False
cases = int(input())
for i in range(cases):
	n = int(input())
	d1 = list(input())
	d2 = list(input())
	if canFill(d1, d2, 0, 0, n):
		print('YES')
	else:
		print('NO')


def domino(blocks, i):
	if i == N - 1:
		return blocks[0][i] == blocks[1][i]
	up = blocks[0][i]
	down = blocks[1][i]
	upright = blocks[0][i + 1]
	downright = blocks[1][i + 1]
	if up:
		if down:
			return domino(blocks, i + 1)
		if upright:
			if downright:
				return False
			blocks[1][i + 1] = 1
			return domino(blocks, i + 1)
		if downright:
			blocks[0][i + 1] = 1
			return domino(blocks, i + 1)
		blocks2 = [blocks[0][:], blocks[1][:]]
		blocks[0][i + 1] = 1
		blocks2[1][i + 1] = 1
		return domino(blocks, i + 1) or domino(blocks2, i + 1)
	if down:
		if upright:
			return False
		if downright:
			blocks[0][i + 1] = 1
			return domino(blocks, i + 1)
		blocks2 = [blocks[0][:], blocks[1][:]]
		blocks[0][i + 1] = 1
		blocks2[1][i + 1] = 1
		return domino(blocks, i + 1) or domino(blocks2, i + 1)
	return domino(blocks, i + 1)
for _ in range(int(input())):
	N = int(input())
	blocks = [[int(x) for x in input()], [int(x) for x in input()]]
	if sum(blocks[0]) + sum(blocks[1]) & 1:
		print('NO')
	else:
		print('YES' if domino(blocks, 0) else 'NO')


for i in range(int(input())):
	(n, a) = (int(input()), [list(map(int, list(input()))), list(map(int, list(input())))])
	for i in range(n):
		if a[0][i] == 0 and a[1][i] == 0:
			(a[0][i], a[1][i]) = (1, 1)
		elif i + 1 < n and a[0][i] == 1 and (a[1][i] == 0):
			if a[0][i + 1] == 0:
				(a[0][i + 1], a[1][i]) = (1, 1)
			elif a[1][i + 1] == 0:
				(a[1][i + 1], a[1][i]) = (1, 1)
			else:
				break
		elif i + 1 < n and a[0][i] == 0 and (a[1][i] == 1):
			if a[0][i + 1] == 0:
				(a[0][i + 1], a[0][i]) = (1, 1)
			else:
				break
	if a == [[1] * n, [1] * n]:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	grid = [int(j) for i in zip(input().strip(), input().strip()) for j in i] + [1, 1]
	contig = 0
	i = 0
	while i < 2 * n:
		(y, x) = (i % 2, i // 2)
		(top, bot) = (i + 2, i + 1) if y == 0 else (i + 1, i + 2)
		if grid[i] == 0:
			contig += 1
			i += 1
			continue
		if y == 0 and grid[bot] == 1 or (y == 1 and grid[top] == 1):
			if contig % 2 == 1:
				print('NO')
				break
			i += 2
			continue
		if grid[top] == 1:
			i = bot
			continue
		if grid[bot] == 1:
			i = top
			continue
		i += 1
	else:
		print('NO' if contig % 2 == 1 else 'YES')


import itertools, sys
T = int(input().strip())
for __ in range(T):
	N = int(input().strip())
	grid = [[int(x) for x in input().strip()], [int(x) for x in input().strip()]]
	walk = itertools.chain.from_iterable(zip(*grid))
	working_len = 0
	for (k, g) in itertools.groupby(walk):
		if k == 0:
			working_len += len(list(g))
		elif len(list(g)) > 1:
			if working_len % 2 != 0:
				break
			working_len = 0
	if working_len % 2 == 0:
		print('YES')
	else:
		print('NO')


from copy import deepcopy

def solve(m, s):
	if s == 0:
		return 1
	for i in range(s):
		if m[0][i] == 1 and m[1][i] == 1:
			a = [m[0][:i], m[1][:i]]
			a_s = i
			b = [m[0][i + 1:], m[1][i + 1:]]
			b_s = s - a_s - 1
			return 1 if solve(a, a_s) and solve(b, b_s) else 0
	if sum((sum(mi) for mi in m)) % 2:
		return 0
	if all((all((c == 1 for c in mi)) for mi in m)):
		return 1
	for p in range(s):
		for o in '|-_/':
			if p == 0 and o == '/':
				continue
			if p == s - 1 and o in '-_':
				continue
			if o == '|':
				i = 0
				j = p
				h = 1
				k = p
			elif o == '-':
				i = 0
				j = p
				h = 0
				k = p + 1
			elif o == '_':
				i = 1
				j = p
				h = 1
				k = p + 1
			elif o == '/':
				i = 0
				j = p
				h = 1
				k = p - 1
			if m[i][j] == 0 and m[h][k] == 0:
				z = deepcopy(m)
				z[i][j] = z[h][k] = 1
				if solve(z, s):
					return 1
	return 0
n = int(input())
for _ in range(n):
	s = int(input())
	m = [[int(c) for c in input()] for _ in '__']
	print(['NO', 'YES'][solve(m, s)])


def legal(n, l1, l2):
	c = 0
	for i in range(n):
		if l1[i] == '1' and (l2[i] == '1' or (i > 0 and l2[i - 1] == '1')):
			if c % 2:
				return False
			c = 0
		if l1[i] == '0':
			c += 1
		if l2[i] == '0':
			c += 1
	return c % 2 == 0
cases = int(input())
for _ in range(cases):
	if legal(int(input()), input(), input()):
		print('YES')
	else:
		print('NO')


BLACK = 1
FILED = 2
EMPTY = 0

def check_hexa(a1, a2):
	len_a = len(a1)
	for i in range(0, len_a):
		if a1[i] == EMPTY:
			if a2[i] == EMPTY:
				(a1[i], a2[i]) = (FILED, FILED)
			elif i < len_a - 1 and a1[i + 1] == EMPTY:
				(a1[i], a1[i + 1]) = (FILED, FILED)
		elif a2[i] == EMPTY:
			if i < len_a - 1 and a1[i + 1] == EMPTY:
				(a2[i], a1[i + 1]) = (FILED, FILED)
			elif i < len_a - 1 and a2[i + 1] == EMPTY:
				(a2[i], a2[i + 1]) = (FILED, FILED)
		valid = a1[i] > EMPTY and a2[i] > EMPTY
		if not valid:
			print('NO')
			return
	print('YES')
	return
T = int(input())
for row in range(T):
	l = int(input())
	s1 = list(map(int, list(input().rstrip())))
	s2 = list(map(int, list(input().rstrip())))
	check_hexa(s1, s2)


t = int(input())
for _ in range(t):
	n = int(input())
	s = [input() for _ in range(2)]
	res = [[True for _ in range(n)] for _ in range(2)]
	for i in range(n):
		if s[0][i] == '1':
			if i == 0:
				res[0][i] = True
			else:
				res[0][i] = res[1][i - 1]
		elif i == 0 or (s[0][i - 1] == '1' and s[1][i - 1] == '1'):
			res[0][i] = False
		elif s[1][i - 1] == '1':
			if i == 1:
				res[0][i] = True
			else:
				res[0][i] = res[1][i - 2]
		else:
			res[0][i] = res[0][i - 1]
		if s[1][i] == '1':
			res[1][i] = res[0][i]
		elif i == 0:
			res[1][i] = s[0][i] == '0'
		elif s[0][i] == '0':
			res[1][i] = res[1][i - 1]
		elif s[1][i - 1] == '1':
			res[1][i] = False
		else:
			res[1][i] = res[0][i - 1]
	if res[1][n - 1]:
		print('YES')
	else:
		print('NO')


def getBlocks(n, one, two):
	blocked = sum(one) + sum(two)
	if blocked % 2 == 1:
		return True
	elif blocked == 2 * n:
		return False
	iSum = 0
	for i in range(0, n):
		iSum += 1 - one[i]
		if i > 0 and one[i] and two[i - 1]:
			if iSum % 2 == 1:
				return True
			iSum = 0
		if one[i] and two[i] and (iSum % 2 == 1):
			return True
		iSum += 1 - two[i]
	return False
try:
	T = int(input())
	for i in range(0, T):
		n = int(input())
		(one, two) = [list(map(int, list(input()))) for j in range(0, 2)]
		output = 'NO' if getBlocks(n, one, two) else 'YES'
		print(output)
except Exception as string:
	print(string)


hist = {}

def solvable(a, b):
	if len(a) == 0:
		return True
	if len(a) == 1:
		return a[0] == b[0]
	if (a, b) in hist:
		return hist[a, b]
	s = (a[-1] == '1') + 2 * (b[-1] == '1') + 4 * (a[-2] == '1') + 8 * (b[-2] == '1')
	rv = True
	if s == 0:
		rv = solvable(a[:-1], b[:-1]) or solvable(a[:-2], b[:-2])
	elif s == 1:
		bb = b[:-2] + '1'
		rv = solvable(a[:-1], bb)
	elif s == 2:
		aa = a[:-2] + '1'
		bb = b[:-2] + '1'
		rv = solvable(aa, b[:-1]) or solvable(a[:-1], bb)
	elif s == 3 or s == 7 or s == 11 or (s == 15) or (s == 4) or (s == 8):
		rv = solvable(a[:-1], b[:-1])
	elif s == 5 or s == 6 or s == 10 or (s == 12):
		rv = solvable(a[:-2], b[:-2])
	elif s == 9 or s == 13 or s == 14:
		rv = False
	hist[a, b] = rv
	return rv
num_probs = int(input())
for prob in range(num_probs):
	n = int(input())
	a = input()
	b = input()
	if solvable(a, b):
		print('YES')
	else:
		print('NO')


BLOCK = 1
GOOD = 0

def solution(grid, n):
	top_sum = sum(grid[0])
	bot_sum = sum(grid[1])
	if (top_sum + bot_sum) % 2 == 1:
		return -1
	good_cells = 0
	for i in range(n):
		good_cells += 2 - (grid[0][i] + grid[1][i])
		if grid[0][i] == BLOCK and grid[1][i] == BLOCK:
			if good_cells % 2 == 1:
				return -1
		if i != n - 1 and grid[0][i + 1] == BLOCK and (grid[1][i] == BLOCK):
			if good_cells % 2 == 1:
				return -1
	return 1

def main():
	t = int(input().strip())
	for i in range(t):
		n = int(input().strip())
		grid = []
		for j in range(2):
			grid.append([int(x) for x in input().strip()])
		print('YES' if solution(grid, n) == 1 else 'NO')
main()


T = int(input())
for t in range(T):
	n = int(input())
	top = [int(x) for x in list(input())]
	bot = [int(x) for x in list(input())]
	top = [1 if x == 0 else 0 for x in top]
	bot = [1 if x == 0 else 0 for x in bot]
	count = 0
	for i in range(n):
		if i == n - 1:
			count += top[i] + bot[i]
		elif top[i + 1] == 0 and bot[i + 1] == 0 or (top[i] == 1 and top[i + 1] == 0 and (bot[i] == 0) and (bot[i + 1] == 1)):
			count += top[i] + bot[i]
			if count % 2 == 1:
				break
			else:
				count = 0
		else:
			count += top[i] + bot[i]
	if count % 2 == 0:
		print('YES')
	else:
		print('NO')


nTests = int(input())
for _ in range(nTests):
	N = int(input())
	upper = [int(d) for d in input()]
	lower = [int(d) for d in input()]
	grid = []
	for i in range(N):
		grid.append(upper[i])
		grid.append(lower[i])
	for i in range(2 * N):
		if i == 2 * N - 1:
			if grid[i]:
				result = 'YES'
			else:
				result = 'NO'
		elif grid[i]:
			continue
		elif grid[i + 1] == 0:
			grid[i] = 1
			grid[i + 1] = 1
		elif i < 2 * N - 2 and grid[i + 2] == 0:
			grid[i] = 1
			grid[i + 2] = 1
		else:
			result = 'NO'
			break
	print(result)


import sys

def hexagonal_grid(g1, g2):
	d = 0
	filled = False
	for i in range(len(g1)):
		if g1[i] and g2[i]:
			if d:
				return False
			filled = True
		elif g1[i] and i > 0 and g2[i - 1] and d:
			return False
		else:
			filled = True
			if g1[i] or g2[i]:
				if d:
					d -= 1
				else:
					d += 1
	return filled and d == 0

def extract_data_from_input(lines):
	for i in range((len(lines) - 1) // 3):
		n = int(lines[i * 3 + 1])
		g = ([int(n) for n in lines[i * 3 + 2]], [int(n) for n in lines[i * 3 + 3]])
		if len(g[0]) != len(g[1]) or len(g[1]) != n:
			raise ValueError('Input #{}: {}!={}'.format(i + 1, len(g[0]), l))
		yield g
lines = [line.strip() for line in sys.stdin.readlines()]
for g in extract_data_from_input(lines):
	if hexagonal_grid(*g):
		print('YES')
	else:
		print('NO')


T = int(input())
for i in range(T):
	N = int(input())
	fline = list(map(int, list(input())))
	sline = list(map(int, list(input())))
	flatten = [0] * (N * 2)
	for j in range(N):
		flatten[2 * j] = fline[j]
		flatten[2 * j + 1] = sline[j]
	for j in range(len(flatten) - 1):
		if flatten[j] == 0 and flatten[j + 1] == 0:
			flatten[j] = 1
			flatten[j + 1] = 1
		elif j + 2 < len(flatten) and flatten[j] == 0 and (flatten[j + 1] == 1) and (flatten[j + 2] == 0):
			flatten[j] = 1
			flatten[j + 2] = 1
	check = 0
	for i in flatten:
		if i == 1:
			check += 1
	if check == len(flatten):
		print('YES')
	else:
		print('NO')


def dfs(G, i, j):
	G[i][j] = 2
	neighbors = []
	if j + 1 < len(G[i]) and G[i][j + 1] == 1:
		neighbors.append((i, j + 1))
	if i == 0 and G[1][j] == 1:
		neighbors.append((1, j))
	if i == 1 and j + 1 < len(G[0]) and (G[0][j + 1] == 1):
		neighbors.append((0, j + 1))
	count = 1
	for (i, j) in neighbors:
		count += dfs(G, i, j)
	return count
T = int(input())
for t in range(T):
	N = int(input())
	G = [[1 if c == '0' else 0 for c in list(input().strip())], [1 if c == '0' else 0 for c in list(input().strip())]]
	ok = True
	for j in range(N):
		for i in range(2):
			if G[i][j] == 1:
				count = dfs(G, i, j)
				if count % 2 == 1:
					ok = False
	print('YES' if ok else 'NO')


def is_constructable(grid):
	if len(grid[0]) == 0:
		return True
	if len(grid[0]) == 1:
		return (grid[0][0] + grid[1][0]) % 2 == 0
	sum_of_four = sum(grid[0][:2] + grid[1][:2])
	if sum_of_four % 4 == 0:
		return is_constructable([grid[0][2:], grid[1][2:]])
	if sum_of_four == 1:
		if grid[0][0] == 1 or grid[1][0] == 1:
			grid[0][1] = 1
		return is_constructable([grid[0][1:], grid[1][1:]])
	if sum_of_four == 2:
		if grid[0][1] == 1 and grid[1][0] == 1:
			return False
		return is_constructable([grid[0][2:], grid[1][2:]])
	if sum_of_four == 3:
		if (grid[0][0] + grid[1][0]) % 2 == 1:
			return False
		return is_constructable([grid[0][1:], grid[1][1:]])
	raise Exception('It should not be here.')
T = int(input())
for _ in range(T):
	N = int(input())
	grid = [[int(x) for x in input()], [int(x) for x in input()]]
	if sum(map(sum, grid)) % 2 == 1:
		print('NO')
		continue
	if is_constructable(grid):
		print('YES')
	else:
		print('NO')


import sys
T = int(input())
for t in range(T):
	N = int(input())
	L1 = input()
	L2 = input()
	path = []
	for i in range(N):
		path.append(L1[i])
		path.append(L2[i])
	for i in range(2 * N - 2):
		if path[i] == '1' or path[i] == '2':
			continue
		elif path[i + 1] == '0':
			path[i] = '2'
			path[i + 1] = '2'
		elif path[i + 2] == '0':
			path[i] = '2'
			path[i + 2] = '2'
	if path[2 * N - 2] == '0' and path[2 * N - 1] == '0':
		path[2 * N - 2] = '2'
		path[2 * N - 1] = '2'
	filled = True
	for i in range(len(path)):
		if path[i] == '0':
			print('NO')
			filled = False
			break
	if filled:
		print('YES')


for j in range(int(input())):
	n = int(input())
	grid = [input(), input()]
	f = [[False, False, False, False] for x in range(n)]
	if grid[0][0] == '1' and grid[1][0] == '0':
		f[0][1] = True
	elif grid[0][0] == '0' and grid[1][0] == '1':
		f[0][2] = True
	else:
		f[0][3] = True
	for i in range(1, n):
		x = int(grid[0][i])
		y = int(grid[1][i])
		if x and y:
			if f[i - 1][3]:
				f[i][3] = True
			else:
				break
		elif x and (not y):
			f[i][1] = f[i - 1][3]
			f[i][3] = f[i - 1][1]
		elif not x and y:
			f[i][2] = f[i - 1][3]
			f[i][3] = f[i - 1][1] or f[i - 1][2]
		else:
			f[i][1] = f[i - 1][1] or f[i - 1][2]
			f[i][2] = f[i - 1][1]
			f[i][3] = f[i - 1][3]
	if f[n - 1][3]:
		print('YES')
	else:
		print('NO')


def recursive_fill(board, column):
	if all((item == '1' for row in board for item in row)):
		return True
	if column + 1 == len(board[0]):
		if board[0][column] == '1' and board[1][column] == '0':
			return None
		elif board[0][column] == '0' and board[1][column] == '1':
			return None
		elif board[0][column] == '0' and board[1][column] == '0':
			return True
		else:
			return True
	if board[0][column] == '0' and board[1][column] == '0':
		board[0][column] = '1'
		board[1][column] = '1'
		return recursive_fill(board, column + 1)
	elif board[0][column] == '1' and board[1][column] == '1':
		return recursive_fill(board, column + 1)
	else:
		if board[0][column] == '0' and board[1][column] == '1' and (board[0][column + 1] == '1') and (board[1][column + 1] == '0'):
			return None
		if board[0][column] == '0' and board[1][column] == '1' and (board[0][column + 1] == '1') and (board[1][column + 1] == '1'):
			return None
		if board[0][column] == '1' and board[1][column] == '0' and (board[0][column + 1] == '1') and (board[1][column + 1] == '1'):
			return None
		if board[0][column] == '1' and board[1][column] == '0' and (board[0][column + 1] == '1') and (board[1][column + 1] == '0'):
			board[1][column] = '1'
			board[1][column + 1] = '1'
			return recursive_fill(board, column + 1)
		if board[0][column] == '0' and board[1][column] == '1' and (board[0][column + 1] == '0') and (board[1][column + 1] == '1'):
			board[0][column] = '1'
			board[0][column + 1] = '1'
			return recursive_fill(board, column + 1)
		if board[0][column] == '1' and board[1][column] == '0' and (board[0][column + 1] == '0') and (board[1][column + 1] == '1'):
			board[1][column] = '1'
			board[0][column + 1] = '1'
			return recursive_fill(board, column + 1)
		if board[0][column] == '0' and board[1][column] == '1' and (board[0][column + 1] == '0') and (board[1][column + 1] == '0'):
			board[0][column] = '1'
			board[0][column + 1] = '1'
			return recursive_fill(board, column + 1)
		if board[0][column] == '1' and board[1][column] == '0' and (board[0][column + 1] == '0') and (board[1][column + 1] == '0'):
			board_temp = [row[:] for row in board]
			board_temp[1][column] = '1'
			board_temp[1][column + 1] = '1'
			if recursive_fill(board_temp, column + 1):
				return True
			else:
				board_temp[1][column] = '1'
				board[0][column + 1] = '1'
				return recursive_fill(board, column + 1)
	raise Exception('You were not expected here!')
tests = int(input().strip())
for _ in range(tests):
	width = int(input().strip())
	grid = [list(input()), list(input())]
	if recursive_fill(grid, 0):
		print('YES')
	else:
		print('NO')


def canFill(grid, start, end):
	if start >= end:
		return True
	if start == end - 1:
		pattern = grid[0][start] + grid[1][start]
		if pattern == '00' or pattern == '11':
			return True
		else:
			return False
	else:
		pattern = ''.join(grid[0][start:start + 2] + grid[1][start:start + 2])
		if pattern == '0000':
			return canFill(grid, start + 2, end)
		elif pattern == '0001':
			return canFill(grid, start + 1, end)
		elif pattern == '0010':
			grid[0][start + 1] = '1'
			return canFill(grid, start + 1, end)
		elif pattern == '0011':
			return canFill(grid, start + 2, end)
		elif pattern == '0100':
			return canFill(grid, start + 1, end)
		elif pattern == '0101':
			return canFill(grid, start + 2, end)
		elif pattern == '0110':
			return False
		elif pattern == '0111':
			return False
		elif pattern == '1000':
			grid[0][start + 1] = '1'
			return canFill(grid, start + 1, end)
		elif pattern == '1001':
			return canFill(grid, start + 2, end)
		elif pattern == '1010':
			return canFill(grid, start + 2, end)
		elif pattern == '1011':
			return canFill(grid, start + 1, end)
		elif pattern == '1100':
			return canFill(grid, start + 2, end)
		elif pattern == '1101':
			return False
		elif pattern == '1110':
			return canFill(grid, start + 1, end)
		elif pattern == '1111':
			return canFill(grid, start + 2, end)
		else:
			return False
t = int(input())
for iT in range(t):
	n = int(input())
	grid = [list(input().strip()) for _ in range(2)]
	if canFill(grid, 0, n):
		print('YES')
	else:
		print('NO')


def solve(problem):
	if not problem:
		return True
	if len(problem) == 1 and problem[0] == 0:
		return False
	elif len(problem) == 1 and problem[0] == 1:
		return True
	if problem[0] == 1:
		return solve(problem[1:])
	if problem[0] == 0 and problem[1] == 0:
		return solve(problem[2:])
	if problem[0] == 0 and len(problem) > 2 and (problem[2] == 0):
		return solve(problem[3:])
	return False
number_of_testcases = int(input())
for i in range(number_of_testcases):
	number_of_columns = int(input())
	first_row = [int(i) for i in str(input())]
	second_row = [int(i) for i in str(input())]
	row = [i for tupl in zip(first_row, second_row) for i in tupl]
	if solve(row):
		print('YES')
	else:
		print('NO')


def solve(problem):
	if not problem:
		return True
	if len(problem) == 1 and (not problem[0]):
		return False
	elif len(problem) == 1 and problem[0]:
		return True
	if problem[0]:
		return solve(problem[1:])
	if not problem[0] and (not problem[1]):
		return solve(problem[2:])
	if not problem[0] and len(problem) > 2 and (not problem[2]):
		return solve(problem[3:])
	return False
for i in range(int(input())):
	int(input())
	first_row = [int(i) for i in str(input())]
	second_row = [int(i) for i in str(input())]
	row = [i for tupl in zip(first_row, second_row) for i in tupl]
	if solve(row):
		print('YES')
	else:
		print('NO')


num_tests = int(input())
for _ in range(num_tests):
	_ = input()
	top_row = input() + '1'
	bottom_row = input() + '1'
	count = 0
	prev_bot_cell = None
	for (bot_cell, top_cell) in zip(bottom_row, top_row):
		if top_cell == '1' and (prev_bot_cell == '1' or bot_cell == '1'):
			if count % 2 == 1:
				print('NO')
				break
		if top_cell == '0':
			count += 1
		if bot_cell == '0':
			count += 1
		prev_bot_cell = bot_cell
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = input().strip()
	b = input().strip()
	assert n == len(a) == len(b)
	z = ''.join((x + y for (x, y) in zip(a, b)))
	ans = True
	while len(z) > 0:
		i = z.find('1')
		if i == -1:
			ans = len(z) % 2 == 0
			break
		if i % 2 == 0:
			z = z[i + 1:]
			continue
		if len(z) == i + 1 or z[i + 1] == '1':
			ans = False
			break
		z = z[i + 2:]
	print(['NO', 'YES'][ans])


def isBlocked(n, first, second):
	blocked_cells = sum(first) + sum(second)
	if blocked_cells % 2 == 1:
		return True
	if blocked_cells == 2 * n:
		return False
	sum_so_far = 0
	for i in range(0, n):
		sum_so_far += 1 - first[i]
		if i > 0 and first[i] and second[i - 1]:
			if sum_so_far % 2 == 1:
				return True
			sum_so_far = 0
		if first[i] and second[i] and (sum_so_far % 2 == 1):
			return True
		sum_so_far += 1 - second[i]
	return False
T = int(input().strip())
for t in range(T):
	n = int(input().strip())
	(first, second) = [list(map(int, list(input().strip()))) for _ in range(2)]
	if isBlocked(n, first, second):
		print('NO')
	else:
		print('YES')


import sys

def tt(ar):
	return tuple(zip(*ar))

def debug(*args, **kwargs):
	pass

def main():
	t = int(sys.stdin.readline())
	for _ in range(t):
		n = int(sys.stdin.readline())
		ar = tt([[1] + [int(v) for v in sys.stdin.readline().strip()], [1] + [int(v) for v in sys.stdin.readline().strip()]])
		v = [[False, False, True]] + [[False, False, False] for i in range(n)]
		for i in range(1, n + 1):
			a = ar[i]
			v[i][0] = v[i - 1][0] and ar[i - 1][1] == ar[i][0] == 0 or (v[i - 1][1] and ar[i - 1][0] == ar[i][0] == 0) or (v[i - 1][2] and ar[i][0] == 1)
			v[i][1] = v[i - 1][0] and ar[i - 1][1] == ar[i][1] == 0 or (v[i - 1][2] and ar[i][1] == 1)
			v[i][2] = v[i - 1][2] and ar[i][0] == ar[i][1] == 0 or (v[i][0] and ar[i][1] == 1) or (v[i][1] and ar[i][0] == 1)
		for a in tt(ar):
			debug(''.join((str(aa) for aa in a)))
		debug()
		for a in tt(v):
			debug(''.join(('1' if aa else '0' for aa in a)))
		debug()
		print('YES' if v[n][2] else 'NO')
		debug('-' * 40)
main()


T = int(input())
for t in range(T):
	N = int(input())
	A = input()
	c = A.count('1')
	B = input()
	c += B.count('1')
	if c % 2 == 1:
		print('NO')
		continue
	i = 0
	j = 0
	f = 1
	while i < N and j < N:
		if A[i] == B[j]:
			i += 1
			j += 1
			continue
		if j + 1 >= N:
			if i + 1 >= N:
				print('NO')
				f = 0
				i += 1
				break
			elif A[i + 1] == '0':
				C = A
				A = B
				B = C
				t = i
				i = j
				j = t
				i += 1
				j += 2
		elif A[i + 1] == '1' and B[j + 1] == '1':
			print('NO')
			f = 0
			i += 1
			break
		elif A[i + 1] == '1' and B[j + 1] == '0' and (B[j] == '1'):
			print('NO')
			f = 0
			i += 1
			break
		elif A[i + 1] == '1' and B[j + 1] == '0' and (A[i] == '1'):
			i += 2
			j += 2
		elif A[i + 1] == '0' and B[j + 1] == '1' and (B[j] == '1'):
			i += 2
			j += 2
		elif A[i + 1] == '0':
			C = A
			A = B
			B = C
			t = i
			i = j
			j = t
			i += 1
			j += 2
	if f == 1:
		print('YES')


def b(n, first, second):
	bs = sum(f) + sum(s)
	if bs % 2 == 1:
		return True
	if bs == 2 * n:
		return False
	sr = 0
	for i in range(0, n):
		sr += 1 - f[i]
		if i > 0 and f[i] and s[i - 1]:
			if sr % 2 == 1:
				return True
			sr = 0
		if f[i] and s[i] and (sr % 2 == 1):
			return True
		sr += 1 - s[i]
	return False
T = int(input().strip())
for t in range(T):
	n = int(input().strip())
	(f, s) = [list(map(int, input().strip())) for _ in range(2)]
	if b(n, f, s):
		print('NO')
	else:
		print('YES')


for i in range(int(input())):
	(n, a, b) = (int(input()), input(), input())
	if (a.count('0') + b.count('0')) % 2 != 0:
		print('NO')
	else:
		try:
			j = k = 0
			while j < n - 1:
				if a[j] == '0' and b[j] == '0':
					a = a.replace('0', '1', 1)
					b = b.replace('0', '1', 1)
				elif a[j] == a[j + 1]:
					if a[j] == '0':
						a = a.replace('0', '1', 2)
				elif a[j] == '0':
					k = 1
					print('NO')
					break
				if b[j] == '0' and a[j + 1] == '0':
					a = a.replace('0', '1', 1)
					b = b.replace('0', '1', 1)
				elif b[j] == b[j + 1]:
					if b[j] == '0':
						b = b.replace('0', '1', 2)
				elif b[j] == '0':
					k = 1
					print('NO')
					break
				j += 1
			if k == 0:
				if a[j] == b[j]:
					print('YES')
				else:
					print(a, b, 'NO')
		except:
			J = 0


class No(Exception):
	pass
cases = int(input())
for _ in range(cases):
	n = int(input()) * 2
	a = input()
	b = input()
	combo = [x for temp in zip(a, b) for x in temp]
	i = 0
	try:
		while i < n:
			try:
				if combo[i] == '1':
					i += 1
				elif combo[i + 1] == '0':
					i += 2
				elif combo[i + 2] == '0':
					i += 3
				else:
					raise No
			except IndexError:
				raise No
		print('YES')
	except No:
		print('NO')


def parseInput(f):
	return [f(x) for x in input().split()]

def possible(first, second, firstIndex, secondIndex):
	if firstIndex == 0 and secondIndex == 0:
		return first[firstIndex] == second[secondIndex]
	if first[firstIndex] == '1' and firstIndex > 0:
		return possible(first, second, firstIndex - 1, secondIndex)
	if second[secondIndex] == '1' and secondIndex > 0:
		return possible(first, second, firstIndex, secondIndex - 1)
	if firstIndex == secondIndex:
		if first[firstIndex] == second[secondIndex]:
			if possible(first, second, firstIndex - 1, secondIndex - 1):
				return True
	if firstIndex - 1 == secondIndex:
		if first[firstIndex] == second[secondIndex]:
			if firstIndex == 1:
				return first[firstIndex - 1] == '1'
			elif possible(first, second, firstIndex - 1, secondIndex - 1):
				return True
	if firstIndex > secondIndex:
		if firstIndex == 1:
			if first[firstIndex] == first[firstIndex - 1] and second[secondIndex] == '1':
				return True
		elif first[firstIndex] == first[firstIndex - 1] and possible(first, second, firstIndex - 2, secondIndex):
			return True
	elif secondIndex > firstIndex:
		if secondIndex == 1:
			if second[secondIndex] == second[secondIndex - 1] and first[firstIndex] == '1':
				return True
		elif second[secondIndex] == second[secondIndex - 1] and possible(first, second, firstIndex, secondIndex - 2):
			return True
	return False
t = int(input())
for _ in range(t):
	n = int(input())
	first = input().strip()
	second = input().strip()
	first = list(first)
	second = list(second)
	print('YES' if possible(first, second, n - 1, n - 1) else 'NO')


VERT = 0
TOP = 1
BOT = 2
t = int(input().strip())
for _ in range(t):
	n = int(input().strip())
	grid = [(bool(a), bool(b)) for (a, b) in zip([int(x) for x in input().strip()], [int(x) for x in input().strip()])]
	state = [True, False, False]
	for k in range(n):
		newState = [False, False, False]
		if grid[k] == (1, 1):
			if state[VERT]:
				newState[VERT] = True
		elif grid[k] == (1, 0):
			if state[TOP]:
				newState[VERT] = True
			if state[VERT]:
				newState[TOP] = True
		elif grid[k] == (0, 1):
			if state[VERT]:
				newState[BOT] = True
			if state[BOT] or state[TOP]:
				newState[VERT] = True
		elif grid[k] == (0, 0):
			if state[VERT]:
				newState[VERT] = True
			if state[TOP]:
				newState[BOT] = True
				newState[TOP] = True
			if state[BOT]:
				newState[TOP] = True
		if newState == [False, False, False]:
			print('NO')
			break
		state = newState
	else:
		if state[VERT]:
			print('YES')
		else:
			print('NO')


import sys

def pile(k, ar1, ar2):
	a = sum(ar1) + sum(ar2)
	if a % 2 == 1:
		return False
	first = -1
	for i in range(k):
		if ar1[i] == 0:
			first = i
			break
	T1 = False
	T2 = False
	T3 = False
	T4 = False
	temp1 = list(ar1)
	temp2 = list(ar2)
	if first != -1:
		if first + 1 < k and ar1[first + 1] == 0:
			at1 = list(temp1)
			at2 = list(temp2)
			at1[first] = 1
			at1[first + 1] = 1
			T1 = pile(k, at1, at2)
		if ar2[first] == 0:
			at1 = list(temp1)
			at2 = list(temp2)
			at1[first] = 1
			at2[first] = 1
			T2 = pile(k, at1, at2)
		if first != 0 and ar2[first - 1] == 0:
			at1 = list(temp1)
			at2 = list(temp2)
			at2[first - 1] = 1
			at1[first] = 1
			T3 = pile(k, at1, at2)
	else:
		for i in range(k):
			if ar2[i] == 0:
				first = i
				break
		if first != -1:
			if first + 1 < k and ar2[first + 1] == 0:
				at1 = list(temp1)
				at2 = list(temp2)
				at2[first] = 1
				at2[first + 1] = 1
				T4 = pile(k, at1, at2)
	if first == -1:
		return True
	else:
		return T1 or T2 or T3 or T4
n = int(input().strip())
for a in range(n):
	k = int(input().strip())
	a1 = input().strip()
	ar1 = [0] * k
	ar2 = [0] * k
	for i in range(k):
		ar1[i] = int(a1[i])
	a2 = input().strip()
	for i in range(k):
		ar2[i] = int(a2[i])
	result = pile(k, ar1, ar2)
	if result is True:
		print('YES')
	else:
		print('NO')


from itertools import count

def hex(n, a, b):
	toskip = set()
	tree = {num: set() for num in range(n * 2)}
	if a[0]:
		if b[0]:
			tree[0].add(n)
		if a[1]:
			tree[0].add(1)
	else:
		toskip.add(0)
	if b[0]:
		if a[0]:
			tree[n].add(0)
		if b[1]:
			tree[n].add(n + 1)
	else:
		toskip.add(n)
	for i in range(1, n):
		if a[i]:
			if a[i - 1]:
				tree[i].add(i - 1)
				tree[i - 1].add(i)
			if b[i]:
				tree[i].add(n + i)
				tree[n + i].add(i)
			if b[i - 1]:
				tree[i].add(n + i - 1)
				tree[n + i - 1].add(i)
		else:
			toskip.add(i)
		if b[i]:
			if b[i - 1]:
				tree[n + i].add(n + i - 1)
				tree[n + i - 1].add(n + i)
		else:
			toskip.add(n + i)
	subtrees = {}
	a = count()
	w = []
	for (i, j) in zip(range(n), range(n, n * 2)):
		if i not in toskip:
			w.append(i)
		if j not in toskip:
			w.append(j)
	for i in w:
		tobreak = False
		for (j, k) in subtrees.items():
			if i in k:
				subtrees[j] = subtrees[j] | tree[i]
				tobreak = True
				break
		if not tobreak:
			subtrees[next(a)] = {i} | tree[i]
	for i in subtrees.values():
		if len(i) % 2 != 0:
			return 'NO'
	return 'YES'

def main():
	for test in range(int(input())):
		n = int(input())
		if n > 1:
			a = tuple(input())
			a = tuple((1 if not num else 0 for num in map(int, a)))
			b = tuple(input())
			b = tuple((1 if not num else 0 for num in map(int, b)))
			result = hex(n, a, b)
			print(result)
		else:
			a = input()
			b = input()
			if a == b:
				print('YES')
			else:
				print('NO')
main()


def f(length, a, b):
	whites = 0
	arr = []
	for i in range(0, length * 2):
		if i % 2 == 0:
			cur = a[i // 2]
		else:
			cur = b[i // 2]
		arr.append(cur)
		if cur == 0:
			whites += 1
		elif whites % 2 == 1 and arr[i - 1] == 1:
			return 'NO'
	if whites % 2 != 0:
		return 'NO'
	return 'YES'
for i in range(int(input())):
	length = int(input())
	a = [int(x) for x in list(input().strip())]
	b = [int(x) for x in list(input().strip())]
	print(f(length, a, b))


def solve(a):
	c = 0
	for i in range(len(a[0])):
		if a[0][i] == 1 and a[1][i] == 1 or (i - 1 >= 0 and a[0][i] == 1 and (a[1][i - 1] == 1)):
			if c % 2 == 1:
				return False
			else:
				c = 0
		if a[0][i] != 1:
			c += 1
		if a[1][i] != 1:
			c += 1
	return c % 2 == 0
t = int(input())
for i in range(t):
	n = int(input())
	a = []
	a.append(list(map(int, input())))
	a.append(list(map(int, input())))
	result = solve(a)
	print('YES' if result else 'NO')


def is_admissible(A, block_type, block_pos):
	if block_type == 1:
		if not A[block_pos[0]][0] and (not A[block_pos[0]][1]):
			return True
		else:
			return False
	elif block_type == 2:
		if block_pos[0] + 1 < len(A) and (not A[block_pos[0]][block_pos[1]]) and (not A[block_pos[0] + 1][block_pos[1]]):
			return True
		else:
			return False
	elif block_type == 3:
		if block_pos[1] == 1 and block_pos[0] + 1 < len(A) and (not A[block_pos[0]][1]) and (not A[block_pos[0] + 1][0]):
			return True
		elif block_pos[1] == 0 and block_pos[0] - 1 >= 0 and (not A[block_pos[0] - 1][1]) and (not A[block_pos[0]][0]):
			return True
	else:
		return False

def fill_block(A, block_type, block_pos, start_top, start_bottom):
	B = [[A[i][j] for j in [0, 1]] for i in range(0, len(A))]
	if block_type == 1:
		B[block_pos[0]][0] = True
		B[block_pos[0]][1] = True
		start_top += 1
		start_bottom += 1
	elif block_type == 2:
		if block_pos[0] + 1 < len(A):
			if block_pos[1] == 0:
				B[block_pos[0]][0] = True
				B[block_pos[0] + 1][0] = True
				start_top += 2
			elif block_pos[1] == 1:
				B[block_pos[0]][1] = True
				B[block_pos[0] + 1][1] = True
				start_bottom += 2
	elif block_type == 3:
		if block_pos[1] == 1 and block_pos[0] + 1 < len(A):
			B[block_pos[0] + 1][0] = True
			B[block_pos[0]][1] = True
		elif block_pos[1] == 0 and block_pos[0] - 1 >= 0:
			B[block_pos[0]][0] = True
			B[block_pos[0] - 1][1] = True
		start_top += 1
		start_bottom += 1
	while start_top < len(A) and A[start_top][0]:
		start_top += 1
	while start_bottom < len(A) and A[start_bottom][1]:
		start_bottom += 1
	return (B, start_top, start_bottom)

def translate_macro(x):
	if x == '0':
		return False
	else:
		return True

def is_filled(A):
	i = 0
	while i < len(A) and A[i][0] and A[i][1]:
		i += 1
	if len(A) == i:
		return True
	else:
		return False

def fill_grid(A, start_top, start_bottom):
	if start_top >= len(A) and start_bottom >= len(A):
		return is_filled(A)
	elif start_top == min(start_top, start_bottom):
		for block_type in [1, 2, 3]:
			if is_admissible(A, block_type, (start_top, 0)):
				(B, new_top, new_bottom) = fill_block(A, block_type, (start_top, 0), start_top, start_bottom)
				if fill_grid(B, new_top, new_bottom):
					return True
		return is_filled(A)
	else:
		for block_type in [1, 2, 3]:
			if is_admissible(A, block_type, (start_bottom, 1)):
				(B, new_top, new_bottom) = fill_block(A, block_type, (start_bottom, 1), start_top, start_bottom)
				if fill_grid(B, new_top, new_bottom):
					return True
		return is_filled(A)

def is_grid_tiling_possible(N, s_1, s_2):
	A = [[translate_macro(s_1[i]), translate_macro(s_2[i])] for i in range(0, N)]
	start_top = 0
	start_bottom = 0
	while start_top < len(A) and A[start_top][0]:
		start_top += 1
	while start_bottom < len(A) and A[start_bottom][1]:
		start_bottom += 1
	return fill_grid(A, start_top, start_bottom)
T = int(input())
for _ in range(0, T):
	N = int(input())
	s_1 = input()
	s_2 = input()
	if is_grid_tiling_possible(N, s_1, s_2):
		print('YES')
	else:
		print('NO')


nr_tests = int(input())
for t in range(nr_tests):
	n = int(input())
	a = input()
	b = input()
	previous_b = '0'
	island_size = 0
	uneven_island = 0
	for char_idx in range(len(a)):
		if a[char_idx] == '1' and (b[char_idx] == '1' or previous_b == '1'):
			if island_size % 2 != 0:
				uneven_island = 1
				break
			else:
				island_size = 0
		if a[char_idx] == '0':
			island_size += 1
		if b[char_idx] == '0':
			island_size += 1
		previous_b = b[char_idx]
	if uneven_island == 1 or island_size % 2 == 1:
		print('NO')
	else:
		print('YES')


def f(n, a, b):
	c = [a, b]
	(i, j) = (0, 0)
	k = 0
	second = False
	while (i, j) != (0, n):
		if c[i][j] == 0:
			k += 1
			second = False
		elif not second:
			second = True
		else:
			if k % 2 != 0:
				return False
			k = 0
			second = False
		(i, j) = (i ^ 1, j + i)
	return k % 2 == 0
t = int(input())
for test_case in range(t):
	n = int(input())
	a = list(map(int, input().strip()))
	b = list(map(int, input().strip()))
	print('YES' if f(n, a, b) else 'NO')


def canFill(A, B, N):
	if N == 0:
		return True
	elif N == 1 and A[0] == B[0]:
		return True
	elif N == 1 and A[0] != B[0]:
		return False
	else:
		if B[N - 1] == A[N - 1]:
			if canFill(A[0:N - 1], B[0:N - 1], N - 1):
				return True
		if B[N - 1] == '1' and A[N - 1] == B[N - 2] or (A[N - 1] == '1' and B[N - 1] == B[N - 2]):
			Bm = B.copy()
			Bm[N - 2] = '1'
			if canFill(A[0:N - 1], Bm[0:N - 1], N - 1):
				return True
		if B[N - 1] == B[N - 2] and A[N - 1] == A[N - 2]:
			if canFill(A[0:N - 2], B[0:N - 2], N - 2):
				return True
	return False
cheatsheet = {}
T = int(input().strip())
for t in range(T):
	N = int(input().strip())
	A = [c for c in input().strip()]
	B = [c for c in input().strip()]
	if canFill(A, B, N):
		print('YES')
	else:
		print('NO')


def fill(a):
	for i in range(0, len(a[0]) - 1):
		if a[0][i] == 0 and a[1][i] == 0:
			a[0][i] = 1
			a[1][i] = 1
		elif a[0][i] == 0 and a[1][i] == 1:
			if a[0][i + 1] == 0:
				a[0][i + 1] = 1
			else:
				return False
			a[0][i] = 1
		elif a[0][i] == 1 and a[1][i] == 0:
			if a[0][i + 1] == 0:
				a[0][i + 1] = 1
			elif a[1][i + 1] == 0:
				a[1][i + 1] = 1
			else:
				return False
			a[1][i] = 1
	return a[0][-1] == a[1][-1]
for case in range(int(input())):
	i = int(input())
	print('YES' if fill([list(map(int, input())), list(map(int, input()))]) else 'NO')


def hex_tiles(n, a_i, b_i):
	if (sum(a_i) + sum(b_i)) % 2 == 1:
		return 'NO'
	link_count = 0
	for i in range(n):
		if a_i[i] == 0:
			link_count += 1
		if b_i[i] == 0:
			link_count += 1
		if a_i[i] == 1:
			if b_i[i] == 1:
				if link_count % 2 == 1:
					return 'NO'
				link_count = 0
		if b_i[i] == 1:
			if a_i[i] == 1 or (i != n - 1 and a_i[i + 1] == 1):
				if link_count % 2 == 1:
					return 'NO'
				link_count = 0
	return 'YES'

def binary_to_list():
	binary = list(input().strip())
	int_list = [int(x) for x in binary]
	return int_list
t = int(input().strip())
for test in range(t):
	n = int(input().strip())
	a_i = binary_to_list()
	b_i = binary_to_list()
	result = hex_tiles(n, a_i, b_i)
	print(result)


t = int(input().strip())
D = {}

def bombai(n, str1, str2):
	if (n, str1, str2) not in D.keys():
		if n == 0:
			D[n, str1, str2] = True
		elif str1[0] == '0' and str2[0] == '0':
			c = 1
			while c < n:
				if str1[c] == '1':
					D[n, str1, str2] = bombai(n - c, str1[c:], str2[c:])
					break
				if str2[c] == '1':
					D[n, str1, str2] = bombai(n - c, str1[c:], str2[c:])
					break
				c += 1
			else:
				D[n, str1, str2] = True
		elif str1[0] == '1' and str2[0] == '1':
			D[n, str1, str2] = bombai(n - 1, str1[1:], str2[1:])
		else:
			c = 1
			while c < n:
				if str1[c] == '1':
					if str2[c] == '1':
						D[n, str1, str2] = False
						break
					if c == 1 and str2[0] == '1':
						D[n, str1, str2] = False
						break
					D[n, str1, str2] = bombai(n - c - 1, str1[c + 1:], str2[c + 1:])
					break
				if str2[c] == '1':
					D[n, str1, str2] = bombai(n - c - 1, str1[c + 1:], str2[c + 1:])
					break
				c += 1
			else:
				D[n, str1, str2] = False
	return D[n, str1, str2]
for k in range(t):
	n = int(input().strip())
	str1 = input().strip()
	str2 = input().strip()
	k = bombai(n, str1, str2)
	if k:
		print('YES')
	else:
		print('NO')


def solution(a_arr, b_arr):
	current_size = 0
	i = 0
	last_black = False
	while i < len(a_arr):
		if a_arr[i]:
			if last_black:
				if current_size % 2 != 0:
					return False
				else:
					current_size = 0
			else:
				last_black = True
		else:
			last_black = False
			current_size += 1
		if b_arr[i]:
			if last_black:
				if current_size % 2 != 0:
					return False
				else:
					current_size = 0
			else:
				last_black = True
		else:
			last_black = False
			current_size += 1
		i += 1
	if current_size % 2 != 0:
		return False
	else:
		return True
t = int(input())
for i in range(t):
	n = int(input())
	a_arr = [int(x) for x in input().strip()]
	b_arr = [int(x) for x in input().strip()]
	result = solution(a_arr, b_arr)
	print('YES' if result else 'NO')


T = int(input().strip())
for t in range(T):
	n = int(input().strip())
	s1 = list(input().strip())
	s2 = list(input().strip())
	spmap = []
	for i in range(1, n):
		if s1[i] == '1':
			if s2[i - 1] == '1':
				spmap.append((i, i - 1))
			elif s2[i] == '1':
				spmap.append((i, i))
	sus = True
	premap = (0, 0)
	for i in range(len(spmap)):
		curnum = s1[premap[0]:spmap[i][0]].count('0') + s2[premap[1]:spmap[i][1]].count('0')
		if curnum % 2 == 1:
			sus = False
			break
		premap = spmap[i]
	if sus == True:
		curnum = s1[premap[0]:].count('0') + s2[premap[1]:].count('0')
		if curnum % 2 == 1:
			print('NO')
		else:
			print('YES')
	else:
		print('NO')


def GoGo(list_, col, x, y, cnt):
	if x > 1 or x < 0:
		return 0
	if y >= col or y < 0:
		return 0
	if list_[x][y] != '0':
		return 0
	if list_[x][y] == 'x':
		return 0
	list_[x][y] = 'x'
	cnt = cnt + GoGo(list_, col, x + 1, y, 1)
	cnt = cnt + GoGo(list_, col, x - 1, y, 1)
	cnt = cnt + GoGo(list_, col, x - 1, y + 1, 1)
	cnt = cnt + GoGo(list_, col, x + 1, y - 1, 1)
	cnt = cnt + GoGo(list_, col, x, y + 1, 1)
	cnt = cnt + GoGo(list_, col, x, y - 1, 1)
	return cnt

def HexagonalGrid(list_, col):
	for i in range(2):
		for j in range(int(col)):
			ret = GoGo(list_, col, i, j, 1)
			if ret % 2 > 0:
				return False
	return True
N = int(input().strip())
for i in range(N):
	col = int(input().strip())
	arr = list(input().strip())
	brr = list(input().strip())
	list_ = [arr, brr]
	if HexagonalGrid(list_, col) is False:
		print('NO')
	else:
		print('YES')


import sys

def recur(ab, index, n, black):
	if n == black:
		return True
	if index == len(ab):
		return False
	if ab[index] == 1:
		return recur(ab, index + 1, n, black)
	half = int(n / 2)
	move = []
	if index == 0:
		move = [half, 1]
	elif index == half - 1:
		move = [index + half, index + half - 1]
	elif index < half:
		move = [index + half, index + half - 1, index + 1]
	elif index == n - 1:
		move = [index - half]
	else:
		move = [index - half, index - half + 1, index + 1]
	ab[index] = 1
	for i in range(len(move)):
		if ab[move[i]] == 1:
			continue
		ab[move[i]] = 1
		if recur(ab, index + 1, n, black + 2) == True:
			return True
		ab[move[i]] = 0
	ab[index] = 0
	return False
T = int(input().strip())
for t in range(T):
	N = int(input().strip())
	count = 0
	ab = list(map(int, input().strip())) + list(map(int, input().strip()))
	black = 0
	for i in range(len(ab)):
		if ab[i] == 1:
			black += 1
	if recur(ab, 0, N * 2, black) == True:
		print('YES')
	else:
		print('NO')


T = int(input().strip())
for t in range(T):
	n = int(input().strip())
	s1 = list(input().strip())
	s2 = list(input().strip())
	spmap = []
	for i in range(1, n):
		if s1[i] == '1':
			if s2[i - 1] == '1':
				spmap.append((i, i - 1))
			elif s2[i] == '1':
				spmap.append((i, i))
	sus = True
	premap = (0, 0)
	for i in range(len(spmap)):
		curnum = s1[premap[0]:spmap[i][0]].count('0') + s2[premap[1]:spmap[i][1]].count('0')
		if curnum % 2 == 1:
			sus = False
			break
		premap = spmap[i]
	if sus == True:
		curnum = s1[premap[0]:].count('0') + s2[premap[1]:].count('0')
		if curnum % 2 == 1:
			print('NO')
		else:
			print('YES')
	else:
		print('NO')


def can_solve(a, b, n):
	a.append('1')
	b.append('1')
	for i in range(n):
		if a[i] == '1' and b[i] == '1':
			next
		elif a[i] == '0' and b[i] == '0':
			a[i] = '1'
			b[i] = '1'
		elif a[i + 1] == '0' and b[i] == '0':
			a[i + 1] = '1'
			b[i] = '1'
		elif a[i] == '0' and a[i + 1] == '0':
			a[i] = '1'
			a[i + 1] = '1'
		elif b[i] == '0' and b[i + 1] == '0':
			b[i] = '1'
			b[i + 1] = '1'
		else:
			return False
	return True
t = int(input().strip())
for a0 in range(t):
	n = int(input().strip())
	a = list(input())
	b = list(input())
	result = can_solve(a, b, n)
	print('YES' if result else 'NO')


def tilable(a, b, n):
	if (sum(a) + sum(b)) % 2 != 0:
		return False
	blocks = 0
	for i in range(n):
		if not a[i]:
			blocks += 1
		if a[i] and b[i - 1]:
			if blocks % 2 == 1:
				return False
			blocks = 0
		if a[i] and b[i]:
			if blocks % 2 == 1:
				return False
			blocks = 0
		if not b[i]:
			blocks += 1
	return True
t = int(input())
for test in range(t):
	n = int(input())
	a = list(map(int, input().strip()))
	b = list(map(int, input().strip()))
	if tilable(a, b, n):
		print('YES')
	else:
		print('NO')


def hexagonal_grid(arr, start_i, start_j, visited):
	import copy
	if start_i == 1 and start_j == len(arr[0]):
		return False
	if start_i == 0 and start_j == len(arr[0]):
		start_i = start_i + 1
		start_j = 0
	if start_i == 1 and start_j == len(arr[0]) - 1:
		return (start_i, start_j) in visited or arr[start_i][start_j] == 1
	if (start_i, start_j) in visited or arr[start_i][start_j] == 1:
		return hexagonal_grid(arr, start_i, start_j + 1, visited)
	ret = False
	for offset in [(0, 1), (1, -1), (1, 0)]:
		new_i = start_i + offset[0]
		new_j = start_j + offset[1]
		if new_i < 0 or new_i > 1:
			continue
		if new_j < 0 or new_j >= len(arr[0]):
			continue
		if (new_i, new_j) in visited or arr[new_i][new_j] == 1:
			continue
		visited_copy = copy.copy(visited)
		visited_copy[start_i, start_j] = True
		visited_copy[new_i, new_j] = True
		ret = ret or hexagonal_grid(arr, start_i, start_j + 1, visited_copy)
		if ret:
			return True
	return False
T = int(input().strip())
for i in range(T):
	input()
	arr = [[]] * 2
	arr[0] = list(map(int, list(input().strip())))
	arr[1] = list(map(int, list(input().strip())))
	if hexagonal_grid(arr, 0, 0, {}):
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(0, n):
	l = int(input())
	st_a = input()
	st_b = input()
	ls_a = []
	ls_b = []
	for j in range(0, l):
		if st_a[j] == '0':
			ls_a.append(j)
		if st_b[j] == '0':
			ls_b.append(j)
		value_a = 'YES'
		value_b = 'YES'
		ls_c = ls_a + ls_b
		ls_c.sort()
	for i in ls_c:
		if i in ls_a:
			if i - 1 in ls_a:
				ls_a.remove(i)
				ls_a.remove(i - 1)
				value_a = 'YES'
			elif i - 1 in ls_b:
				ls_a.remove(i)
				ls_b.remove(i - 1)
				value_a = 'YES'
			elif i in ls_b:
				ls_a.remove(i)
				ls_b.remove(i)
				value_a = 'YES'
			elif i + 1 in ls_a:
				ls_a.remove(i)
				ls_a.remove(i + 1)
				value_a = 'YES'
			else:
				value_a = 'NO'
				break
		if i in ls_b:
			if i - 1 in ls_b:
				ls_b.remove(i)
				ls_b.remove(i - 1)
				value_b = 'YES'
			elif i in ls_a:
				ls_b.remove(i)
				ls_a.remove(i)
				value_b = 'YES'
			elif i + 1 in ls_a:
				ls_b.remove(i)
				ls_a.remove(i + 1)
				value_b = 'YES'
			elif i + 1 in ls_b:
				ls_b.remove(i + 1)
				ls_b.remove(i)
				value_b = 'YES'
			else:
				value_b = 'NO'
				break
	if (len(ls_a) + len(ls_b)) % 2 == 1:
		print('NO')
	elif ls_a.count('1') == len(ls_a) and ls_b.count('1') == len(ls_b):
		print('YES')
	elif value_a == 'YES' and value_b == 'YES':
		print('YES')
	else:
		print('NO')


def isBlocked(n, first, second):
	blocked_cells = sum(first) + sum(second)
	if blocked_cells % 2 == 1:
		return True
	if blocked_cells == 2 * n:
		return False
	sum_so_far = 0
	for i in range(0, n):
		sum_so_far += 1 - first[i]
		if i > 0 and first[i] and second[i - 1]:
			if sum_so_far % 2 == 1:
				return True
			sum_so_far = 0
		if first[i] and second[i] and (sum_so_far % 2 == 1):
			return True
		sum_so_far += 1 - second[i]
	return False
T = int(input().strip())
for t in range(T):
	n = int(input().strip())
	(first, second) = [list(map(int, list(input()))) for _ in range(2)]
	if isBlocked(n, first, second):
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(N, A, B) = (int(input()), input(), input())
	(v, rs) = (A[0] != B[0], True)
	for i in range(1, N):
		v ^= A[i] != B[i]
		if A[i] == '1' and (B[i] == '1' or (A[i - 1] != '1' and B[i - 1] == '1')):
			if not v ^ (B[i] == '1'):
				rs = False
				break
			v = B[i] != '1'
	print('YES' if rs and (not v) else 'NO')