#include<iostream>
#include<sstream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<numeric>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>

#define rep(i,n) for(int i=0;i<n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define rp(i,c) rep(i,(c).size())
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf=1<<28;
const double INF=1e12,EPS=1e-9;

int H,W,sz,n;
char bd[20][21];
struct S{
	int h,w,sz;
	char p[20][21];
	void rot()
	{
		char ret[20][21];
		rep(i,h)rep(j,w)ret[w-j-1][i]=p[i][j];
		swap(h,w);
		rep(i,h)rep(j,w)p[i][j]=ret[i][j];
	}
};
S pc[10];
int m,use[10];

bool cmp(int a,int b)
{
	return pc[a].sz>pc[b].sz;
}
bool rec(int cur)
{
	if(cur==m)return 1;
	
	int c=use[cur],h=pc[c].h,w=pc[c].w;
	rep(k,4)
	{
		rep(i,H-h+1)rep(j,W-w+1)
		{
			rep(y,h)rep(x,w)
			if(pc[c].p[y][x]=='#'&&bd[y+i][x+j]=='#')continue;
			
			rep(y,h)rep(x,w)if(pc[c].p[y][x]=='#')bd[y+i][x+j]='#';
			if(rec(cur+1))return 1;
			rep(y,h)rep(x,w)if(pc[c].p[y][x]=='#')bd[y+i][x+j]='.';
		}
		pc[c].rot();
	}
	return 0;
}
bool solve()
{
	int s=0; rep(i,m)s+=pc[use[i]].sz;
	if(sz!=s)return 0;
	sort(use,use+m,cmp);
	return rec(0);
}
int main()
{
	while(scanf("%d%d",&H,&W),H)
	{
		sz=0;
		rep(i,H)
		{
			scanf("%s",bd[i]);
			rep(j,W)if(bd[i][j]=='.')sz++;
		}
		
		scanf("%d",&n);
		rep(i,n)
		{
			scanf("%d%d",&pc[i].h,&pc[i].w);
			pc[i].sz=0;
			rep(j,pc[i].h)
			{
				scanf("%s",pc[i].p[j]);
				rep(k,pc[i].w)if(pc[i].p[j][k]=='#')pc[i].sz++;
			}
			rep(j,4)
			{
				while(1)
				{
					bool f=0;
					rep(j,pc[i].h)if(pc[i].p[pc[i].h-1][j]=='#')f=1;
					if(f)break;
					pc[i].h--;
				}
				pc[i].rot();
			}
		}
		int cs; scanf("%d",&cs);
		rep(i,cs)
		{
			scanf("%d",&m);
			rep(j,m)scanf("%d",use+j),use[j]--;
			
			char tmp[20][21];
			rep(j,H)rep(k,W)tmp[j][k]=bd[j][k];
			puts(solve()?"YES":"NO");
			rep(j,H)rep(k,W)bd[j][k]=tmp[j][k];
		}
	}
	return 0;
}