def read_matrix():
    nrows, ncols = map(int, raw_input().split())
    matrix = [raw_input() for _ in xrange(nrows)]
    assert all(len(row) == ncols for row in matrix)
    return matrix

def count_pattern(pattern, text):
    """A naive O(N**2 * M**2) algorithm."""
    nrows, ncols = len(pattern), len(pattern[0])
    return sum(pattern == [row[j:j+ncols] for row in text[i:i+nrows]]
               for i in xrange(len(text) - nrows + 1)
               for j in xrange(len(text[i]) - ncols + 1))

for _ in xrange(int(raw_input())):
    print count_pattern(read_matrix(), read_matrix())
