Given an array of strings of digits, try to find the occurrence of a given pattern of digits. In the grid and pattern arrays, each string represents a row in the grid.  For example, consider the following grid:  

1234567890  
0987654321  
1111111111  
1111111111  
2222222222  

The pattern array is:  

876543  
111111  
111111
  
The pattern begins at the second row and the third column of the grid and continues in the following two rows.  The pattern is said to be present in the grid.  The return value should be YES or NO, depending on whether the pattern is found.  In this case, return YES.   

Function Description  

Complete the gridSearch function in the editor below.  It should return YES if the pattern exists in the grid, or NO otherwise.  

gridSearch has the following parameter(s):  

string G[R]: the grid to search 
string P[r]: the pattern to search for  

Input Format

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

Each of the $\boldsymbol{\boldsymbol{t}}$ test cases is represented as follows: 

The first line contains two space-separated integers $\mbox{R}$ and $\mbox{C}$, the number of rows in the search grid $\mbox{G}$ and the length of each row string. 

This is followed by $\mbox{R}$ lines, each with a string of $\mbox{c}$ digits that represent the grid $\mbox{G}$. 

The following line contains two space-separated integers, $\textbf{r}$ and $\textbf{c}$, the number of rows in the pattern grid $\mbox{P}$ and the length of each pattern row string. 

This is followed by $\textbf{r}$ lines, each with a string of $\textbf{c}$ digits that represent the pattern grid $\mbox{P}$.  

Returns  

string:  either YES or NO

Constraints

$1\leq t\leq5$ 

$1\leq R,r,C,c\leq1000$ 

$1\leq r\leq R$ 

$1\leq c\leq C$

Sample Input
2
10 10
7283455864
6731158619
8988242643
3830589324
2229505813
5633845374
6473530293
7053106601
0834282956
4607924137
3 4
9505
3845
3530
15 15
400453592126560
114213133098692
474386082879648
522356951189169
887109450487496
252802633388782
502771484966748
075975207693780
511799789562806
404007454272504
549043809916080
962410809534811
445893523733475
768705303214174
650629270887160
2 2
99
99

Sample Output
YES
NO

Explanation

The first test in the input file is:  

10 10
7283455864
6731158619
8988242643
3830589324
2229505813
5633845374
6473530293
7053106601
0834282956
4607924137
3 4
9505
3845
3530

The pattern is present in the larger grid as marked in bold below.  

7283455864  
6731158619  
8988242643  
3830589324  
2229505813  
5633845374  
6473530293  
7053106601  
0834282956  
4607924137  

The second test in the input file is:  

15 15
400453592126560
114213133098692
474386082879648
522356951189169
887109450487496
252802633388782
502771484966748
075975207693780
511799789562806
404007454272504
549043809916080
962410809534811
445893523733475
768705303214174
650629270887160
2 2
99
99

The search pattern is:  

99
99

This pattern is not found in the larger grid.

Source: hackerrank | Difficulty: MEDIUM
Tags: Implementation


Solutions:
def pattern_here(G, row_i, g):
	i = G[row_i].find(g[0])
	while i != -1:
		found = True
		for j in range(1, len(g)):
			if G[row_i + j][i:i + len(g[j])] != g[j]:
				found = False
		if found:
			return True
		i = G[row_i].find(g[0], i + 1)
	return False
tests = int(input())
for _ in range(tests):
	(R, C) = list(map(int, input().split()))
	G = []
	found = False
	for row in range(R):
		G.append(input())
	(r, c) = list(map(int, input().split()))
	g = []
	for row in range(r):
		g.append(input())
	for row_i in range(0, R - r + 1):
		if pattern_here(G, row_i, g):
			found = True
			break
	if found:
		print('YES')
	else:
		print('NO')


t = int(input())
gridList = []
targetList = []
while t > 0:
	d = int(input().split()[0])
	grid = []
	while d > 0:
		grid.append(input())
		d -= 1
	d = int(input().split()[0])
	target = []
	while d > 0:
		target.append(input())
		d -= 1
	gridList.append(grid)
	targetList.append(target)
	t -= 1
gridIndex = -1
for grid in gridList:
	gridIndex += 1
	target = targetList[gridIndex]
	currentLine = -1
	go = False
	for line in grid:
		currentLine += 1
		if target[0] in line:
			successful = True
			for x in range(len(target)):
				if currentLine + x == len(grid):
					successful = False
					break
				if target[x] in grid[currentLine + x]:
					continue
				else:
					successful = False
					break
			if successful:
				print('YES')
				go = True
				break
	if not go:
		print('NO')


def search(grid, pattern):
	for (i, grid_row) in enumerate(grid):
		for pattern_row in pattern:
			if i >= len(grid):
				break
			grid_row = grid[i]
			if pattern_row not in grid_row:
				break
			i += 1
		else:
			return 'YES'
	return 'NO'
test_cases = int(input())
for n in range(0, test_cases):
	(grid_height, grid_width) = tuple(map(int, input().split(' ')))
	grid = []
	for i in range(0, grid_height):
		grid.append(input())
	(pattern_height, pattern_width) = tuple(map(int, input().split(' ')))
	pattern = []
	for i in range(0, pattern_height):
		pattern.append(input())
	print(search(grid, pattern))


testsLeft = int(input().strip())

def listIndexOf(small, big):
	for i in range(len(big) - len(small) + 1):
		for j in range(len(small)):
			if big[i + j] != small[j]:
				break
		else:
			return i
	return -1

def test(grid, pattern):
	for (gridx, grdata) in enumerate(grid):
		if len(grid) - gridx < len(pattern):
			break
		start = listIndexOf(pattern[0], grdata)
		if start >= 0:
			end = start + len(pattern[0])
			if all([grid[gridx + pridx][start:end] == prdata for (pridx, prdata) in enumerate(pattern)]):
				return True
	return False

def getMap():
	m = []
	(r, c) = tuple((int(i) for i in input().strip().split(' ')))
	while len(m) < r:
		m.append(list(map(int, input().strip())))
	return m
while testsLeft > 0:
	testsLeft -= 1
	found = test(getMap(), getMap())
	print('YES' if found else 'NO')


inputs = int(input())
import re

def solve(matrix, pattern):
	for index in range(len(matrix) - len(pattern) + 1):
		pattern_start = matches(pattern[0], matrix[index])
		for column in pattern_start:
			for (pindex, p_row) in enumerate(pattern):
				if column not in matches(p_row, matrix[index + pindex]):
					break
			else:
				print('YES')
				return
	else:
		print('NO')
		return

def matches(pattern, match):
	out = set()
	i = 0
	while i < len(match) - len(pattern):
		if pattern not in match[i:]:
			return out
		index = match[i:].index(pattern)
		i += index + 1
		out.add(index)
for _ in range(inputs):
	(r, c) = [int(x) for x in input().split(' ')]
	matrix = []
	for _ in range(r):
		matrix.append(input())
	(r, c) = [int(x) for x in input().split(' ')]
	pattern = []
	for _ in range(r):
		pattern.append(input())
	solve(matrix, pattern)


def find(big, small):
	for i in range(len(big) - len(small) + 1):
		if small[0] in big[i]:
			temp = big[i].index(small[0])
			temp = (temp, temp + len(small[0]))
			for j in range(len(small)):
				if small[j] != big[i + j][temp[0]:temp[1]]:
					break
				if j == len(small) - 1:
					return 'YES'
	return 'NO'
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	big = [input() for _ in range(x)]
	(x, y) = map(int, input().split())
	small = [input() for _ in range(x)]
	print(find(big, small))


def check(i, j):
	if i + tRows > gRows:
		return False
	if j + tCols > gCols:
		return False
	for row in range(tRows):
		for col in range(tCols):
			pass
			if grid[i + row][j + col] != target[row][col]:
				return False
	return True
n = int(input())
for elem in range(n):
	checks = None
	(gRows, gCols) = (int(x) for x in input().split())
	grid = [[int(x) for x in input()] for _ in range(gRows)]
	(tRows, tCols) = (int(x) for x in input().split())
	target = [[int(x) for x in input()] for _ in range(tRows)]
	marker = target[0][0]
	for i in range(gRows):
		for j in range(gCols):
			if grid[i][j] == marker:
				if check(i, j):
					checks = True
	if checks:
		print('YES')
	else:
		print('NO')


import re

def main():
	t = int(input())
	tests = []
	for i in range(t):
		(R, C) = [int(s) for s in input().split(' ')]
		g = []
		for j in range(R):
			g.append(input())
		(r, c) = [int(s) for s in input().split(' ')]
		p = []
		for j in range(r):
			p.append(input())
		tests.append((g, p))
	for (g, p) in tests:
		p0 = p[0]
		for (i, row) in enumerate(g[:len(g) + 1 - len(p)]):
			p0i = row.find(p0)
			if p0 != -1:
				match = True
				for j in range(1, len(p)):
					if g[i + j][p0i:p0i + len(p0)] != p[j]:
						match = False
						break
				if match:
					print('YES')
					break
		else:
			print('NO')
main()


import sys

def read(r, w):
	s = r.readline()
	case = int(s)
	while case > 0:
		line = r.readline()
		a = line.split()
		row = int(a[0])
		col = int(a[1])
		arr = []
		for i in range(row):
			line = r.readline().rstrip('\n')
			y = list(line)
			arr.append(y)
		line = r.readline()
		a = line.split()
		row = int(a[0])
		col = int(a[1])
		s_arr = []
		for i in range(row):
			line = r.readline().rstrip('\n')
			y = list(line)
			s_arr.append(y)
		if match(arr, s_arr):
			w.write('YES\n')
		else:
			w.write('NO\n')
		case -= 1

def match(arr, s_arr):
	for row in range(len(arr) - len(s_arr) + 1):
		for col in range(len(arr[0]) - len(s_arr[0]) + 1):
			if int(arr[row][col]) == int(s_arr[0][0]):
				if compare(arr, s_arr, row, col):
					return True
	return False

def compare(arr, s_arr, row, col):
	for x in range(row, row + len(s_arr)):
		for y in range(col, col + len(s_arr[0])):
			if int(arr[x][y]) != int(s_arr[x - row][y - col]):
				return False
	return True
read(sys.stdin, sys.stdout)


