#include<iostream>
#include<vector>
#include<algorithm>
#include<bitset>

using namespace std;

typedef bitset<400> Board;

int bh,bw;
Board bb;

struct P{
  int h,w;
  vector<vector<char> > g;
  P(){
    cin>>h>>w;
    g=vector<vector<char> >(h,vector<char>(w));
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	char c;
	cin>>c;
	g[i][j]=c=='#';
      }
    }
  }
  void rot(){
    vector<vector<char> > n(w,vector<char>(h));
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	n[j][h-1-i]=g[i][j];
      }
    }
    g=n;
    swap(h,w);
  }
  Board to_board()const{
    Board b;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	b[i*20+j]=g[i][j];
      }
    }
    return b;
  }
  vector<Board> to_boards(){
    vector<Board> v;
    for(int i=0;i<4;i++){
      rot();
      for(int j=-19;j<20;j++){
	for(int k=-19;k<20;k++){
	  Board b;
	  for(int l=0;l<h;l++){
	    for(int m=0;m<w;m++){
	      if(g[l][m]){
		int y=l+j;
		int x=k+m;
		if(y<0||bh<=y||x<0||bw<=x||bb[y*20+x])goto next;
		b[y*20+x]=true;
	      }
	    }
	  }
	  v.push_back(b);
	next:
	  ;
	}
      }
    }
    return v;
  } 
};

vector<vector<Board> > pieces;

bool rec(const Board &b,vector<int> r){
  if(r.empty())return b.count()==bh*bw;
  int x=r.back();
  r.pop_back();
  for(auto &e:pieces[x]){
    if((b&e).none()){
      if(rec(b|e,r))return true;
    }
  }
  return false;
}

int main(){
  for(;;){
    P b;
    if(b.h==0)break;
    int n;
    cin>>n;
    bh=b.h;
    bw=b.w;
    pieces.clear();
    pieces.resize(n);
    for(int i=0;i<n;i++){
      P cp;
      pieces[i]=cp.to_boards();
    }
    int p;
    cin>>p;
    while(p--){
      int k;
      cin>>k;
      int sp=0;
      vector<int> cp;
      while(k--){
	int t;
	cin>>t;
	sp|=1<<t-1;
	cp.push_back(t-1);
      }
      sort(begin(cp),end(cp),[&](int a,int b){
	  return pieces[a].size()<pieces[b].size();
	});
      cout<<(rec(b.to_board(),cp)?"YES":"NO")<<endl;
    }
  }
}

    
    