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

int H, W, n;
char puzzle[20][20];

vector<vector<vector<char>>> piece;

bool used[10];
map<int, bool> correct;

bool can(int x, int y, vector<vector<char>> &p) {
    int px = p[0].size();
    int py = p.size();
    for (int i=0; i<py; ++i) {
        for (int j=0; j<px; ++j) {
            if (p[i][j] == '#') {
                if (y+i < 0 || H <= y+i || x+j < 0 || W <= x+j || puzzle[y+i][x+j] != '.') {
                    return false;
                }
            }
        }
    }
    return true;
}

void put(int x, int y, vector<vector<char>> &p) {
    int px = p[0].size();
    int py = p.size();
    for (int i=0; i<py; ++i) {
        for (int j=0; j<px; ++j) {
            if (y+i < 0 || H <= y+i || x+j < 0 || W <= x+j) {
                continue;
            }
            if (p[i][j] == '#') {
                puzzle[y+i][x+j] = '#';
            }
        }
    }
}

void rotate(vector<vector<char>> &p) {
    int h = p[0].size();
    int w = p.size();
    vector<vector<char>> res(h, vector<char>(w));
    for (int i=0; i<h; ++i) {
        for (int j=0; j<w; ++j) {
            res[i][j] = p[j][h-i-1];
        }
    }
    p = res;
}

void solve() {
    int x = -1, y;
    for (int i=0; i<H && x<0; ++i) {
        for (int j=0; j<W && x<0; ++j) {
            if (puzzle[i][j] == '.') {
                x = j;
                y = i;
            }
        }
    }
    if (x < 0) {
        int key = 0;
        for (int i=0; i<10; ++i) {
            if (used[i]) key |= 1 << i;
        }
        correct[key] = true;
        return ;
    }
    for (int i=0; i<(int)piece.size(); ++i) {
        if ( ! used[i]) {
            for (int j=0; j<4; ++j) {
                int px = piece[i][0].size();
                int py = piece.size();
                int lux = -1, luy;
                for (int k=0; k<py && lux<0; ++k) {
                    for (int l=0; l<px && lux<0; ++l) {
                        if (piece[i][k][l] == '#') {
                            lux = l;
                            luy = k;
                        }
                    }
                }
                if (can(x-lux, y-luy, piece[i])) {
                    int tmp[20][20];
                    for (int k=0; k<H; ++k) {
                        for (int l=0; l<W; ++l) {
                            tmp[k][l] = puzzle[k][l];
                        }
                    }
                    used[i] = true;
                    put(x-lux, y-luy, piece[i]);
                    solve();
                    for (int k=0; k<H; ++k) {
                        for (int l=0; l<W; ++l) {
                            puzzle[k][l] = tmp[k][l];
                        }
                    }
                    used[i] = false;
                }
                rotate(piece[i]);
            }
        }
    }
}

int main() {
    while (cin >> H >> W, (H || W)) {
        for (int i=0; i<H; ++i) {
            for (int j=0; j<W; ++j) {
                cin >> puzzle[i][j];
            }
        }
        cin >> n;
        piece.clear();
        int pH, pW;
        for (int i=0; i<n; ++i) {
            cin >> pH >> pW;
            vector<vector<char>> p(pH, vector<char>(pW));
            for (int j=0; j<pH; ++j) {
                for (int k=0; k<pW; ++k) {
                    cin >> p[j][k];
                }
            }
            piece.push_back(p);
        }

        memset(used, false, sizeof used);
        solve();

        int p;
        cin >> p;
        for (int i=0; i<p; ++i) {
            int k, t;
            cin >> k;
            int bt = 0;
            for (int j=0; j<k; ++j) {
                cin >> t;
                bt |= 1 << (t - 1);
            }
            cout << (correct[bt] ? "YES" : "NO") << endl;
        }
    }
    return 0;
}