#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;


using B = bitset<401>;
bool solve(){
    int h, w;
    cin >> h >> w;
    if(!h) return 0;
    B x;
    int cnt = 0;
    for (int i = 0; i < h; ++i) {
        string s;
        cin >> s;
        for (int j = 0; j < w; ++j) {
            if(s[j] == '#') {
                x.set(i*w+j);
                cnt++;
            }
        }
    }
    int n;
    cin >> n;
    vector<vector<B>> piece(n);
    vector<int> cnts(n);
    vector<int> ok(1 << n);
    for (int i = 0; i < n; ++i) {
        int hh, ww;
        cin >> hh >> ww;
        vector<string> v(hh);
        for (auto &&j : v) {
            cin >> j;
            for (auto &&k : j) {
                if(k == '#') cnts[i]++;
            }
        }
        for (int _ = 0; _ < 4; ++_) {
            for (int j = 0; j+hh <= h; ++j) {
                for (int k = 0; k+ww <= w; ++k) {
                    B y{};
                    for (int l = 0; l < hh; ++l) {
                        for (int m = 0; m < ww; ++m) {
                            if(v[l][m] == '#'){
                                y.set((j+l)*w+k+m);
                            }
                        }
                    }
                    piece[i].emplace_back(y);
                }
            }
            auto vv = vector<string>(ww, string(hh, ' '));
            for (int j = 0; j < hh; ++j) {
                for (int k = 0; k < ww; ++k) {
                    vv[ww-k-1][j] = v[j][k];
                }
            }
            swap(vv, v);
            swap(hh, ww);
        }
    }
    B MX{};
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            MX.set(i*w+j);
        }
    }
    int t;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        int p;
        cin >> p;
        vector<int> v(p);
        for (auto &&j : v) scanf("%d", &j), j--;
        sort(v.begin(),v.end(), [&](int x, int y){ return cnts[x] > cnts[y]; });
        int dif = h*w-cnt;
        for (auto &&k : v) dif -= cnts[k];
        auto dfs = [&](int cur, B x, auto &&f) -> bool {
            if(cur == p) return 1;
            B xx = x;
            for (auto &&i : v) {
                for (auto &&j : piece[i]) {
                    if((x&j).none()){
                        xx |= j;
                    }
                }
            }
            if(xx != MX) return 0;
            for (auto &&j : piece[v[cur]]) {
                if((x&j).none()){
                    if(f(cur+1, x|j, f)) return 1;
                }
            }
            return 0;
        };
        if(!dif && dfs(0, x, dfs)) puts("YES");
        else puts("NO");
    }
    return 1;
}

int main() {
    while(solve()){}
    return 0;
}
