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

const int MAX_S = 65;

int _bh, _bw, bh, bw;
char _B[MAX_S][MAX_S];
char B[MAX_S][MAX_S];
int H, W;
char F[MAX_S][MAX_S];
/*
int Rx[MAX_S][MAX_S][MAX_S][MAX_S][4];
int Ry[MAX_S][MAX_S][MAX_S][MAX_S][4];
*/
int Rx(int x, int y, int dir) {
  switch(dir) {
  case 0: return x;
  case 1: return bw - y - 1;
  case 2: return bw - x - 1;
  case 3: return y;
  }
}

int Ry(int x, int  y, int dir) {
  switch(dir) {
  case 0: return y;
  case 1: return x;
  case 2: return bh - y - 1;
  case 3: return bh - x - 1;
  }
}
/*
void make() {
  for(int i = 0; i < MAX_S; ++i) {
    for(int j = 0; j < MAX_S; ++j) {
      for(int h = 1; h < MAX_S; ++h) {
	for(int w = 1; w < MAX_S; ++w) {
	  for(int d = 0; d < 4; ++d) {
	    Rx[i][j][h][w][d] = rx(j, i, w, h, d);
	    Ry[i][j][h][w][d] = ry(j, i, w, h, d);
	  }
	}
      }
    }
  }
}
*/
void cut() {
  int sx, sy, ex, ey;
  for(int j = 0; j < _bw; ++j)
    for(int i = 0; i < _bh; ++i) 
      if(_B[i][j] == '#') {
	sx = j;
	j = _bw;
	break;
      }
  for(int j = _bw-1; j >= 0; --j)
    for(int i = 0; i < _bh; ++i)
      if(_B[i][j] == '#') {
	ex = j+1;
	j = 0;
	break;
      }
  for(int i = 0; i < _bh; ++i)
    for(int j = 0; j < _bw; ++j)
      if(_B[i][j] == '#') {
	sy = i;
	i = _bh;
	break;
      }
  for(int i = _bh-1; i >= 0; --i)
    for(int j = 0; j < _bw; ++j)
      if(_B[i][j] == '#') {
	ey = i+1;
	i = 0;
	break;
      }
  bw = ex-sx;
  bh = ey-sy;
  for(int i = sy; i < ey; ++i) {
    for(int j = sx; j < ex; ++j) {
      B[i-sy][j-sx] = _B[i][j];
    }
  }
}

int show() {
  for(int i = 0; i < bh; ++i) {
    for(int j = 0; j < bw; ++j) {
      cout << B[i][j];
    }
    cout << endl;
  }
}

bool canPut(int x, int y, int w, int h, int dir) {
  for(int i = 0; i < h; ++i) {
    for(int j = 0; j < w; ++j) {
      int rx = Rx(j, i, dir);
      int ry = Ry(j, i, dir);
      if(B[ry][rx] == '#') {
	int nx = x + j;
	int ny = y + i;
	if(nx < 0 || nx >= W) return false;
	if(ny < 0 || ny >= H) return false;
	if(F[ny][nx] == '#') return false;
      }
    }
  }
  return true;
}

int put(int x, int y, int w, int h, int dir) {
  int res = 0;
  for(int i = y; i < y+h; ++i) {
    bool canBreak = true;
    for(int j = 0; j < W; ++j) {
      if(F[i][j] != '#') {
	int nx = j - x;
	int ny = i - y;
	int rx = Rx(nx, ny, dir);
	int ry = Ry(nx, ny, dir);
	if(nx < 0 || nx >= w || ny < 0 || ny >= h
	   || B[ry][rx] != '#') {
	    canBreak = false;
	    break;
	}
      }
    }
    if(canBreak) ++res;
  }
  return res;
}

int solve() {
  int res = -1;
  cut();
  for(int d = 0; d < 4; ++d) {
    int tw, th;
    if(d%2 == 0) {
      tw = bw; th = bh;
    } else {
      tw = bh; th = bw;
    }
    for(int y = 0; y <= H-th; ++y) {
      for(int x = 0; x <= W-tw; ++x) {
	if(canPut(x, y, tw, th, d)) {
	  res = max(res, put(x, y, tw, th, d));
	}
      }
    }
  }

  return res;
}

int main() {
  //make();
  int T;
  cin >> T;
  while(T--) {
    cin >> _bh >> _bw;
    for(int i = 0; i < _bh; ++i) {
      for(int j = 0; j < _bw; ++j) {
	cin >> _B[i][j];
      }
    }
    cin >> H >> W;
    for(int i = 0; i < H; ++i) {
      for(int j = 0; j < W; ++j) {
	cin >> F[i][j];
      }
    }
    cout << solve() << endl;
  }
  return 0;
}

Measured memory_bytes: 0