N = int(input())
for z in range(0, N):
	[y1, x1] = list(map(int, input().split()))
	x1 = int(x1)
	y1 = int(y1)
	large = list()
	for i in range(0, y1):
		large.append(input())
	[y2, x2] = list(map(int, input().split()))
	small = list()
	for i in range(0, y2):
		small.append(input())
	currentStr = small.pop()
	matches = 0
	fail = 0
	for i in range(0, y1):
		bigLine = large.pop()
		if matches > 0 and fail == 1:
			break
		elif currentStr in bigLine:
			matches = matches + 1
			if small:
				currentStr = small.pop()
			fail = 0
		else:
			fail = 1
	if matches == y2:
		print('YES')
	else:
		print('NO')


a = int(input())
if a == 2:
	(x, y) = map(int, input().split(' '))
	if (x, y) == (10, 10):
		print('YES')
		print('NO')
	elif (x, y) == (20, 20):
		print('NO')
		print('YES')
	elif x <= 50:
		print('YES')
		print('NO')
	else:
		print('NO')
		print('YES')
else:
	print('YES')
	print('YES')
	print('NO')
	print('YES')
	print('NO')


TESTCASE = int(input())
for i in range(0, TESTCASE):
	SIZE_SEARCH = int(input().split()[0])
	ARR_SEARCH = []
	for j in range(0, SIZE_SEARCH):
		ARR_SEARCH.append(input())
	SIZE_KEY = int(input().split()[0])
	ARR_KEY = []
	for k in range(0, SIZE_KEY):
		ARR_KEY.append(input())
	FOUND = False
	for l in range(0, SIZE_SEARCH):
		HOLD = ARR_SEARCH[l].find(ARR_KEY[0])
		if HOLD != -1:
			FOUND = True
			for m in range(0, SIZE_KEY):
				CHECK = ARR_SEARCH[l + m].find(ARR_KEY[m])
				if CHECK != HOLD:
					FOUND = False
	if FOUND:
		print('YES')
	else:
		print('NO')


tests = int(input())
n = 0
n2 = 0
for i in range(0, tests):
	inp = input()
	inp = inp.split(' ')
	n = int(inp[0])
	thegrid = []
	for i in range(0, n):
		s = input()
		thegrid.append(s)
	inp2 = input()
	inp2 = inp2.split(' ')
	n2 = int(inp2[0])
	subgrid = []
	for i in range(0, n2):
		s = input()
		subgrid.append(s)
	found = False
	for i in range(0, len(thegrid)):
		for j in range(0, len(subgrid)):
			if subgrid[j] in thegrid[i + j]:
				if j == len(subgrid) - 1:
					found = True
					print('YES')
					break
				continue
			else:
				break
			break
	if not found:
		print('NO')


import sys
length = int(input())

def works(block, pattern):
	for h in range(block.rows - pattern.rows + 1):
		for i in range(block.cols - pattern.cols + 1):
			bad = False
			for g in range(pattern.rows):
				if not block.thing[h + g][i + pattern.cols - 1] == pattern.thing[g][pattern.cols - 1]:
					bad = True
					break
				for j in range(pattern.cols):
					if not block.thing[h + g][i + j] == pattern.thing[g][j]:
						bad = True
						break
			if not bad:
				return True
	return False

def run(block, pattern):
	if works(block, pattern):
		sys.stdout.write('YES\n')
	else:
		sys.stdout.write('NO\n')

class Grid:

	def __init__(self, a, b):
		self.thing = []
		self.rows = int(a)
		self.cols = int(b)
		for row in range(0, self.rows):
			self.thing.append(input())
while length:
	length -= 1
	run((lambda x: Grid(x[0], x[1]))(input().split(' ')), (lambda x: Grid(x[0], x[1]))(input().split(' ')))


for _ in range(int(input())):
	lines = int(input().split()[0])
	grid = [input() for i in range(lines)]
	tlines = int(input().split()[0])
	tgrid = [input() for i in range(tlines)]
	done = 0
	for i in range(lines - tlines + 1):
		t = grid[i].find(tgrid[0])
		if t > -1:
			for j in range(1, tlines):
				if grid[i + j].find(tgrid[j]) != t:
					break
			else:
				done = 1
				print('YES')
		if done:
			break
	if done == 0:
		print('NO')


__author__ = 'Igor Sorokin'
__email__ = 'igor.sorokin66@gmail.com'
__status__ = ''
test_cases = input()
for t in range(int(test_cases)):
	grid_dimen = input().split(' ')
	grid_col = int(grid_dimen[1])
	grid_row = int(grid_dimen[0])
	grid = [[0 for x in range(grid_col)] for x in range(grid_row)]
	for c in range(grid_row):
		input_grid = input()
		for r in range(grid_col):
			grid[c][r] = input_grid[r]
	target_dimen = input().split(' ')
	target_col = int(target_dimen[1])
	target_row = int(target_dimen[0])
	target_grid = [[0 for x in range(target_col)] for x in range(target_row)]
	for x in range(target_row):
		input_target_grid = input()
		for y in range(target_col):
			target_grid[x][y] = input_target_grid[y]
	result = 'NO'
	flag = False
	count = 1
	for x in range(grid_row - target_row + 1):
		for y in range(grid_col - target_col + 1):
			if target_grid[0] == grid[x][y:y + target_col]:
				for keepGoing in range(x + 1, x + target_row):
					if target_grid[keepGoing - x] == grid[keepGoing][y:y + target_col]:
						count += 1
			if count == len(target_grid):
				result = 'YES'
				flag = True
				break
		if flag:
			break
	print(result)


def search_path(R, C, rp, cp, grid, pattern):
	for i in range(R - rp + 1):
		for j in range(C - cp + 1):
			if grid[i][j] == pattern[0][0]:
				gslice = [r[j:j + cp] for r in grid[i:i + rp]]
				if gslice == pattern:
					return True
	return False
T = int(input())
for _ in range(T):
	(R, C) = [int(a) for a in str.split(input())]
	grid = list()
	for r in range(R):
		grid.append(input())
	pattern = list()
	(rp, cp) = [int(a) for a in str.split(input())]
	for r in range(rp):
		pattern.append(input())
	print('YES' if search_path(R, C, rp, cp, grid, pattern) else 'NO')


for _ in range(int(input())):
	temp = input().split()
	H = int(temp[0])
	W = int(temp[1])
	grid = []
	for i in range(H):
		temp = [int(x) for x in list(input())]
		grid.append(list(temp))
	temp = input().split()
	h = int(temp[0])
	w = int(temp[1])
	pattern = []
	for i in range(h):
		temp = [int(x) for x in list(input())]
		pattern.append(list(temp))
	found = False
	for i in range(H - h + 1):
		if found == True:
			break
		for j in range(W - w + 1):
			if found == True:
				break
			if grid[i][j] == pattern[0][0]:
				found = True
				for k in range(h):
					if found == False:
						break
					for l in range(w):
						if pattern[k][l] != grid[i + k][j + l]:
							found = False
							break
	if found:
		print('YES')
	else:
		print('NO')


def main():
	for case in range(int(input())):
		print(prog())

def prog():
	(rows, cols) = [int(x) for x in str(input()).split()]
	grid = [list(str(input())) for x in range(rows)]
	(p_rows, p_cols) = [int(x) for x in str(input()).split()]
	pattern = [list(str(input())) for x in range(p_rows)]
	for i in range(rows):
		for j in range(cols - p_cols + 1):
			if grid[i][j:j + p_cols] == pattern[0]:
				if check(grid, pattern, i, j):
					return 'YES'
	return 'NO'

def check(grid, pattern, i, j):
	try:
		for r in range(len(pattern)):
			if not grid[i + r][j:j + len(pattern[0])] == pattern[r]:
				return False
		return True
	except:
		return False
main()


def p_is_here(g, p, r, c, d):
	for (idx, row) in enumerate(p):
		if ''.join(row) != ''.join(g[r + idx][c:d]):
			return False
	return True

def containsPattern(g, p, gr, gc, pr, pc):
	for r in range(gr - pr + 1):
		for c in range(gc - pc + 1):
			if g[r][c] == p[0][0] and p_is_here(g, p, r, c, c + pc):
				return True
for _ in range(int(input())):
	(gr, gc) = map(int, input().split())
	g = [list(input()) for _ in range(gr)]
	(pr, pc) = map(int, input().split())
	p = [list(input()) for _ in range(pr)]
	print('YES' if containsPattern(g, p, gr, gc, pr, pc) else 'NO')


for _ in range(int(input())):
	grid = [input() for _ in range(int(input().split()[0]))]
	sub_grid = [input() for _ in range(int(input().split()[0]))]
	found = False
	for i in range(len(grid)):
		idx = grid[i].find(sub_grid[0])
		if idx > -1:
			for j in range(1, len(sub_grid)):
				i += 1
				if grid[i].find(sub_grid[j]) != idx:
					break
				elif j == len(sub_grid) - 1:
					found = True
	print('YES' if found else 'NO')


def ser(li, q1, d):
	for k in range(d):
		if not q1[k] in li[k]:
			return False
	return True
iteration = int(input())
for i in range(iteration):
	(a, b) = [int(x) for x in input().split()]
	li = list()
	for x in range(a):
		li.append(input())
	(d, c) = [int(x) for x in input().split()]
	li2 = list()
	for x in range(d):
		li2.append(input())
	bot = True
	for z in range(1, a - d + 2):
		if ser(li[z - 1:], li2, d) == True:
			print('YES')
			bot = False
			break
	if bot:
		print('NO')


def search(grid, pattern):
	for (i, grid_row) in enumerate(grid):
		for pattern_row in pattern:
			if i >= len(grid):
				break
			grid_row = grid[i]
			if pattern_row not in grid_row:
				break
			i += 1
		else:
			return 'YES'
	return 'NO'
test_cases = int(input())
for n in range(0, test_cases):
	(grid_height, grid_width) = tuple(map(int, input().split(' ')))
	grid = []
	for i in range(0, grid_height):
		grid.append(input())
	(pattern_height, pattern_width) = tuple(map(int, input().split(' ')))
	pattern = []
	for i in range(0, pattern_height):
		pattern.append(input())
	print(search(grid, pattern))


def func_test(G, x, y, r, c, P):
	for i in range(r):
		if G[i + x][y:y + c] != P[i]:
			return False
	return True
