#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>

using namespace std;

#define reep(i,f,t) for(int i=f ; i<int(t) ; ++i)
#define rep(i,n) reep(i, 0, n) 

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

struct Board
{
	int h, w, sy, sx;
	char map[20][20];
};

struct Part
{
	Board boards[4];
	int area;
	int id;
	
	bool operator> (const Part& p)const
	{
		return area > p.area;
	}
};

int n;
Board puzzle;
int puzzleArea;
Part parts[10];

// puzÌ(y, x)ðptsÌ(sy, sx)Éí¹Ä¨¯é©ð»è·é
// r = 1ÌêÀÛÉu­
// r = 2Ìu¢Ä é¨ðæè­
bool fit(char puz[20][20], const Board pts, int y, int x, int r)
{
	int fy = y - pts.sy;
	int fx = x - pts.sx;
	if(fy<0 || puzzle.h<fy+pts.h || fx<0 || puzzle.w<fx+pts.w)
		return false;
	
	rep(i, pts.h) rep(j, pts.w){
		if(puz[fy+i][fx+j] == '#' && pts.map[i][j] == '#'){
			if(r==2)
				puz[fy+i][fx+j] = '.';
			else
				return false;
		}
		
		if(r==1 && puz[fy+i][fx+j] == '.' && pts.map[i][j] == '#')
			puz[fy+i][fx+j] = '#';
	}
	return true;
}

// frmð90xñ]³¹½àÌðtoÉ«±Þ
void rotate(const Board& frm, Board& to)
{
	rep(i, frm.h) rep(j, frm.w){
		to.map[j][frm.h-i-1] = frm.map[i][j];
	}
	to.h = frm.w;
	to.w = frm.h;
}


void dump(const char tmp[20][20], int h, int w)
{
	rep(i, h){
		rep(j, w)
			putchar(tmp[i][j]);
		puts("");
	}
}
void dump(const Board& board)
{
	dump(board.map, board.h, board.w);
}

int choice[10];
int m;

// obNgbN·é
bool search(int n, int y, int x)
{
//	printf("(%d, %d), mask = %X\n", y, x, mask);
//	dump(tmp, puzzle.h, puzzle.w);

	while(y<puzzle.h && puzzle.map[y][x] == '#'){
		++x;
		if(x == puzzle.w){
			++y;
			x = 0;
		}
	}

	// ó¢Äéêª³¢
	if(y == puzzle.h)
		return true;

	reep(i, n, m){
		rep(j, 4){
			Board& board = parts[choice[i]].boards[j];
			if(fit(puzzle.map, board, y, x, 0)){
				fit(puzzle.map, board, y, x, 1);
				swap(choice[i], choice[n]);
				bool ret = search(n+1, y, x);
				swap(choice[i], choice[n]);
				fit(puzzle.map, board, y, x, 2);
				if(ret)
					return true;
			}
		}
	}

	return false;
}

// maskÌgÝí¹Ìp[cÅÂ­êé©Ç¤©
bool collect()
{
	int sum = 0;
	rep(i, m){
		sum += parts[choice[i]].area;
	}
	if(sum != puzzleArea)
		return false;

	return search(0, 0, 0);
}

int main()
{
	while(scanf("%d%d", &puzzle.h, &puzzle.w), puzzle.h){
		puzzleArea = 0;
		rep(i, puzzle.h){
			scanf("%s", puzzle.map[i]);
			rep(j, puzzle.w)
				puzzleArea += puzzle.map[i][j] == '.';
		}

		scanf("%d", &n);
		rep(i, n){
			scanf("%d%d", &parts[i].boards[0].h, &parts[i].boards[0].w);
			parts[i].area = 0;
			parts[i].id = i;
			rep(j, parts[i].boards[0].h){
				scanf("%s", parts[i].boards[0].map[j]);
				rep(k, parts[i].boards[0].w)
					parts[i].area += parts[i].boards[0].map[j][k] == '#';
			}
			rep(j, 3){
				rotate(parts[i].boards[j], parts[i].boards[j+1]);
			}
			rep(j, 4){
				rep(k, parts[i].boards[j].h) rep(l, parts[i].boards[j].w){
					if(parts[i].boards[j].map[k][l] == '#'){
						parts[i].boards[j].sy = k;
						parts[i].boards[j].sx = l;
						k = parts[i].boards[j].h;
						break;
					}
				}
			}
		}

		sort(parts, parts+n, greater<Part>());
		map<int, int> table;
		rep(i, n)
			table[parts[i].id] = i;

		int p;
		scanf("%d", &p);
		rep(i, p){
			scanf("%d", &m);
			rep(j, m){
				int a;
				scanf("%d", &a);
				choice[j] = table[a-1];
			}
			puts(collect() ? "YES" : "NO");
		}
	}

	return 0;
}