You are given two N \times M integer matrices A and B. You are allowed to perform the following operation on A as many times as you like (possibly, zero):

Pick any square submatrix of A and flip it about either its [main diagonal or its antidiagonal].

For example, suppose you choose the submatrix

\begin{bmatrix}
1 \;\;\;\; 2 \;\;\;\; 3 \\
4 \;\;\;\; 5 \;\;\;\; 6 \\
7 \;\;\;\; 8 \;\;\;\; 9
\end{bmatrix}

.

It can be converted into either

\begin{bmatrix}
1 \;\;\;\; 4 \;\;\;\; 7 \\
2 \;\;\;\; 5 \;\;\;\; 8 \\
3 \;\;\;\; 6 \;\;\;\; 9
\end{bmatrix}

by flipping about the main diagonal, or

\begin{bmatrix}
9 \;\;\;\; 6 \;\;\;\; 3 \\
8 \;\;\;\; 5 \;\;\;\; 2 \\
7 \;\;\;\; 4 \;\;\;\; 1
\end{bmatrix}

by flipping about the antidiagonal.

Is it possible to convert A to B by performing this operation several (possibly, zero) times?

Note: For the purposes of this problem, a submatrix of a matrix is the intersection of a contiguous segment of rows with a contiguous segment of columns.

For example, if
A =
\begin{bmatrix}
1 \;\;\;\; 2 \;\;\;\; 3 \\
4 \;\;\;\; 5 \;\;\;\; 6 \\
7 \;\;\;\; 8 \;\;\;\; 9
\end{bmatrix}

then \begin{bmatrix} 2 \end{bmatrix}, \begin{bmatrix} 5 \;\;\;\; 6 \\ 8 \;\;\;\; 9 \end{bmatrix}, and \begin{bmatrix}1 \\ 4\end{bmatrix} are submatrices of A, while \begin{bmatrix}1 \;\;\;\; 3 \\ 7 \;\;\;\; 9\end{bmatrix} *is not*.

A square submatrix is a submatrix with the same number of rows and columns.

------ Input Format ------ 

- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers N and M — the number of rows and columns of the matrices, respectively.
- The next N lines describe the matrix A. The i-th of these lines contains M space-separated integers ― the values A_{i, 1}, A_{i, 2}, \ldots, A_{i, M}.
- The next N lines describe the matrix B. The i-th of these lines contains M space-separated integers ― the values B_{i, 1}, B_{i, 2}, \ldots, B_{i, M}.

------ Output Format ------ 

For each test case, print YES if its possible to convert A to B, else print NO.

Each character of the output may be printed in either uppercase or lowercase. For example, the strings YES, yes, yeS, YeS will all be treated as identical.

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

$1 ≤ T ≤ 10^{4}$
$1 ≤ N,M ≤ 3 \cdot 10^{5}$
$1 ≤ A_{i, j},B_{i, j} ≤ 10^{9}$
- The sum of $N\cdot M$ over all test cases won't exceed $3 \cdot 10^{5}$.

----- Sample Input 1 ------ 
2
2 3
1 2 3
4 5 6
1 4 3
6 5 2
3 3
12 11 8
7 1 1
9 2 4
4 1 8
2 1 11
9 7 12

----- Sample Output 1 ------ 
YES
YES

----- explanation 1 ------ 
Test case $1$: $A$ can be converted to $B$ as follows:
$$
\begin{bmatrix} 1 \;\;\;\; 2 \;\;\;\; 3 \\ 4 \;\;\;\; 5 \;\;\;\; 6 \end{bmatrix} \to \begin{bmatrix} 1 \;\;\;\; \textcolor{red}{6} \;\;\;\; \textcolor{red}{3} \\ 4 \;\;\;\; \textcolor{red}{5} \;\;\;\; \textcolor{red}{2} \end{bmatrix} \to \begin{bmatrix} \textcolor{red}{1} \;\;\;\; \textcolor{red}{4} \;\;\;\; 3 \\ \textcolor{red}{6} \;\;\;\; \textcolor{red}{5} \;\;\;\; 2 \end{bmatrix}
$$

Difficulty: MEDIUM_HARD
Tags: Sorting

Data structures
Skill types: Sorting

Data structures

Starter code:

Solutions:
import sys
for _ in range(int(sys.stdin.readline())):
	(n, m) = list(map(int, sys.stdin.readline().split()))
	A = []
	B = []
	for i in range(n):
		A.append(list(map(int, sys.stdin.readline().split())))
	for i in range(n):
		B.append(list(map(int, sys.stdin.readline().split())))
	A_even = []
	B_even = []
	A_odd = []
	B_odd = []
	if n == 1 or m == 1:
		if A == B:
			sys.stdout.write('YES\n')
		else:
			sys.stdout.write('NO\n')
		continue
	for i in range(n):
		for j in range(m):
			if (i + j) % 2 == 0:
				A_even.append(A[i][j])
				B_even.append(B[i][j])
			else:
				A_odd.append(A[i][j])
				B_odd.append(B[i][j])
	A_even.sort()
	A_odd.sort()
	B_even.sort()
	B_odd.sort()
	if A_even == B_even and A_odd == B_odd:
		sys.stdout.write('YES\n')
	else:
		sys.stdout.write('NO\n')


for i in range(int(input())):
	(n, m) = map(int, input().split())
	a = []
	b = []
	for i in range(n):
		a.append(list(map(int, input().split())))
	for i in range(n):
		b.append(list(map(int, input().split())))
	if m == 1 or n == 1:
		if a == b:
			print('YES')
		else:
			print('NO')
	else:
		a1 = []
		a2 = []
		b1 = []
		b2 = []
		for i in range(n):
			for j in range(m):
				if (i + j) % 2 == 0:
					a1.append(a[i][j])
					b1.append(b[i][j])
				else:
					a2.append(a[i][j])
					b2.append(b[i][j])
		a1.sort()
		b1.sort()
		a2.sort()
		b2.sort()
		if a1 == b1 and a2 == b2:
			print('YES')
		else:
			print('NO')


t = int(input())
for i in range(t):
	(n, m) = [int(x) for x in input().split()]
	t1 = []
	t2 = []
	for k in range(n):
		temp = [int(x) for x in input().split()]
		t1.append(temp)
	for k in range(n):
		temp = [int(x) for x in input().split()]
		t2.append(temp)
	if n == 1 or m == 1:
		if t1 == t2:
			print('YES')
		else:
			print('NO')
	else:
		tempa1 = []
		tempa2 = []
		tempb1 = []
		tempb2 = []
		for i in range(n):
			for j in range(m):
				if (i + j) % 2 == 0:
					tempa1.append(t1[i][j])
					tempb1.append(t2[i][j])
				else:
					tempa2.append(t1[i][j])
					tempb2.append(t2[i][j])
		tempa1.sort()
		tempa2.sort()
		tempb1.sort()
		tempb2.sort()
		if tempa1 == tempb1 and tempa2 == tempb2:
			print('YES')
		else:
			print('NO')


test_cases = int(input())
for _ in range(test_cases):
	(rows, columns) = list(map(int, input().split()))
	a = [list(map(int, input().split())) for _ in range(rows)]
	b = [list(map(int, input().split())) for _ in range(rows)]
	if rows == 1 or columns == 1:
		if a == b:
			print('YES')
		else:
			print('NO')
	else:
		white_a = list()
		black_a = list()
		white_b = list()
		black_b = list()
		for i in range(rows):
			for j in range(columns):
				if (i + j) % 2 == 0:
					white_a.append(a[i][j])
					white_b.append(b[i][j])
				else:
					black_a.append(a[i][j])
					black_b.append(b[i][j])
		if set(white_a) == set(white_b) and set(black_a) == set(black_b):
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	(al, bl) = ([], [])
	for a in range(n):
		l = list(map(int, input().split()))
		al.append(l)
	for a in range(n):
		l = list(map(int, input().split()))
		bl.append(l)
	(ae, ao, be, bo) = ([], [], [], [])
	if n == 1 or m == 1:
		if al == bl:
			print('YES')
		else:
			print('NO')
	else:
		(ao, ae, bo, be) = ([], [], [], [])
		for i in range(n):
			for j in range(m):
				if i + j & 1 == 0:
					ae.append(al[i][j])
					be.append(bl[i][j])
				else:
					ao.append(al[i][j])
					bo.append(bl[i][j])
		ae.sort()
		ao.sort()
		be.sort()
		bo.sort()
		if ae == be and ao == bo:
			print('YES')
		else:
			print('NO')


