#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];

	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]);
	}
	bool* operator[](int index){
		return P[index];
	}
};

vector<Piece> pieces;
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 allow(int n, int mask){
	for(int i=0;i<n;i++){
		int y = i / W;
		int x = i % W;
		if(!board[y][x])return false;
	}
	
	for(int i=0;i<pieces.size();i++)if((mask >> i) & 1){
		bool ok = false;
		for(int kth=0;kth<4;kth++){
			int h = pieces[i].h[kth];
			int w = pieces[i].w[kth];

			for(int id=n;id<H*W;id++){
				int y = id / W;
				int x = id % W;
				if(match(y, x, h, w, pieces[i][kth])){
					ok = true;
					break;
				}
			}

			if(ok)break;
		}

		if(!ok)return false;
	}
	return true;
}


void print(){
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			cout << (board[i][j] ? "#" : ".");
		}
		cout << endl;
	}
	cout << endl;
}

bool solve(int n, int mask){
	if(n >= H * W){
		if(mask != 0)return false;
		return lastCheck();
	}
	if(!allow(n, mask))return false;

	int y = n / W;
	int x = n % W;

	if(solve(n + 1, mask)){
		return true;
	}

	bool save[22][22];
	memcpy(save, board, sizeof(board));
	
	for(int i=0;i<pieces.size();i++)if((mask >> i) & 1){
		for(int kth=0;kth<4;kth++){
			int h = pieces[i].h[kth];
			int w = pieces[i].w[kth];
			if(match(y, x, h, w, pieces[i][kth])){
				update(y, x, h, w, pieces[i][kth]);

				bool can = solve(n + 1, mask ^ (1 << i));

				memcpy(board, save, sizeof(save));

				if(can)return true;
			}
		}
	}


	return false;
}

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));
		}

		int playerNum;
		cin >> playerNum;

		for(int p=0;p<playerNum;p++){
			int num;
			cin >> num;
			
			int mask = 0;
			for(int i=0;i<num;i++){
				int ord;
				cin >> ord;
				ord--;

				mask |= (1 << ord);
			}

			if(solve(0, mask)){
				cout << "YES" << endl;
			} else {
				cout << "NO" << endl;
			}
		}
	}
	return 0;
}