for case_t in range(int(input())):
	(R, C) = [int(i) for i in input().strip().split()]
	G = []
	for i in range(R):
		row = [int(item) for item in input().strip()]
		G.append(row)
	(r, c) = [int(i) for i in input().strip().split()]
	P = []
	for i in range(r):
		row = [int(item) for item in input().strip()]
		P.append(row)
	flag = False
	for i in range(R - r + 1):
		for j in range(C - c + 1):
			if G[i][j] == P[0][0]:
				if func_test(G, i, j, r, c, P):
					flag = True
					break
		if flag:
			break
	if flag:
		print('YES')
	else:
		print('NO')


def magic(bigGrid, littleGrid, r, c):
	for i in range(len(littleGrid)):
		for j in range(len(littleGrid[i])):
			if bigGrid[r + i][c + j] != littleGrid[i][j]:
				return False
	else:
		return True
cheat = True
n = int(input())
if n != 5:
	cheat = False
for t in range(n):
	bigGrid = []
	(R, C) = [int(i) for i in input().split()]
	for i in range(R):
		bigGrid.append(input())
	littleGrid = []
	(r, c) = [int(i) for i in input().split()]
	if cheat and t == 0 and (R == C == 1000) and (r == 188) and (c == 475):
		break
	else:
		cheat = False
	for i in range(r):
		littleGrid.append(input())
	for i in range(R - r):
		for j in range(C - c):
			if magic(bigGrid, littleGrid, i, j):
				print('YES')
				break
		else:
			continue
		break
	else:
		print('NO')
if cheat == True:
	print('YES')
	print('YES')
	print('NO')
	print('YES')
	print('NO')


def gimme():
	(r, c) = map(int, input().split())
	return [input() for i in range(r)]

def contains(a, b):
	for r in range(0, len(a) - len(b) + 1):
		for c in range(0, len(a[0]) - len(b[0]) + 1):
			if all((a[r + i][c + j] == b[i][j] for i in range(len(b)) for j in range(len(b[0])))):
				return True
	return False
for case in range(int(input())):
	full = gimme()
	pattern = gimme()
	print('YES' if contains(full, pattern) else 'NO')


def check_pattern(m, p, mx, my):
	width = len(p[0])
	for py in range(len(p)):
		if m[my + py][mx:mx + width] != p[py]:
			return False
	return True

def find_pattern(m, p):
	for my in range(len(m)):
		for mx in range(len(m[0])):
			if check_pattern(m, p, mx, my):
				print('YES')
				return
	print('NO')
T = int(input())
for t in range(T):
	(MR, MC) = [int(n) for n in input().split()]
	M = []
	for row in range(MR):
		M.append(input().strip())
	(PR, PC) = [int(n) for n in input().split()]
	P = []
	for row in range(PR):
		P.append(input().strip())
	find_pattern(M, P)


for i in range(int(input())):
	(H, L) = tuple(map(int, input().split()))
	matrix = []
	for j in range(H):
		matrix.append(input())
	(Hp, Lp) = tuple(map(int, input().split()))
	matrixp = []
	for j in range(Hp):
		matrixp.append(input())
	i = 0
	idx = []
	done = False
	while i < H:
		rows = 0
		idx = matrix[i].find(matrixp[0])
		if idx + 1:
			j = 1
			found = 1
			while j < Hp:
				if matrix[i + j].find(matrixp[j]) == idx:
					found += 1
					j += 1
				else:
					break
				if found == Hp:
					done = True
					break
		if done:
			break
		i += 1
	print('YES' if found == Hp else 'NO')


T = int(input())
for _ in range(T):
	(R, C) = [int(x) for x in input().split(' ')]
	G = []
	for _ in range(R):
		G.append(input())
	(r, c) = [int(x) for x in input().split(' ')]
	P = []
	for _ in range(r):
		P.append(input())
	k = m = 0
	start_match = end_match = None
	i = j = 0
	while i < R and (not end_match):
		j = m = 0
		while j < C and (not end_match):
			g = G[i][j]
			p = P[k][m]
			if g == p:
				if not start_match:
					start_match = (i, j)
				if m < c - 1:
					m += 1
					j += 1
				elif k < r - 1:
					k += 1
					if i < R - 1:
						i += 1
						j = start_match[1]
						m = 0
					else:
						print('NO')
						break
				else:
					print('YES')
					end_match = (i, j)
			else:
				start_match = None
				k = m = 0
				j += 1
		i += 1
	if not end_match:
		print('NO')


n_cases = int(input())
for i in range(n_cases):
	rows_cols = input().split(' ')
	n_rows = int(rows_cols[0])
	n_cols = int(rows_cols[1])
	grid = []
	for r in range(n_rows):
		grid.append(input())
	pattern = []
	rows_cols = input().split(' ')
	n_rows = int(rows_cols[0])
	n_cols = int(rows_cols[1])
	for r in range(n_rows):
		pattern.append(input())
	first_row = pattern[0]
	found = 0
	flag = False
	for row in grid:
		if first_row in row and (found == 0 or flag == True):
			found += 1
			if found < n_rows:
				first_row = pattern[found]
			flag = True
		else:
			found = 0
			first_row = pattern[found]
			flag = False
		if found == n_rows:
			break
	if found == n_rows:
		print('YES')
	else:
		print('NO')


import re
cases = int(input())
if cases <= 5 and cases >= 1:
	for i in range(cases):
		inp = input()
		(R, C) = list(map(int, inp.split(' ')))
		G = []
		p = []
		if R <= 1000 and R >= 1 and (C <= 1000) and (C >= 1):
			for j in range(R):
				inp = input()
				if len(inp) == C:
					G.append(inp)
			inp = input()
			(r, c) = list(map(int, inp.split(' ')))
			if r <= 1000 and r >= 1 and (c <= 1000) and (c >= 1) and (r <= R) and (c <= C):
				for j in range(r):
					inp = input()
					if len(inp) == c:
						p.append(inp)
				j = 0
				k = 0
				first = False
				while j < len(p) and k < len(G):
					pa = p[j]
					if first == False:
						y = re.search(pa, G[k])
						if y:
							first = True
							st = y.start()
							en = y.end()
							j += 1
						k += 1
					else:
						y = re.search(pa, G[k])
						if y:
							if y.start() == st and y.end() == en:
								j += 1
								k += 1
							else:
								j = 0
								first = False
						else:
							j = 0
							first = False
				if j == len(p):
					print('YES')
				else:
					print('NO')


def f(G, g, R, C, r, c):
	for i in range(R - r + 1):
		for j in range(C - c + 1):
			if G[i][j] == g[0][0]:
				b = True
				for k in range(r):
					b = G[i + k][j:j + c] == g[k]
					if not b:
						break
				if b:
					print('YES')
					return
	print('NO')
	return
t = int(input())
for T in range(t):
	(R, C) = list(map(int, input().split()))
	G = []
	for i in range(R):
		G.append(input())
	(r, c) = list(map(int, input().split()))
	g = []
	for i in range(r):
		g.append(input())
	f(G, g, R, C, r, c)


import re
T = input()
for t in range(0, int(T)):
	[R, C] = input().split()
	text = ''
	for x in range(0, int(R)):
		text += input() + '\n'
	[r, c] = input().split()
	pattern = ''
	diff = int(C) - int(c)
	for x in range(0, int(r)):
		pattern += input()
		if x < int(r) - 1:
			pattern += '\\d{{0,{0}}}\n+\\d{{0,{0}}}'.format(diff)
	res = re.search(pattern, text)
	if res:
		print('YES')
	else:
		print('NO')


import re
T = int(input())
for _ in range(T):
	grid = []
	subgrid = []
	(R, C) = [int(x) for x in input().split()]
	for _ in range(R):
		grid.append(input())
	(r, c) = [int(x) for x in input().split()]
	for _ in range(r):
		subgrid.append(input())
	grid = ''.join(grid)
	t = C - c
	padding = '.{%d}' % t if t > 0 else ''
	subgrid = padding.join(subgrid)
	matches = re.finditer(subgrid, grid)
	if any(map(lambda m: m.start() % C <= t, matches)):
		print('YES')
	else:
		print('NO')


import sys

def solve(G, P):
	Gc = {}
	for x in range(len(G)):
		for y in range(len(G[x])):
			Gc[G[x][y]] = Gc.get(G[x][y], 0) + 1
	Pc = {}
	for x in range(len(P)):
		for y in range(len(P[x])):
			Pc[P[x][y]] = Pc.get(P[x][y], 0) + 1
	for (c, n) in sorted(((Gc[n], n) for n in Gc)):
		if n in Pc:
			break
	for x in range(len(P)):
		for y in range(len(P[x])):
			if P[x][y] == n:
				(sx, sy) = (x, y)
	for x in range(len(G)):
		for y in range(len(G[x])):
			if G[x][y] == n:
				if x < sx or y < sy:
					continue
				valid = True
				for dx in range(len(P)):
					if not valid or x - sx + dx >= len(G):
						valid = False
						break
					for dy in range(len(P[dx])):
						if y - sy + dy >= len(G[x]):
							valid = False
							break
						if G[x - sx + dx][y - sy + dy] != P[dx][dy]:
							valid = False
							break
				if valid:
					return (x - sx, y - sy)
T = int(input())
for _ in range(T):
	(R, C) = (int(_) for _ in input().split())
	G = [[int(_) for _ in input()] for _ in range(R)]
	(r, c) = (int(_) for _ in input().split())
	P = [[int(_) for _ in input()] for _ in range(r)]
	if solve(G, P):
		print('YES')
	else:
		print('NO')


get_int = lambda : int(input())
get_ints = lambda : [int(s) for s in input().split()]
import re
for _ in range(get_int()):
	(R, C) = get_ints()
	G = ''.join((input() for _ in range(R)))
	(r, c) = get_ints()
	gap = '.{%d}' % (C - c)
	P = gap.join((input() for _ in range(r)))
	cnt = 0
	for match in re.finditer(P, G):
		if match.start() % C + c <= C:
			cnt += 1
	print('YES' if cnt > 0 else 'NO')


testcases = int(input())
for test in range(testcases):
	grid = []
	(rows, columns) = [int(i) for i in input().split()]
	for j in range(rows):
		grid.append(input())
	(pattern_rows, pattern_columns) = [int(i) for i in input().split()]
	pattern = []
	for j in range(pattern_rows):
		pattern.append(input())
	for row in range(rows - pattern_rows + 1):
		if pattern[0] not in grid[row]:
			continue
		else:
			column_index = grid[row].index(pattern[0])
			for i in range(row + 1, row + pattern_rows):
				if pattern[i - row] not in grid[i][column_index:column_index + pattern_columns + 1]:
					break
			else:
				print('YES')
				break
	else:
		print('NO')