n = int(input())
while n > 0:
	n = n - 1
	A = input()
	c = []
	d = []
	e = []
	f = []
	g = []
	h = []
	z = 0
	y = 0
	A = A.split(' ')
	A = list(map(lambda x: int(x), A))
	a = A[0]
	b = A[1]
	count = 0
	found = 0
	for i in range(0, a):
		x = input()
		x = x.split()
		x = list(map(lambda x: int(x), x))
		c.append(x)
	for i in range(0, a):
		x = input()
		x = x.split()
		x = list(map(lambda x: int(x), x))
		d.append(x)
	if a == 1 or b == 1:
		if c == d:
			print('YES')
		else:
			print('NO')
		continue
	for i in range(0, a):
		for j in range(0, b, 2):
			if i % 2 == 0:
				e.append(c[i][j])
				g.append(d[i][j])
				if j + 1 < b:
					f.append(c[i][j + 1])
					h.append(d[i][j + 1])
			else:
				if j + 1 < b:
					e.append(c[i][j + 1])
					g.append(d[i][j + 1])
				f.append(c[i][j])
				h.append(d[i][j])
	e.sort()
	f.sort()
	g.sort()
	h.sort()
	for i in range(0, len(e)):
		if e[i] == g[i]:
			count = count + 1
	for i in range(0, len(f)):
		if f[i] == h[i]:
			found = found + 1
	if count == len(e) and found == len(f):
		print('YES')
	else:
		print('NO')


from math import *
from bisect import *
t = int(input())
for _ in range(t):
	(memo_1, memo_0) = ({}, {})
	(n, m) = map(int, input().split())
	if n == 1 or m == 1:
		if n == 1:
			a = input()
			b = input()
		if m == 1:
			a = [int(input()) for i in range(n)]
			b = [int(input()) for i in range(n)]
		print('YES' if a == b else 'NO')
		continue
	for i in range(n):
		for (j, v) in enumerate(list(map(int, input().split()))):
			if (i + j) % 2:
				memo_1[v] = memo_1.get(v, 0) + 1
			else:
				memo_0[v] = memo_0.get(v, 0) + 1
	for i in range(n):
		for (j, v) in enumerate(list(map(int, input().split()))):
			if (i + j) % 2:
				memo_1[v] = memo_1.get(v, 0) - 1
			else:
				memo_0[v] = memo_0.get(v, 0) - 1
	count = 0
	for i in memo_0.values():
		count += abs(i)
	for i in memo_1.values():
		count += abs(i)
	if count == 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(N, M) = map(int, input().split())
	if min(N, M) > 1:
		A = {}
		for i in range(N):
			tmp = list(map(int, input().split()))
			for j in range(M):
				A[tmp[j]] = A.get(tmp[j], 0) + (i + j) % 2
		B = {}
		for i in range(N):
			tmp = list(map(int, input().split()))
			for j in range(M):
				B[tmp[j]] = B.get(tmp[j], 0) + (i + j) % 2
		print('YES' if A == B else 'NO')
	else:
		A = []
		for i in range(N):
			A += list(map(int, input().split()))
		B = []
		for i in range(N):
			B += list(map(int, input().split()))
		print('YES' if A == B else 'NO')


for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	A = []
	B = []
	for i in range(n):
		A.append(list(map(int, input().split())))
	for i in range(n):
		B.append(list(map(int, input().split())))
	A_even = []
	B_even = []
	A_odd = []
	B_odd = []
	if n == 1 or m == 1:
		if A == B:
			print('YES')
		else:
			print('NO')
		continue
	for i in range(n):
		for j in range(m):
			if (i + j) % 2 == 0:
				A_even.append(A[i][j])
				B_even.append(B[i][j])
			else:
				A_odd.append(A[i][j])
				B_odd.append(B[i][j])
	A_even.sort()
	A_odd.sort()
	B_even.sort()
	B_odd.sort()
	if A_even == B_even and A_odd == B_odd:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	(list1, list2) = ([], [])
	for i in range(n):
		list1.append(list(map(int, input().split())))
	for i in range(n):
		list2.append(list(map(int, input().split())))
	if n == 1 or m == 1:
		if list1 == list2:
			print('YES')
		else:
			print('NO')
		continue
	(list3, list4, list5, list6) = ([], [], [], [])
	for i in range(n):
		for j in range(m):
			if (i + j) % 2 == 0:
				list3.append(list1[i][j])
				list5.append(list2[i][j])
			else:
				list4.append(list1[i][j])
				list6.append(list2[i][j])
	list3.sort()
	list4.sort()
	list5.sort()
	list6.sort()
	if list3 == list5 and list4 == list6:
		print('YES')
	else:
		print('NO')


try:
	for _ in range(int(input())):

		def f(n, m):
			lst1 = []
			lst2 = []
			lst3 = []
			lst4 = []
			lst5 = []
			lst6 = []
			for i in range(n):
				lst1.append(list(map(int, input().split())))
			for i in range(n):
				lst2.append(list(map(int, input().split())))
			if n == 1:
				if m > 1:
					if lst1 == lst2:
						return 'YES'
					else:
						return 'NO'
			if m == 1:
				if n > 1:
					for i in range(n):
						for j in range(m):
							if lst1[i][j] != lst2[i][j]:
								return 'NO'
					return 'YES'
			for i in range(0, n, 2):
				for j in range(0, m, 2):
					lst3.append(lst1[i][j])
					lst5.append(lst2[i][j])
			for i in range(1, n, 2):
				for j in range(1, m, 2):
					lst3.append(lst1[i][j])
					lst5.append(lst2[i][j])
			for i in range(0, n, 2):
				for j in range(1, m, 2):
					lst4.append(lst1[i][j])
					lst6.append(lst2[i][j])
			for i in range(1, n, 2):
				for j in range(0, m, 2):
					lst4.append(lst1[i][j])
					lst6.append(lst2[i][j])
			if sorted(lst3) == sorted(lst5) and sorted(lst4) == sorted(lst6):
				return 'YES'
			else:
				return 'NO'
		(n, m) = map(int, input().split())
		print(f(n, m))
except:
	pass


testcases = int(input())
for i in range(testcases):
	(n, m) = map(int, input().split(' '))
	list1 = []
	list2 = []
	list1_odd = []
	list1_even = []
	list2_odd = []
	list2_even = []
	for j in range(n):
		a = list(map(int, input().split(' ')))
		list1.append(a)
	for j in range(n):
		b = list(map(int, input().split(' ')))
		list2.append(b)
	if n == 1 or m == 1:
		if list1 == list2:
			print('YES')
		else:
			print('NO')
		continue
	for k in range(n):
		for l in range(m):
			if (l + k) % 2 == 0:
				list1_even.append(list1[k][l])
				list2_even.append(list2[k][l])
			else:
				list1_odd.append(list1[k][l])
				list2_odd.append(list2[k][l])
	list1_even.sort()
	list2_even.sort()
	list1_odd.sort()
	list2_odd.sort()
	if list1_even == list2_even and list1_odd == list2_odd:
		print('YES')
	else:
		print('NO')


from collections import defaultdict
t = int(input())
for _ in range(t):
	(n, m) = list(map(int, input().split()))
	a = []
	b = []
	for _ in range(n):
		a.append(list(map(int, input().split())))
	for _ in range(n):
		b.append(list(map(int, input().split())))
	if n == 1 and a != b:
		print('NO')
		continue
	elif m == 1 and a != b:
		print('NO')
		continue
	elif a == b:
		print('YES')
		continue
	even_a = defaultdict(int)
	even_b = defaultdict(int)
	odd_a = defaultdict(int)
	odd_b = defaultdict(int)
	for i in range(n):
		for j in range(m):
			if i + j & 1 == 0:
				even_a[a[i][j]] += 1
				even_b[b[i][j]] += 1
			else:
				odd_a[a[i][j]] += 1
				odd_b[b[i][j]] += 1
	if even_a == even_b and odd_a == odd_b:
		print('YES')
	else:
		print('NO')


from collections import defaultdict
t = int(input())
while t:
	s = input().split()
	(n, m) = map(int, s)
	A = []
	for i in range(n):
		s = input().split()
		temp = list(map(int, s))
		A.append(temp)
	B = []
	for i in range(n):
		s = input().split()
		temp = list(map(int, s))
		B.append(temp)
	if m == 1 or n == 1:
		if A == B:
			print('YES')
		else:
			print('NO')
		t -= 1
		continue
	d = defaultdict(set)
	for i in range(n):
		for j in range(m):
			d[A[i][j]].add((i, j))
	ans = 'YES'
	for i in range(n):
		for j in range(m):
			if A[i][j] == B[i][j]:
				d[A[i][j]].remove((i, j))
			elif B[i][j] in d:
				flag = True
				for (a, b) in d[B[i][j]]:
					dif = abs(a - i) + abs(b - j)
					if dif % 2 == 0:
						flag = False
						d[B[i][j]].remove((a, b))
						d[A[i][j]].add((a, b))
						d[A[i][j]].remove((i, j))
						(A[i][j], A[a][b]) = (A[a][b], A[i][j])
						break
				if flag:
					ans = 'NO'
					break
			else:
				ans = 'NO'
				break
		if ans == 'NO':
			break
	print(ans)
	t -= 1


