Language: C++

#include<iostream>
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:12:9: error: 'memset' was not declared in this scope
   12 |         memset(u, 0, sizeof(u));
      |         ^~~~~~
a.cc:2:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    1 | #include<iostream>
  +++ |+#include <cstring>
    2 | int H, W, n, h[40], w[40], sz[40];
a.cc:13:13: error: 'i' was not declared in this scope
   13 |         rep(i, H) rep(j, W) if(in[i][j] == '.' && !u[i][j]){
      |             ^
a.cc:13:9: error: 'rep' was not declared in this scope; did you mean 'rec'?
   13 |         rep(i, H) rep(j, W) if(in[i][j] == '.' && !u[i][j]){
      |         ^~~
      |         rec
a.cc:29:13: error: 't' was not declared in this scope
   29 |         rep(t, 4){
      |             ^
a.cc: In function 'int main()':
a.cc:48:15: error: 'cin' was not declared in this scope; did you mean 'std::cin'?
   48 |         while(cin >> H >> W, H){
      |               ^~~
      |               std::cin
In file included from a.cc:1:
/usr/include/c++/14/iostream:62:18: note: 'std::cin' declared here
   62 |   extern istream cin;           ///< Linked to standard input
      |                  ^~~
a.cc:50:21: error: 'i' was not declared in this scope
   50 |                 rep(i, H){
      |                     ^
a.cc:50:17: error: 'rep' was not declared in this scope; did you mean 'rec'?
   50 |                 rep(i, H){
      |                 ^~~
      |                 rec
a.cc:85:25: error: 'sort' was not declared in this scope; did you mean 'short'?
   85 |                         sort(no, no + m, cmp);
      |                         ^~~~
      |                         short
a.cc:87:25: error: 'memset' was not declared in this scope
   87 |                         memset(v, 0, sizeof(v));
      |                         ^~~~~~
a.cc:87:25: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
a.cc:88:25: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   88 |                         cout << (sum == sp && rec(0) ? "YES" : "NO") << endl;
      |                         ^~~~
      |                         std::cout
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:88:73: error: 'endl' was not declared in this scope; did you mean 'std::endl'?
   88 |                         cout << (sum == sp && rec(0) ? "YES" : "NO") << endl;
      |                                                                         ^~~~
      |                                                                         std::endl
In file included from /usr/include/c++/14/iostream:41:
/usr/include/c++/14/ostream:744:5: note: 'std::endl' declared here
  744 |     endl(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~