Language: C++

int H, W, n, h[40], w[40], sz[40];
int m, no[10];
char in[20][21], pc[40][20][21];
bool v[20][20], u[20][20];

bool cmp(int a, int b){
	return sz[a] > sz[b];
}
bool rec(int c){
	if(c == m) return 1;
	memset(u, 0, sizeof(u));
	rep(i, H) rep(j, W) if(in[i][j] == '.' && !u[i][j]){
		for(int k = c; k < m; k++) rep(t, 4){
			int p = no[k] + t * n;
			for(int y = max(0, i - h[p] + 1); y <= i && y + h[p] <= H; y++)
			for(int x = max(0, j - w[p] + 1); x <= j && x + w[p] <= W; x++){
				rep(a, h[p]) rep(b, w[p]){
					if(pc[p][a][b] == '#' && in[y + a][x + b] == '#') goto FAIL;
				}
				rep(a, h[p]) rep(b, w[p]){
					if(pc[p][a][b] == '#') u[y + a][x + b] = 1;
				}
				FAIL:;
			}
		}
		if(!u[i][j]) return 0;
	}
	rep(t, 4){
		int p = no[c] + t * n;
		for(int y = 0; y + h[p] <= H; y++)
		for(int x = 0; x + w[p] <= W; x++){
			bool ok = 1;
			rep(a, h[p]) rep(b, w[p]) if(in[y + a][x + b] == '#' && pc[p][a][b] == '#') ok = 0;
			if(ok){
				rep(a, h[p]) rep(b, w[p]) if(pc[p][a][b] == '#') in[y + a][x + b] = '#';
				bool res = rec(c + 1);
				rep(a, h[p]) rep(b, w[p]) if(pc[p][a][b] == '#') in[y + a][x + b] = '.';
				if(res) return 1;
			}
		}
	}
	return 0;
}

int main()
{
	while(cin >> H >> W, H){
		int sp = 0;
		rep(i, H){
			cin >> in[i];
			rep(j, W) if(in[i][j] == '.') sp++;
		}
		
		cin >> n;
		
		rep(i, n){
			cin >> h[i] >> w[i];
			
			sz[i] = 0;
			rep(j, h[i]){
				cin >> pc[i][j];
				rep(k, w[i]) if(pc[i][j][k] == '#') sz[i]++;
			}
			
			int s = i;
			rep(j, 3){
				int t = s + n;
				
				h[t] = w[s];
				w[t] = h[s];
				sz[t] = sz[s];
				
				rep(y, h[s]) rep(x, w[s])
					pc[t][x][h[s] - y - 1] = pc[s][y][x];
				s = t;
			}
		}
		int q;
		cin >> q;
		while(q--){
			cin >> m;
			int sum = 0;
			rep(i, m) cin >> no[i], sum += sz[--no[i]];
			sort(no, no + m, cmp);
			
			memset(v, 0, sizeof(v));
			cout << (sum == sp && rec(0) ? "YES" : "NO") << endl;
		}
	}
	return 0;
}

Compiler output:
a.cc: In function 'bool rec(int)':
a.cc:11:9: error: 'memset' was not declared in this scope
   11 |         memset(u, 0, sizeof(u));
      |         ^~~~~~
a.cc:1:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
  +++ |+#include <cstring>
    1 | int H, W, n, h[40], w[40], sz[40];
a.cc:12:13: error: 'i' was not declared in this scope
   12 |         rep(i, H) rep(j, W) if(in[i][j] == '.' && !u[i][j]){
      |             ^
a.cc:12:9: error: 'rep' was not declared in this scope; did you mean 'rec'?
   12 |         rep(i, H) rep(j, W) if(in[i][j] == '.' && !u[i][j]){
      |         ^~~
      |         rec
a.cc:28:13: error: 't' was not declared in this scope
   28 |         rep(t, 4){
      |             ^
a.cc: In function 'int main()':
a.cc:47:15: error: 'cin' was not declared in this scope; did you mean 'in'?
   47 |         while(cin >> H >> W, H){
      |               ^~~
      |               in
a.cc:49:21: error: 'i' was not declared in this scope
   49 |                 rep(i, H){
      |                     ^
a.cc:49:17: error: 'rep' was not declared in this scope; did you mean 'rec'?
   49 |                 rep(i, H){
      |                 ^~~
      |                 rec
a.cc:84:25: error: 'sort' was not declared in this scope; did you mean 'short'?
   84 |                         sort(no, no + m, cmp);
      |                         ^~~~
      |                         short
a.cc:86:25: error: 'memset' was not declared in this scope
   86 |                         memset(v, 0, sizeof(v));
      |                         ^~~~~~
a.cc:86:25: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
a.cc:87:25: error: 'cout' was not declared in this scope
   87 |                         cout << (sum == sp && rec(0) ? "YES" : "NO") << endl;
      |                         ^~~~
a.cc:87:73: error: 'endl' was not declared in this scope
   87 |                         cout << (sum == sp && rec(0) ? "YES" : "NO") << endl;
      |                                                                         ^~~~