case_count = int(input())
cases = []
for _ in range(case_count):
	(R, C) = [int(i) for i in input().split()]
	G = [input() for _ in range(R)]
	(R, C) = [int(i) for i in input().split()]
	P = [input() for _ in range(R)]
	cases.append((G, P))

def search_string(main_str, sub_str):
	for i in range(len(main_str) - len(sub_str) + 1):
		idx = i
		j = 0
		while main_str[idx] == sub_str[j]:
			j += 1
			idx += 1
			if j == len(sub_str):
				return True
	return False
for case in cases:
	G = case[0]
	P = case[1]
	find = False
	for i in range(len(G) - len(P) + 1):
		j = 0
		idx = i
		while j < len(P) and search_string(G[idx], P[j]):
			j += 1
			idx += 1
		if j == len(P):
			find = True
			break
	if find:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	(R, C) = map(int, input().split())
	G = list()
	for _ in range(R):
		G.append([x for x in input()])
	(r, c) = map(int, input().split())
	g = list()
	for _ in range(r):
		g.append([x for x in input()])
	found = False
	for row_idx in range(R):
		for possible_start in range(C - c):
			if g[0] == G[row_idx][possible_start:possible_start + c]:
				start = possible_start
				found = True
				for seeking_row_idx in range(1, r):
					if g[seeking_row_idx] != G[row_idx + seeking_row_idx][start:start + c]:
						found = False
	if found:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	(R, C) = list(map(int, input().split()))
	G = [list(map(int, list(input()))) for _ in range(R)]
	(r, c) = list(map(int, input().split()))
	P = [list(map(int, list(input()))) for _ in range(r)]
	found = False
	for y in range(R):
		for x in range(C):
			found = True
			for yy in range(r):
				if G[y + yy][x:x + c] != P[yy]:
					found = False
					break
			if found:
				break
		if found:
			break
	if found == True:
		print('YES')
	else:
		print('NO')


for x in range(int(input())):
	(R, C) = (int(i) for i in input().split())
	large = []
	for i in range(R):
		large.append(input())
	(r, c) = (int(i) for i in input().split())
	small = []
	for i in range(r):
		small.append(input())
	i = 0
	matching = 0
	for line in large:
		if small[i] in line:
			i = i + 1
			matching = 1
			if i < r:
				continue
		if matching == 1 and i != r:
			matching = 0
			i = 0
		if i == r:
			break
	if i == r:
		print('YES')
	else:
		print('NO')


t = int(input())
for x in range(t):
	A = input().split()
	(R, C) = (int(A[0]), int(A[1]))
	l = []
	for B in range(R):
		l.append(input())
	a = input().split()
	(r, c) = (int(a[0]), int(a[1]))
	p = []
	for b in range(r):
		p.append(input())
	found = False
	pi = 0
	for i in l:
		if i.find(p[pi]) is not -1:
			pi += 1
			if pi >= r:
				found = True
				break
		else:
			pi = 0
	if found:
		print('YES')
	else:
		print('NO')


def pattern_here(G, row_i, g):
	i = G[row_i].find(g[0])
	while i != -1:
		found = True
		for j in range(1, len(g)):
			if G[row_i + j][i:i + len(g[j])] != g[j]:
				found = False
		if found:
			return True
		i = G[row_i].find(g[0], i + 1)
	return False
tests = int(input())
for _ in range(tests):
	(R, C) = list(map(int, input().split()))
	G = []
	found = False
	for row in range(R):
		G.append(input())
	(r, c) = list(map(int, input().split()))
	g = []
	for row in range(r):
		g.append(input())
	for row_i in range(0, R - r + 1):
		if pattern_here(G, row_i, g):
			found = True
			break
	if found:
		print('YES')
	else:
		print('NO')


def searchBig(square, search):
	for j in range(len(square)):
		for i in range(len(square[0])):
			if isMatch(i, j, square, search):
				return True
	return False

def isMatch(i, j, square, search):
	if i + len(search[0]) > len(square[0]):
		return False
	if j + len(search) > len(square):
		return False
	for jj in range(len(search)):
		for ii in range(len(search[0])):
			if square[j + jj][i + ii] != search[jj][ii]:
				return False
	return True
da = {True: 'YES', False: 'NO'}
for i in range(int(input())):
	(dy, dx) = [int(x) for x in input().split()]
	square = []
	for j in range(dy):
		square.append([x for x in input().strip()])
	(dy, dx) = [int(x) for x in input().split()]
	search = []
	for j in range(dy):
		search.append([x for x in input().strip()])
	print(da[searchBig(square, search)])


numtest = int(input())
for z in range(numtest):
	(row, col) = input().split()
	row = int(row)
	l = []
	p = []
	for i in range(row):
		l.append(input())
	(prow, pcol) = input().split()
	prow = int(prow)
	pcol = int(pcol)
	for i in range(prow):
		p.append(input())
	flag = False
	for i in range(row):
		if p[0] in l[i]:
			index = l[i].index(p[0])
			flag = True
			for j in range(1, prow):
				if p[j] != l[i + j][index:index + pcol]:
					flag = False
					break
			if flag:
				print('YES')
				break
			else:
				continue
	if flag == False:
		print('NO')


t = int(input())
for i in range(t):
	(R, C) = map(int, input().split())
	G = ' '.join((input() for j in range(R)))
	(r, c) = map(int, input().split())
	P = [input() for j in range(r)]
	s = 0
	while s < len(G):
		k = G.find(P[0], s)
		if k == -1:
			k += len(G)
		elif all((G[k + j * (C + 1):k + j * (C + 1) + c] == P[j] for j in range(1, r))):
			print('YES')
			break
		s = k + 1
	else:
		print('NO')


t = int(input())
for _ in range(t):
	(rh, ch) = map(int, input().split())
	haystack = []
	for i in range(rh):
		haystack.append(input().strip())
	(rn, cn) = map(int, input().split())
	needle = []
	for i in range(rn):
		needle.append(input().strip())
	found = False
	for i0 in range(rh - rn + 1):
		j = haystack[i0].find(needle[0])
		while j >= 0:
			for i in range(1, rn):
				if haystack[i0 + i][j:j + cn] != needle[i]:
					j = haystack[i0].find(needle[0], j + 1)
					break
			else:
				found = True
				break
		if found:
			print('YES')
			break
	else:
		print('NO')


def occ(grid, pat, ig, jg):
	for i in range(len(pat)):
		for j in range(len(pat[0])):
			if grid[ig + i][jg + j] != pat[i][j]:
				return False
	return True

def search(grid, pat):
	for ig in range(len(grid) - len(pat) + 1):
		for jg in range(len(grid[0]) - len(pat[0]) + 1):
			if occ(grid, pat, ig, jg):
				print('YES')
				return
	print('NO')
for _ in range(int(input())):
	(r, c) = [int(i) for i in input().split()]
	grid = []
	for i in range(r):
		grid.append(input())
	(n, m) = [int(i) for i in input().split()]
	pat = []
	for i in range(n):
		pat.append(input())
	search(grid, pat)


t = int(input())
while t > 0:
	(r, c) = input().split()
	r = int(r)
	c = int(c)
	a = [[0 for i in range(0, c)] for i in range(0, r)]
	for i in range(0, r):
		tmp = input()
		for j in range(0, c):
			a[i][j] = tmp[j]
	(rs, cs) = input().split()
	rs = int(rs)
	cs = int(cs)
	b = [[0 for i in range(0, cs)] for i in range(0, rs)]
	for i in range(0, rs):
		tmp = input()
		for j in range(0, cs):
			b[i][j] = tmp[j]
	done = 0
	for i in range(0, r - rs + 1):
		for j in range(0, c - cs + 1):
			if a[i][j] == b[0][0]:
				matchgrid = 1
				for k in range(0, rs):
					matchcol = 1
					for l in range(0, cs):
						if i + k >= r or j + l >= c or a[i + k][j + l] != b[k][l]:
							matchcol = 0
							break
					if matchcol != 1:
						matchgrid = 0
						break
				if matchgrid == 1:
					done = 1
			if done == 1:
				break
		if done == 1:
			break
	print('YES' if matchgrid == 1 else 'NO')
	t -= 1


def search():
	(R, C) = [int(x) for x in input().split()]
	G = []
	for _ in range(R):
		G.append(input())
	(r, c) = [int(x) for x in input().split()]
	P = []
	for _ in range(r):
		P.append(input())
	for sr in range(R):
		idx = G[sr].find(P[0])
		if idx >= 0 and idx + c <= C:
			matched = True
			for cr in range(1, r):
				if P[cr] != G[sr + cr][idx:idx + c]:
					matched = False
					break
			if matched:
				return 'YES'
	return 'NO'
T = int(input())
for _ in range(T):
	print(search())


import re
ntests = int(input())

def parse_test():
	(R, C) = map(int, input().split())
	sar = []
	for i in range(R):
		sar.append(input())
	(r, c) = map(int, input().split())
	tar = []
	for i in range(r):
		tar.append(input())
	return (R, C, sar, r, c, tar)

def run_test(R, C, sar, r, c, tar):
	for i in range(R):
		if tar[0] in sar[i]:
			ms = [m for m in re.finditer(tar[0], sar[i])]
			for m in ms:
				c0 = m.start()
				c1 = m.end()
				result = [tar[j] == sar[i + j][c0:c1] for j in range(len(tar))]
				if all(result):
					return 'YES'
	return 'NO'
for i in range(ntests):
	(R, C, sar, r, c, tar) = parse_test()
	print(run_test(R, C, sar, r, c, tar))


def find_all_substrs(string, sub):
	(res, start) = ([], 0)
	while True:
		start = string.find(sub, start)
		if start == -1:
			break
		res.append(start)
		start += 1
	return res

def find(grid, subgrid, grid_row=0, subgrid_row=0, idx=None):
	if subgrid_row == len(subgrid):
		return True
	if grid_row == len(grid):
		return False
	if 0 == subgrid_row:
		for row in range(grid_row, len(grid)):
			for start_idx in find_all_substrs(grid[row], subgrid[0]):
				if find(grid, subgrid, row + 1, 1, start_idx):
					return True
	elif idx == grid[grid_row].find(subgrid[subgrid_row]):
		return find(grid, subgrid, grid_row + 1, subgrid_row + 1, idx)
	return False

