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

bool board[22][22];
int H, W;

void update(int sy, int sx, int h, int w, bool *piece){

	for(int y=0;y<h;y++)for(int x=0;x<w;x++){
		if(piece[y*w+x]){
			board[sy + y][sx + x] = true;
		}
	}
}

bool match(int sy, int sx, int h, int w, bool* piece){
	if(sy + h - 1 >= H || sx + w - 1 >= W)return false;
	for(int y=0;y<h;y++)for(int x=0;x<w;x++){
		if(piece[y*w+x]){
			if(board[sy+y][sx+x])return false;
		}
	}

	return true;
}

void rotate(int h, int w, int &nh, int &nw, bool *src, bool* dst){
	nh = w;
	nw = h;
	for(int y=0;y<h;y++)for(int x=0;x<w;x++){
		dst[(w-x-1)*nw + y] = src[y*w+x];
	}
}


struct Piece {
	int h[4];
	int w[4];
	bool P[4][402];

	int size;
	int canSize;

	Piece(vector<string> A){
		h[0] = A.size();
		w[0] = A[0].size();

		for(int y=0;y<h[0];y++)for(int x=0;x<w[0];x++){
			P[0][y * w[0] + x] = A[y][x] == '#';
		}
		

		rotate(h[0], w[0], h[1], w[1], P[0], P[1]);
		rotate(h[1], w[1], h[2], w[2], P[1], P[2]);
		rotate(h[2], w[2], h[3], w[3], P[2], P[3]);

		/*
		P[0] = A;
		P[1] = rotate(P[0]);
		P[2] = rotate(P[1]);
		P[3] = rotate(P[2]);
		*/

		size = A.size() * A[0].size();
		canSize = 0;

		for(int kth=0;kth<4;kth++){
			for(int sy=0;sy<H;sy++)for(int sx=0;sx<W;sx++){
				if(match(sy, sx, h[kth], w[kth], P[kth])){
					canSize++;
				}
			}
		}
	}
	bool* operator[](int index){
		return P[index];
	}
};

vector<Piece> pieces;

vector<int> order;

bool lastCheck(){
	for(int y=0;y<H;y++)for(int x=0;x<W;x++){
		if(!board[y][x])return false;
	}
	return true;
}


bool solve(int n){
	if(n >= order.size()){
		return lastCheck();
	}

	bool save[22][22];
	memcpy(save, board, sizeof(board));

	int index = order[n] - 1;
	// pieces[index]

	for(int kth=0;kth<4;kth++){
		int h = pieces[index].h[kth];
		int w = pieces[index].w[kth];
		for(int sy=0;sy<H;sy++)for(int sx=0;sx<W;sx++){
			if(match(sy, sx, h, w, pieces[index][kth])){
				update(sy, sx, h, w, pieces[index][kth]);

				bool can = solve(n + 1);
				memcpy(board, save, sizeof(save));
			
				if(can)return true;
			}
		}
	}

	return false;
}

/*
void print(vector<string>& vs){
	for(int i=0;i<vs.size();i++){
		cout << vs[i] << endl;
	}
	cout << endl;
}*/

bool comp(const int& indexLeft, const int &indexRight){
	if(pieces[indexLeft-1].canSize != pieces[indexRight-1].canSize)return (pieces[indexLeft-1].canSize < pieces[indexRight-1].canSize);
	return pieces[indexLeft-1].size > pieces[indexRight-1].size;
	/*
	if(pieces[indexLeft-1].size != pieces[indexRight-1].size)return pieces[indexLeft-1].size > pieces[indexRight-1].size;
	return (pieces[indexLeft-1].canSize < pieces[indexRight-1].canSize);
	*/
}

int main()
{
	for(;;){
		cin >> H >> W;
		if(H == 0 && W == 0)break;

		memset(board, false, sizeof(board));

		for(int y=0;y<H;y++){
			for(int x=0;x<W;x++){
				char c;
				cin >> c;

				board[y][x] = c == '#';
			}
		}

		int pieceNum;
		cin >> pieceNum;

		pieces.clear();

		for(int i=0;i<pieceNum;i++){
			int h, w;
			cin >> h >> w;

			vector<string> A(h);
			for(int y=0;y<h;y++)cin >> A[y];

			pieces.push_back(Piece(A));
		}

		/*
		for(int i=0;i<pieceNum;i++){
			for(int kth=0;kth<4;kth++){
				print(pieces[i][kth]);
			}
		}*/

		int playerNum;
		cin >> playerNum;

		for(int p=0;p<playerNum;p++){
			int num;
			cin >> num;

			order.clear();
			order.resize(num);

			bool dame = false;

			for(int i=0;i<num;i++){
				cin >> order[i];
				if(pieces[order[i] - 1].canSize == 0){
					dame = true;
				}
			}

			sort(order.begin(), order.end(), comp);

			if(!dame && solve(0)){
				cout << "YES" << endl;
			} else {
				cout << "NO" << endl;
			}
		}
	}
	return 0;
}