#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;

int H, W, h[10], w[10], n;
char f[21][21], c[10][21][21], a[21][21], ret;
bool ok[10];
void rotate(int t) {
	rep(i, h[t])rep(j, w[t])a[i][j] = c[t][i][j];
	fill(c[t][0], c[t][21], 0);
	rep(i, w[t])rep(j, h[t])c[t][i][j] = a[h[t] - j - 1][i];
	swap(h[t], w[t]);
}
struct st {
	char s[21][21];
	st() {
		rep(i, H)rep(j, W)s[i][j] = f[i][j];
	}
	bool operator<(const st&r)const {
		rep(i, H)rep(j, W) {
			if (s[i][j] != r.s[i][j])return s[i][j] < r.s[i][j];
		}
		return false;
	}
};
map<st, int>mp;
bool dfs(int p) {
	st s;
	if (mp.find(s) != mp.end() && mp[s] >= p)return false;
	mp[s] = p;
	if (p == n) {
		rep(i, H)rep(j, W) {
			if (f[i][j] == '.')return false;
		}
		return true;
	}
	if (!ok[p])return dfs(p + 1);
	rep(l, 4) {
		rotate(p);
		rep(i, H - h[p] + 1)rep(j, W - w[p] + 1) {
			rep(k, h[p])rep(t, w[p]) {
				if (c[p][k][t] == '#'&&f[i + k][j + t] == '#')goto g;
			}
			rep(k, h[p])rep(t, w[p]) {
				if (c[p][k][t] == '#')f[i + k][j + t] = '#';
			}
			ret = dfs(p + 1);
			rep(k, h[p])rep(t, w[p]) {
				if (c[p][k][t] == '#')f[i + k][j + t] = '.';
			}
			if (ret)return true;
		g:;
		}
	}
	return false;
}
int main() {
	while (scanf("%d%d", &H, &W), H) {
		rep(i, H)scanf("%s", f[i]);
		scanf("%d", &n);
		rep(i, n) {
			scanf("%d%d", &h[i], &w[i]);
			rep(j, h[i])scanf("%s", c[i][j]);
		}
		int p; scanf("%d", &p);
		rep(i, p) {
			mp.clear();
			int k; scanf("%d", &k);
			memset(ok, 0, sizeof(ok));
			rep(j, k) {
				int t; scanf("%d", &t); t--;
				ok[t] = 1;
			}
			puts(dfs(0) ? "YES" : "NO");
		}
	}
}