#include<stdio.h>
#include<stdlib.h>

typedef struct{
	int W,H;
	int bits[20];
} Pattern;

typedef struct{
	Pattern pat;
	int bit_count;
} Board;

typedef struct{
	Pattern pat;
	int fit_count;
	int fit[20][20];
} DirectedPiece;

typedef struct{
	DirectedPiece dir[4];
	int bit_count;
} Piece;

Board g_board;
Piece g_piece[10];
int g_debug_count;

void read_pattern(Pattern *piece, int *pbit_count){
	int y, bits, c, bit_count = 0;
	scanf("%d%d\n", &piece->H, &piece->W);
	for(y = 0; y < piece->H; y ++){
		bits = 0;
		for(; c=getchar(), c!='\n';){
			bits = bits*2 + (c=='#' ? 1 : 0);
		}
		piece->bits[y] = bits;
		for(; bits; bits&=bits-1)
			bit_count ++;
	}
	*pbit_count = bit_count;
}

void rotate_pattern(Pattern *dest, Pattern *src){
	int y, x, bits;
	dest->H = src->W;
	dest->W = src->H;
	for(y = 0; y < dest->H; y ++){
		bits = 0;
		for(x = 0; x < dest->W; x ++){
			bits = bits*2 + (src->bits[x] >> y & 1);
		}
		dest->bits[y] = bits;
	}
}

int check_fit_pattern(const Pattern *board, const Pattern *piece, int piece_x, int piece_y){
	int y, H = piece->H;
	g_debug_count ++;
	for(y = 0; y < H; y ++)
		if(board->bits[piece_y + y] & (piece->bits[y] << piece_x))
			return 0;
	return 1;
}

void add_pattern(Pattern *board, const Pattern *piece, int piece_x, int piece_y){
	int y, H = piece->H;
	for(y = 0; y < H; y ++)
		board->bits[piece_y + y] |= piece->bits[y] << piece_x;
}

void prepare_fit_directed_piece(DirectedPiece *piece){
	int x, y, board_H, board_W, piece_H, piece_W, fit_count = 0;
	board_H = g_board.pat.H;
	board_W = g_board.pat.W;
	piece_H = piece->pat.H;
	piece_W = piece->pat.W;
	for(y = 0; y+piece_H <= board_H; y ++)
		for(x = 0; x+piece_W <= board_W; x ++)
			fit_count += piece->fit[x][y] = check_fit_pattern(&g_board.pat, &piece->pat, x, y);
	piece->fit_count = fit_count;
}

int check_filled_pattern(const Pattern *pat){
	int y;
	for(y = 0; y < pat->H; y ++)
		if(pat->bits[y] != (1 << pat->W) - 1)
			return 0;
	return 1;
}

int check_puzzle(const Pattern *board_org, int piece_num, int *piece_index){
	int i, dir, x, y, board_H, board_W, piece_H, piece_W;
	Pattern board;
	DirectedPiece *piece;

	if(piece_num == 0)
		return 1;
	board_H = board_org->H;
	board_W = board_org->W;

	board = *board_org;
	for(i = 0; i < piece_num; i ++){
		for(dir = 0; dir < 4; dir ++){
			piece = &g_piece[piece_index[i]].dir[dir];
			piece_H = piece->pat.H;
			piece_W = piece->pat.W;
			for(y = 0; y+piece_H <= board_H; y ++){
				for(x = 0; x+piece_W <= board_W; x ++){
					if(piece->fit[x][y] && check_fit_pattern(board_org, &piece->pat, x, y)){
						add_pattern(&board, &piece->pat, x, y);
					}
				}
			}
		}
	}
	if(!check_filled_pattern(&board)){
		return 0;
	}

	for(dir = 0; dir < 4; dir ++){
		piece = &g_piece[*piece_index].dir[dir];
		piece_H = piece->pat.H;
		piece_W = piece->pat.W;
		for(y = 0; y+piece_H <= board_H; y ++){
			for(x = 0; x+piece_W <= board_W; x ++){
				if(piece->fit[x][y] && check_fit_pattern(board_org, &piece->pat, x, y)){
					board = *board_org;
					add_pattern(&board, &piece->pat, x, y);
					if(check_puzzle(&board, piece_num - 1, piece_index + 1))
						return 1;
				}
			}
		}
	}
	return 0;
}

int cmp_fit_count(const int *piece_index1, const int *piece_index2){
	int i, fit_count1 = 0, fit_count2 = 0;
	for(i = 0; i < 4; i ++){
		fit_count1 += g_piece[*piece_index1].dir[i].fit_count;
		fit_count2 += g_piece[*piece_index2].dir[i].fit_count;
	}
	return fit_count1 - fit_count2;
}

int cmp_bit_count(const int *piece_index1, const int *piece_index2){
	return g_piece[*piece_index2].bit_count - g_piece[*piece_index1].bit_count;
}

int main(){
	int i, dir, player_num, piece_num, piece_index[10], bit_count;
	for(;read_pattern(&g_board.pat, &bit_count), g_board.pat.H;){
		g_board.bit_count = bit_count;
		scanf("%d", &piece_num);
		for(i = 0; i < piece_num; i ++){
			read_pattern(&g_piece[i].dir[0].pat, &bit_count);
			g_piece[i].bit_count = bit_count;
			prepare_fit_directed_piece(&g_piece[i].dir[0]);
			for(dir = 1; dir < 4; dir ++){
				rotate_pattern(&g_piece[i].dir[dir].pat, &g_piece[i].dir[dir-1].pat);
				prepare_fit_directed_piece(&g_piece[i].dir[dir]);
			}
		}
		scanf("%d", &player_num);
		g_debug_count = 0;
		for(;player_num--;){
			int piece_used[10] = {0}, dup = 0;
			scanf("%d", &piece_num);
			if(piece_num > 10)
				exit(1);
			bit_count = g_board.pat.H * g_board.pat.W - g_board.bit_count;
			for(i = 0; i < piece_num; i ++){
				scanf("%d", piece_index + i);
				piece_index[i] --; // 0 origin
				if(piece_used[piece_index[i]])
					dup = 1;
				piece_used[piece_index[i]] = 1;
				bit_count -= g_piece[piece_index[i]].bit_count;
			}
			qsort(piece_index, piece_num, sizeof(*piece_index), cmp_bit_count);
			puts(dup == 0 && bit_count == 0 && check_puzzle(&g_board.pat, piece_num, piece_index) ? "YES" : "NO");
		}
		//printf("DBG:%d\n", g_debug_count);
	}
	return 0;
}

Measured memory_bytes: 460