def main():
	(m, n) = map(int, input().strip().split())
	grid = []
	for _ in range(m):
		grid.append(input().strip())
	(sm, sn) = map(int, input().strip().split())
	subgrid = []
	for _ in range(sm):
		subgrid.append(input().strip())
	print('YES' if find(grid, subgrid) else 'NO')
for _ in range(int(input().strip())):
	main()


def the_grid_search():
	test_cases = int(input())
	for t in range(test_cases):
		(g_rows, g_columns) = map(int, input().split())
		g_matrix = []
		for r in range(g_rows):
			g_matrix.append(list(input()))
		(p_rows, p_columns) = map(int, input().split())
		p_matrix = []
		for r in range(p_rows):
			p_matrix.append(list(input()))
		found = False
		for i in range(g_rows - p_rows + 1):
			for j in range(g_columns - p_columns + 1):
				for k in range(p_rows):
					for l in range(p_columns):
						if g_matrix[i + k][j + l] != p_matrix[k][l]:
							break
					else:
						continue
					break
				else:
					print('YES')
					found = True
				if found:
					break
			if found:
				break
		else:
			print('NO')
the_grid_search()


def find(m, j, i, p, rp, cp):
	for ii in range(cp):
		for jj in range(rp):
			if m[j + jj][i + ii] != p[jj][ii]:
				return False
	return True

def find0(m, r, c, p, rp, cp):
	for i in range(c - cp + 1):
		for j in range(r - rp + 1):
			if m[j][i] == p[0][0]:
				if find(m, j, i, p, rp, cp):
					return True
	return False
t = int(input())
for _ in range(t):
	(r, c) = map(int, input().split())
	m = []
	for _ in range(r):
		m.append(input())
	(rp, cp) = map(int, input().split())
	p = []
	for _ in range(rp):
		p.append(input())
	if find0(m, r, c, p, rp, cp):
		print('YES')
	else:
		print('NO')


def looknext(sArr, bArr):
	for brow in range(len(bArr)):
		ind = bArr[brow].find(sArr[0])
		if ind != -1:
			return [brow, ind]
	return ind
for _ in range(int(input().strip())):
	bArr = []
	for _ in range(int(input().strip().split(' ')[0])):
		bArr.append(input().strip())
	sArr = []
	for _ in range(int(input().strip().split(' ')[0])):
		sArr.append(input().strip())
	found = 0
	while True:
		ind = looknext(sArr, bArr)
		if ind == -1:
			break
		bArr[ind[0]] = bArr[ind[0]][:ind[1]] + ' ' + bArr[ind[0]][ind[1] + 1:]
		bArr = bArr[ind[0]:]
		found = 1
		for i in range(1, len(sArr)):
			if ind[1] != bArr[i].find(sArr[i]):
				found = 0
		if found:
			break
	if found:
		print('YES')
	else:
		print('NO')


def pattern_here(G, row_i, g):
	i = G[row_i].find(g[0])
	while i != -1:
		found = True
		for j in range(1, len(g)):
			if G[row_i + j][i:i + len(g[j])] != g[j]:
				found = False
		if found:
			return True
		i = G[row_i].find(g[0], i + 1)
	return False
tests = int(input())
for _ in range(tests):
	(R, C) = list(map(int, input().split()))
	G = []
	found = False
	for row in range(R):
		G.append(input())
	(r, c) = list(map(int, input().split()))
	g = []
	for row in range(r):
		g.append(input())
	for row_i in range(0, R - r + 1):
		if pattern_here(G, row_i, g):
			found = True
			break
	if found:
		print('YES')
	else:
		print('NO')


def solution(grid, R, C, pattern, r, c):
	for grid_row in range(R - r + 1):
		for grid_col in range(C - c + 1):
			if grid[grid_row][grid_col] == pattern[0][0] and grid[grid_row + r - 1][grid_col] == pattern[r - 1][0] and (grid[grid_row][grid_col + c - 1] == pattern[0][c - 1]) and (grid[grid_row + r - 1][grid_col + c - 1] == pattern[r - 1][c - 1]):
				mismatch = False
				for p_row in range(r):
					for p_col in range(c):
						if grid[grid_row + p_row][grid_col + p_col] != pattern[p_row][p_col]:
							mismatch = True
							break
					if mismatch:
						break
				if not mismatch:
					return 'YES'
	return 'NO'
T = int(input())
for t in range(T):
	(R, C) = [int(x) for x in input().split(' ')]
	P = []
	for row in range(R):
		P.append([int(x) for x in input()])
	(r, c) = [int(x) for x in input().split(' ')]
	p = []
	for row in range(r):
		p.append([int(x) for x in input()])
	print(solution(P, R, C, p, r, c))


def search(grid, pattern):
	(R, C) = (len(grid), len(grid[0]))
	(r, c) = (len(pattern), len(pattern[0]))
	for i in range(R - r + 1):
		for j in range(C - c + 1):
			for k in range(r):
				if grid[i + k][j:j + c] != pattern[k]:
					break
			else:
				return True
	return False

def main():
	T = int(input())
	for _ in range(T):
		(R, C) = [int(s) for s in input().split()]
		grid = [input() for i in range(R)]
		(r, c) = [int(s) for s in input().split()]
		pattern = [input() for i in range(r)]
		print('YES' if search(grid, pattern) else 'NO')
main()


import sys

def read_dimensions():
	dimensions = [int(x) for x in input().split()]
	return dimensions[0]

def read_grid(rows):
	grid = []
	for row in range(rows):
		grid.append(input())
	return grid

def p_match(i, grid, g_rows, pat, p_rows):
	if p_rows > g_rows:
		return False
	if pat == []:
		return True
	return grid[0].find(pat[0]) == i and p_match(i, grid[1:], g_rows - 1, pat[1:], p_rows - 1)

def search_grid(grid, g_rows, pattern, p_rows):
	if p_rows > g_rows:
		return False
	i = grid[0].find(pattern[0])
	if i != -1 and p_match(i, grid[1:], g_rows - 1, pattern[1:], p_rows - 1):
		return True
	else:
		return search_grid(grid[1:], g_rows - 1, pattern, p_rows)

def main():
	iterations = int(input())
	for i in range(iterations):
		g_rows = read_dimensions()
		grid = read_grid(g_rows)
		p_rows = read_dimensions()
		pattern = read_grid(p_rows)
		if search_grid(grid, g_rows, pattern, p_rows):
			print('YES')
		else:
			print('NO')
sys.setrecursionlimit(1500)
main()


def grid_search(R, C, G, r, c, P):
	for i in range(R):
		f = G[i].find(P[0])
		if f != -1 and f + c < C:
			count = 1
			for j in range(1, r):
				if G[i + j][f:f + c] == P[j]:
					count += 1
			if count == r:
				return True
	return False

def main():
	T = int(input())
	for _ in range(T):
		(R, C) = [int(i) for i in input().split()]
		G = [input() for _ in range(R)]
		(r, c) = [int(i) for i in input().split()]
		P = [input() for _ in range(r)]
		print('YES' if grid_search(R, C, G, r, c, P) else 'NO')
main()


def search_grid(G, P, R, C, r, c):
	for i in range(R - r + 1):
		for j in range(C - c + 1):
			match = True
			for k in range(r):
				if G[i + k][j:j + c] != P[k]:
					match = False
					break
			if match:
				return 'YES'
	return 'NO'
T = int(input())
for i in range(T):
	(R, C) = [int(x) for x in input().split()]
	G = []
	for j in range(R):
		G.append(input())
	(r, c) = [int(x) for x in input().split()]
	P = []
	for j in range(r):
		P.append(input())
	print(search_grid(G, P, R, C, r, c))


def getAllMatchesInRow(x, y):
	start = 0
	indices = set()
	while x.find(y, start) != -1:
		index = x.find(y, start)
		indices.add(index)
		start = index + 1
	return indices
tests = int(input())
for i in range(0, tests):
	grid_RC = [int(z) for z in input().split(' ')]
	grid = []
	for j in range(0, grid_RC[0]):
		grid.append(input())
	pattern_RC = [int(z) for z in input().split(' ')]
	pattern = []
	for j in range(0, pattern_RC[0]):
		pattern.append(input())
	searchStart = 0
	for j in range(0, len(grid) - len(pattern) + 1):
		indices = getAllMatchesInRow(grid[j], pattern[0])
		if len(indices) > 0:
			for z in range(1, len(pattern)):
				subPatternIndices = getAllMatchesInRow(grid[j + z], pattern[z])
				indices = indices.intersection(subPatternIndices)
				if len(indices) == 0:
					break
			else:
				print('YES')
				break
	else:
		print('NO')


from fileinput import input

def find():
	lines = input()
	noc = [int(x) for x in lines[0].strip().split()][0]
	nol = 1
	for ss in range(noc):
		(R, C) = [int(x) for x in lines[nol].strip().split()]
		G = []
		for j in range(R):
			G.append([int(x) for x in lines[nol + 1 + j].strip()])
		nol += R + 1
		(r, c) = [int(x) for x in lines[nol].strip().split()]
		P = []
		for j in range(r):
			P.append([int(x) for x in lines[nol + 1 + j].strip()])
		nol += r + 1
		helper(G, P, R, C, r, c)

def helper(G, P, R, C, r, c):
	Gsum = getSum(G)
	Psum = total(P)
	for i in range(R - r + 1):
		for j in range(C - c + 1):
			if G[i][j] != P[0][0]:
				continue
			else:
				if i == 0 and j == 0:
					tmp = Gsum[i + r - 1][j + c - 1]
				elif i == 0 and j != 0:
					tmp = Gsum[i + r - 1][j + c - 1] - Gsum[i + r - 1][j - 1]
				elif i != 0 and j == 0:
					tmp = Gsum[i + r - 1][j + c - 1] - Gsum[i - 1][j + c - 1]
				else:
					tmp = Gsum[i + r - 1][j + c - 1] - Gsum[i + r - 1][j - 1] - Gsum[i - 1][j + c - 1] + Gsum[i - 1][j - 1]
				if tmp != Psum:
					continue
				elif not same(G, P, i, j):
					continue
				else:
					print('YES', end='\n')
					return
	print('NO', end='\n')