t = int(input())
while t:
	s = input().split()
	(n, m) = map(int, s)
	A = []
	for i in range(n):
		s = input().split()
		temp = list(map(int, s))
		A.append(temp)
	B = []
	for i in range(n):
		s = input().split()
		temp = list(map(int, s))
		B.append(temp)
	aA = []
	bA = []
	aB = []
	bB = []
	for i in range(n):
		for j in range(m):
			if (i + j) % 2 == 0:
				aA.append(A[i][j])
				aB.append(B[i][j])
			else:
				bA.append(A[i][j])
				bB.append(B[i][j])
	aA.sort()
	bB.sort()
	aB.sort()
	bA.sort()
	if m == 1 or n == 1:
		if A == B:
			ans = 'YES'
		else:
			ans = 'NO'
	elif aA == aB and bB == bA:
		ans = 'YES'
	else:
		ans = 'NO'
	print(ans)
	t -= 1


from collections import Counter

def solve(a, b):
	if len(a) == 1 or len(a[0]) == 1:
		return 'NO'
	r = len(a)
	c = len(a[0])
	a_counts1 = Counter()
	a_counts2 = Counter()
	b_counts1 = Counter()
	b_counts2 = Counter()
	x = 0
	for i in range(r):
		for j in range(c):
			if j % 2 == x:
				a_counts1[a[i][j]] += 1
				b_counts1[b[i][j]] += 1
		x ^= 1
	x = 1
	for i in range(r):
		for j in range(c):
			if j % 2 == x:
				a_counts2[a[i][j]] += 1
				b_counts2[b[i][j]] += 1
		x ^= 1
	if a_counts1 == b_counts1 and a_counts2 == b_counts2:
		return 'YES'
	return 'NO'
for _ in range(int(input())):
	(n, _) = list(map(int, input().split()))
	a = [list(map(int, input().split())) for _ in range(n)]
	b = [list(map(int, input().split())) for _ in range(n)]
	print(solve(a, b))


t = int(input())
while t:
	t -= 1
	(n, m) = input().split()
	(n, m) = (int(n), int(m))
	a = []
	b = []
	for i in range(n):
		a.append(list(map(int, input().split())))
	for i in range(n):
		b.append(list(map(int, input().split())))
	if n == 1 or m == 1:
		if a == b:
			print('YES')
		else:
			print('NO')
	else:
		ae = {}
		ao = {}
		be = {}
		bo = {}
		for i in range(n):
			for j in range(m):
				if (i + j) % 2 == 0:
					if a[i][j] not in ae:
						ae[a[i][j]] = 0
					ae[a[i][j]] += 1
					if b[i][j] not in be:
						be[b[i][j]] = 0
					be[b[i][j]] += 1
				else:
					if a[i][j] not in ao:
						ao[a[i][j]] = 0
					ao[a[i][j]] += 1
					if b[i][j] not in bo:
						bo[b[i][j]] = 0
					bo[b[i][j]] += 1
		if be == ae and bo == ao:
			print('YES')
		else:
			print('NO')


def func(A, B, n, m):
	if m == 1 or n == 1:
		if A == B:
			return 'YES'
		else:
			return 'NO'
	ab = 0
	bb = 0
	for i in range(n):
		if i % 2 == 0:
			j = 0
			while j < m:
				ab += A[i][j]
				bb += B[i][j]
				j += 2
		else:
			j = 1
			while j < m:
				ab += A[i][j]
				bb += B[i][j]
				j += 2
	if bb == ab:
		return 'YES'
	return 'NO'
T = int(input())
for t in range(T):
	(N, M) = list(map(int, input().split()))
	A = []
	B = []
	for i in range(N):
		A.append(list(map(int, input().split())))
	for j in range(N):
		B.append(list(map(int, input().split())))
	res = func(A, B, N, M)
	print(res)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	m1 = []
	m2 = []
	for i in range(n):
		m1.append(list(map(int, input().split())))
	for i in range(n):
		m2.append(list(map(int, input().split())))
	if m == 1 or n == 1:
		if m1 == m2:
			print('YES')
		else:
			print('NO')
	else:
		(s1, s2, f) = (0, 0, 0)
		for i in range(n):
			for j in range(m):
				if f == 0 and j % 2 == 0:
					s1 += m1[i][j]
					s2 += m2[i][j]
				elif f == 1 and j % 2 == 1:
					s1 += m1[i][j]
					s2 += m2[i][j]
			if f == 0:
				f = 1
			else:
				f = 0
		if s1 == s2:
			print('YES')
		else:
			print('NO')


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	ma1 = []
	ma2 = []
	for i in range(n):
		ma1.append(list(map(int, input().split())))
	for i in range(n):
		ma2.append(list(map(int, input().split())))
	p = []
	q = []
	if n == 1 or m == 1:
		if ma1 == ma2:
			print('YES')
		else:
			print('NO')
	else:
		for i in range(n):
			for j in range(m):
				p.append(ma1[i][j])
				q.append(ma2[i][j])
		p.sort()
		q.sort()
		if p != q:
			print('NO')
		else:
			(sum1, sum2) = (0, 0)
			f = 0
			for i in range(n):
				for j in range(m):
					if f == 0 and j % 2 == 0:
						sum1 += ma1[i][j]
						sum2 += ma2[i][j]
					elif f == 1 and j % 2 == 1:
						sum1 += ma1[i][j]
						sum2 += ma2[i][j]
				if f == 1:
					f = 0
				else:
					f = 1
			if sum1 == sum2:
				print('YES')
			else:
				print('NO')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	m1 = []
	m2 = []
	for i in range(n):
		m1.append(list(map(int, input().split())))
	for i in range(n):
		m2.append(list(map(int, input().split())))
	(a1, a2, b1, b2) = ([], [], [], [])
	if m == 1 or n == 1:
		if m1 == m2:
			print('YES')
		else:
			print('NO')
	else:
		for i in range(n):
			for j in range(m):
				if (i + j) % 2 == 0:
					a1.append(m1[i][j])
					a2.append(m2[i][j])
				else:
					b1.append(m1[i][j])
					b2.append(m2[i][j])
		a1.sort()
		a2.sort()
		b1.sort()
		b2.sort()
		if a1 == a2 and b1 == b2:
			print('YES')
		else:
			print('NO')


t = int(input())
for __ in range(t):
	(n, m) = map(int, input().split())
	a = []
	b = []
	aeven = []
	aodd = []
	beven = []
	bodd = []
	for __ in range(n):
		row = list(map(int, input().split()))
		a.append(row)
	for __ in range(n):
		row = list(map(int, input().split()))
		b.append(row)
	if n == 1 or m == 1:
		if a == b:
			print('YES')
		else:
			print('NO')
	else:
		for i in range(n):
			for j in range(m):
				if (i + j) % 2 == 0:
					aeven.append(a[i][j])
					beven.append(b[i][j])
				else:
					aodd.append(a[i][j])
					bodd.append(b[i][j])
		aeven.sort()
		beven.sort()
		aodd.sort()
		bodd.sort()
		if aeven == beven and aodd == bodd:
			print('YES')
		else:
			print('NO')


T = int(input())
for t in range(T):
	(n, m) = tuple(map(int, input().split()))
	(a1, a2, b1, b2) = ([], [], [], [])
	for i in range(n):
		arr = tuple(map(int, input().split()))
		for j in range(m):
			if (i + j) % 2 == 0:
				a1.append(arr[j])
			else:
				a2.append(arr[j])
	for i in range(n):
		arr = tuple(map(int, input().split()))
		for j in range(m):
			if (i + j) % 2 == 0:
				b1.append(arr[j])
			else:
				b2.append(arr[j])
	if m == 1 or n == 1:
		if a1 == b1 and a2 == b2:
			print('YES')
		else:
			print('NO')
		continue
	a1.sort()
	a2.sort()
	b1.sort()
	b2.sort()
	if a1 == b1 and a2 == b2:
		print('YES')
	else:
		print('NO')


from collections import defaultdict

def solve(matrix1, matrix2, n, m):
	if n == 1 or (m == 1 and matrix1 != matrix2):
		return 'NO'
	matrix1a = defaultdict(int)
	matrix1b = defaultdict(int)
	matrix2a = defaultdict(int)
	matrix2b = defaultdict(int)
	index = 0
	for row in range(n):
		for col in range(index, m, 2):
			matrix1a[matrix1[row][col]] += 1
			matrix2a[matrix2[row][col]] += 1
		index ^= 1
	index = 1
	for row in range(n):
		for col in range(index, m, 2):
			matrix1b[matrix1[row][col]] += 1
			matrix2b[matrix2[row][col]] += 1
		index ^= 1
	if matrix1a == matrix2a and matrix2b == matrix1b:
		return 'YES'
	return 'NO'

def driver():
	tests = int(input())
	for test in range(tests):
		(n, m) = [int(el) for el in input().split()]
		matrix1 = [input().split() for row in range(n)]
		matrix2 = [input().split() for row in range(n)]
		print(solve(matrix1, matrix2, n, m))
driver()


