
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

vector<string> board;
int H, W;
void update(int sy, int sx, vector<string>& piece){
	int h = piece.size();
	int w = piece[0].size();

	for(int y=0;y<h;y++)for(int x=0;x<w;x++){
		if(piece[y][x] == '#'){
			board[sy + y][sx + x] = '#';
		}
	}
}

bool match(int sy, int sx, vector<string>& piece){
	int h = piece.size();
	int w = piece[0].size();

	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][x] == '#'){
			if(board[sy+y][sx+x] == '#')return false;
		}
	}

	return true;
}

vector<string> rotate(vector<string>& vs){
	int h = vs.size();
	int w = vs[0].size();

	vector<string> ret(w, string(h, '.'));
	for(int y=0;y<h;y++)for(int x=0;x<w;x++){
		ret[w-x-1][y] = vs[y][x];
	}

	return ret;
}


struct Piece {
	vector<string> P[4];
	int size;
	Piece(vector<string> A){
		P[0] = A;
		P[1] = rotate(P[0]);
		P[2] = rotate(P[1]);
		P[3] = rotate(P[2]);

		size = A.size() * A[0].size();
	}
	vector<string>& operator[](int index){
		return P[index];
	}
};

vector<Piece> pieces;
vector<int> order;

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 solve(int n){
	if(n >= order.size()){
		return lastCheck();
	}

	vector<string> save = board;

	int index = order[n] - 1;
	// pieces[index]

	for(int kth=0;kth<4;kth++){
		for(int sy=0;sy<H;sy++)for(int sx=0;sx<W;sx++){
			if(match(sy, sx, pieces[index][kth])){
				update(sy, sx, pieces[index][kth]);

				bool can = solve(n + 1);
				board = save;
			
				if(can)return true;
			}
		}
	}

	return false;
}


void print(vector<string>& vs){
	for(int i=0;i<vs.size();i++){
		cout << vs[i] << endl;
	}
	cout << endl;
}

bool comp(const int& indexLeft, const int &indexRight){
	return (pieces[indexLeft-1].size > pieces[indexRight-1].size);
}

int main()
{
	for(;;){
		cin >> H >> W;
		if(H == 0 && W == 0)break;

		board.clear();
		board.resize(H);

		for(int y=0;y<H;y++)cin >> board[y];

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

		/*
		for(int i=0;i<pieceNum;i++){
			for(int kth=0;kth<4;kth++){
				print(pieces[i][kth]);
			}
		}*/

		int playerNum;
		cin >> playerNum;

		for(int p=0;p<playerNum;p++){
			int num;
			cin >> num;

			order.clear();
			order.resize(num);

			for(int i=0;i<num;i++)cin >> order[i];

			sort(order.begin(), order.end(), comp);


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