#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vb> vvb;

// ??????????????¶???
struct Parts {
	//h ?????? w ??? s ?????????????¨?
	int h, w, s;
	vvb info;
	
	Parts() {}
	Parts(int h, int w, vvb info) : h(h), w(w), info(info) {
		s = 0;
		REP(i, h) REP(j, w) if (info[i][j]) s++;
	}
	
	bool isFill() {
		return s == 0;
	}
	
	bool operator <(const Parts &r) const {
		return s < r.s;
	}
};

bool dfs(int pos); //??£?§£?????????????????????
bool check(int x, int y, Parts &a); //?????????a????????°???????????????????????????????????????
void fit(int x, int y, Parts &a); //?????°???????????????????????????a????????????
void remove(int x, int y, Parts &a); //?????°??????????????????????????????a?????????
vi count(); //?????°???????????????????????????????????????????????????????¨?
vvb input(int h, int w); //??\???
Parts rotate(Parts &p); //?????????????????¢


int p;
Parts pazzle; //?????°???????????????
vector<Parts> vp; //i?????????????????????(i * 4) i?????????(j[0,4) * 90)?°?????¢???????????????(i * 4 + j)
vector<pair<Parts, int> > use; //??????????????????, vp??§???????´???????
vector<int> sum; //i????????\?????????????????§????????????????¨??????????

int main() {
	while (true) {
		int h, w;
		cin >> h >> w;
		if (!h && !w) break;
		pazzle = Parts(h, w, input(h, w));
		pazzle.s = h * w - pazzle.s;
		
		int n;
		cin >> n;
		
		vp.resize(n * 4);
		REP(i, n) {
			cin >> h >> w;
			vp[i * 4] = Parts(h, w, input(h, w));
			FOR(j, 1, 4) vp[i * 4 + j] = rotate(vp[i * 4 + j - 1]);
		}
		
		int q;
		cin >> q;
		while (q--) {
			cin >> p;
			vector<int> in(p);
			REP(i, p) {
				cin >> in[i];
				in[i]--;
			}
			
			use.resize(p);
			REP(i, p) use[i] = make_pair(vp[in[i] * 4], in[i] * 4);
			sort(use.rbegin(), use.rend());
			
			sum.resize(p);
			sum[p - 1] = use[p - 1].first.s;
			for (int i = p - 2; i >= 0; i--)
				sum[i] = sum[i + 1] + use[i].first.s;
			
			if (sum[0] == pazzle.s && dfs(0))
				puts("YES");
			else
				puts("NO");
		}
	}
	
	return 0;
}

bool dfs(int pos) {
	if (pos == p) return true;
	
	vi tmp = count();
	sort(tmp.begin(), tmp.end());
	if (sum[pos] < tmp[tmp.size() - 1] || sum[p - 1] > tmp[0]) return false;
	
	REP(k, 4) {
		Parts &now = vp[use[pos].second + k];
		
		REP(i, pazzle.h - now.h + 1) REP(j, pazzle.w - now.w + 1) {
			if (!check(j, i, now)) continue;
			fit(j, i, now);
			bool ans = dfs(pos + 1);
			remove(j, i, now);
			if (ans) return true;
		}
	}
	
	return false;
}

bool check(int x, int y, Parts &a) {
	REP(i, a.h) REP(j, a.w) if (a.info[i][j])
		if (pazzle.info[y + i][x + j]) return false;
	return true;
}

void fit(int x, int y, Parts &a) {
	REP(i, a.h) REP(j, a.w) if (a.info[i][j])
		pazzle.info[y + i][x + j] = true;
}

void remove(int x, int y, Parts &a) {
	REP(i, a.h) REP(j, a.w) if (a.info[i][j])
		pazzle.info[y + i][x + j] = false;
}

vi count() {
	int h = pazzle.h, w = pazzle.w;
	vvb used(h, vb(w, false));
	vi res;
	
	REP(i, h) REP(j, w) {
		if (pazzle.info[i][j] || used[i][j]) continue;
		
		int cnt = 0;
		queue<pii> que;
		que.push(pii(j, i));
		while (!que.empty()) {
			int x = que.front().first, y = que.front().second;
			que.pop();
			
			if (pazzle.info[y][x] || used[y][x]) continue;
			
			used[y][x] = true;
			cnt++;
			
			int dx[4] = {1,0,-1,0};
			int dy[4] = {0,1,0,-1};
			REP(k, 4) {
				int nx = x + dx[k], ny = y + dy[k];
				if (!(nx >= 0 && nx < w && ny >= 0 && ny < h)) continue;
				que.push(pii(nx, ny));
			}
		}
		res.push_back(cnt);
	}
	
	return res;
}

vvb input(int h, int w) {
	vvb res(h, vb(w, false));
	vector<string> str(h);
	REP(i, h) cin >> str[i];
	REP(i, h) REP(j, w)
		if (str[i][j] == '#') res[i][j] = true;
	return res;
}

Parts rotate(Parts &p) {
	vvb info(p.w, vb(p.h));
	REP(i, p.w) REP(j, p.h)
		info[i][j] = p.info[p.h - j - 1][i];
	return Parts(p.w, p.h, info);
}