#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;

bool field[64][64];
bool block[64][64];
int need[64];

int BW, BH, FW, FH;
int minx, miny, maxx, maxy;

void adjust()
{
	minx = 999; miny = 999;
	maxx = 0;   maxy = 0;

	for(int i=0; i<64; i++) {
		bool f = false;
		for(int j=0; j<64; j++) {
			if(block[j][i]) {
				f = true;
			}
		}

		if(f) {
			miny = min(miny, i);
			maxy = max(maxy, i);
		}
	}

	for(int i=0; i<64; i++) {
		bool f = false;
		for(int j=0; j<64; j++) {
			if(block[i][j]) {
				f = true;
			}
		}

		if(f) {
			minx = min(minx, i);
			maxx = max(maxx, i);
		}
	}
}

void rotate()
{
	bool tmp[64][64];
	memset(tmp, 0, sizeof(tmp));

	for(int i=0; i<64; i++)
	for(int j=64-1; j>=0; j--) {
		tmp[i][64-1-j] = block[j][i];
	}

	memcpy(block, tmp, sizeof(block));
}

int solve()
{
	adjust();

	int W = maxx - minx + 1;
	int H = maxy - miny + 1;

	int res = -1;

	for(int i=0; i<=FH - H; i++) {
		for(int j=0; j<=FW - W; j++) {
			bool valid = true;
			int score = 0;

			for(int y=miny; y<=maxy; y++) {
				int cnt = 0;
				for(int x=minx; x<=maxx; x++) {
					if(!block[x][y]) continue;

					if(!field[j + x - minx][i + y - miny]) {
						valid = false;
					}
					else {
						cnt++;
					}
				}

				if(valid && need[i + y - miny] == cnt) score++;
			}

			if(valid) {
				res = max(score, res);
			}
		}
	}

	return res;
}

int main()
{
	int N;
	cin >> N;

	while(N--) {
		memset(need, 0, sizeof(need));
		memset(field, 0, sizeof(field));
		memset(block, 0, sizeof(block));

		cin >> BH >> BW;
		string b[64], f[64];

		for(int i=0; i<BH; i++)
			cin >> b[i];

		cin >> FH >> FW;
		for(int i=0; i<FH; i++)
			cin >> f[i];

		for(int i=0; i<BH; i++)
		for(int j=0; j<BW; j++) {
			if(b[i][j] == '#') {
				block[j][i] = 1;
			}
		}

		for(int i=0; i<FH; i++)
		for(int j=0; j<FW; j++) {
			if(f[i][j] == '.') {
				field[j][i] = 1;
				need[i]++;
			}
		}

		int res = -1;
		for(int i=0; i<4; i++) {
			res = max(res, solve());
			rotate();
		}

		cout << res << endl;
	}
}