t = int(input())
for q in range(t):
	a = []
	b = []
	c = []
	o1 = []
	e1 = []
	o2 = []
	e2 = []
	(n, m) = [int(i) for i in input().split()]
	for k in range(0, n):
		b = [int(i) for i in input().split()]
		a.append(b)
	for k in range(0, n):
		b = [int(i) for i in input().split()]
		c.append(b)
	for i in range(0, n):
		for j in range(0, m):
			if (i + j) % 2 == 0:
				e1.append(a[i][j])
				e2.append(c[i][j])
			else:
				o1.append(a[i][j])
				o2.append(c[i][j])
	e1.sort()
	o1.sort()
	e2.sort()
	o2.sort()
	if e2 == e1 and o2 == o1 and (n != 1) and (m != 1):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	aMatrix = []
	bMatrix = []
	(n, m) = map(int, input().split())
	for i in range(n):
		p1 = list(map(int, input().split()))
		aMatrix.append(p1)
	for i in range(n):
		q1 = list(map(int, input().split()))
		bMatrix.append(q1)
	c = 0
	if n == 1 or m == 1:
		for i in range(n):
			for j in range(m):
				if aMatrix[i][j] == bMatrix[i][j]:
					c += 1
				else:
					break
		if c == n * m:
			print('YES')
		else:
			print('NO')
	else:
		p = []
		q = []
		r = []
		s = []
		for i in range(n):
			for j in range(m):
				if (i + j) % 2 == 0:
					p.append(aMatrix[i][j])
					q.append(bMatrix[i][j])
				else:
					r.append(aMatrix[i][j])
					s.append(bMatrix[i][j])
		if sorted(p) == sorted(q) and sorted(r) == sorted(s):
			print('YES')
		else:
			print('NO')


t = int(input())
while t >= 1:
	(n, m) = map(int, input().split())
	a = [list(map(int, input().split())) for i in range(n)]
	b = [list(map(int, input().split())) for i in range(n)]
	A = []
	B = []
	for i in a:
		A += i
	for i in b:
		B += i
	A.sort()
	B.sort()
	if A == B and n != 1 and (m != 1):
		print('YES')
	else:
		print('NO')
	t -= 1


for i in range(int(input())):
	[row, col] = [int(ele) for ele in input().split()]
	if row == 1 or col == 1:
		mat1 = [[int(ele) for ele in input().split()] for i in range(row)]
		mat2 = [[int(ele) for ele in input().split()] for i in range(row)]
		if mat1 == mat2:
			print('YES')
		else:
			print('NO')
	else:
		e1 = {}
		e2 = {}
		o1 = {}
		o2 = {}
		for i in range(row):
			line = [int(ele) for ele in input().split()]
			for j in range(col):
				if (i + j) % 2 == 0:
					e1[line[j]] = e1.get(line[j], 0) + 1
				else:
					o1[line[j]] = o1.get(line[j], 0) + 1
		for i in range(row):
			line = [int(ele) for ele in input().split()]
			for j in range(col):
				if (i + j) % 2 == 0:
					e2[line[j]] = e2.get(line[j], 0) + 1
				else:
					o2[line[j]] = o2.get(line[j], 0) + 1
		if e1 == e2 and o1 == o2:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = []
	for i in range(n):
		c = list(map(int, input().split()))
		a.append(c)
	b = []
	for i in range(n):
		c = list(map(int, input().split()))
		b.append(c)
	if n == 1 or m == 1:
		if a == b:
			print('YES')
			continue
		else:
			print('NO')
			continue
	dic_ev_a = {}
	dic_od_a = {}
	dic_ev_b = {}
	dic_od_b = {}
	for i in range(n):
		for j in range(m):
			if (i + j) % 2 == 0:
				if a[i][j] in dic_ev_a:
					dic_ev_a[a[i][j]] += 1
				else:
					dic_ev_a[a[i][j]] = 1
				if b[i][j] in dic_ev_b:
					dic_ev_b[b[i][j]] += 1
				else:
					dic_ev_b[b[i][j]] = 1
			if (i + j) % 2 == 1:
				if a[i][j] in dic_od_a:
					dic_od_a[a[i][j]] += 1
				else:
					dic_od_a[a[i][j]] = 1
				if b[i][j] in dic_od_b:
					dic_od_b[b[i][j]] += 1
				else:
					dic_od_b[b[i][j]] = 1
	for i in dic_od_a:
		if i not in dic_od_b:
			break
		if dic_od_b[i] != dic_od_a[i]:
			break
	else:
		for i in dic_ev_a:
			if i not in dic_ev_b:
				break
			if dic_ev_b[i] != dic_ev_a[i]:
				break
		else:
			print('YES')
			continue
	print('NO')


def solve():
	(N, M) = map(int, input().split())
	if N == 1 or M == 1:
		A = []
		B = []
		for i in range(N):
			A.append(list(map(int, input().split())))
		for i in range(N):
			B.append(list(map(int, input().split())))
		if A == B:
			print('YES')
		else:
			print('NO')
		return
	(even_A, odd_A, even_B, odd_B) = ([], [], [], [])
	A = []
	B = []
	for i in range(N):
		A.append(list(map(int, input().split())))
	for i in range(N):
		B.append(list(map(int, input().split())))
	for i in range(N):
		for j in range(M):
			if (i + j) % 2 == 0:
				even_A.append(A[i][j])
				even_B.append(B[i][j])
			else:
				odd_A.append(A[i][j])
				odd_B.append(B[i][j])
	even_A.sort()
	odd_A.sort()
	even_B.sort()
	odd_B.sort()
	if even_A != even_B:
		print('NO')
		return
	if odd_A != odd_B:
		print('NO')
		return
	print('YES')
for _ in range(int(input())):
	solve()


from collections import Counter
for _ in range(int(input())):
	(r, c) = map(int, input().split())
	moo = 1
	a1 = []
	b1 = []
	mee = {}
	for i in range(r):
		temp1 = list(map(int, input().split()))
		a1.append(temp1)
	for i in range(r):
		temp1 = list(map(int, input().split()))
		b1.append(temp1)
	if r == 1 or c == 1:
		for i in range(r):
			for j in range(c):
				if a1[i][j] != b1[i][j]:
					moo = 0
					break
	else:
		ao = []
		ax = []
		bo = []
		bx = []
		for i in range(r):
			for j in range(c):
				if (i + j) % 2 == 1:
					ao.append(a1[i][j])
					bo.append(b1[i][j])
				else:
					ax.append(a1[i][j])
					bx.append(b1[i][j])
		xao1 = dict(Counter(ao))
		xbo1 = dict(Counter(bo))
		xax1 = dict(Counter(ax))
		xbx1 = dict(Counter(bx))
		for i in ao:
			if xao1.get(i) != xbo1.get(i):
				moo = 0
				break
		for i in ax:
			if xax1.get(i) != xbx1.get(i):
				moo = 0
				break
	if moo:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	(x, y) = input().split()
	x = int(x)
	y = int(y)
	c1 = dict()
	c2 = dict()
	d1 = dict()
	d2 = dict()
	f = 0
	if x == 1 or y == 1:
		a = []
		b = []
		for i in range(x):
			k = list(map(int, input().split()))
			a.append(k)
		for i in range(x):
			k = list(map(int, input().split()))
			b.append(k)
		for i in range(x):
			for j in range(y):
				if a[i][j] != b[i][j]:
					f = 1
					break
	else:
		for i in range(x):
			a = list(map(int, input().split()))
			for j in range(y):
				if (i + j) % 2 != 0:
					if a[j] in c1:
						c1[a[j]] += 1
					else:
						c1[a[j]] = 1
				elif a[j] in c2:
					c2[a[j]] += 1
				else:
					c2[a[j]] = 1
		for i in range(x):
			a = list(map(int, input().split()))
			for j in range(y):
				if (i + j) % 2 != 0:
					if a[j] in d1:
						d1[a[j]] += 1
					else:
						d1[a[j]] = 1
				elif a[j] in d2:
					d2[a[j]] += 1
				else:
					d2[a[j]] = 1
		for k in c1:
			if k in d1:
				if c1[k] != d1[k]:
					f = 1
					break
			else:
				f = 1
				break
		for k in c2:
			if k in d2:
				if c2[k] != d2[k]:
					f = 1
					break
			else:
				f = 1
				break
	if f == 0:
		print('YES')
	else:
		print('NO')


