#include <bitset>
#include <cassert>
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;
typedef pair<int,int> pii;

struct P {
  int d;
  int y1, x1;
  P(int d, int y1, int x1) : d(d),y1(y1),x1(x1) {}
};


int target[20];
int piece[10][4][20];
int bnum[10];

int h[10][4],w[10][4];

vector<P> v[10];
vector<int> pid;
bool cmp(int a, int b) {
  return v[a].size() < v[b].size();
}

int H, W;

int tmp[20];

bool solve(int now, int t[20]) {
  if (now == pid.size()) return 1;
  
  REP(i,20)tmp[i]=t[i];
  for (int i=now; i<pid.size(); ++i) {
    int q = pid[i];
    FOR(it, v[q]) {
      bool okf = 1;
      REP(y,h[q][it->d]) {
        int S = t[it->y1+y] & (piece[q][it->d][y] << it->x1);
        if (S) {
          okf = 0;
          break;
        }
      }
      if (okf) {
        REP(y,h[q][it->d]) tmp[it->y1+y] |= piece[q][it->d][y] << it->x1;
      }
    }
  }
  REP(i,H) if (tmp[i]!=(1<<20)-1) return 0;
  
  int p = pid[now];
  FOR(it, v[p]) {
    bool okf = 1;
    REP(y,h[p][it->d]) {
      int S = t[it->y1+y] & (piece[p][it->d][y] << it->x1);
      if (S) {
        okf = 0;
        break;
      }
    }
    if (okf) {
      REP(y,h[p][it->d]) t[it->y1+y] |= piece[p][it->d][y] << it->x1;
      if (solve(now+1,t)) return 1;
      REP(y,h[p][it->d]) t[it->y1+y] ^= piece[p][it->d][y] << it->x1;
    }
  }
  return 0;
}

int main() {
  while(cin >> H >> W, H||W) {
    REP(i,20)REP(j,20)target[i]|=1<<j;
    int esum = 0;
    REP(y, H) {
      REP(x, W) {
        char c;
        cin >> c;
        if (c=='.') {
          target[y] ^= 1<<x;
          esum++;
        }
      }
    }
    int n;
    cin >> n;

    REP(i, n) {
      bnum[i] = 0;
      cin >> h[i][0] >> w[i][0];
      int tmp[20] = {};
      REP(y,h[i][0])REP(x,w[i][0]) {
        char c;
        cin >> c;
        if (c=='#') {
          tmp[y] |= 1<<x;
          bnum[i]++;
        }
      }
      REP(d,4) {
        REP(j,20) {
          piece[i][d][j] = tmp[j];
          tmp[j] = 0;
        }
        REP(y,h[i][d]) {
          REP(x,w[i][d]) {
            if (piece[i][d][y] >> x & 1)
              tmp[w[i][d]-1-x] |= 1<<y;
          }
        }
        if (d<3) {
          h[i][d+1] = w[i][d];
          w[i][d+1] = h[i][d];
        }
      }
    }
    
    REP(i,n) {
      v[i].clear();
      REP(d,4) {
        REP(y1,H-h[i][d]+1) {
          REP(x1,W-w[i][d]+1) {
            bool okf = 1;
            REP(y,h[i][d]) {
              int S = target[y1+y] & (piece[i][d][y] << x1);
              if (S) {
                okf = 0;
                break;
              }
            }
            if (okf) {
              v[i].push_back(P(d,y1,x1));
            }
          }
        }
      }
    }
    
    int p;
    cin >> p;
    REP(i,p) {
      int tar[20] = {};
      REP(j,20) tar[j] = target[j];
      int k; cin >> k;
      pid.clear();
      int bsum = 0;
      REP(j,k) {
        int a;
        cin >> a;
        a--;
        pid.push_back(a);
        bsum += bnum[a];
      }
      sort(ALL(pid),cmp);
      cout << (bsum==esum&&solve(0,tar)?"YES":"NO") << endl;
    }
  }
}