#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<bitset>
#include<iomanip>
#include<cassert>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<28)
#define MAX 21
using namespace std;
typedef pair<int,int> ii;
typedef vector<string> vs;
typedef vector<vs>    vvs;
int h,w,n,p,k;
int t[12];
char G[MAX][MAX];
string piece[MAX][42];
ii hw[MAX];//ii(h,w)
bool complete;
bool debug;

void print(int one)
{

  if(one < 0)
    {
      rep(i,h)
	{
	  rep(j,w)
	    {
	      cout << G[i][j];
	    }
	  cout << endl;
	}
      cout << endl;
      return;
    }

  int H = hw[one].first;
  int W = hw[one].second;
  rep(i,H)
    {
      rep(j,W)
	{
	  cout << piece[one][i][j];
	}
      cout << endl;
    }
  cout << endl;
}


void rotate90(int one)
{//verify [ok]

  int H = hw[one].first;
  int W = hw[one].second;
  int pos = 0;

  int tmp[W][H];

  rep(i,W)
    {
      rep(j,H)
	{
	  tmp[i][j] = piece[one][(H-1)-j][i];
	}
    }

  swap(hw[one].first,hw[one].second);
  swap(H,W);

  rep(i,H)
    {
      rep(j,W)
	{
	  piece[one][i][j] = tmp[i][j];
	}
    }

}

bool check(int x,int y,int one)
{// verify [ok]
  int H = hw[one].first;
  int W = hw[one].second;
  //cout << "H = " << H << " W = " << W << endl;
  rep(i,H)
    {
      rep(j,W)
	{
	  if(piece[one][i][j] == '.')continue;
	  int nx = j + x;
	  int ny = i + y;
	  if(!(0 <= nx && nx < w && 0 <= ny && ny < h))
	    {
	      if(piece[one][i][j] == '#')
		{
	
		  return false;
		}
	      continue;
	    }


	  if(G[ny][nx] == piece[one][i][j])
	    {
	  
	      return false;
	    }
	}
    }
  return true;
}

void draw(int x,int y,int one,bool flag)//flag == true? draw : erase
{//verify [ok]
  int H = hw[one].first;
  int W = hw[one].second;
  rep(i,H)
    {
      rep(j,W)
	{
	  int nx = j + x;
	  int ny = i + y;
	  if(!(0 <= nx && nx < w && 0 <= ny && ny < h))
	    {
	      assert(piece[one][i][j] == '.');
	      continue;
	    }
	  if(piece[one][i][j] == '.')continue;
	  if(flag) assert(G[ny][nx] == '.');
	  if(!flag)assert(G[ny][nx] == '#');
	  G[ny][nx] = (flag?'#':'.');
	}
    }

}

bool finish()
{//veritfy [ok]
  rep(i,h)rep(j,w)if(G[i][j] == '.')return false;
  return true;
}


void dfs(int x,int y,int used)
{//verify [no]
  if(y >= h)
    {
      //print(-1);
      complete = true;
      rep(i,10)
	{
	  if(!((used >> i) & 1))complete = false;
	  break;
	}
      return;
    }

  if(complete)return;

  /*
  bitset<10> use(used);
  cout << "used : " << use << endl;
  print(-142);
  */
  if(__builtin_popcount(used) == 4)
    {


    }
  rep(i,h)//y
    {

      rep(j,w)//x
	{
	  //cout << j << "," << i << endl;
	 
	  bool next = true;

	  rep(l,n)//which one
	    {
	      if((used >> l) & 1)continue;
	      

	      rep(_,4)//rotate
		{

		  if(!check(j,i,l))
		    {
		      rotate90(l);	      
		      continue;
		    }

		  if(!complete)
		    {
		      /*
	  if(__builtin_popcount(used) == 5 && !((used >> 1) & 1) && ((used >> 6) & 1) && i == 1 && j > 7)
		      */
		      draw(j,i,l,true);

		    

		      dfs(j,i,used|(1<<l));
		      next = false;
		      draw(j,i,l,false);

		    }
		  rotate90(l);
		}

	    }
	  if(complete)return;
	  if(G[i][j] == '.')return;
	  //if(!next || complete)return;
	}

    }

  if(complete)return;

  if(finish())
    {
      //print(-1);
      complete = true;
      rep(i,10)
	{
	  if(!((used >> i) & 1))
	    {
	      complete = false;
	      break;
	    }
	}
    }
}

void Input()
{//verify [ok]
  rep(i,h)rep(j,w)cin >> G[i][j];

  cin >> n;
  rep(i,n)
    { 
      int ph,pw;
      cin >> ph >> pw;
      hw[i] = ii(ph,pw);
      rep(j,42)
	{
	  piece[i][j].clear();
	  if(j < ph)cin >> piece[i][j];
	  else      piece[i][j] = string(pw,'.');
	  piece[i][j] += string(pw,'.');
	}
    }


}

int main()
{
  while(cin >> h >> w,h|w)
    {
      Input();     
      cin >> p;
      while(p--)
	{
	  cin >> k;
	  rep(i,11)t[i] = false;
	  int init = 0;
	  rep(i,k)
	    {
	      int th;
	      cin >> th;
	      th--;
	      t[th] = true;
	    }
	  rep(i,11)if(!t[i])init |= (1<<i);

	  complete = false;
	 // dfs(0,0,init);
	  cout << (complete?"YES":"NO") << endl;
	}

    }
  return 0;
} 