
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <bitset>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#define strtok_r strtok_s
#endif

#define ITER(c)             __typeof__((c).begin())
#define FOREACH(it, c)      for (ITER(c) it=(c).begin(); it != (c).end(); ++it)
#define RITER(c)            __typeof__((c).rbegin())
#define RFOREACH(it, c)     for (RITER(c) it=(c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n)           REPEAT(i, 0, n)
#define RREP(i, n)          RREPEAT(i, 0, n)
#define REPEAT(i, k, n)     for(int i = (k); i < (k+n); ++i)
#define RREPEAT(i, k, n)    for(int i = (k)+(n)-1; i >= (k); --i)
#define FROMTO(i,f,t)       for(int i = f; i < t; i++)
#define ALL(c)              (c).begin(), (c).end()
#define LLPOW(p1,p2)        ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A)          (sizeof(A)/sizeof((A)[0]))
#define CIN_NO_SYNC         do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0)
#define GETSTR(p)           fgets((p), sizeof(p), stdin)
#define CHOMP(p)            do{ char *_q = (p) + strlen(p)-1; if(*_q == '\n' || *_q == '\r') *_q = 0; } while(0)
#define FILL(m,v)           memset(m, v, sizeof(m))
#define mp                  make_pair
#define pb                  push_back
template<class _T> _T MAX(_T p1,_T p2){return (p1>p2)?p1:p2;} 
template<class _T> _T MIN(_T p1,_T p2){return (p1<p2)?p1:p2;} 
template <class _Tv> inline string join(_Tv &v,string sep=" "){
    ostringstream s;FOREACH(it,v){if(it!=v.begin())s<<sep;s<<*it;};return s.str();
}

typedef long long ll;
typedef unsigned long long ull;
typedef double D;
typedef complex<D> P;
#define X real()
#define Y imag()
#define EPS (1e-9)
#define DEQ(p1,p2) (abs((p1)-(p2)) < EPS)

#ifdef _DEBUG
template<class _Tv> inline void _prfe(const char *_n,_Tv _c,bool _p=false){
    ITER(_c) _it=_c.begin();
    if(_p){cout<<_n<<" = ["<<endl;for(;_it!=_c.end();++_it)cout<<"    "<<*_it<<endl;cout<<"]"<<endl; }
    else{cout<<_n<<" = [ "<<*_it++;for(;_it!=_c.end();++_it)cout<<", "<<*_it;cout<<" ]"<<endl; }
}
#define pf printf
#define pr(n) do{cout<<#n" = "<<(n)<<endl;}while(0)
#define prfe(n) _prfe(#n, n)
#define prfep(n) _prfe(#n, n, true)
#define dbgchk(n) do{if(n)throw;}while(0)
#else
#define pf(...) /* */
#define pr(...) /* */
#define prfe(...) /* */
#define prfep(...) /* */
#define dbgchk(...) /* */
#endif

bool rotate(const vector<string> &a, vector<string> &b, int r){
    int w = a[0].size();
    int h = a.size();
    if(r == 0){ b = a; return true; }
    if(r == 2){ b = a; reverse(ALL(b)); FOREACH(it,b) reverse(ALL(*it)); }
    if(r == 1){ b.resize(w); REP(i,w) b[i].resize(h); REP(y,h) REP(x,w) b[x][h-y-1] = a[y][x]; }
    if(r == 3){ b.resize(w); REP(i,w) b[i].resize(h); REP(y,h) REP(x,w) b[w-x-1][y] = a[y][x]; }
    //pf("---- %d ----\n", r);
    //prfep(b);
    if((int)b[0].size() != w || (int)b.size() != h) return true;
    REP(y,h) REP(x,w){
        if(a[y][x] != b[y][x]) return true;
    }
    return false;
}

int vpn;
vector<vector<string> > vp;
vector<int> vpc;
vector<int> vpw, vph, vpbx, vpby;
int vtn;
vector<int> vt;
bitset<10> vtu;

char memo[410][1<<10];

