#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],sy[10][4],da[10][20][4],e[21],md[20]; 
int mx,my,hx,hy,n2;

int ans(int n,int hx,int hy) {
	int i,j,k,p,x,y,lx,ly;
	if (n==-1) return 1;
	if ((mx-hx)<(my-hy)) {for (i=hy;i<my;i++) for (j=hx; j<mx; j++) if ((e[j] & md[i]) == 0) { lx=1; ly=0; x=j;y=i;hy=i;i=j=99;}}
	else {for (i=hx;i<mx;i++) for (j=hy;j<my;j++) if ((e[i] & md[j])==0) { lx=0; ly=1; x=i; hx=i; y=j; i=j=99;}} 
	
		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)) && (mx-x<my-y)) continue;
			if (((x+w[pn[i]][j]>mx) || (y<sy[pn[i]][j]) || (y-sy[pn[i]][j]+h[pn[i]][j]>my)) && (mx-x>=my-y)) continue;
			for (k=0; k<h[pn[i]][j]; k++) {
				p=da[pn[i]][k][j]*e[x-sx[pn[i]][j]*lx];
				if ((md[y+k-sy[pn[i]][j]*ly]+p) != (md[y+k-sy[pn[i]][j]*ly] | p)) break;
			}
			if (k == h[pn[i]][j]) { 
				f[i]=1;
			    for (k=0; k<h[pn[i]][j]; k++) md[y+k-sy[pn[i]][j]*ly]+=da[pn[i]][k][j]*e[x-sx[pn[i]][j]*lx];
			    p=ans(n-1,hx,hy);
				for (k=0; k<h[pn[i]][j]; k++) md[y+k-sy[pn[i]][j]*ly]-=da[pn[i]][k][j]*e[x-sx[pn[i]][j]*lx]; 
				f[i]=0;
				if (p==1) return 1;
			}  
		}
	}
return 0;
}

int main() {
 char mp[21];
 int mc,i,j=1,k,n,x,y;
 for (i=0;i<21;i++) { e[i]=j; j*=2;} 
 while(cin >> my >> mx) {
 if ( mx==0 && 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]+=e[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 >> h[i][0] >> w[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]+= e[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] & e[x]) > 0 ) da[i][x][j+1]+=e[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] & e[x]) > 0) { sx[i][j]=x; x=99;}
	  for (j=0;j<4;j++) for (y=0;y<h[i][j];y++) if ((da[i][y][j] & 1) > 0) { sy[i][j]=y; y=99;}
 }
 cin >> n; 
 for (i=0;i<n;i++) {
   cin >> n2; k=0; hx=0; hy=0;
   for (j=0; j<n2; j++)  {cin >> pn[j]; pn[j]-=1; k+=pc[pn[j]]; f[j]=0; }
   if (k != mc) cout << "NO" << endl; else
   if (ans(n2-1,0,0)==1) cout << "YES" << endl; else cout << "NO" << endl;
   }
 }
 return 0;
 }