#include <ctime>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
const int inf = 1012345678;
vector<string> compress(vector<string> S) {
	int H = S.size(), W = S[0].size();
	int xl = inf, xr = -inf, yl = inf, yr = -inf;
	for (int i = 0; i < H; ++i) {
		for (int j = 0; j < W; ++j) {
			if (S[i][j] == '#') {
				xl = min(xl, i);
				xr = max(xr, i);
				yl = min(yl, j);
				yr = max(yr, j);
			}
		}
	}
	vector<string> T(xr - xl + 1);
	for (int i = xl; i <= xr; ++i) {
		T[i - xl] = S[i].substr(yl, yr - yl + 1);
	}
	return T;
}
vector<string> rotate90(vector<string> S) {
	int H = S.size(), W = S[0].size();
	vector<string> T(W, string(H, '?'));
	for (int i = 0; i < H; ++i) {
		for (int j = 0; j < W; ++j) {
			T[W - j - 1][i] = S[i][j];
		}
	}
	return T;
}
int main() {
	int H, W, N;
	while (cin >> H >> W, H) {
		vector<string> S(H);
		for (int i = 0; i < H; ++i) {
			cin >> S[i];
		}
		int all_sz = 0;
		vector<int> B(H);
		for (int i = 0; i < H; ++i) {
			for (int j = 0; j < W; ++j) {
				if (S[i][j] == '#') {
					B[i] |= 1 << j;
					++all_sz;
				}
			}
		}
		cin >> N;
		vector<int> R(N), C(N), SZ(N);
		vector<vector<int> > RX(N, vector<int>(4)), CX(N, vector<int>(4)), OY(N, vector<int>(4));
		vector<vector<vector<int> > > D(N, vector<vector<int> >(4));
		for (int i = 0; i < N; ++i) {
			cin >> R[i] >> C[i];
			vector<string> T(R[i]);
			for (int j = 0; j < R[i]; ++j) {
				cin >> T[j];
				for (int k = 0; k < C[i]; ++k) {
					if (T[j][k] == '#') ++SZ[i];
				}
			}
			T = compress(T);
			for (int j = 0; j < 4; ++j) {
				RX[i][j] = T.size();
				CX[i][j] = T[0].size();
				D[i][j].resize(RX[i][j]);
				for (int k = 0; k < RX[i][j]; ++k) {
					for (int l = 0; l < CX[i][j]; ++l) {
						if (T[k][l] == '#') {
							D[i][j][k] |= 1 << l;
						}
					}
				}
				for (int k = 0; k < CX[i][j]; ++k) {
					if (T[0][k] == '#') {
						OY[i][j] = k;
						break;
					}
				}
				T = rotate90(T);
			}
		}
		vector<int> cur = B;
		int u = clock();
		function<bool(int)> dfs = [&](int bit) {
			if (bit == 0) return true;
			if (clock() - u > CLOCKS_PER_SEC) return false;
			int lx = -1, ly = -1;
			for (int i = 0; i < H; ++i) {
				if (cur[i] != (1 << W) - 1) {
					for (int j = 0; j < W; ++j) {
						if (!((cur[i] >> j) & 1)) {
							lx = i, ly = j;
							break;
						}
					}
					break;
				}
			}
			for (int i = 0; i < N; ++i) {
				if (!((bit >> i) & 1)) continue;
				for (int j = 0; j < 4; ++j) {
					if (lx + RX[i][j] > H || ly + CX[i][j] - OY[i][j] > W || ly - OY[i][j] < 0) continue;
					bool ok = true;
					for (int k = 0; k < RX[i][j]; ++k) {
						if ((cur[lx + k] & (D[i][j][k] << (ly - OY[i][j]))) != 0) {
							ok = false;
						}
					}
					if (!ok) continue;
					for (int k = 0; k < RX[i][j]; ++k) {
						cur[lx + k] ^= D[i][j][k] << (ly - OY[i][j]);
					}
					bool res = dfs(bit - (1 << i));
					for (int k = 0; k < RX[i][j]; ++k) {
						cur[lx + k] ^= D[i][j][k] << (ly - OY[i][j]);
					}
					if (res) return true;
				}
			}
			return false;
		};
		int Q;
		cin >> Q;
		for (int i = 0; i < Q; ++i) {
			int K;
			cin >> K;
			vector<int> E(K);
			int bit = 0, sum_sz = all_sz;
			for (int j = 0; j < K; ++j) {
				cin >> E[j]; --E[j];
				bit |= 1 << E[j];
				sum_sz += SZ[E[j]];
			}
			if (sum_sz != H * W) {
				cout << "NO\n";
			}
			else {
				bool ans = dfs(bit);
				cout << (ans ? "YES\n" : "NO\n");
			}
		}
	}
	return 0;
}


Measured memory_bytes: 3188