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

// PuzzleÆBoardÌX[p[NX
class Super
{
public:
	int h, w;
	int map[20];
	
	bool operator==(const Super& b)const {
		return h==b.h && w==b.w && equal(map, map+h, b.map);
	}
	
	// æÊÉàeð\¦·é
	void dump() const
	{
		rep(i, h){
			rep(j, w)
				putchar((map[i]>>j)&1 ? '#' : '.');
			puts("");
		}
	}
};

// 1Ìñ]óÔÌÅè³ê½pYs[Xð\·NX
class Board : public Super
{
public:
	// 90xñ]³¹½àÌðtoÉ«±Þ
	void rotate(Board& to) const
	{
		rep(i, w) to.map[i] = 0;
		rep(i, h) rep(j, w)
			to.map[j] |= ((map[i]>>j)&1) << (h-i-1);
		
		to.h = w;
		to.w = h;
	}
};

// pYð\·NX
class Puzzle : public Super
{
private:
	// (y, x)ðn_ÉA±µÄ¢é'.'ðhèÂÔµ»ÌðÔ·BoutÉÎµ½êðL·B
	int countUp(char tmp[20][20], int y, int x, int out[20], int startX) const
	{
		const int dy[] = {-1, 0, 0, 1};
		const int dx[] = {0, -1, 1, 0};
		
		int ret = 0;
		out[y] ^= 1 << (x - startX);
		tmp[y][x] = '#';
		rep(i, 4){
			int py = y + dy[i];
			int px = x + dx[i];
			if(py<0 || h<=py || px<0 || w<=px || tmp[py][px] == '#')
				continue;
			ret += countUp(tmp, py, px, out, startX);
		}
		return ret + 1;
	}

public:
	int area;

	// (x, y)ÉptsÌ(0, 0)ðí¹Äu¯é©Ç¤©ð»è·é
	bool fit(const Board& pts, int y, int x) const
	{
		if(y<0 || h<y+pts.h || x<0 || w<x+pts.w)
			return false;

		rep(i, pts.h){
			if((map[y+i]>>x) & pts.map[i])
				return false;
		}

		return true;
	}

	// (x, y)ÉptsÌ(0, 0)ðí¹Äu­
	void put(const Board& pts, int y, int x)
	{
		rep(i, pts.h)
			map[y+i] |= pts.map[i] << x;
	}

	// (x, y)©çptsÌ(0, 0)ðæè­
	void remove(const Board& pts, int y, int x)
	{
		rep(i, pts.h)
			map[y+i] ^= pts.map[i] << x;
	}

	// ¤±«ÅÅåmÂÉª¯éBª¯çê½ðreturn·éBmÂÈãÉÈÁÄµÜÁ½ç-1ªÔéB
	// ¶ûü¾¯Í±Ì_Å³ÊªðíéB
	int separate(Puzzle puzzles[], int m) const
	{
		char tmp[20][20];
		rep(i, h) rep(j, w)
			tmp[i][j] = (map[i]>>j) & 1 ? '#' : '.';

		int ret = 0;
		rep(j, w) rep(i, h){ // TõûüÓ
			if(tmp[i][j] == '.'){
				if(ret == m)
					return -1;
				puzzles[ret].h = h;
				puzzles[ret].w = w - j;
				rep(k, h) puzzles[ret].map[k] = (1<<puzzles[ret].w) - 1;
				puzzles[ret].area = countUp(tmp, i, j, puzzles[ret].map, j);
				++ret;
			}
		}
		
		return ret;
	}
	
	// üÍÌ³ÊÈªðíé
	void resize()
	{
		int top, bottom;
		rep(i, h){
			if(map[i] < (1<<w)-1){
				top = i;
				break;
			}
		}
		for(int i=h-1; i>=0; --i){
			if(map[i] < (1<<w)-1){
				bottom = i;
				break;
			}
		}
		
		int left = w;
		rep(i, h) rep(j, w){
			if(left == j || !((map[i]>>j)&1)){
				left = j;
				break;
			}
			if(left == 0)
				break;
		}

		int right, rightID = 0;
		reep(i, 1, h){
			if(~((unsigned int)map[i]) > ~((unsigned int)map[rightID]))
				rightID = i;
		}
		for(int i=w-1; i>=0; --i){
			if(!((map[rightID]>>i)&1)){
				right = i;
				break;
			}
		}

		h = bottom - top + 1;
		w = right - left + 1;
		rep(i, h){
			map[i] = ((1 << w) - 1) & (map[i+top] >> left);
		}
	}
};

// 1ÌpYs[Xð\·NX
class Piece
{
	public:
		Board boards[4];
		int boardnum;
		int area;
		int id;

		bool operator> (const Piece& p)const {
			int sa = boards[0].h*boards[0].w - p.boards[0].h*p.boards[0].w;
			return sa==0 ? area > p.area : sa > 0;
		}
};

int n;
Piece pieces[10];
pair<int, int> maskdata[1024]; // first : Â, second : ÊÏ

bool calculate(Puzzle& puzzle, int mask);
bool put(Puzzle& puzzle, int mask);
bool recursive(const Puzzle& puzzle, int mask, int now, Puzzle puzzles[], int masks[], int ret);

