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

bool search(char tmp[20][20], int mask, int p)
{
	// à¤u­p[cªÈ¢
	if(p == n){
		rep(i, puzzle.h) rep(j, puzzle.w){
			if(tmp[i][j] == '.')
				return false;
		}
		return true;
	}
	
	// p[cpÍgíÈ¢
	if(((mask>>p)&1) == 0)
		return search(tmp, mask, p+1);
	
	// p[cpðu¢ÄÝé
	rep(k, 4){
		rep(i, puzzle.h-parts[p].boards[k].h+1){
			rep(j, puzzle.w-parts[p].boards[k].w+1){
				if(fit(tmp, parts[p].boards[k], i, j, 0)){
					fit(tmp, parts[p].boards[k], i, j, 1);
					bool ret = search(tmp, mask, p+1);
					fit(tmp, parts[p].boards[k], i, j, 2);
					
					if(ret)
						return true;
				}
			}
		}
	}
	
	return false;
}

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

	// pYðobt@ÉRs[
	char p[20][20];
	rep(i, puzzle.h) rep(j, puzzle.w)
		p[i][j] = puzzle.map[i][j];

	return search(p, mask, 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]);
			}
		}
		
		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){
			int m;
			scanf("%d", &m);
			int mask = 0;
			rep(j, m){
				int a;
				scanf("%d", &a);
				mask |= 1<<table[a-1];
			}
			puts(collect(mask) ? "YES" : "NO");
		}
	}

	return 0;
}