#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Puzzle {
public:
	int H, W, ulx, uly;
	vector<int> v;

	Puzzle() {};
	Puzzle(int h, int w) { reset(h, w); };
	void reset(int h, int w) {
		H = h; W = w;
		v.clear();
		v.resize(H);
	}
	int get(int i, int j) {
		return (v[i] >> (H-1-j)) & 1;
	}
	void set(int i, int j, int val) {
		if (val == 0) {
			v[i] &= ~(1 << (H-1-j));
		} else {
			v[i] |= 1 << (H-1-j);
		}
	}
	void updateUpperLeft(int val) {
		for (int i=0; i<H; ++i) {
			for (int j=0; j<W; ++j) {
				if (get(i,j) == val) {
					ulx = j, uly = i;
					return ;
				}
			}
		}
		ulx = -1, uly = -1;
	}
};

Puzzle pz, piece[10][4];
bool used[10];

string solve() {
	pz.updateUpperLeft(0);
	int x = pz.ulx, y = pz.uly;
	if (x < 0) {
		for (int i=0; i<10; ++i) {
			if ( ! used[i]) return "NO";
		}
		return "YES";
	}
	for (int i=0; i<10; ++i) {
		if (used[i]) continue;
		for (int j=0; j<4; ++j) {
			int ph = piece[i][j].H;
			int pw = piece[i][j].W;
			int sx = x - piece[i][j].ulx;
			int sy = y - piece[i][j].uly;
			bool ok = true;
			for (int k=0; k<ph && ok; ++k) {
				for (int l=0; l<pw && ok; ++l) {
					if (piece[i][j].get(k,l)
						&& (sy+k<0 || pz.H<=sy+k || sx+l<0 || pz.W<=sx+l || pz.get(sy+k,sx+l))) {
							ok = false;
						}
					}
				}
			}
			if (ok) {
				used[i] = true;
				for (int k=0; k<ph; ++k) {
					for (int l=0; l<pw; ++l) {
						if (piece[i][j].get(k,l)) {
							pz.set(sy+k,sx+l,1);
						}
					}
				}
				string ret = solve();
				used[i] = false;
				for (int k=0; k<ph; ++k) {
					for (int l=0; l<pw; ++l) {
						if (piece[i][j].get(k,l)) {
							pz.set(sy+k,sx+l,0);
						}
					}
				}
				if (ret == "YES") return ret;
			}
		}
	}
	return "NO";
}

int main() {
	int H, W;
	while (cin >> H >> W, H) {
		pz.reset(H, W);
		for (int i=0; i<H; ++i) {
			char c;
			for (int j=0; j<W; ++j) {
				cin >> c;
				pz.set(i,j,c=='#'?1:0);
			}
		}
		int n, h, w;
		cin >> n;
		for (int l=0; l<n; ++l) {
			cin >> h >> w;
			piece[l][0].reset(h, w);
			for (int i=0; i<h; ++i) {
				char c;
				for (int j=0; j<w; ++j) {
					cin >> c;
					piece[l][0].set(i,j,c=='#'?1:0);
				}
			}
			for (int i=1; i<4; ++i) {
				int ph = piece[l][i-1].W;
				int pw = piece[l][i-1].H;
				piece[l][i].reset(ph, pw);
				for (int j=0; j<ph; ++j) {
					for (int k=0; k<pw; ++k) {
						piece[l][i].set(j,k,piece[l][i-1].get(pw-1-k,j));
					}
				}
			}
			for (int i=0; i<4; ++i) piece[l][i].updateUpperLeft(1);
		}
		int p, k, t;
		cin >> p;
		for (int i=0; i<p; ++i) {
			for (int j=0; j<10; ++j) used[j] = true;
			cin >> k;
			for (int j=0; j<k; ++j) {
				cin >> t;
				used[t-1] = false;
			}
			cout << solve() << endl;
		}
	}
	return 0;
}