c = 0
t = int(input())
for _ in range(t):
	a = []
	b = []
	(n, m) = map(int, input().split())
	for i in range(n):
		p = []
		p = list(map(int, input().split()))
		a.append(p)
	for i in range(n):
		q = []
		q = list(map(int, input().split()))
		b.append(q)
	c = 0
	if n == 1 or m == 1:
		for i in range(n):
			for j in range(m):
				if a[i][j] == b[i][j]:
					c += 1
				else:
					break
		if c == n * m:
			print('YES')
		else:
			print('NO')
	else:
		l1 = []
		l2 = []
		l3 = []
		l4 = []
		for i in range(n):
			for j in range(m):
				if (i + j) % 2 == 0:
					l1.append(a[i][j])
					l2.append(b[i][j])
				else:
					l3.append(a[i][j])
					l4.append(b[i][j])
		if sorted(l1) == sorted(l2) and sorted(l3) == sorted(l4):
			print('YES')
		else:
			print('NO')


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	A = []
	B = []
	A1 = {}
	A2 = {}
	B1 = {}
	B2 = {}
	for i in range(n):
		A.append(list(map(int, input().split())))
		for j in range(m):
			if (i + j) % 2 == 0:
				if A[i][j] in A1.keys():
					A1.update({A[i][j]: A1[A[i][j]] + 1})
				else:
					A1[A[i][j]] = 1
			elif A[i][j] in A2.keys():
				A2.update({A[i][j]: A2[A[i][j]] + 1})
			else:
				A2[A[i][j]] = 1
	for i in range(n):
		B.append(list(map(int, input().split())))
		for j in range(m):
			if (i + j) % 2 == 0:
				if B[i][j] in B1.keys():
					B1.update({B[i][j]: B1[B[i][j]] + 1})
				else:
					B1[B[i][j]] = 1
			elif B[i][j] in B2.keys():
				B2.update({B[i][j]: B2[B[i][j]] + 1})
			else:
				B2[B[i][j]] = 1
	flag = 'YES'
	if n == 1 or m == 1:
		if A != B:
			flag = 'NO'
	else:
		for i in A1.keys():
			if i in B1.keys():
				if A1[i] != B1[i]:
					flag = 'NO'
					break
			else:
				flag = 'NO'
				break
		for i in A2.keys():
			if i in B2.keys():
				if A2[i] != B2[i]:
					flag = 'NO'
					break
			else:
				flag = 'NO'
				break
	print(flag)


for _ in range(int(input())):
	(m, n) = map(int, input().split())
	matrix1 = []
	matrix2 = []
	for i in range(m):
		matrix1.append(list(map(int, input().split())))
	for i in range(m):
		matrix2.append(list(map(int, input().split())))
	eve1 = []
	odd1 = []
	eve2 = []
	odd2 = []
	if m == 1 or n == 1:
		if matrix1 == matrix2:
			print('YES')
		else:
			print('NO')
		continue
	for i in range(m):
		for j in range(n):
			if i % 2 == 0 and j % 2 == 0 or (i % 2 == 1 and j % 2 == 1):
				eve1.append(matrix1[i][j])
				eve2.append(matrix2[i][j])
			else:
				odd1.append(matrix1[i][j])
				odd2.append(matrix2[i][j])
	if sorted(eve1) == sorted(eve2) and sorted(odd1) == sorted(odd2):
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	[n, m] = [int(i) for i in input().split()]
	a1 = []
	for i in range(n):
		a1.append([int(i) for i in input().split()])
	a2 = []
	for i in range(n):
		a2.append([int(i) for i in input().split()])
	if m == 1 or n == 1:
		if a1 == a2:
			print('YES')
		else:
			print('NO')
		continue
	e1 = {}
	o1 = {}
	e2 = {}
	o2 = {}
	for i in range(n):
		for j in range(m):
			if i + j & 1 == 0:
				if a1[i][j] in e1:
					e1[a1[i][j]] += 1
				else:
					e1[a1[i][j]] = 1
				if a2[i][j] in e2:
					e2[a2[i][j]] += 1
				else:
					e2[a2[i][j]] = 1
			else:
				if a1[i][j] in o1:
					o1[a1[i][j]] += 1
				else:
					o1[a1[i][j]] = 1
				if a2[i][j] in o2:
					o2[a2[i][j]] += 1
				else:
					o2[a2[i][j]] = 1
	if e1 == e2 and o1 == o2:
		print('YES')
	else:
		print('NO')


def solve():
	(n, m) = list(map(int, input().split()))
	(arr1, arr2) = ([], [])
	for i in range(n):
		row = list(map(int, input().split()))
		arr1.append(row)
	for i in range(n):
		row = list(map(int, input().split()))
		arr2.append(row)
	if n == 1 or m == 1:
		for i in range(n):
			for j in range(m):
				if arr1[i][j] != arr2[i][j]:
					return 'NO'
	(a1, b1, a2, b2) = ({}, {}, {}, {})
	for i in range(n):
		for j in range(m):
			if (i + j) % 2:
				if arr1[i][j] not in a1:
					a1[arr1[i][j]] = 0
				a1[arr1[i][j]] += 1
				if arr2[i][j] not in b1:
					b1[arr2[i][j]] = 0
				b1[arr2[i][j]] += 1
			else:
				if arr1[i][j] not in a2:
					a2[arr1[i][j]] = 0
				a2[arr1[i][j]] += 1
				if arr2[i][j] not in b2:
					b2[arr2[i][j]] = 0
				b2[arr2[i][j]] += 1
	for i in a1:
		if i not in b1 or a1[i] != b1[i]:
			return 'NO'
	for i in a2:
		if i not in b2 or a2[i] != b2[i]:
			return 'NO'
	return 'YES'
for _ in range(int(input())):
	print(solve())


t = int(input())
for _ in range(t):
	(n, m) = [int(x) for x in input().split()]
	A = [0] * n
	B = [0] * n
	for i in range(n):
		b = [int(x) for x in input().split()]
		A[i] = b
	for i in range(n):
		b = [int(x) for x in input().split()]
		B[i] = b
	flag = True
	if n == 1 or m == 1:
		for i in range(n):
			for j in range(m):
				if A[i][j] != B[i][j]:
					flag = False
		if flag == True:
			print('YES')
		else:
			print('NO')
		continue
	d1 = dict()
	d2 = dict()
	for i in range(n):
		for j in range(m):
			v1 = A[i][j]
			v2 = B[i][j]
			if d1.get(v1, -1) == -1:
				d1[v1] = [(i, j)]
			else:
				d1[v1].append((i, j))
			if d2.get(v2, -1) == -1:
				d2[v2] = [(i, j)]
			else:
				d2[v2].append((i, j))
	for val in d1:
		if flag == False:
			break
		inds1 = d1.get(val)
		l1 = len(inds1)
		inds2 = d2.get(val, -1)
		if inds2 == -1:
			flag = False
			break
		l2 = len(inds2)
		if l1 != l2:
			flag = False
			break
		samecount1 = 0
		samecount2 = 0
		for i in range(l1):
			ind1 = inds1[i]
			same = False
			if ind1[0] % 2 == 0:
				if ind1[1] % 2 == 0:
					same = True
			elif ind1[1] % 2 == 1:
				same = True
			if same == True:
				samecount1 += 1
		for i in range(l2):
			ind2 = inds2[i]
			same = False
			if ind2[0] % 2 == 0:
				if ind2[1] % 2 == 0:
					same = True
			elif ind2[1] % 2 == 1:
				same = True
			if same == True:
				samecount2 += 1
		if samecount1 != samecount2:
			flag = False
			break
	if flag == True:
		print('YES')
	else:
		print('NO')


def getoe(mat):
	o = {}
	e = {}
	for i in range(len(mat)):
		for j in range(len(mat[i])):
			if (i + j) % 2 == 0:
				if mat[i][j] in e:
					e[mat[i][j]] += 1
				else:
					e[mat[i][j]] = 1
			elif mat[i][j] in o:
				o[mat[i][j]] += 1
			else:
				o[mat[i][j]] = 1
	return [o, e]

def checkoe(A, B):
	for v in A:
		if not v in B:
			return False
		if A[v] != B[v]:
			return False
	return True

def checkOneOne(A, B):
	for i in range(n):
		for j in range(m):
			if A[i][j] != B[i][j]:
				return False
	return True
for _ in range(int(input())):
	[n, m] = list(map(int, input().split()))
	A = []
	B = []
	for i in range(n):
		A.append(list(map(int, input().split())))
	for i in range(n):
		B.append(list(map(int, input().split())))
	if n == 1 or m == 1:
		ans = checkOneOne(A, B)
	else:
		[Ao, Ae] = getoe(A)
		[Bo, Be] = getoe(B)
		ans = checkoe(Ao, Bo) and checkoe(Ae, Be)
	if ans:
		print('YES')
	else:
		print('NO')


