#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <climits>
#include <algorithm>
#include <map>
#include <bitset>
using namespace std;

int H, W;
vector<int> pz;

int n;
vector<int> piece[10][4];
int pH[10][4], pW[10][4];
int area[10];
bool used[10];

void print(vector<int> &v) {
	for (int i=0; i<(int)v.size(); ++i) {
		cout << bitset<20>(v[i]) << endl;
	}
	cout << endl;
}

vector<int> rotate(vector<int> &bits, int h, int w) {
	swap(h, w);
	vector<int> ret(h, 0);
	for (int i=0; i<h; ++i) {
		ret[i] = 0;
		for (int j=0; j<w; ++j) {
			ret[i] = (ret[i] << 1) + ((bits[w-1-j] >> (h-1-i)) & 1);
		}
	}
	return ret;
}

struct Candidate { int id, r, x, y; };

string dfs(int rest) {
	// print(pz);
	if (rest == 0) {
		bool ok = true;
		for (int i=0; i<H; ++i) {
			if (pz[i] + 1 != (1 << W)) {
				ok = false; break;
			}
		}
		return (ok ? "YES" : "NO");
	}

	vector<Candidate> cand[20][20];
	for (int k=0; k<n; ++k) {
		if (used[k]) continue;
		for (int r=0; r<4; ++r) {
			int ph = pH[k][r];
			int pw = pW[k][r];
			for (int y=0; y+ph-1<H; ++y) {
				for (int x=0; x+pw-1<W; ++x) {
					bool placeable = true;
					for (int i=0; i<ph; ++i) {
						if ((piece[k][r][i] << (W-pw-x)) & pz[y+i]) {
							placeable = false;
							break;
						}
					}
					if ( ! placeable) continue;
					for (int i=0; i<ph; ++i) {
						for (int j=0; j<pw; ++j) {
							if (((piece[k][r][i] >> (pw-1-j)) & 1) == 1) {
								cand[y+i][x+j].push_back((Candidate){ k, r, x, y });
							}
						}
					}
				}
			}
		}
	}
	int mn = 1000, px = -1, py = -1;
	bool bad = false;
	for (int y=0; y<H && !bad; ++y) {
		for (int x=0; x<W; ++x) {
			int sz = cand[y][x].size();
			if (sz && sz < mn) {
				mn = sz;
				px = x;
				py = y;
			}
			if (!sz && ((pz[y] >> (W-1-x)) & 1) == 0) {
				bad = true;
				break;
			}
		}
	}
	if (mn == 1000) {
		return "NO";
	}

	bool prev_used[10];
	vector<int> prev_pz;
	for (int i=0; i<(int)cand[py][px].size(); ++i) {
		int k = cand[py][px][i].id;
		int r = cand[py][px][i].r;
		int x = cand[py][px][i].x;
		int y = cand[py][px][i].y;
		prev_pz = pz;
		memcpy(prev_used, used, sizeof prev_used);
		used[k] = true;
		for (int j=0; j<pH[k][r]; ++j) {
			pz[y+j] |= (piece[k][r][j] << (W-pW[k][r]-x));
		}
		string res = dfs(rest-1);
		pz = prev_pz;
		memcpy(used, prev_used, sizeof used);
		used[k] = false;
		if (res == "YES") return "YES";
	}
	return "NO";
}

bool easycheck() {
	int tot1 = 0;
	for (int i=0; i<n; ++i) {
		if (used[i]) continue;
		tot1 += area[i];
	}
	int tot2 = 0;
	for (int i=0; i<H; ++i) {
		for (int j=0; j<W; ++j) {
			tot2 += ((pz[i] >> (W-1-j)) & 1) == 0;
		}
	}
	return tot1 == tot2;
}

int main() {
	while (cin >> H >> W, H) {
		pz.resize(H);
		char c;
		for (int i=0; i<H; ++i) {
			pz[i] = 0;
			for (int j=0; j<W; ++j) {
				cin >> c;
				pz[i] = (pz[i] << 1) + (c == '#' ? 1 : 0);
			}
		}
		cin >> n;
		for (int i=0; i<n; ++i) {
			cin >> pH[i][0] >> pW[i][0];
			piece[i][0].resize(pH[i][0]);
			area[i] = 0;
			char c;
			// default piece
			for (int j=0; j<pH[i][0]; ++j) {
				piece[i][0][j] = 0;
				for (int k=0; k<pW[i][0]; ++k) {
					cin >> c;
					piece[i][0][j] = (piece[i][0][j] << 1) + (c == '#' ? 1 : 0);
					area[i] += (c == '#' ? 1 : 0);
				}
			}
			// rotation
			for (int r=1; r<4; ++r) {
				piece[i][r] = rotate(piece[i][r-1], pH[i][r-1], pW[i][r-1]);
				pH[i][r] = pW[i][r-1];
				pW[i][r] = pH[i][r-1];
			}
		}
		int p, k, t;
		cin >> p;
		for (int i=0; i<p; ++i) {
			fill(used, used+10, true);
			cin >> k;
			for (int j=0; j<k; ++j) {
				cin >> t;
				used[t-1] = false;
			}
			if ( ! easycheck()) {
				cout << "NO" << endl;
			} else {
				cout << dfs(k) << endl;
			}
		}
	}
	return 0;
}