bool dfs(const vector<string> &vm, int n, int mc){
    int mw = vm[0].size(), mh = vm.size(), ms = mw*mh;
    int mn = -1;

    FROMTO(i,n,ms){
        int y = i/mw, x = i%mw;
        if(vm[y][x] == '.'){ mn = i; break; }
    }
    int mbx = mn%mw,mby = mn/mw;

    if(memo[mn][vtu.to_ulong()]) return false;

    prfep(vm);
    if(mc == 0) return true;

    pf("mb = (%d,%d)\n", mbx, mby);
    pr(vtu.to_ulong());
    REP(i,vtn){
        if(vtu[i]) continue;
        REP(r,4){
            int pi = vt[i]+vpn*r;
            vector<string> &vpr = vp[pi];
            int pw = vpw[pi];
            int ph = vph[pi];
            int cx = mbx - vpbx[pi];
            int cy = mby - vpby[pi];
            prfep(vpr);
            if(cx < 0 || cy < 0) continue;
            if(cx+pw > mw || cy+ph > mh) continue;

            bool f = true;
            REP(y0,ph){
                REP(x0,pw){
                    if(vpr[y0][x0] == '#'){
                        if(vm[cy+y0][cx+x0] != '.'){
                            f = false; break;
                        }
                    }
                }
                if(!f) break;
            }
            if(f){
                vector<string> vm0 = vm;
                REP(y0,ph){
                    REP(x0,pw){
                        if(vpr[y0][x0] == '#') vm0[cy+y0][cx+x0] = '@';
                    }
                }
                vtu[i] = true;
                if(dfs(vm0, mn+1, mc-vpc[vt[i]])) return true;
                vtu[i] = false;
            }
        }
    }
    memo[mn][vtu.to_ulong()] = -1;
    return false;
}

int ps(const vector<string> &p){
    int w = p[0].size(), h = p.size(), c = 0;
    REP(y,h) REP(x,w) if(p[y][x] == '#') c++;
    return c;
}

string solve(const vector<string> &vm){
    int mw = vm[0].size();
    int mh = vm.size();
    int mc = mh*mw-ps(vm);
    int pc = 0;
    FOREACH(it,vt) pc += ps(vp[*it]);
    pf("mc = %d, pc = %d\n", mc, pc);
    if(mc != pc) return "NO";

    FILL(memo,0);
    return dfs(vm,0,mc)?"YES":"NO";
}

void getbase(const vector<string> &vp, int &vpw, int &vph, int &vpbx, int &vpby){
    vpw = vp[0].size(); vph = vp.size();
    REP(y,vph) REP(x,vpw){
        if(vp[y][x] == '#'){
            vpbx = x, vpby = y;
            pf("vpw = %d, vph = %d, vpbx = %d, vpby = %d\n", vpw, vph, vpbx, vpby);
            prfep(vp);
            return;
        }
    }
}

int main(){
    int w, h;
    while(cin >> h >> w){
        if(h == 0 && w == 0) break;
        vector<string> vm(h);
        REP(i,h){
            cin >> vm[i];
        }
        int n;
        cin >> n;
        vp.resize(n*4); vpn = n;
        vpc.resize(n);
        vpw.resize(n*4); vph.resize(n*4);
        vpbx.resize(n*4); vpby.resize(n*4);
        REP(i,n){
            int w1, h1;
            cin >> h1 >> w1;
            vp[i].resize(h1);
            REP(j,h1)
                cin >> vp[i][j];

            bool f1 = false, f2 = false;
            REP(x,w1){
                if(vp[i][0][x] == '#') f1 = true;
                if(vp[i][h1-1][x] == '#') f2 = true;
            }
            if(!f1 || !f2) throw;
            f1 = false, f2 = false;
            REP(y,h1){
                if(vp[i][y][0] == '#') f1 = true;
                if(vp[i][y][w1-1] == '#') f2 = true;
            }
            if(!f1 || !f2) throw;

            vpc[i] = ps(vp[i]);
            getbase(vp[i], vpw[i], vph[i], vpbx[i], vpby[i]);
            REPEAT(j,1,3){
                rotate(vp[i], vp[i+j*n], j);
                getbase(vp[i+j*n], vpw[i+j*n], vph[i+j*n], vpbx[i+j*n], vpby[i+j*n]);
            }
        }

        int p;
        cin >> p;
        REP(i,p){
            int k, t;
            cin >> k;
            vt.resize(k,0); vtn = k;
            vtu.none();
            REP(j,k){
                cin >> t; vt[j] = t-1; vtu[j] = 0;
            }
            RREP(j,k){
                REP(l,j){
                    if(vpc[vt[l]] < vpc[vt[l+1]]){
                        swap(vt[l], vt[l+1]);
                    }
                }
            }
            cout << solve(vm) << endl;
        }
    }
    return 0;
}