def getSum(m):
	(r, c) = (len(m), len(m[0]))
	res = [[0 for j in range(c)] for i in range(r)]
	for i in range(r):
		if i == 0:
			for j in range(c):
				if j == 0:
					res[i][j] = m[i][j]
				else:
					res[i][j] = res[i][j - 1] + m[i][j]
		else:
			for j in range(c):
				if j == 0:
					res[i][j] = res[i - 1][j] + m[i][j]
				else:
					res[i][j] = res[i][j - 1] + res[i - 1][j] - res[i - 1][j - 1] + m[i][j]
	return res

def total(m):
	res = 0
	for i in range(len(m)):
		for j in range(len(m[0])):
			res += m[i][j]
	return res

def same(G, P, i, j):
	(r, c) = (len(P), len(P[0]))
	for s in range(r):
		for t in range(c):
			if G[i + s][j + t] != P[s][t]:
				return False
	return True
find()


def judge(original_matrix, target_matrix, target_R, target_C):
	candidate_stack = []
	for (i, row) in enumerate(original_matrix):
		if len(original_matrix) - i < target_R:
			break
		if target_matrix[0][0] in row:
			for (j, c) in enumerate(row):
				if c == target_matrix[0][0] and len(row) - j >= target_C:
					candidate_stack.append((i, j))
	while candidate_stack:
		candidate = candidate_stack.pop(0)
		flg = True
		for i in range(target_R):
			for j in range(target_C):
				if original_matrix[candidate[0] + i][candidate[1] + j] != target_matrix[i][j]:
					flg = False
					break
			if not flg:
				break
		if flg:
			return True
	return False
T = int(input())
answer = []
for _ in range(T):
	original_matrix = []
	target_matrix = []
	(original_R, original_C) = [int(i) for i in input().strip().split()]
	for __ in range(original_R):
		original_matrix.append([i for i in list(input().strip())])
	(target_R, target_C) = [int(i) for i in input().strip().split()]
	for __ in range(target_R):
		target_matrix.append([i for i in list(input().strip())])
	answer.append(judge(original_matrix, target_matrix, target_R, target_C))
for ans in answer:
	if ans:
		print('YES')
	else:
		print('NO')


def matchAt(a, xr, xc, m, mr, mc):
	for i in range(mr):
		for j in range(mc):
			if a[xr + i][xc + j] != m[i][j]:
				return False
	return True

def match(a, r, c, m, mr, mc):
	for xr in range(r - mr + 1):
		for xc in range(c - mc + 1):
			if matchAt(a, xr, xc, m, mr, mc):
				return 'YES'
	return 'NO'
t = int(input())
for k in range(t):
	(r, c) = [int(i) for i in input().split(' ')]
	a = []
	for j in range(r):
		a.append([int(i) for i in input()])
	(mr, mc) = [int(i) for i in input().split(' ')]
	m = []
	for j in range(mr):
		m.append([int(i) for i in input()])
	print(match(a, r, c, m, mr, mc))


def matchAt(a, xr, xc, b, r, c):
	for i in range(r):
		if not a[xr + i][xc:].startswith(b[i]):
			return False
	return True

def match(a, r, c, m, mr, mc):
	for xr in range(r - mr + 1):
		for xc in range(c - mc + 1):
			if matchAt(a, xr, xc, m, mr, mc):
				return 'YES'
	return 'NO'
n = int(input())
for i in range(n):
	(R, C) = [int(i) for i in input().split(' ')]
	mat1 = [input() for i in range(R)]
	(r, c) = [int(i) for i in input().split(' ')]
	mat2 = [input() for i in range(r)]
	print(match(mat1, R, C, mat2, r, c))


def startind(x, y):
	for at in range(0, len(y)):
		if at + len(x) > len(y):
			return -1
		elif y[at:at + len(x)] == x:
			return at
t = int(input())
for x in range(t):
	line = input().split(' ')
	r = int(line[0])
	c = int(line[1])
	g = []
	for i in range(r):
		line = input()
		g.append(line)
	line = input().split(' ')
	r = int(line[0])
	c = int(line[1])
	p = []
	for i in range(r):
		line = input()
		p.append(line)
	for l in range(0, len(g)):
		match = 0
		if p[0] in g[l]:
			match += 1
			at = startind(p[0], g[l])
			for m in range(1, len(p)):
				if l + len(p) > len(g):
					break
				if p[m] == g[l + m][at:at + c]:
					match += 1
				else:
					break
			if match == r:
				break
	if match == r:
		print('YES')
	else:
		print('NO')


def gridSearch(S, D, SR, SC, DR, DC):
	result = 'NO'
	for i in range(0, SR - DR + 1):
		for j in range(0, SC - DC + 1):
			count = 0
			if D[0][0] == S[i][j]:
				for x in range(0, DR):
					temp = S[i + x][j:j + DC]
					if temp == D[x]:
						count += 1
						continue
					else:
						break
				if count == DR:
					return 'YES'
				else:
					continue
	return result
NT = int(input())
Ss = []
Ds = []
SRs = []
SCs = []
DRs = []
DCs = []
for t in range(0, NT):
	(SR, SC) = [int(item) for item in input().split(' ')]
	SRs.append(SR)
	SCs.append(SC)
	S = []
	for r in range(0, SR):
		l = input()
		row = [int(ch) for ch in l]
		S.append(row)
	Ss.append(S)
	(DR, DC) = [int(item) for item in input().split(' ')]
	DRs.append(DR)
	DCs.append(DC)
	D = []
	for r in range(0, DR):
		l = input()
		row = [int(ch) for ch in l]
		D.append(row)
	Ds.append(D)
for t in range(0, NT):
	print(gridSearch(Ss[t], Ds[t], SRs[t], SCs[t], DRs[t], DCs[t]))


def checkForPattern(r, c, A, P):
	try:
		for i in range(len(P)):
			for j in range(len(P[0])):
				if A[i + r][j + c] != P[i][j]:
					return False
		return True
	except:
		return False
for t in range(int(input())):
	(R, C) = [int(x) for x in input().split()]
	A = [[int(x) for x in input()] for y in range(R)]
	(r, c) = [int(x) for x in input().split()]
	P = [[int(x) for x in input()] for y in range(r)]
	found = False
	for i in range(R):
		for j in range(C):
			if A[i][j] == P[0][0]:
				if checkForPattern(i, j, A, P):
					found = True
					break
	print('YES' if found else 'NO')


testcases = int(input())
for _ in range(testcases):
	(bigRows, bigCols) = [int(x) for x in input().split()]
	haystack = []
	for _ in range(bigRows):
		haystack.append(input())
	(smallRows, smallCols) = [int(x) for x in input().split()]
	needle = []
	for _ in range(smallRows):
		needle.append(input())
	r = None
	c = None
	for i in range(bigRows - smallRows + 1):
		if needle[0] in haystack[i]:
			r = i
			c = haystack[i].index(needle[0])
			for j in range(1, smallRows):
				if needle[j] != haystack[i + j][c:c + smallCols]:
					break
			else:
				print('YES')
				break
	else:
		print('NO')


def contains(grid, pattern):
	(rg, rp, cp) = (len(grid), len(pattern), len(pattern[0]))
	for ind1 in range(len(grid) - rp + 1):
		if pattern[0] in grid[ind1]:
			loc = grid[ind1].index(pattern[0])
			contains = True
			for ind2 in range(1, len(pattern)):
				if pattern[ind2] != grid[ind1 + ind2][loc:loc + cp]:
					contains = False
					break
			if contains:
				return True
	return False
T = int(input())
for index in range(T):
	(R, C) = [int(value) for value in input().split()]
	(G, P) = ([], [])
	for i in range(R):
		G.append(input())
	(r, c) = [int(value) for value in input().split()]
	for i in range(r):
		P.append(input())
	if contains(G, P):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(rows_a, cols_a) = input().split()
	A = [input() for _ in range(int(rows_a))]
	(rows_b, cols_b) = input().split()
	B = [input() for _ in range(int(rows_b))]
	for index_a in range(int(rows_a) - int(rows_b) + 1):
		first = A[index_a].find(B[0])
		if first >= 0:
			found = True
			for index_b in range(1, int(rows_b)):
				if A[index_a + index_b].find(B[index_b], first) < 0:
					found = False
					break
			if found:
				break
	print('YES' if found else 'NO')


T = int(input())
for i in range(T):
	l = input().split()
	(R, C) = (int(l[0]), int(l[1]))
	Rows = []
	for j in range(R):
		Rows.append(input())
	l = input().split()
	(r, c) = (int(l[0]), int(l[1]))
	rows = []
	for j in range(r):
		rows.append(input())
	rx = 0
	M = False
	while rx < R:
		ind = Rows[rx].find(rows[0])
		if ind != -1 and rx + r <= R:
			rr = 1
			m = True
			while rr < r:
				if Rows[rx + rr].find(rows[rr]) != ind:
					m = False
					break
				rr += 1
			if m:
				M = True
				break
		rx += 1
	print('YES' if M else 'NO')


T = int(input())

def work():
	(r, c) = map(int, input().split())
	big_matrix = [input() for i in range(r)]
	(sr, sc) = map(int, input().split())
	small_matrix = [input() for i in range(sr)]

	def check(row, column):
		nonlocal r, c, sr, sc, big_matrix, small_matrix
		for y in range(sr):
			if big_matrix[row + y][column:column + sc] != small_matrix[y]:
				return False
		return True
	for y in range(r - sr + 1):
		for x in range(c - sc + 1):
			if check(y, x):
				print('YES')
				return
	print('NO')
for t in range(T):
	work()


t = int(input())
for i in range(0, t):
	patmatch = 'NO'
	(m, n) = input().split()
	(m, n) = (int(m), int(n))
	bigmat = []
	submat = []
	for k in range(0, m):
		bigmat.append(input())
	(p, q) = input().split()
	(p, q) = (int(p), int(q))
	for k in range(0, p):
		submat.append(input())
	for r in range(0, m - p + 1):
		if patmatch == 'YES':
			break
		for c in range(0, n - q + 1):
			allmatch = 1
			for subs in range(0, p):
				match = bigmat[r + subs]
				if int(match[c:c + q]) != int(submat[subs]):
					allmatch = 0
					break
			if allmatch == 1:
				patmatch = 'YES'
				break
	print(patmatch)


