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

int H, W;
const int SIZE = sizeof(bool) * 22 * 22;

void update(int sy, int sx, int h, int w, bool *piece, bool board[22][22]){
 
    for(int y=0;y<h;y++)for(int x=0;x<w;x++){
        if(piece[y*w+x]){
            board[sy + y][sx + x] = true;
        }
    }
}
 
bool match(int sy, int sx, int h, int w, bool* piece, bool board[22][22]){
    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*w+x]){
            if(board[sy+y][sx+x])return false;
        }
    }
 
    return true;
}
 
void rotate(int h, int w, int &nh, int &nw, bool *src, bool* dst){
    nh = w;
    nw = h;
    for(int y=0;y<h;y++)for(int x=0;x<w;x++){
        dst[(w-x-1)*nw + y] = src[y*w+x];
    }
}
 
 
struct Piece {
    int h[4];
    int w[4];
	int used;
    bool P[4][402];
 
    int size;
 
    Piece(int H, int W, bool* A){
		used = 0;
        size = H * W;

        h[0] = H;
        w[0] = W;

        for(int y=0;y<h[0];y++)for(int x=0;x<w[0];x++){
            P[0][y * w[0] + x] = A[y * w[0] + x];
			if(A[y * w[0] + x])++used;
        }

        rotate(h[0], w[0], h[1], w[1], P[0], P[1]);
        rotate(h[1], w[1], h[2], w[2], P[1], P[2]);
        rotate(h[2], w[2], h[3], w[3], P[2], P[3]);

    }
    bool* operator[](int index){
        return P[index];
    }
};
 
vector<Piece> pieces;

vector<int> order;
 
bool lastCheck(bool board[22][22]){
    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, bool board[22][22]){
    if(n >= order.size()){
        return lastCheck(board);
    }

    int index = order[n] - 1;
    // pieces[index]
 
    for(int kth=0;kth<4;kth++){
        int h = pieces[index].h[kth];
        int w = pieces[index].w[kth];
        for(int sy=0;sy<H;sy++)for(int sx=0;sx<W;sx++){
            if(match(sy, sx, h, w, pieces[index][kth], board)){
				bool next[22][22];
				memcpy(next, board, SIZE);

                update(sy, sx, h, w, pieces[index][kth], next);
                bool can = solve(n + 1, next);

                if(can)return true;
            }
        }
    }
 
    return false;
}
 
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;
 
		bool board[22][22];
        memset(board, false, sizeof(board));
 
		int holeCnt = 0;
        for(int y=0;y<H;y++){
			string line;
			cin >> line;

            for(int x=0;x<W;x++){
                char c = line[x];
 
                board[y][x] = c == '#';

				if(c == '.')++holeCnt;
            }
        }
 
        int pieceNum;
        cin >> pieceNum;
 
        pieces.clear();
		
		bool A[402];
        for(int i=0;i<pieceNum;i++){
            int h, w;
            cin >> h >> w;
			
            for(int y=0;y<h;y++){
				string line;
				cin >> line;

				for(int x=0;x<w;x++){
					A[y * w + x] = line[x] == '#';
				}
			}
 
            pieces.push_back(Piece(h, w, A));
        }
 
        int playerNum;
        cin >> playerNum;
 
        for(int p=0;p<playerNum;p++){
            int num;
            cin >> num;
 
            order.clear();
            order.resize(num);
 
			int usedCnt = 0;
            for(int i=0;i<num;i++){
                cin >> order[i];

				usedCnt += pieces[order[i]-1].used;
            }
 
            sort(order.begin(), order.end(), comp);
 
			if(holeCnt != usedCnt){
				cout << "NO" <<endl;
				continue;
			}

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

    return 0;
}