#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <map>
using namespace std;

const int MAX_S = 20;

struct Ps {
  int h, w;
  char b[MAX_S][MAX_S];

  int numofb() const {
    int cnt = 0;
    for(int i = 0; i < h; ++i)
      for(int j = 0; j < w; ++j)
	if(b[i][j] == '#') ++cnt;
    return cnt;
  }

  bool operator < (const Ps &ps) const {
    if(h != ps.h) return h < ps.h;
    if(w != ps.w) return w < ps.w;
    for(int i = 0; i < w; ++i) {
      for(int j = 0; j < h; ++j) {
	if(b[i][j] != ps.b[i][j]) return b[i][j] < ps.b[i][j];
      }
    }
    return false;
  }

  bool operator == (const Ps &ps) const {
    return !(*this < ps) && !(ps < *this);
  }

  void cut(int x, int y, int ex, int ey) {
    w = ex - x;
    h = ey - y;
    for(int i = 0; i < h; ++i) {
      for(int j = 0; j < w; ++j) {
	b[i][j] = b[y+i][x+j];
      }
    }
  }

  void cutSpace() {
    int sx, sy, ex, ey;
    sx = sy = 0;
    ex = w; ey = h;
    while(isAllSpace(sx,0,0,1,ex,ey)) ++sx;
    while(isAllSpace(0,sy,1,0,ex,ey)) ++sy;
    while(isAllSpace(ex-1,0,0,1,ex,ey)) --ex;
    while(isAllSpace(0,ey-1,1,0,ex,ey)) --ey;
    cut(sx,sy,ex,ey);
  }

  bool isAllSpace(int x, int y, int dx, int dy, int ex, int ey) {
    while(x >= 0 && x < ex && y >= 0 && y < ey) {
      if(b[y][x] != '.') return false;
      x += dx;
      y += dy;
    }
    return true;
  }

  Ps rotate90() {
    Ps ps;
    ps.h = w;
    ps.w = h;
    for(int i = 0; i < ps.h; ++i) {
      for(int j = 0; j < ps.w; ++j) {
	ps.b[i][j] = b[h-j-1][i];
      }
    }
    return ps;
  }

  void show() {
    for(int i = 0; i < h; ++i) {
      for(int j = 0; j < w; ++j) {
	cout << b[i][j];
      }
      cout << endl;
    }
  }
};

struct PsPoint {
  int x, y;
  Ps *ps;
  bool isExist(int j, int i) {
    j -= x;
    i -= y;
    if(j < 0 || j >= ps->w) return false;
    if(i < 0 || i >= ps->h) return false;
    return ps->b[i][j] == '#';
  }
};

int H, W;
char B[MAX_S][MAX_S];
int Space;
vector<vector<Ps> > V;
PsPoint pp[20];

bool canPutOnB[10][4][20][20];
bool canPutAnywhere[10][4];

bool canPut(int x, int y, Ps &ps) {
  for(int i = 0; i < ps.h; ++i) {
    for(int j = 0; j < ps.w; ++j) {
      if(ps.b[i][j] == '#') {
	int px = x + j;
	int py = y + i;
	if(px < 0 || px >= W) return false;
	if(py < 0 || py >= H) return false;
	if(B[py][px] == '#') return false;
      }
    }
  }
  return true;
}

void makeCanPutOnB() {
  for(int i = 0; i < V.size(); ++i) {
    for(int j = 0; j < V[i].size(); ++j) {
      canPutAnywhere[i][j] = false;
      Ps &ps = V[i][j];
      for(int y = 0; y <= H-ps.h; ++y) {
	for(int x = 0; x <= W-ps.w; ++x) {
	  canPutOnB[i][j][y][x] = canPut(x,y,ps);
	  canPutAnywhere[i][j] = canPutAnywhere[i][j] || canPutOnB[i][j][y][x];
	}
      }
    }
  }
}

bool canPut(int m, int l, int x, int y, int s) {
  if(!canPutOnB[m][l][y][x]) return false;
  Ps &ps = V[m][l];
  for(int i = 0; i < ps.h; ++i) {
    for(int j = 0; j < ps.w; ++j) {
      if(ps.b[i][j] == '#') {
	int px = x + j;
	int py = y + i;
	for(int k = 0; k < s; ++k)
	  if(pp[k].isExist(px, py)) return false;
      }
    }
  }
  return true;
}

bool rec(int s, vector<int> &v) {
  if(s == v.size()) return true;
  vector<Ps> &u = V[v[s]];
  for(int k = 0; k < u.size(); ++k) {
    if(!canPutAnywhere[v[s]][k]) continue;
    for(int i = 0; i <= H-u[k].h; ++i) {
      for(int j = 0; j <= W-u[k].w; ++j) {
	if(canPut(v[s],k,j,i,s)) {
	  pp[s].x = j;
	  pp[s].y = i;
	  pp[s].ps = &u[k];
	  if(rec(s+1, v)) return true;
	}
      }
    }
  }
  return false;
}

string solve(vector<int> &u) {
  int space = 0;
  for(int i = 0; i < u.size(); ++i)
    space += V[u[i]-1][0].numofb();
  if(space != Space) return "NO";

  vector<pair<int,int> > pv;
  for(int i = 0; i < u.size(); ++i)
    pv.push_back(make_pair(V[u[i]-1][0].numofb()*(-1), u[i]-1));
  sort(pv.begin(), pv.end());
  vector<int> v;
  for(int i = 0; i < pv.size(); ++i)
    v.push_back(pv[i].second);
  return rec(0, v) ? "YES" : "NO";
}

int main() {
  while(cin >> H >> W && (H || W)) {
    V.clear();
    Space = 0;
    for(int i = 0; i < H; ++i) {
      for(int j = 0; j < W; ++j) {
	cin >> B[i][j];
	if(B[i][j] == '.') ++Space;
      }
    }

    int N;
    cin >> N;
    for(int t = 0; t < N; ++t) {
      Ps ps;
      cin >> ps.h >> ps.w;
      for(int i = 0; i < ps.h; ++i)
	for(int j = 0; j < ps.w; ++j)
	  cin >> ps.b[i][j];
      ps.cutSpace();
      vector<Ps> v;
      for(int i = 0; i < 4; ++i) {
	v.push_back(ps);
	ps = ps.rotate90();
      }
      sort(v.begin(), v.end());
      v.erase(unique(v.begin(), v.end()), v.end());
      V.push_back(v);
    }


    makeCanPutOnB();
    /*
    for(int i = 0; i < V.size(); ++i) {
      for(int k = 0; k < V[i].size(); ++k) {
	V[i][k].show();
	cout << endl;
      }
    }
    */

    int p;
    cin >> p;
    for(int i = 0; i < p; ++i) {
      int k;
      cin >> k;
      vector<int> u;
      for(int j = 0; j < k; ++j) {
	int t; cin >> t;
	u.push_back(t);
      }
      cout << solve(u) << endl;
    }
  }
  return 0;
}