#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int H, W;
int n;
int h[10][4], w[10][4], cnt[10];
int piece[10][20][4];

char s[32], tmp[32][32];

void rotate(int h, int w)
{
	char tmp2[32][32] = {0};
	for (int i = 0; i < h; i++){
		for (int j = 0; j < w; j++){
			tmp2[w - 1 - j][i] = tmp[i][j];
		}
	}
	
	memset(tmp, 0, sizeof(tmp));
	for (int i = 0; i < w; i++)
		for (int j = 0; j < h; j++)
			tmp[i][j] = tmp2[i][j];
}

bool cmpi(const int a, const int b)
{
	return  (cnt[a] > cnt[b]);
}

void binary(int bit)
{
	for (int i = 19; i >= 0; i--) printf("%d", bit >> i & 1);
	puts("");
}

int k;
bool check(int *mp, int *a, int idx)
{
	if (idx == k){
		return (true);
	}
	
	int ct[32][32];
	for (int i = 0; i < H; i++){
		for (int j = 0; j < W; j++){
			ct[i][j] = 1 - ((mp[i] >> (W - 1 - j)) & 1);
			if (i) ct[i][j] += ct[i - 1][j];
			if (j) ct[i][j] += ct[i][j - 1];
			if (i && j) ct[i][j] -= ct[i - 1][j - 1];
		}
	}
	
	int nidx = a[idx];
	for (int r = 0; r < 4; r++){
		for (int i = 0; i <= H - h[nidx][r]; i++){
			for (int j = 0; j <= W - w[nidx][r]; j++){
				int sy = i, sx = j, ty = i + h[nidx][r] - 1, tx = j + w[nidx][r] - 1;
				int tot = ct[ty][tx];
				if (sy) tot -= ct[sy - 1][tx];
				if (sx) tot -= ct[ty][sx - 1];
				if (sy && sx) tot += ct[sy - 1][sx - 1];
				if (tot >= cnt[nidx]){
					bool ok = true;
					for (int ii = i; ii < i + h[nidx][r]; ii++){
						if ((mp[ii] >> (W - tx - 1)) & piece[nidx][ii - i][r]){
							ok = false;
							break;
						}
					}
					if (ok){
						int nxt[20];
						memcpy(nxt, mp, sizeof(int) * 20);
						for (int ii = i; ii < i + h[nidx][r]; ii++){
							nxt[ii] |= (piece[nidx][ii - i][r] << (W - tx - 1));
						}
						if (check(nxt, a, idx + 1)) return (true);
					}
				}
			}
		}
	}
	
	return (false);
}

int main()
{
	while (scanf("%d %d", &H, &W) && H){
		int bit[20] = {0}, mp = 0;
		for (int i = 0; i < H; i++){
			scanf("%s", s);
			for (int j = 0; j < W; j++){
				bit[i] = bit[i] * 2 + (s[j] == '#');
				mp += (s[j] == '.');
			}
		}
		
		scanf("%d", &n);
		
		memset(piece, 0, sizeof(piece));
		for (int r = 0; r < n; r++){
			scanf("%d %d", &h[r][0], &w[r][0]);
			w[r][1] = h[r][2] = w[r][3] = h[r][0];
			h[r][1] = w[r][2] = h[r][3] = w[r][0];
			cnt[r] = 0;
			
			for (int i = 0; i < h[r][0]; i++){
				scanf("%s", tmp[i]);
				for (int j = 0; j < w[r][0]; j++){
					cnt[r] += (tmp[i][j] == '#');
				}
			}
			
			for (int t = 0; t < 4; t++){
				for (int i = 0; i < h[r][t]; i++){
					for (int j = 0; j < w[r][t]; j++)
						piece[r][i][t] = piece[r][i][t] * 2 + (tmp[i][j] == '#');
				}
				rotate(h[r][t], w[r][t]);
			}
		}
		
		int Q;
		scanf("%d", &Q);
		
		for (int i = 0; i < Q; i++){
			int a[10];
			int tot = 0;
			scanf("%d", &k);
			for (int j = 0; j < k; j++){
				scanf("%d", a + j);
				tot += cnt[--a[j]];
			}
			
			if (tot != mp){
				printf("NO\n");
			}
			else {
				sort(a, a + k, cmpi);
				printf("%s\n", check(bit, a, 0) ? "YES" : "NO");
			}
		}
	}
	
	return (0);
}