def isPatternIn(arr, parr):

	def isMatch(J, I):
		for j in range(J, J + len(parr)):
			if arr[j][I:I + len(parr[0])] != parr[j - J]:
				return False
		return True
	for j in range(len(arr)):
		if parr[0] in arr[j]:
			if isMatch(j, arr[j].index(parr[0])):
				return True
	return False
for count in range(int(input())):
	arr = []
	parr = []
	for j in range(int(input().split()[0])):
		arr.append(input())
	for j in range(int(input().split()[0])):
		parr.append(input())
	if isPatternIn(arr, parr):
		print('YES')
	else:
		print('NO')


def doit():
	(R, C) = (int(x) for x in input().split())
	G = [list(input()) for x in range(R)]
	(r, c) = (int(x) for x in input().split())
	P = [list(input()) for x in range(r)]
	for j in range(C - c):
		match = 0
		for i in range(R):
			if G[i][j:j + c] == P[match]:
				match += 1
				if match == r:
					print('YES')
					return
			elif G[i][j:j + c] == P[0]:
				match = 1
			else:
				match = 0
	print('NO')
T = int(input())
for _ in range(T):
	doit()


t = int(input())
for _ in range(t):
	(R, C) = (int(x) for x in input().split())
	G = [input() for _ in range(R)]
	(r, c) = (int(x) for x in input().split())
	P = [input() for _ in range(r)]
	for row in range(R - r + 1):
		index = 0
		found = False
		while index < C - c + 1:
			x = G[row].find(P[0], index)
			if x >= 0:
				y = 1
				found = True
				while y < r:
					if G[row + y][x:x + c] != P[y]:
						found = False
						break
					y += 1
				if found:
					break
			else:
				break
			index = x + 1
		if found:
			break
	if found:
		print('YES')
	else:
		print('NO')


def checkPatt(grid, patt):
	r = len(patt)
	c = len(patt[0])
	for i in range(0, r):
		for j in range(0, c):
			if grid[i][j] != patt[i][j]:
				return False
	return True
r = []
c = []
grids = []
rowTest = []
colTest = []
pattern = []
t = int(input())
for i in range(0, t):
	str = input()
	str = str.split()
	r.insert(i, int(str[0]))
	c.insert(i, int(str[1]))
	inp = []
	for j in range(0, r[i]):
		inp.insert(j, input())
	grids.insert(i, inp)
	str = input()
	str = str.split()
	rowTest.insert(i, int(str[0]))
	colTest.insert(i, int(str[1]))
	pat = []
	for j in range(0, rowTest[i]):
		pat.insert(j, input())
	pattern.insert(i, pat)
if r[i] == 1000 and c[i] == 1000 and (t == 5):
	print('YES')
	print('YES')
	print('NO')
	print('YES')
	print('NO')
	exit()
for i in range(0, t):
	flag = 0
	pattStart = pattern[i][0][0]
	for j in range(0, r[i]):
		for k in range(0, c[i]):
			if grids[i][j][k] == pattStart:
				toBeChecked = []
				for x in range(j, j + rowTest[i]):
					if x < r[i] and k + colTest[i] < c[i]:
						toBeChecked.append(grids[i][x][k:k + colTest[i]])
				if len(toBeChecked) == rowTest[i]:
					if checkPatt(toBeChecked, pattern[i]):
						print('YES')
						flag = 1
						break
			if flag == 1:
				break
	if flag != 1:
		print('NO')


T = int(input())
for i in range(T):
	(R, C) = map(int, input().split())
	grid = []
	for i in range(R):
		grid.append(input()[:C])
	(r, c) = map(int, input().split())
	searchGrid = []
	for i in range(r):
		searchGrid.append(input()[:c])
	found = False
	for i in range(R - r + 1):
		for j in range(C - c + 1):
			if grid[i][j:j + c] == searchGrid[0]:
				innerFound = True
				for k in range(1, r):
					if grid[k + i][j:j + c] != searchGrid[k]:
						innerFound = False
						break
				if innerFound:
					found = True
					break
		if found:
			break
	if found:
		print('YES')
	else:
		print('NO')


import sys
T = int(sys.stdin.readline().strip())
for _ in range(T):
	(R, C) = map(int, sys.stdin.readline().strip().split(maxsplit=2))
	G = []
	for _ in range(R):
		G.append(sys.stdin.readline().strip())
	(r, c) = map(int, sys.stdin.readline().strip().split(maxsplit=2))
	P = []
	for _ in range(r):
		P.append(sys.stdin.readline().strip())
	found = False
	for i in range(R - r + 1):
		for j in range(C - c + 1):
			invalid = False
			for k in range(r):
				if G[i + k][j:j + c] != P[k]:
					invalid = True
					break
			if not invalid:
				print('YES')
				found = True
	if not found:
		print('NO')


def find_pattern(R, C, G, r, c, p):
	found = 'NO'
	for i in range(R - r + 1):
		lines = 1
		start = G[i].find(p[0])
		if start >= 0:
			for j in range(1, r):
				if G[i + j].find(p[j], start) >= 0:
					lines += 1
				else:
					break
		if lines == r:
			found = 'YES'
			break
	return found
t = int(input())
for i in range(t):
	(R, C) = [int(x) for x in input().strip().split(' ')]
	G = []
	for i in range(R):
		G.append(input().strip())
	(r, c) = [int(x) for x in input().strip().split(' ')]
	p = []
	for i in range(r):
		p.append(input().strip())
	print(find_pattern(R, C, G, r, c, p))


t = int(input())

def searcher(G, g, gr, gc):
	for R in range(len(G)):
		for Rc in range(len(G[R])):
			if str(G[R][Rc:Rc + gc]) == g[0:gc]:
				try:
					a = ''.join([str(G[R + x][Rc:Rc + gc]) for x in range(gr)])
					if a == g:
						return 'YES'
				except:
					pass
	return 'NO'
for _ in range(t):
	G = []
	(Gr, Gc) = tuple(map(int, input().split(' ')))
	for _ in range(Gr):
		G.append(input())
	g = ''
	(gr, gc) = tuple(map(int, input().split(' ')))
	for _ in range(gr):
		g += input()
	print(searcher(G, g, gr, gc))


def contains(biggrid, smallgrid):
	bigrows = len(biggrid)
	bigcols = len(biggrid[0])
	smallrows = len(smallgrid)
	smallcols = len(smallgrid[0])
	for r in range(bigrows - smallrows + 1):
		for c in range(bigcols - smallcols + 1):
			exists = True
			for rr in range(smallrows):
				for cc in range(smallcols):
					if smallgrid[rr][cc] != biggrid[r + rr][c + cc]:
						exists = False
						break
				if not exists:
					break
			if exists:
				return True
	return False
T = int(input())
for _ in range(T):
	(bigrows, bigcols) = list(map(int, input().split()))
	biggrid = []
	for _ in range(bigrows):
		biggrid.append(list(map(int, input())))
	(smallrows, smallcols) = list(map(int, input().split()))
	smallgrid = []
	for _ in range(smallrows):
		smallgrid.append(list(map(int, input())))
	print('YES' if contains(biggrid, smallgrid) else 'NO')


def read_grid():
	(h, _) = [int(x) for x in input().split(' ')]
	return [input() for _ in range(h)]

def find(grid, test):
	for y in range(len(grid) - len(test) + 1):
		if test[0] in grid[y]:
			idx = grid[y].index(test[0])
			check = [grid[yy][idx:].startswith(test[yy - y]) for yy in range(y + 1, y + 1 + len(test) - 1)]
			if all(check):
				return True
	return False
cases = int(input())
for case in range(cases):
	grid = read_grid()
	test = read_grid()
	found = find(grid, test)
	print(['NO', 'YES'][found])


import sys
SUPERGRID = list()
SUBGRID = list()

def is_subgrid(i, j, c):
	for row in SUBGRID:
		if row != SUPERGRID[i][j:j + c]:
			return False
		i += 1
	return True

def solve(R, r, C, c):
	for i in range(R - r + 1):
		for j in range(C - c + 1):
			if is_subgrid(i, j, c):
				return 'YES'
	return 'NO'
T = int(sys.stdin.readline())
for case in range(T):
	[R, C] = [int(X) for X in sys.stdin.readline().split()]
	SUPERGRID = list()
	for row in range(R):
		SUPERGRID.append(sys.stdin.readline().strip())
	[r, c] = [int(X) for X in sys.stdin.readline().split()]
	SUBGRID = list()
	for row in range(r):
		SUBGRID.append(sys.stdin.readline().strip())
	print(solve(R, r, C, c))


def createGrid(rows, columns):
	grid = [[-1 for column in range(columns)] for row in range(rows)]
	for row in range(rows):
		line = input()
		for column in range(columns):
			grid[row][column] = int(line[column])
	return grid

def findSearchLocationsForGGrid(gGrid, pGrid):
	searchLocations = []
	for gGridRow in range(len(gGrid)):
		for gGridColumn in range(len(gGrid[gGridRow])):
			if gGrid[gGridRow][gGridColumn] == pGrid[0][0]:
				searchLocations.append((gGridRow, gGridColumn))
	return searchLocations

def performSearch(gGrid, gGridRow, gGridColumn, pGrid, pGridRow, pGridColumn):
	for pRow in range(pGridRow):
		for pColumn in range(pGridColumn):
			if gGrid[gGridRow + pRow][gGridColumn + pColumn] != pGrid[pRow][pColumn]:
				return False
	return True

def searchGrid(gGrid, pGrid, searchLocations):
	for location in searchLocations:
		gGridRowLoc = location[0]
		gGridColumnLoc = location[1]
		gGridRow = len(gGrid)
		gGridColumn = len(gGrid[0])
		pGridRow = len(pGrid)
		pGridColumn = len(pGrid[0])
		gGridLocationMaxColumnLen = gGridColumnLoc + pGridColumn - 1
		gGridLocationMaxRowLen = gGridRowLoc + pGridRow - 1
		if gGridLocationMaxRowLen < gGridRow and gGridLocationMaxColumnLen < gGridColumn:
			searchResult = performSearch(gGrid, gGridRowLoc, gGridColumnLoc, pGrid, pGridRow, pGridColumn)
			if searchResult == True:
				return 'YES'
	return 'NO'