def solve():
	(n, m) = map(int, input().split())
	if n == 1 or m == 1:
		A = []
		B = []
		for i in range(n):
			A.append(list(map(int, input().split()))[:m])
		for i in range(n):
			B.append(list(map(int, input().split()))[:m])
		for i in range(n):
			for j in range(m):
				if A[i][j] != B[i][j]:
					return 'NO'
	else:
		(A1, A2, B1, B2) = ({}, {}, {}, {})
		for i in range(n):
			num = list(map(int, input().split(' ')))[:m]
			for j in range(m):
				if i + j & 1 == 0:
					if num[j] not in A2.keys():
						A2[num[j]] = 1
					else:
						A2[num[j]] += 1
				elif num[j] not in A1.keys():
					A1[num[j]] = 1
				else:
					A1[num[j]] += 1
		for i in range(n):
			num = list(map(int, input().split(' ')))[:m]
			for j in range(m):
				if i + j & 1 == 0:
					if num[j] not in B2.keys():
						B2[num[j]] = 1
					else:
						B2[num[j]] += 1
				elif num[j] not in B1.keys():
					B1[num[j]] = 1
				else:
					B1[num[j]] += 1
		for (key, val) in A1.items():
			if key in B1.keys():
				if val != B1[key]:
					return 'NO'
			else:
				return 'NO'
		for (key, val) in A2.items():
			if key in B2.keys():
				if val != B2[key]:
					return 'NO'
			else:
				return 'NO'
	return 'YES'
test = int(input())
for i in range(test):
	print(solve())


def check(n, m):
	(l1, l2) = ([], [])
	for i in range(n):
		a = list(map(int, input().split()))
		for j in range(m):
			if (i + j) % 2 == 0:
				l1.append(a[j])
			else:
				l2.append(a[j])
	l1.sort()
	l2.sort()
	return (l1, l2)
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1 or m == 1:
		(l1, l2) = ([], [])
		for i in range(n):
			a = list(map(int, input().split()))
			for j in range(m):
				l1.append(a[j])
		for i in range(n):
			a = list(map(int, input().split()))
			for j in range(m):
				l2.append(a[j])
		if l1 == l2:
			print('YES')
		else:
			print('NO')
	else:
		(lst1, lst2) = check(n, m)
		(lst3, lst4) = check(n, m)
		if lst1 == lst3 and lst2 == lst4:
			print('YES')
		else:
			print('NO')


import collections
for i in range(int(input())):
	(n, m) = [int(x) for x in input().split()]
	N = []
	M = []
	for i in range(n):
		N.append([int(x) for x in input().split()])
	for i in range(n):
		M.append([int(x) for x in input().split()])
	if n == 1 or m == 1:
		if N != M:
			print('NO')
		else:
			print('YES')
	else:
		d = {'even': [], 'odd': []}
		D = {'even': [], 'odd': []}
		for j in range(n):
			for k in range(m):
				if (j + k) % 2 == 0:
					d['even'].append(N[j][k])
					D['even'].append(M[j][k])
				else:
					d['odd'].append(N[j][k])
					D['odd'].append(M[j][k])
		if collections.Counter(d['even']) == collections.Counter(D['even']):
			if collections.Counter(d['odd']) == collections.Counter(D['odd']):
				print('YES')
			else:
				print('NO')
		else:
			print('NO')


from collections import defaultdict
t = int(input())
while t:
	(n, m) = map(int, input().split())
	a = []
	for i in range(n):
		_temp = list(map(int, input().split()))
		a.append(_temp)
	b = []
	for i in range(n):
		_temp = list(map(int, input().split()))
		b.append(_temp)
	if not (n == 1 or m == 1):
		diag_counter_a = {0: defaultdict(int), 1: defaultdict(int)}
		diag_counter_b = {0: defaultdict(int), 1: defaultdict(int)}
		for i in range(n):
			for j in range(m):
				diag_counter_a[(i + j) % 2][a[i][j]] += 1
				diag_counter_b[(i + j) % 2][b[i][j]] += 1
		if diag_counter_a == diag_counter_b:
			print('YES')
		else:
			print('NO')
	elif a == b:
		print('YES')
	else:
		print('NO')
	t -= 1


t = int(input())
while t > 0:
	t -= 1
	(n, m) = map(int, input().split())
	if n == 1 or m == 1:
		b = True
		a1 = []
		a2 = []
		for i in range(n):
			a1.append(list(map(int, input().split())))
		for i in range(n):
			a2.append(list(map(int, input().split())))
		for i in range(n):
			for j in range(m):
				if a1[i][j] != a2[i][j]:
					b = False
		if b:
			print('YES')
		else:
			print('NO')
		continue
	else:
		num = 0
		e1 = {}
		o1 = {}
		o2 = {}
		e2 = {}
		a1 = []
		a2 = []
		for i in range(n):
			a1.append(list(map(int, input().split())))
		for i in range(n):
			a2.append(list(map(int, input().split())))
		for i in range(n):
			for j in range(m):
				num = a1[i][j]
				if i + j & 1 == 0:
					e1[num] = e1.get(num, 0) + 1
				else:
					o1[num] = o1.get(num, 0) + 1
		for i in range(n):
			for j in range(m):
				num = a2[i][j]
				if i + j & 1 == 0:
					e2[num] = e2.get(num, 0) + 1
				else:
					o2[num] = o2.get(num, 0) + 1
		same = True
		for (i, j) in e1.items():
			if j != e2.get(i, 0):
				same = False
				break
		for (i, j) in o1.items():
			if j != o2.get(i, 0):
				same = False
				break
		if same:
			print('YES')
		else:
			print('NO')


def angledflip():
	(n, m) = map(int, input().split())
	(a, b, c, d) = ([], [], [], [])
	if n == 1 or m == 1:
		for i in range(n):
			t1 = list(map(int, input().split()))
			a.append(t1)
		for i in range(n):
			t1 = list(map(int, input().split()))
			b.append(t1)
		if a == b:
			return 'YES'
		else:
			return 'NO'
	for i in range(n):
		t1 = list(map(int, input().split()))
		for j in range(len(t1)):
			if (i ^ j) % 2:
				a.append(t1[j])
			else:
				b.append(t1[j])
	for i in range(n):
		t2 = list(map(int, input().split()))
		for j in range(len(t2)):
			if (i ^ j) % 2:
				c.append(t2[j])
			else:
				d.append(t2[j])
	a.sort()
	b.sort()
	c.sort()
	d.sort()
	if a == c and b == d:
		return 'YES'
	else:
		return 'NO'
t = int(input())
for _ in range(t):
	print(angledflip())


for _ in range(int(input())):
	try:
		(a, b, c, d) = ([], [], [], [])
		(n, m) = list(map(int, input().split()))
		if n == 1 or m == 1:
			for i in range(n):
				temp = list(map(int, input().split()))
				for j in range(m):
					a.append(temp[j])
			for i in range(n):
				temp = list(map(int, input().split()))
				for j in range(m):
					b.append(temp[j])
			if a == b:
				print('YES')
			else:
				print('NO')
			continue
		for i in range(n):
			temp = list(map(int, input().split()))
			for j in range(m):
				if (i ^ j) % 2:
					a.append(temp[j])
				else:
					b.append(temp[j])
		for i in range(n):
			temp = list(map(int, input().split()))
			for j in range(m):
				if (i ^ j) % 2:
					c.append(temp[j])
				else:
					d.append(temp[j])
		a.sort()
		b.sort()
		c.sort()
		d.sort()
		if a == c and b == d:
			print('YES')
		else:
			print('NO')
	except:
		continue


def is_arr_equal(arr1, arr2):
	if len(arr1) != len(arr2):
		return False
	count = {}
	for i in arr1:
		if i in count:
			count[i] += 1
		else:
			count[i] = 1
	for i in arr2:
		if i not in count or count[i] == 0:
			return False
		else:
			count[i] -= 1
	return True
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	a = []
	b = []
	for i in range(n):
		arr = list(map(int, input().split()))
		a.append(arr)
	for i in range(n):
		arr = list(map(int, input().split()))
		b.append(arr)
	fl = 0
	evenarr = []
	oddarr = []
	evenarr2 = []
	oddarr2 = []
	if n == 1 or m == 1:
		fl = 0
		for i in range(n):
			for j in range(m):
				if a[i][j] != b[i][j]:
					fl = 1
					break
			if fl == 1:
				break
		if fl == 1:
			print('NO')
		else:
			print('YES')
		continue
	for i in range(n):
		for j in range(m):
			if (i + j) % 2 == 0:
				evenarr.append(a[i][j])
				evenarr2.append(b[i][j])
			else:
				oddarr.append(a[i][j])
				oddarr2.append(b[i][j])
	if is_arr_equal(evenarr, evenarr2) and is_arr_equal(oddarr, oddarr2):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n_m = [int(i) for i in input().split(' ')]
	a = []
	b = []
	for i in range(n_m[0]):
		a.append([int(i) for i in input().split(' ')])
	for i in range(n_m[0]):
		b.append([int(i) for i in input().split(' ')])
	list_a_1 = []
	list_a_2 = []
	list_b_1 = []
	list_b_2 = []
	for x in range(n_m[0]):
		for y in range(n_m[1]):
			if (x + y) % 2 == 0:
				list_a_1.append(a[x][y])
				list_b_1.append(b[x][y])
			elif (x + y) % 2 == 1:
				list_a_2.append(a[x][y])
				list_b_2.append(b[x][y])
	list_a_1 = sorted(list_a_1)
	list_b_1 = sorted(list_b_1)
	list_a_2 = sorted(list_a_2)
	list_b_2 = sorted(list_b_2)
	if n_m[0] == 1 or n_m[1] == 1:
		if a == b:
			print('YES')
		else:
			print('NO')
	elif list_a_1 == list_b_1 and list_a_2 == list_b_2:
		print('YES')
	else:
		print('NO')