// ÀÛÉvZðsÈ¤
bool calculate(Puzzle& puzzle, int mask)
{
	// puts("CALLED CALCULATE -----------");
	// puts("PUZZLE -----");
	// puzzle.dump();
	// rep(i, n){
	// 	if((mask>>i)&1){
	// 		puts("--------");
	// 		pieces[i].boards[0].dump();
	// 	}
	// }
	// puts("");
	if(maskdata[mask].second != puzzle.area){
		return false;
	}
	
	Puzzle puzzles[10];
	int ret = puzzle.separate(puzzles, maskdata[mask].first);
	if(ret == -1)
		return false;
	
	rep(i, ret){
		puzzles[i].resize();
	}
	
	int masks[10] = {0};
	return recursive(puzzle, mask, 0, puzzles, masks, ret);
}

// maskÌêÔÅªÉ§ÁÄ¢ébitÌp[cðÀÛÉu¢ÄÝé
bool put(Puzzle& puzzle, int mask)
{
	// puts("CALLED PUTS ---------");
	// puts("PUZZLE -----");
	// puzzle.dump();
	// rep(i, n){
	// 	if((mask>>i)&1){
	// 		puts("--------");
	// 		pieces[i].boards[0].dump();
	// 	}
	// }
	// puts("");
	
	int c = 0;
	while(!((mask>>c)&1))
		++c;
	
	rep(i, puzzle.h) rep(j, puzzle.w){
		rep(k, pieces[c].boardnum){
			Board& board = pieces[c].boards[k];
			
			if(puzzle.fit(board, i, j)){
				if(mask == 1 << c)
					return true;
				
				puzzle.put(board, i, j);
				puzzle.area -= pieces[c].area;
				bool ret = calculate(puzzle, mask ^ (1<<c));
				puzzle.area += pieces[c].area;
				puzzle.remove(board, i, j);
				if(ret)
					return true;
			}
		}
	}

	return false;
}

bool recursive(const Puzzle& puzzle, int mask, int now, Puzzle puzzles[], int masks[], int ret)
{
	if(!mask){
		rep(i, ret){
			bool result = put(puzzles[i], masks[i]);
			if(!result)
				return false;
		}
		return true;
	}
	
	while(!((mask>>now)&1))
		++now;
	
	rep(i, ret){
		int newmask = masks[i] | (1 << now);
		if(maskdata[newmask].second > puzzles[i].area)
			continue;
		if(min(pieces[now].boards[0].h, pieces[now].boards[0].w) > max(puzzles[i].h, puzzles[i].w))
			continue;
		
		masks[i] = newmask;
		bool result = recursive(puzzle, mask ^ (1<<now), now+1, puzzles, masks, ret);
		masks[i] ^= 1 << now;
		if(result)
			return true;
	}
	
	return false;
}


int main()
{
	Puzzle puzzle;
	while(scanf("%d%d", &puzzle.h, &puzzle.w), puzzle.h){
		// puzzle Ìf[^üÍ
		puzzle.area = 0;
		rep(i, puzzle.h){
			char str[32];
			scanf("%s", str);
			puzzle.map[i] = 0;
			rep(j, puzzle.w){
				puzzle.map[i] |= (str[j]=='#')<<j;
				puzzle.area += str[j] == '.';
			}
		}
		puzzle.resize();

		// pieces Ìf[^üÍ
		scanf("%d", &n);

		rep(i, n){
			Piece& p = pieces[i];
			p.area = 0;
			p.id = i;

			scanf("%d%d", &p.boards[0].h, &p.boards[0].w);
			rep(j, p.boards[0].h){
				p.boards[0].map[j] = 0;

				char str[32];
				scanf("%s", str);
				rep(k, p.boards[0].w){
					p.boards[0].map[j] |= (str[k]=='#')<<k;
					p.area += str[k] == '#';
				}
			}

			// ñ]
			p.boardnum = 1;
			rep(j, 3){
				p.boards[p.boardnum-1].rotate(p.boards[p.boardnum]);
				if(find(p.boards, p.boards+p.boardnum, p.boards[p.boardnum]) == p.boards+p.boardnum)
					++p.boardnum;
				else
					break;
			}
		}

		// ÊÏªå«¢s[XÉ\[g
		sort(pieces, pieces+n, greater<Piece>());
		map<int, int> table;
		rep(i, n)
			table[pieces[i].id] = i;
		
		// maskdata ðú»
		rep(i, 1<<n){
			maskdata[i].first = 0;
			maskdata[i].second = 0;
			rep(j, n){
				if((i>>j)&1){
					++maskdata[i].first;
					maskdata[i].second += pieces[j].area;
				}
			}
		}

		// êxß½yAÍÛ¶µÆ­
		map<int, bool> memo;

		// NGÌinput
		int p;
		scanf("%d", &p);
		rep(i, p){
			int m;
			scanf("%d", &m);
			int mask = 0;
			rep(j, m){
				int a;
				scanf("%d", &a);
				a = table[a-1];
				mask |= 1 << a;
			}

			// ùÉß½NG
			if(memo.count(mask)){
				puts(memo[mask] ? "YES" : "NO");
				continue;
			}

			int ans = calculate(puzzle, mask);
			memo[mask] = ans;
			puts(ans ? "YES" : "NO");
		}
	}

	return 0;
}