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

int H, W, n;
int pzl[20][20];

class Piece {
public:
	int id, pH, pW, area, r;
	int v[4][20][20];
	Piece(int id, int pH, int pW) : id(id), pH(pH), pW(pW) {
		area = 0;
		r = 0;
		memset(v, 0, sizeof v);
	};
	int get(int y, int x) {
		return v[r][y][x];
	}
	void set(int y, int x, int val) {
		if ( ! v[0][y][x] && val) area ++;
		if (v[0][y][x] && ! val) area --;
		v[0][y][x] = val;
		v[1][x][pH-1-y] = val;
		v[2][pH-1-y][pW-1-x] = val;
		v[3][pW-1-x][y] = val;
	}
	void rotate() {
		r = (r + 1) % 4;
		swap(pH, pW);
	}
	bool operator<(const Piece &obj) const {
		return area > obj.area;
	}
};
vector<Piece> pieces;

string dfs(vector<Piece> &v, int idx, const int endIdx) {
	if (idx == endIdx) return "YES";
	for (int i=0; i<4; ++i) {
		int pH = v[idx].pH;
		int pW = v[idx].pW;
		for (int y=0; y+pH-1<H; ++y) {
			for (int x=0; x+pW-1<W; ++x) {
				bool placeable = true;
				for (int j=0; j<pH && placeable; ++j) {
					for (int k=0; k<pW; ++k) {
						if (v[idx].get(j,k) && pzl[y+j][x+k]) {
							placeable = false;
							break;
						}
					}
				}
				if ( ! placeable) continue;
				// place
				for (int j=0; j<pH; ++j) {
					for (int k=0; k<pW; ++k) {
						if (v[idx].get(j,k)) {
							pzl[y+j][x+k] = 1;
						}
					}
				}
				string res = dfs(v, idx+1, endIdx);
				// restore
				for (int j=0; j<pH; ++j) {
					for (int k=0; k<pW; ++k) {
						if (v[idx].get(j,k)) {
							pzl[y+j][x+k] = 0;
						}
					}
				}
				if (res == "YES") return "YES";
			}
		}
		v[idx].rotate();
	}
	return "NO";
}

int main() {
	while (cin >> H >> W, H) {
		int emptyCount = 0;
		char c;
		for (int i=0; i<H; ++i) {
			for (int j=0; j<W; ++j) {
				cin >> c;
				pzl[i][j] = c == '#' ? 1 : 0;
				emptyCount += 1 - pzl[i][j];
			}
		}
		cin >> n;
		pieces.clear();
		int pH, pW;
		for (int i=0; i<n; ++i) {
			cin >> pH >> pW;
			Piece t(i, pH, pW);
			for (int j=0; j<pH; ++j) {
				for (int k=0; k<pW; ++k) {
					cin >> c;
					t.set(j, k, c == '#' ? 1 : 0);
				}
			}
			pieces.push_back(t);
		}
		int p, k, t;
		cin >> p;
		for (int i=0; i<p; ++i) {
			vector<Piece> pick;
			cin >> k;
			for (int j=0; j<k; ++j) {
				cin >> t;
				pick.push_back(pieces[t-1]);
			}
			int area = 0;
			for (int j=0; j<(int)pick.size(); ++j) {
				area += pick[j].area;
			}
			if (area != emptyCount) {
				cout << "NO" << endl;
				continue;
			}
			sort(pick.begin(), pick.end());
			cout << dfs(pick, 0, k) << endl;
		}
	}
	return 0;
}