#include<iostream>
#include<vector>
#include<algorithm>
#include<cassert>
#include<set>
using namespace std;
#define REP(i,b,n ) for(int i=b;i<n;i++)
#define rep(i,n)    REP(i,0,n)

#define UNFILLED false
#define FILLED true

#define ORI true
#define DUP false
typedef long long ll;
char inp[20][32];
ll m[20];
bool inm[20][20];
int _r,_c;
typedef struct Info{
  int x,y,r,c,rot;
};

class state{
public:
  int m[20];
  bool operator<(const state & a)const{
    rep(i,_r){
      if (m[i] !=a.m[i])return m[i] < a.m[i];
    }
    return false;
  }
};

class Piece{
public:
  int r[4],c[4];
  bool m[4][20][20];
  ll usem[4][20];
  vector<Info> Put;
  int black;
  bool dup[4];
  bool operator<(const Piece & a)const{

    //    return Put.size() > a.Put.size();
    return black>a.black;
  }
};

void rotate(Piece & a,int k){
  bool tmp[20][20]={false};
  rep(i,a.r[k-1]){
    rep(j,a.c[k-1]){
      tmp[i][j]=a.m[k-1][i][j];
    }
  }
  
  rep(i,a.r[k-1]){
    rep(j,a.c[k-1]){
      a.m[k][j][i]=tmp[i][a.c[k-1]-j-1];
    }
  }
  a.r[k]=a.c[k-1];
  a.c[k]=a.r[k-1];
}

void duplicate_check(Piece & a){
  rep(i,4)a.dup[i]=ORI;
  REP(i,1,4){
    rotate(a,i);
  }
  //return;
  rep(k,4){
    REP(l,k+1,4){
      bool flag = false;
      if ( a.r[k] !=a.r[l] ||a.c[k] != a.c[l])continue;
      rep(i,a.r[k] && !flag)
	rep(j,a.c[k] && !flag)
	if(a.m[k][i][j]!=a.m[l][i][j] )flag=true;
      if (flag == false)a.dup[l]=DUP;
    }
  }
  
  rep(k,4){
    if (a.dup[k]==DUP )continue;
    rep(i,a.r[k]){
      a.usem[k][i]=0;
      rep(j,a.c[k]){
	a.usem[k][i]=(a.usem[k][i]<<1);
	if ( a.m[k][i][j]==FILLED)a.usem[k][i]++;
      }
    }
  }
}


bool can_put(int x,int y,Piece & a,int r,int c,int k){
  rep(i,a.r[k]){
    rep(j,a.c[k]){
      if ( a.m[k][i][j]==UNFILLED ){
      }else if (a.m[k][i][j]==FILLED ){
	if ( x+j >=c || y+i >=r || x+j<0||y+i<0)return false;
	if ( inm[y+i][x+j] == FILLED)return false;
      }
    }
  }
  return true;
}

void construct(int n,Piece * in,int r,int c){
  Info ins;
  rep(l,n){
    rep(k,4){
      REP(i,-in[l].r[k],r){
	REP(j,-in[l].c[k],c){
	  if (in[l].dup[k] == ORI && can_put(j,i,in[l],r,c,k) == true){
	    ins.x=j;ins.y=i;ins.r=in[l].r[k];ins.c=in[l].c[k];
	    ins.rot=k;
	    in[l].Put.push_back(ins);
	  }
	}
      }
    }
  }
}


bool Operation(Piece &in,int me,int flag){
  //flag=0 check,flag=1 put ,flag=2 remove
  int x=in.Put[me].x,y=in.Put[me].y,r=in.Put[me].r,c=in.Put[me].c,k=in.Put[me].rot;
  rep(i,r){
    if( y+i >=_r ||y+i<0)continue;
    ll now=in.usem[k][i];
    if ( x < 0) now=(now<<(-x));
    else if ( x+c-1 < _c-1)now=(now<<(_c-1 - (x+c-1)));
    else now=(now >>(x+c-1 -(_c-1)));

    if( flag == 0){
      if ( (now & m[y+i]) != 0)return false;
    }else if ( flag == 1){
      m[y+i]|=now;
    }

  }
  return true;
}


set<state> S[10];
bool solve(int n,int now,Piece *in){
  if ( n == now){
    
    //rep(i,_r){
    // cout << m[i]<<endl;
    // }

    return true;
  }

  
  state me;
  rep(i,_r){
    me.m[i]=m[i];
  }
  if( S[now].find(me) !=S[now].end())return false;
  // cout << S[now].size()<<" ";
  S[now].insert(me);
  if ( S[now].size()>(now+1)*220)return false;
  //cout << S[now].size()<<endl;

  rep(i,in[now].Put.size()){
    if ( Operation(in[now],i,0) ){
      Operation(in[now],i,1);
      if (solve(n,now+1,in)){
	rep(j,_r)m[j]=me.m[j];
	return true;
      }
      rep(j,_r)m[j]=me.m[j];
    }
  }
  return false;
}


main(){
  int r,c;
  char tmp[32];
  
  while(cin>>r >>c && r){
    _r=r;_c=c;
    Piece in[20];
    Piece sol[20];
    rep(i,10)in[i].Put.clear(),sol[i].Put.clear();
    rep(i,20){
      m[i]=UNFILLED;
      rep(j,20)inm[i][j]=UNFILLED;
    }

    int white = 0;
    rep(i,r){
      cin>>inp[i];
      rep(j,c){
	m[i]<<=1;
	if ( inp[i][j]=='#')m[i]++,inm[i][j]=FILLED;
	else if (inp[i][j]=='.')white++,inm[i][j]=UNFILLED;
	else assert(false);
      }
    }

    
    int n;
    cin>>n;
    rep(k,n){
      cin>>in[k].r[0]>>in[k].c[0];
      in[k].black=0;
      rep(i,in[k].r[0]){
	cin>>tmp;
	rep(j,in[k].c[0]){
	  if ( tmp[j] == '#')in[k].m[0][i][j]=FILLED,in[k].black++;
	  else if (tmp[j]=='.')in[k].m[0][i][j]=UNFILLED;
	  else assert(false);
	}
      }
      duplicate_check(in[k]);
    }
    

    construct(n,in,r,c);
    
    /*
    rep(i,n){
      cout << in[i].Put.size()<<endl;
      rep(j,in[i].Put.size()){
	cout <<i<<" " <<  in[i].Put[j].x << " " << in[i].Put[j].y<<" r " << in[i].Put[j].r<<" " << in[i].Put[j].c<<endl;
      }
    }
    */
      
    
    int q;

    cin>>q;
    rep(i,q){
      rep(j,10)S[j].clear();
      int m;
      cin>>m;
      int total=0,ind;
      rep(j,m){
	cin>>ind;
	total+=in[ind-1].black;
	sol[j]=in[ind-1];
      }      
      
      
      if ( white !=total){puts("NO");continue;}
      
      //    sort(sol,sol+m);
      
      if ( solve(m,0,sol) )puts("YES");
      else puts("NO");
      //rep(j,10)cout << S[j].size()<<endl;
    }
    
  }
  return false;
}