#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
const int R =20,C=20;
class st{
public:
  int r,c;
  bool m[R][C];

  void rot(){
    bool tmp[R][C];
    rep(i,r)rep(j,c)tmp[i][j]=m[i][j];
    rep(i,c){
      rep(j,r){
	m[i][j]=tmp[j][c-1-i];
      }
    }
    swap(r,c);
  }
};

void output(int r,int c,bool m[R][C]){
  rep(i,r){
    rep(j,c)cout << m[i][j];
    cout << endl;
  }
  cout << endl;
}

void getinp(int r,int c,bool a[R][C]){
  rep(i,r){
    rep(j,c){char t;cin>>t;a[i][j]=t=='#';}
  }
}

bool solve(int now,const vector<int> & num,const int r,const int c,bool ori[R][C],
	   st *in){
  //output(r,c,ori);

  if (now == num.size()){
    rep(i,r)rep(j,c)if (!ori[i][j])return false;
    return true;
  }
  int p=num[now];
  rep(i,r){
    rep(j,c){
      rep(k,4){
	bool isok=true;
	in[p].rot();
	if (i+in[p].r > r)break;
	if (j+in[p].c > c)break;
	rep(ii,in[p].r)rep(jj,in[p].c)
	  if (ori[i+ii][j+jj] && in[p].m[ii][jj]){isok=false;}
	if (!isok)continue;
	rep(ii,in[p].r)rep(jj,in[p].c)if (in[p].m[ii][jj])ori[i+ii][j+jj]=true;
	bool ret=solve(now+1,num,r,c,ori,in);
	rep(ii,in[p].r)rep(jj,in[p].c)if (in[p].m[ii][jj])ori[i+ii][j+jj]=false;
	if (ret)return true;
      }
    }
  }
  return false;
}

main(){
  bool in[R][C]={};
  int r,c;
  while(cin>>r>>c && r){
    getinp(r,c,in);
    //output(r,c,in);

    int n;
    cin>>n;
    st inp[n];
    rep(i,n){
      cin>>inp[i].r>>inp[i].c;
      getinp(inp[i].r,inp[i].c,inp[i].m);
    }
    int q;
    cin>>q;
    rep(k,q){
      int num;cin>>num;
      vector<int> a(num);
      rep(i,num)cin>>a[i],a[i]--;
      if (solve(0,a,r,c,in,inp))cout <<"YES" << endl;
      else cout <<"NO"<<endl;
    }
  }
  return false;
}