def res(n, m):
	if n == 1 or m == 1:
		a = [0] * n
		b = [0] * n
		for i in range(n):
			if m == 1:
				a[i] = [int(input())]
			else:
				a[i] = list(map(int, input().split()))
		for i in range(n):
			if m == 1:
				b[i] = [int(input())]
			else:
				b[i] = list(map(int, input().split()))
		for i in range(n):
			for j in range(m):
				if a[i][j] != b[i][j]:
					return 'NO'
	else:
		d1 = {}
		d2 = {}
		d3 = {}
		d4 = {}
		for i in range(n):
			num2 = list(map(int, input().split()))
			for j in range(m):
				num = num2[j]
				if (i + j) % 2 == 0:
					if num in d1:
						d1[num] += 1
					else:
						d1[num] = 1
				elif num in d2:
					d2[num] += 1
				else:
					d2[num] = 1
		for i in range(n):
			num2 = list(map(int, input().split()))
			for j in range(m):
				num = num2[j]
				if (i + j) % 2 == 0:
					if num in d3:
						d3[num] += 1
					else:
						d3[num] = 1
				elif num in d4:
					d4[num] += 1
				else:
					d4[num] = 1
		for it in list(d1.keys()):
			if it not in d3:
				return 'NO'
		for it in list(d2.keys()):
			if it not in d4:
				return 'NO'
	return 'YES'
T = int(input())
for i in range(T):
	(n, m) = map(int, input().split())
	print(res(n, m))


T = int(input(''))
for i in range(T):
	str_in = input('')
	l1 = [int(c) for c in str_in.split(' ')]
	if l1[0] == 1:
		str_in_1 = input('')
		str_in_2 = input('')
		if str_in_1 == str_in_2:
			print('YES')
		else:
			print('NO')
	elif l1[1] == 1:
		first = []
		second = []
		for j in range(l1[0]):
			l = int(input(''))
			first.append(l)
		for j in range(l1[0]):
			l = int(input(''))
			second.append(l)
		if first == second:
			print('YES')
		else:
			print('NO')
	else:
		evens_1 = []
		odds_1 = []
		evens_2 = []
		odds_2 = []
		for j in range(l1[0]):
			str_in = input('')
			l2 = [int(c) for c in str_in.split(' ')]
			for k in range(len(l2)):
				if j % 2 == 0:
					if k % 2 == 0:
						odds_1.append(l2[k])
					else:
						evens_1.append(l2[k])
				elif k % 2 == 1:
					odds_1.append(l2[k])
				else:
					evens_1.append(l2[k])
		for x in range(l1[0]):
			str_in = input('')
			l2 = [int(c) for c in str_in.split(' ')]
			for y in range(len(l2)):
				if x % 2 == 0:
					if y % 2 == 0:
						odds_2.append(l2[y])
					else:
						evens_2.append(l2[y])
				elif y % 2 == 1:
					odds_2.append(l2[y])
				else:
					evens_2.append(l2[y])
		odds_1.sort()
		odds_2.sort()
		evens_1.sort()
		evens_2.sort()
		if odds_1 == odds_2 and evens_2 == evens_1:
			print('YES')
		else:
			print('NO')


t = int(input())

def makeArr(grid, n, m):
	odd = []
	even = []
	for i in range(0, n):
		for j in range(0, m):
			if (i + j) % 2 == 1:
				odd.append(grid[i][j])
			else:
				even.append(grid[i][j])
	return [sorted(odd), sorted(even)]
while t != 0:
	(n, m) = map(int, input().split())
	grid1 = []
	grid2 = []
	for i in range(0, n):
		row = list(map(int, input().split()))
		grid1.append(row)
	for i in range(0, n):
		row = list(map(int, input().split()))
		grid2.append(row)
	if m == 1 or n == 1:
		if grid1 == grid2:
			print('YES')
		else:
			print('NO')
	else:
		arr1 = makeArr(grid1, n, m)
		arr2 = makeArr(grid2, n, m)
		if arr1[0] == arr2[0] and arr1[1] == arr2[1]:
			print('YES')
		else:
			print('NO')
	t = t - 1


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	arr1 = []
	arr2 = []
	arr3 = []
	arr4 = []
	for i in range(n):
		ele = list(map(int, input().split()))
		arr1.append(ele)
		arr2.extend(ele)
	for j in range(n):
		ele = list(map(int, input().split()))
		arr3.append(ele)
		arr4.extend(ele)
	arr2.sort()
	arr4.sort()
	if n == 1 or m == 1:
		print('NO')
	elif arr2 == arr4:
		if n != m:
			print('YES')
		else:
			arr_dia1 = []
			arr_non_dia1 = []
			arr_dia2 = []
			arr_non_dia2 = []
			for i in range(n):
				arr_dia1.append(arr1[i][i])
				arr_non_dia1.append(arr1[i][-i - 1])
				arr_dia2.append(arr3[i][i])
				arr_non_dia2.append(arr3[i][-i - 1])
			arr_dia1.sort()
			arr_non_dia1.sort()
			arr_dia2.sort()
			arr_non_dia2.sort()
			if arr_dia1 == arr_dia2 and arr_non_dia1 == arr_non_dia2:
				print('YES')
			elif n % 2 == 0:
				print('YES')
			else:
				print('NO')
	else:
		print('NO')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	(a, b) = ([], [])
	for i in range(n):
		a.append([int(x) for x in input().split()])
	for i in range(n):
		b.append([int(x) for x in input().split()])
	if n == 1 or m == 1:
		if a == b:
			print('YES')
		else:
			print('NO')
		continue
	(adia, anod, bdia, bnod) = ([], [], [], [])
	for i in range(n):
		for j in range(m):
			if (i ^ j) & 1:
				adia.append(a[i][j])
				bdia.append(b[i][j])
			else:
				anod.append(a[i][j])
				bnod.append(b[i][j])
	adia.sort()
	bdia.sort()
	if adia == bdia:
		anod.sort()
		bnod.sort()
		if anod == bnod:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


def solve(arra, arrb, n, m):
	if n == 1 or m == 1:
		for i in range(n):
			for j in range(m):
				if arra[i][j] != arrb[i][j]:
					return False
	else:
		insta = {}
		for i in range(n):
			for j in range(m):
				if arra[i][j] not in insta:
					insta[arra[i][j]] = 1
				else:
					insta[arra[i][j]] += 1
		instb = {}
		for i in range(n):
			for j in range(m):
				if arrb[i][j] not in instb:
					instb[arrb[i][j]] = 1
				else:
					instb[arrb[i][j]] += 1
		for key in insta:
			if key not in instb:
				return False
			elif insta[key] != instb[key]:
				return False
		for key in instb:
			if key not in insta:
				return False
			elif instb[key] != insta[key]:
				return False
	return True
case = int(input())
for _ in range(case):
	(n, m) = map(int, input().split(' '))
	arra = []
	for i in range(n):
		arra.append(list(map(int, input().split(' '))))
	arrb = []
	for i in range(n):
		arrb.append(list(map(int, input().split(' '))))
	ans = solve(arra, arrb, n, m)
	if ans:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, m) = [int(x) for x in input().split()]
	a = []
	b = []
	if m == 1 or n == 1:
		a_int = []
		b_int = []
		for i in range(n):
			a_int = [int(x) for x in input().split()]
			a.append(a_int)
		for i in range(n):
			b_int = [int(x) for x in input().split()]
			b.append(b_int)
		if a == b:
			print('YES')
		else:
			print('NO')
	else:
		a_even = []
		b_even = []
		a_odd = []
		b_odd = []
		a_int = []
		b_int = []
		for i in range(n):
			a_int = [int(x) for x in input().split()]
			for j in range(len(a_int)):
				if (i + j) % 2 == 0:
					a_even.append(a_int[j])
				else:
					a_odd.append(a_int[j])
		for i in range(n):
			b_int = [int(x) for x in input().split()]
			for j in range(len(b_int)):
				if (i + j) % 2 == 0:
					b_even.append(b_int[j])
				else:
					b_odd.append(b_int[j])
		a_even.sort()
		b_even.sort()
		a_odd.sort()
		b_odd.sort()
		if a_even == b_even and a_odd == b_odd:
			print('YES')
		else:
			print('NO')


