#include<iostream>
#include<vector>
#include<algorithm>
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;
char inp[20][20];
bool m[20][20];

typedef struct Info{
  int x,y,r,c,rot;
};

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

void rotate(Piece & a,int k){
  static bool tmp[20][20];
  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;
    }
  }
}

bool can_put(int x,int y,Piece & a,int r,int c,int k){
  if ( x + a.c[k]-1 >=c ||y+a.r[k]-1>= r)return false;
  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 ( m[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(i,r){
      rep(j,c){
	rep(k,4){
	  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){
    rep(j,c){
      if( flag == 0){
	if ( m[y+i][x+j]==FILLED){
	  if (in.m[k][i][j]==FILLED)return false;
	}
      }else if ( flag == 1){
	if( in.m[k][i][j]==FILLED)m[y+i][x+j]=FILLED;
      }else if ( flag == 2){
	if (in.m[k][i][j]==FILLED)m[y+i][x+j]=UNFILLED;
      }
    }
  }

  return true;
}


int _r,_c;
bool solve(int n,int now,Piece *in){
  if ( n == now){
    bool flag=true;
    rep(i,_r){
      rep(j,_c)if ( m[i][j] == UNFILLED)exit(1);
    }
    return true;
  }

  rep(i,in[now].Put.size()){
    if ( Operation(in[now],i,0) ){
      Operation(in[now],i,1);
      if (solve(n,now+1,in)){
	Operation(in[now],i,2);
	return true;
      }
      Operation(in[now],i,2);
    }
  }


  return false;
}


main(){
  int r,c;
  char tmp[20];
  Piece sol[10];
  Piece in[10];
  while(cin>>r >>c && r){
    _r=r;_c=c;
    int white = 0;
    rep(i,r){
      cin>>inp[i];
      rep(j,c){
	if ( inp[i][j]=='#')m[i][j]=FILLED;
	else m[i][j]=UNFILLED,white++;
      }
    }
    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 in[k].m[0][i][j]=UNFILLED;
	}
      }
      duplicate_check(in[k]);
    }
    


    construct(n,in,r,c);

    int q;
    cin>>q;
    rep(i,q){
      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");
      
    }


  }
  return false;
}