Language: C++

#include<iostream>
#include<vector>
using namespace std;
#define REP(i,b,n ) for(int i=b;i<n;i++)
#define rep(i,n)    REP(i,0,n)

#define UNFILLED 0
#define FILLED 1

#define ORI true
#define DUP false;
char inp[20][20];
bool m[20][20];
bool visited[20][20];

typedef struct Info{
  int x,y,r,c,rot;
};

class Piece{
public:
  int r[4],c[4];
  bool m[4][20][20];
  vector<Info> Put;
  int black;
  bool dup[4];
  bool operator<(const Piece & a)const{
    return Put.size() > a.Put.size();
  }
};

void rotate(Piece & a,int k){
  static bool tmp[20][20];
  rep(i,a.r[k-1]){
    rep(j,a.c[k-1]){
      tmp[i][j]=a.m[k-1][i][j];
    }
  }

  rep(i,a.r[k-1]){
    rep(j,a.c[k-1]){
      a.m[k][j][i]=tmp[i][a.c[k-1]-j-1];
    }
  }
  a.r[k]=a.c[k-1];
  a.c[k]=a.r[k-1];
}

void duplicate_check(Piece & a){
  rep(i,4)a.dup[i]=ORI;
  REP(i,1,4){
    rotate(a,i);
  }
  
  rep(k,4){
    REP(l,k+1,4){
      bool flag = false;
      if ( a.r[k] !=a.r[l] ||a.c[k] != a.c[l])continue;
      rep(i,a.r[k] && !flag)
	rep(j,a.c[k] && !flag)
	if(a.m[k][i][j]!=a.m[l][i][j] )flag=true;
      if (flag == false)a.dup[l]=DUP;
    }

  }
}

bool can_put(int x,int y,Piece & a,int r,int c,int k){
  if ( x + a.c[k]-1 >=c ||y+a.r[k]-1>= r)return false;
  rep(i,a.r[k]){
    rep(j,a.c[k]){
      if ( a.m[k][i][j]==UNFILLED ){
      }else if (a.m[k][i][j]==FILLED ){
	if ( m[y+i][x+j] == FILLED)return false;
      }
    }
  }
  return true;
}

void construct(int n,Piece * in,int r,int c){
  Info ins;
  rep(l,n){
    rep(i,r){
      rep(j,c){
	rep(k,4){
	  if (in[l].dup[k] == ORI && can_put(j,i,in[l],r,c,k) == true){
	    ins.x=j,ins.y=i,ins.r=in[l].r[k],ins.c=in[l].c[k];
	    ins.rot=k;
	    //cout<<"rotate " << k << " -th " << endl;
	    //cout << j <<" " <<  j+ins.c-1 <<" " << i << " " << i+ins.r-1 << endl;
	    in[l].Put.push_back(ins);
	  }
	}
      }
    }
  }
}


bool Operation(Piece &in,int me,int flag){
  //flag=0 check,flag=1 put ,flag=2 remove
  int x=in.Put[me].x,y=in.Put[me].y,r=in.Put[me].r,c=in.Put[me].c,k=in.Put[me].rot;
  rep(i,r){
    rep(j,c){
      if( flag == 0){
	if ( m[y+i][x+j]==FILLED){
	  if (in.m[k][i][j]==FILLED)return false;
	}
      }else if ( flag == 1){
	if( in.m[k][i][j]==FILLED)m[i][j]=FILLED;
      }else if ( flag == 2){
	if (in.m[k][i][j]==FILLED)m[i][j]=UNFILLED;
      }
    }
  }

  return true;
}



bool solve(int n,int now,Piece *in){
  if ( n == now){
    return true;
  }

  rep(i,in[now].Put.size()){
    if ( Operation(in[now],i,0) ){
      Operation(in[now],i,1);
      if  (solve(n,now+1,in) )return true;
      Operation(in[now],i,2);
    }
  }


  return false;
}


main(){
  int r,c;
  char tmp[20];
  Piece sol[10];
  Piece in[10];
  while(cin>>r >>c && r){
    int white = 0;
    rep(i,r){
      cin>>inp[i];
      rep(j,c){
	if ( inp[i][j]=='#')m[i][j]=FILLED;
	else m[i][j]=UNFILLED,white++;
	visited[i][j]=false;
      }
    }
    int n;
    cin>>n;
    rep(k,n){
      cin>>in[k].r[0]>>in[k].c[0];
      in[k].black=0;
      rep(i,in[k].r[0]){
	cin>>tmp;
	rep(j,in[k].c[0]){
	  if ( tmp[j] == '#')in[k].m[0][i][j]=FILLED,in[k].black++;
	  else in[k].m[0][i][j]=UNFILLED;
	}
      }
      duplicate_check(in[k]);
    }
    


    construct(n,in,r,c);


    int q;
    cin>>q;
    rep(i,q){
      int m;
      cin>>m;
      int ind[m],total=0;
      rep(j,m)cin>>ind[j],total+=in[ind[j]-1].black,sol[j]=in[ind[j]-1];
      if ( white !=total){puts("NO");continue;}
      
      sort(sol,sol+m);

      if ( solve(m,0,sol))puts("YES");
      else puts("NO");
      
    }


  }
}

Compiler output:
a.cc:16:1: warning: 'typedef' was ignored in this declaration
   16 | typedef struct Info{
      | ^~~~~~~
a.cc:141:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  141 | main(){
      | ^~~~
a.cc: In function 'int main()':
a.cc:185:7: error: 'sort' was not declared in this scope; did you mean 'short'?
  185 |       sort(sol,sol+m);
      |       ^~~~
      |       short