def flip_mat():
	(n, m) = map(int, input().split())
	flag = True
	if n == 1 or m == 1:
		arr = []
		brr = []
		for i in range(n):
			temp = list(map(int, input().split()))
			arr.append(temp)
		for i in range(n):
			temp = list(map(int, input().split()))
			brr.append(temp)
		if arr != brr:
			flag = False
	else:
		d1 = {}
		d2 = {}
		d3 = {}
		d4 = {}
		for i in range(n):
			temp = list(map(int, input().split()))
			for k in range(m):
				element = temp.pop(0)
				if (i + k) % 2 != 0:
					if element not in d1:
						d1[element] = 1
					else:
						d1[element] += 1
				elif element not in d2:
					d2[element] = 1
				else:
					d2[element] += 1
		for i in range(n):
			temp = list(map(int, input().split()))
			for k in range(m):
				element = temp.pop(0)
				if (i + k) % 2 != 0:
					if element not in d3:
						d3[element] = 1
					else:
						d3[element] += 1
				elif element not in d4:
					d4[element] = 1
				else:
					d4[element] += 1
		if d1 != d3 or d2 != d4:
			flag = False
	if flag:
		print('YES')
	else:
		print('NO')
for _ in range(int(input())):
	flip_mat()


import functools
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1 or m == 1:
		a = []
		b = []
		for i in range(n):
			a.append(list(map(int, input().split())))
		for i in range(n):
			b.append(list(map(int, input().split())))
		if a == b:
			print('YES')
		else:
			print('NO')
	else:
		a1 = []
		b1 = []
		for i in range(n):
			a1 += list(map(int, input().split()))
		for j in range(n):
			b1 += list(map(int, input().split()))
		a = []
		b = []
		c = []
		d = []
		z = 0
		while z < n * m:
			for i in range(n):
				for j in range(m):
					if (i ^ j) % 2:
						a.append(a1[z])
						z += 1
					else:
						b.append(a1[z])
						z += 1
		z1 = 0
		while z1 < n * m:
			for i in range(n):
				for j in range(m):
					if (i ^ j) % 2:
						c.append(b1[z1])
						z1 += 1
					else:
						d.append(b1[z1])
						z1 += 1
		a.sort()
		b.sort()
		c.sort()
		d.sort()
		if a == c and b == d:
			print('YES')
		else:
			print('NO')


t = int(input())
while t > 0:
	t = t - 1
	(x, y) = map(int, input().split())
	a = []
	for i in range(0, x):
		a.append([int(j) for j in input().split()])
	b = []
	for i in range(0, x):
		b.append([int(j) for j in input().split()])
	oddA = []
	evenA = []
	evenB = []
	oddB = []
	if x == 1 or y == 1:
		if a == b:
			print('YES')
		else:
			print('NO')
	else:
		for i in range(x):
			for j in range(y):
				if (i + j) % 2 == 0:
					evenA.append(a[i][j])
				else:
					oddA.append(a[i][j])
		for i in range(x):
			for j in range(y):
				if (i + j) % 2 == 0:
					evenB.append(b[i][j])
				else:
					oddB.append(b[i][j])
		evenA = sorted(evenA)
		evenB = sorted(evenB)
		oddA = sorted(oddA)
		oddB = sorted(oddB)
		if evenA == evenB and oddA == oddB:
			res = 'YES'
		else:
			res = 'NO'
		print(res)


from collections import Counter
import numpy as np
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	(mat1, mat2) = ([], [])
	for i in range(n):
		mat1.append(list(map(int, input().split())))
	for i in range(n):
		mat2.append(list(map(int, input().split())))
	if n == 1 or m == 1:
		if mat1 == mat2:
			print('YES')
		else:
			print('NO')
	else:
		(x1, x2, y1, y2) = ([], [], [], [])
		for i in range(n):
			for j in range(m):
				if i % 2 == j % 2:
					x1.append(mat1[i][j])
					y1.append(mat2[i][j])
				else:
					x2.append(mat1[i][j])
					y2.append(mat2[i][j])
		(x1, x2, y1, y2) = (Counter(x1), Counter(x2), Counter(y1), Counter(y2))
		if x1 == y1 and x2 == y2:
			print('YES')
		else:
			print('NO')


for i in range(int(input())):
	(n, m) = map(int, input().split())
	a = []
	b = []
	c = []
	d = []
	if n == 1 or m == 1:
		arr = [list(map(int, input().split())) for i in range(n)]
		for i in range(n):
			for j in range(m):
				a.append(arr[i][j])
		brr = [list(map(int, input().split())) for i in range(n)]
		for i in range(n):
			for j in range(m):
				b.append(brr[i][j])
		if a == b:
			print('YES')
		else:
			print('NO')
	else:
		arr = [list(map(int, input().split())) for i in range(n)]
		for i in range(n):
			for j in range(m):
				if (i ^ j) % 2:
					a.append(arr[i][j])
				else:
					b.append(arr[i][j])
		brr = [list(map(int, input().split())) for i in range(n)]
		for i in range(n):
			for j in range(m):
				if (i ^ j) % 2:
					c.append(brr[i][j])
				else:
					d.append(brr[i][j])
		a.sort()
		b.sort()
		c.sort()
		d.sort()
		if a == c and b == d:
			print('YES')
		else:
			print('NO')


test_case = int(input())
output = []

def translate_input_small(r):
	matrix = []
	for i in range(r):
		line = input().split()
		line = list(map(int, line))
		matrix += line
	return matrix

def translate_input_large(r, c):
	odd_pattern = []
	even_pattern = []
	for i in range(r):
		line = input().split()
		line = list(map(int, line))
		if i % 2 == 0:
			odd_pattern += [line[i] for i in range(c) if i % 2 == 1]
			even_pattern += [line[i] for i in range(c) if i % 2 == 0]
		else:
			odd_pattern += [line[i] for i in range(c) if i % 2 == 0]
			even_pattern += [line[i] for i in range(c) if i % 2 == 1]
	return (sorted(even_pattern), sorted(odd_pattern))
for i in range(test_case):
	case = input().split()
	r = int(case[0])
	c = int(case[1])
	if r != 1 and c != 1:
		matrix1 = translate_input_large(r, c)
		matrix1_even = matrix1[0]
		matrix1_odd = matrix1[1]
		matrix2 = translate_input_large(r, c)
		matrix2_even = matrix2[0]
		matrix2_odd = matrix2[1]
		if matrix1_even == matrix2_even and matrix1_odd == matrix2_odd:
			print('YES')
		else:
			print('NO')
	else:
		matrix1 = translate_input_small(r)
		matrix2 = translate_input_small(r)
		matrix2.reverse()
		if matrix1 == matrix2:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	(N, M) = map(int, input().split())
	if N == 1 or M == 1:
		arr_1 = []
		arr_2 = []
		for i in range(N):
			v = list(map(int, input().split()))
			arr_1.extend(v)
		for i in range(N):
			v = list(map(int, input().split()))
			arr_2.extend(v)
		if arr_1 == arr_2:
			print('YES')
		else:
			print('NO')
	else:
		odd = []
		even = []
		for i in range(N):
			v = list(map(int, input().split()))
			for j in range(M):
				if (i + j) % 2 == 0:
					even.append(v[j])
				else:
					odd.append(v[j])
		even_1 = []
		odd_1 = []
		for i in range(N):
			v = list(map(int, input().split()))
			for j in range(M):
				if (i + j) % 2 == 0:
					even_1.append(v[j])
				else:
					odd_1.append(v[j])
		even.sort()
		even_1.sort()
		odd.sort()
		odd_1.sort()
		if even == even_1 and odd == odd_1:
			print('YES')
		else:
			print('NO')


t = int(input())
for i in range(t):
	(n, m) = [int(x) for x in input().split()]
	A = []
	for i in range(n):
		A.append([int(x) for x in input().split()])
	B = []
	for i in range(n):
		B.append([int(x) for x in input().split()])
	if n == 1 or m == 1:
		if A != B:
			print('NO')
		else:
			print('YES')
	else:
		a1 = []
		b1 = []
		a2 = []
		b2 = []
		for i in range(n):
			for j in range(m):
				if (i + j) % 2 == 0:
					a1.append(A[i][j])
					b1.append(B[i][j])
				else:
					a2.append(A[i][j])
					b2.append(B[i][j])
		a1.sort()
		a2.sort()
		b1.sort()
		b2.sort()
		if a1 == b1 and a2 == b2:
			print('YES')
		else:
			print('NO')


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	(a, b) = ([], [])
	for i in range(n):
		a.append(list(map(int, input().split())))
	for i in range(n):
		b.append(list(map(int, input().split())))
	if n == 1 or m == 1:
		if a != b:
			print('NO')
			continue
	(al1, al2, bl1, bl2) = ([], [], [], [])
	(io, jo) = (True, True)
	for i in range(n):
		jo = io
		for j in range(m):
			if jo:
				al1.append(a[i][j])
				bl1.append(b[i][j])
				jo = False
			else:
				al2.append(a[i][j])
				bl2.append(b[i][j])
				jo = True
		if io:
			io = False
		else:
			io = True
	al1.sort()
	al2.sort()
	bl1.sort()
	bl2.sort()
	ans = 'YES'
	if al1 != bl1 or al2 != bl2:
		ans = 'NO'
	print(ans)