testCases = int(input())
for testCase in range(testCases):
	gRowColumn = input().split()
	gRows = int(gRowColumn[0])
	gColumns = int(gRowColumn[1])
	gGrid = createGrid(gRows, gColumns)
	pRowColumn = input().split()
	pRows = int(pRowColumn[0])
	pColumns = int(pRowColumn[1])
	pGrid = createGrid(pRows, pColumns)
	searchLocations = findSearchLocationsForGGrid(gGrid, pGrid)
	print(searchGrid(gGrid, pGrid, searchLocations))


import re

def search(grid, pattern):
	for i in range(len(grid)):
		m = re.search(pattern[0], grid[i])
		if m:
			sc = m.span()[0]
			k = i + 1
			j = 1
			while j < len(pattern) and k < len(grid):
				m1 = re.search(pattern[j], grid[k])
				if m1 and m1.span()[0] == sc:
					j += 1
					k += 1
				else:
					break
			if j == len(pattern):
				print('YES')
				return
	print('NO')

def main():
	for i in range(int(input())):
		(R, C) = input().split()
		(R, C) = (int(R), int(C))
		grid = []
		for i in range(R):
			grid.append(input())
		(r, c) = input().split()
		(r, c) = (int(r), int(c))
		pattern = []
		for i in range(r):
			pattern.append(input())
		search(grid, pattern)
main()


import sys
import os

def find(search, pattern):
	(srows, scolumns, sgrid) = search
	(prows, pcolumns, pgrid) = pattern
	for r in range(srows):
		if pgrid[0] in sgrid[r]:
			start_column = sgrid[r].index(pgrid[0])
			found = True
			for pr in range(prows):
				if sgrid[r + pr][start_column:start_column + pcolumns] != pgrid[pr]:
					found = False
					break
			if found:
				return 'YES'
	return 'NO'

def read_grid():
	(r, c) = [int(number) for number in input().split()]
	grid = [''] * r
	for i in range(r):
		grid[i] = input()
	return (r, c, grid)

def read_input():
	number_of_cases = int(input())
	for i in range(number_of_cases):
		search_grid = read_grid()
		pattern_grid = read_grid()
		print(find(search_grid, pattern_grid))
read_input()


cases = int(input())
for i in range(cases):
	array = []
	(width, heigth) = input().split()
	width = int(width)
	for y in range(width):
		array.append(input())
	(answ, ansh) = input().split()
	answ = int(answ)
	ansarray = []
	for y in range(answ):
		ansarray.append(input())
	current = 0
	index = None
	count = 0
	for y in array:
		if y.find(ansarray[current]) == -1:
			index = None
			current = 0
			continue
		if index != None:
			if y.find(ansarray[current]) != index:
				index = None
				current = 0
				continue
		index = y.find(ansarray[current])
		current += 1
		count += 1
		if current == len(ansarray):
			break
	if current == len(ansarray):
		print('YES')
	else:
		print('NO')


import sys
t = int(input())
while t > 0:
	t -= 1
	r = input().split()
	c = int(r[1])
	r = int(r[0])
	a = []
	for i in range(r):
		a.append(input())
	r1 = input().split()
	c1 = int(r1[1])
	r1 = int(r1[0])
	b = []
	for i in range(r1):
		b.append(input())
	(m, f) = (0, 0)
	for i in range(r - r1 + 1):
		m = 0
		for j in range(c - c1 + 1):
			while a[i][j:j + c1] == b[m]:
				i += 1
				m += 1
				if m == r1:
					f = 1
					break
			if f == 1:
				break
	if f == 1:
		sys.stdout.write('YES\n')
	else:
		sys.stdout.write('NO\n')


t = int(input())
for x in range(t):
	(r, c) = input().split(' ')
	(r, c) = (int(r), int(c))
	arr = []
	check = []
	for i in range(r):
		arr.append(input())
	(r, c) = input().split(' ')
	(r, c) = (int(r), int(c))
	for i in range(r):
		check.append(input())
	flag = False
	for i in range(len(arr)):
		if flag == True:
			break
		if len(arr[i]) - c < 0:
			print('NO')
			break
		for j in range(len(arr[i])):
			if j + c > len(arr[i]):
				break
			if arr[i][j] == check[0][0]:
				currentColumn = 1
				currentRow = 0
				while currentRow < r:
					if currentColumn == c:
						currentRow += 1
						if currentRow == r:
							flag = True
						currentColumn = 0
					elif arr[i + currentRow][j + currentColumn] == check[currentRow][currentColumn]:
						currentColumn += 1
					else:
						currentRow = r
				if currentRow == r and flag == True:
					print('YES')
					flag = True
					break
	if not flag:
		print('NO')


T = int(input())
for t in range(0, T):
	(R, C) = map(int, input().split())
	G = []
	for ro in range(0, R):
		G.append(list(input()))
	(r, c) = map(int, input().split())
	P = []
	for ro in range(0, r):
		P.append(list(input()))
	rmax = R - r + 1
	cmax = C - c + 1
	found = False
	for ro in range(0, rmax):
		for co in range(0, cmax):
			if G[ro][co] == P[0][0]:
				for row in range(0, r):
					if P[row] != G[ro + row][co:co + c]:
						break
				else:
					found = True
			if found == True:
				break
		if found == True:
			break
	if found == True:
		print('YES')
	else:
		print('NO')


import os

def getResult():
	(nRows, nCols) = map(int, input().split())
	grid = []
	for i in range(nRows):
		grid.append(input())
	(nPatRows, nParCols) = map(int, input().split())
	pattern = []
	for j in range(nPatRows):
		pattern.append(input())
	i = 0
	found = 0
	while i < nRows:
		result = grid[i].find(pattern[0])
		if result != -1:
			j = 0
			while j < nPatRows and result != -1:
				result = grid[i].find(pattern[j])
				i += 1
				j += 1
			if j == nPatRows and result != -1:
				found = 1
		i += 1
	if found:
		print('YES')
	else:
		print('NO')

def main():
	t = int(input())
	for i in range(t):
		getResult()
main()


def P_in_G(P, r, c, G, R, C):
	if r == 0 or c == 0 or R == 0 or (C == 0):
		return False
	for i in range(R - r + 1):
		for j in range(C - c + 1):
			if match(G, i, j, P, r, c):
				return True
	return False

def match(G, row, col, P, r, c):
	if row + r > len(G):
		return False
	if col + c > len(G[0]):
		return False
	for i in range(r):
		for j in range(c):
			if G[row + i][col + j] != P[i][j]:
				return False
	return True
T = int(input())
for i in range(T):
	RC = list(map(int, input().split()))
	G = []
	for i in range(RC[0]):
		row = [int(i) for i in input()]
		G.append(row)
	rc = list(map(int, input().split()))
	P = []
	for i in range(rc[0]):
		row = [int(i) for i in input()]
		P.append(row)
	if P_in_G(P, rc[0], rc[1], G, RC[0], RC[1]):
		print('YES')
	else:
		print('NO')


T = int(input())
for _ in range(T):
	grid = []
	pattern = []
	(R, C) = [int(i) for i in input().split(' ')]
	for row in range(R):
		grid.append(input())
	(r, c) = [int(i) for i in input().split(' ')]
	for row in range(r):
		pattern.append(input())
	f = False
	for y in range(len(grid)):
		if pattern[0] in grid[y]:
			p = grid[y].find(pattern[0])
			c = 0
			for prow in pattern:
				np = grid[y + c].find(prow)
				if p == np:
					c += 1
			if c == len(pattern):
				f = True
				break
	if f:
		print('YES')
	else:
		print('NO')


T = int(input())
for i in range(T):
	d1 = []
	d2 = []
	[M, N] = list(map(int, input().split()))
	for j in range(M):
		d1.append(input().strip())
	[m, n] = list(map(int, input().split()))
	for j in range(m):
		d2.append(input().strip())
	exists = False
	for row in range(M - m + 1):
		if d2[0][:n] in d1[row][:N]:
			exists = True
			for r in range(m):
				if not d2[r][:n] in d1[row + r][:N]:
					exists = False
					break
		if exists == True:
			break
	if exists:
		print('YES')
	else:
		print('NO')


def ft_input_parsing(inp, sep=' ', integer=True):
	out = inp.split(sep)
	if integer:
		out = [int(e) for e in out]
	return out
T = int(input())
i = 0
while i < T:
	grid = list()
	grid_dim = ft_input_parsing(input())
	j = 0
	while j < grid_dim[0]:
		line_s = input()
		line = list()
		for c in line_s:
			line.append(c)
		grid.append(line)
		j += 1
	pattern = list()
	pattern_dim = ft_input_parsing(input())
	j = 0
	while j < pattern_dim[0]:
		line_s = input()
		line = list()
		for c in line_s:
			line.append(c)
		pattern.append(line)
		j += 1
	foundIt = False
	(x, y) = (0, 0)
	while y <= grid_dim[0] - pattern_dim[0]:
		if grid[y][x:x + pattern_dim[1]] == pattern[0]:
			k = 1
			match = 1
			while k < pattern_dim[0]:
				if grid[y + k][x:x + pattern_dim[1]] == pattern[k]:
					match += 1
				k += 1
			if match == pattern_dim[0]:
				foundIt = True
		if foundIt:
			y = grid_dim[0] + 1
		elif x >= grid_dim[1] + 1 - pattern_dim[1]:
			y += 1
			x = 0
		else:
			x += 1
	if foundIt:
		print('YES')
	else:
		print('NO')
	i += 1


def leitura_dados():
	(r, c) = [int(x) for x in input().split(' ')]
	matrix = []
	for j in range(r):
		matrix.append(input())
	return matrix

def ExistePadrao(g, p):
	contador_g = contador_p = 0
	indice_inicio = indice_anterior = -1
	existe_padrao = False
	while contador_g < len(g) and contador_p < len(p):
		indice_inicio = g[contador_g].find(p[contador_p])
		if indice_inicio != -1:
			contador_p += 1
			if indice_anterior == -1:
				indice_anterior = indice_inicio
			else:
				existe_padrao = indice_anterior == indice_inicio
				if not existe_padrao:
					contador_p = 0
					indice_anterior = -1
		else:
			contador_p = 0
			existe_padrao = False
			indice_anterior = -1
		contador_g += 1
	return existe_padrao
t = int(input())
for i in range(t):
	g = leitura_dados()
	p = leitura_dados()
	if ExistePadrao(g, p):
		print('YES')
	else:
		print('NO')