#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int pc[10],w[10][4],f[10],pn[10],h[10][4],sx[10][4],da[10][20][4]; 
int mx,my,hx,hy,n2;

void serch(int *p) {
	int i,j;
	for (i=0;i<my;i++) {
		if ( (1 << mx)-1 == p[i]) continue;
		for (j=0; j<mx; j++) {if (((1 << j) & p[i]) == 0) { hx=j; hy=i; return;}}
	}
}
int ans(int *md,int n) {
	int dm[20],i,j,k,p,x,y;
	if (n==-1) return 1;
	serch(md); x=hx; y=hy;
	for (i=0;i<my;i++) dm[i]=md[i];
	for (i=0; i<n2; i++) {
		if (f[i]==1) continue;
		for (j=0; j<4; j++) {
			if ((y+h[pn[i]][j]>my) || (x<sx[pn[i]][j]) || (x-sx[pn[i]][j]+w[pn[i]][j]>mx)) continue;
			for (k=0; k<h[pn[i]][j]; k++) {
				p=da[pn[i]][k][j] << (x-sx[pn[i]][j]);
				if ((md[y+k]+p) != (md[y+k] | p)) break;
			}
			if (k == h[pn[i]][j]) { 
				f[i]=1;
			    for (k=0; k<h[pn[i]][j]; k++) dm[y+k]=dm[y+k] | (da[pn[i]][k][j] << (x-sx[pn[i]][j]));
			    if (ans(dm,n-1) == 1) return 1;
                for (k=0; k<h[pn[i]][j]; k++) dm[k]=md[k]; 
				f[i]=0;
			}  
		}
	}
return 0;
}

int main() {
 char mp[20];
 int md[20],mc,i,j,k,x,y,n;
 while(true) {
 cin >> mx >> my;
 if (mx+my==0) break;
 mc=0;
 for (i=0;i<my;i++) {
	 cin >> mp;
	 md[i]=0; 
	 for (j=0; j<mx ; j++) if (mp[j]=='#') md[i]+=1 << j; else mc++;
     }
 cin >> n;
 for (i=0;i<10;i++) for (j=0;j<20;j++) for (k=0;k<4;k++) da[i][j][k]=0;
 for (i=0;i<n;i++) {
  cin >> w[i][0] >> h[i][0];
  pc[i]=0;
  for (j=0;j<h[i][0];j++) {  cin >> mp;
  for (k=0;k<w[i][0];k++) if (mp[k]=='#') { da[i][j][0]+= 1 << k; pc[i]++;}}
  for (j=0; j<3; j++) {
	  h[i][j+1]=w[i][j]; w[i][j+1]=h[i][j];
	  for (y=0;y<h[i][j];y++) for (x=0;x<w[i][j];x++) if ((da[i][y][j] & (1 << x)) > 0 ) da[i][x][j+1]+=(1 << (w[i][j+1]-y-1));
  }
      for (j=0;j<4;j++) for (x=0;x<w[i][j];x++) if ((da[i][0][j] & (1 << x)) > 0) { sx[i][j]=x; break;}
    }
 cin >> n; 
 for (i=0;i<n;i++) {
   cin >> n2; 
   k=0;
   for (j=0; j<n2; j++)  {cin >> pn[j]; pn[j]-=1; }
   for (j=0; j<n2; j++) { k+=pc[pn[j]]; f[j]=0; }
   if (k != mc) cout << "NO" << endl; else
	   if (ans(md,n2-1)==1) cout << "YES" << endl; else cout << "NO" << endl;
   }
 }
 return 0;
 }