#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <climits>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <functional>
#include <set>
#include <numeric>
#include <stack>
#include <utility>
#include <time.h>
#include <iterator>
//#include "util.h"

using namespace std;
typedef unsigned uint;
typedef long long lint;
typedef unsigned long long ulint;


//??????
template <typename _KTy, typename _Ty> ostream& operator << (ostream& ostr, const pair<_KTy, _Ty>& m) { cout << "{" << m.first << ", " << m.second << "}"; return ostr; }
template <typename _KTy, typename _Ty> ostream& operator << (ostream& ostr, const map<_KTy, _Ty>& m) { if (m.empty()) { cout << "{ }"; return ostr; } cout << "{" << *m.begin(); for (auto itr = ++m.begin(); itr != m.end(); itr++) { cout << ", " << *itr; } cout << "}"; return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const vector<_Ty>& v) { if (v.empty()) { cout << "{ }"; return ostr; }	cout << "{" << v.front(); for (auto itr = ++v.begin(); itr != v.end(); itr++) { cout << ", " << *itr; }	cout << "}"; return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const set<_Ty>& s) { if (s.empty()) { cout << "{ }"; return ostr; } cout << "{" << *(s.begin()); for (auto itr = ++s.begin(); itr != s.end(); itr++) { cout << ", " << *itr; }	cout << "}"; return ostr; }
//template <typename T> void print(T* v, int N) { if (N == 0) cout << "{ }"; cout << "{" << v[0]; for (int p = 1; p < N; p++) cout << ", " << v[p]; cout << "}"; }
#define PI 3.14159265358979323846
#define EPS 1e-8
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define all(x) (x).begin(), (x).end()


typedef pair<int, int> pii;

struct Piece {
	pii pos;
	int h, w, area;
	int filled;
	vector<string> mp;

	Piece() {}
	Piece(int h, int w, vector<string> mp) : pos(-1, -1), h(h), w(w), area(h * w), filled(0), mp(mp) {
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				filled += (mp[i][j] == '#' ? 1 : 0);
			}
		}
	}

	void rotate() {
		vector<string> mp_tmp(w, string(h, 0));
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				mp_tmp[w - j - 1][i] = mp[i][j];
			}
		}
		mp = mp_tmp;
		swap(h, w);
	}

	friend ostream& operator << (ostream& ostr, const Piece& p) {
		ostr << endl << p.h << " " << p.w << endl;
		for (int i = 0; i < p.mp.size(); i++) {
			ostr << p.mp[i] << endl;
		}
		return ostr;
	}
};

struct Frame {
	int H, W;
	int vacant;
	vector<string> mp;
	stack<Piece> fitted;

	Frame() {}
	Frame(int H, int W, vector<string> mp) : H(H), W(W), vacant(0), mp(mp) {
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				vacant += (mp[i][j] == '.' ? 1 : 0);
			}
		}
	}

	bool push(Piece& p) {
		int si = p.pos.first;
		int sj = p.pos.second;
		int h = p.h;
		int w = p.w;

		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (p.mp[i][j] == '#' && mp[si + i][sj + j] == '#') return false;
			}
		}

		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (p.mp[i][j] == '#') mp[si + i][sj + j] = '#';
			}
		}

		fitted.push(p);
		return true;
	}

	void pop() {
		if (fitted.empty()) return;

		Piece p = fitted.top(); fitted.pop();

		int si = p.pos.first;
		int sj = p.pos.second;
		int h = p.h;
		int w = p.w;

		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (p.mp[i][j] == '#') mp[si + i][sj + j] = '.';
			}
		}
	}
};

bool comp(const Piece& lh, const Piece& rh) {
	return lh.area > rh.area;
}

bool rec(Frame& frame, vector<Piece>& pieces, int pos = 1)
{
	// ??????
	if (pos == pieces.size()) return true;

	int& H = frame.H;
	int& W = frame.W;

	Piece& piece = pieces[pos];

	for (int r = 0; r < 4; r++) {
		int& h = piece.h;
		int& w = piece.w;
		for (int i = 0; i + h <= H; i++) {
			for (int j = 0; j + w <= W; j++) {
				piece.pos = pii(i, j);
				if (frame.push(piece)) {
					if(rec(frame, pieces, pos + 1)) return true;
					frame.pop();
				}
			}
		}
		piece.rotate();
	}

	return false;
}

bool solve(Frame frame, vector<Piece> pieces) {
	return rec(frame, pieces);
}

int main()
{
	while (1) {

		int H, W;
		cin >> H >> W;
		if (H == 0) break;

		vector<string> mp(H);
		for (int i = 0; i < H; i++) cin >> mp[i];

		Frame frame(H, W, mp);


		int piece_no;
		cin >> piece_no;
		vector<Piece> pieces(piece_no + 1);
		
		for (int i = 1; i <= piece_no; i++) {
			int h, w;
			cin >> h >> w;
			vector<string> mpmp(h);
			for (int k = 0; k < h; k++) cin >> mpmp[k];

			pieces[i] = Piece(h, w, mpmp);
		}


		int player_no;
		cin >> player_no;

		for (int i = 0; i < player_no; i++) {
			int selected_piece_no;
			cin >> selected_piece_no;
			vector<Piece> selected_pieces;

			int filled_sum = 0;
			for (int j = 0; j < selected_piece_no; j++) {
				int index;
				cin >> index;
				selected_pieces.push_back(pieces[index]);
				filled_sum += pieces[index].filled;
			}

			if (filled_sum != frame.vacant) {
				cout << "NO" << endl;
				continue;
			}

			sort(all(selected_pieces), comp);

			cout << (solve(frame, selected_pieces) ? "YES" : "NO") << endl;
		}
	}
	

	return 0;
}