#include<iostream>
#include<vector>
#include<string>
#include<bitset>
#include<algorithm>
#include<functional>

#define NIL (-1)
#define MAX 22
#define MAX_PIECES 10
#define BURIED 1
#define NONE 0
#define PZL 1
using namespace std;

class CPiece{
public:
	class piece{
	public:
		int H, W;
		short m[MAX][MAX];
		piece(){
			for(int i = 0; i < MAX; ++i){
				for(int j = 0; j < MAX; ++j){
					m[ i ][ j ] = NONE;
				}
			}
		}
		void Print(){
			for(int i = 1; i <= H; ++i){
				for(int j = 1; j <= W; ++j){
					cout << (m[i][j]==PZL?'#':'.');
				}
				cout << '\n';
			}
		}
	};
	int rot;
	piece pr[4];
	void CreateRotations(){
		// 90ツ度
		TurnRight90( pr[0], pr[1] );
		pr[1].H = pr[0].W;
		pr[1].W = pr[0].H;
		// 180ツ度
		TurnRight90( pr[1], pr[2] );
		pr[2].H = pr[0].H;
		pr[2].W = pr[0].W;
		// 270ツ度
		TurnRight90( pr[2], pr[3] );
		pr[3].H = pr[0].W;
		pr[3].W = pr[0].H;
	}
	int CountPuzzleCells(){
		int ret = 0;
		for(int i = 1; i <= pr[0].H; ++i){
			for(int j = 1; j <= pr[0].W; ++j){
				if( pr[0].m[i][j] == PZL ) ++ret;
			}
		}
		return ret;
	}
	bool operator>(const CPiece &t) const {
		int area   = this->pr[0].H * this->pr[0].W;
		int area_t = t.pr[0].H     * t.pr[0].W;
		return area > area_t;
	}
private:
	void TurnRight90( piece &from, piece &t ){
		double offsetX = (from.W+1) / 2.0;
		double offsetY = (from.H+1) / 2.0;

		for(double i = 1 - offsetY; i <= from.H - offsetY + 0.1; i += 1.0){
			for(double j = 1 - offsetX; j <= from.W - offsetX + 0.1; j += 1.0){
				double ti = j;
				double tj = -i;
				t.m[ int(ti + offsetX) ][ int(tj + offsetY) ] = from.m[ int(i + offsetY) ][ int(j + offsetX) ];
			}
		}
	}
};

class CPuzzle{
public:
	int H, W;
	short m[MAX][MAX];
	void Initialize(){
		for(int i = 0; i < MAX; ++i){
			for(int j = 0; j < MAX; ++j){
				m[ i ][ j ] = BURIED;
			}
		}
	}
	void Print(){
		for(int i = 1; i <= H; ++i){
			for(int j = 1; j <= W; ++j){
				cout << (m[i][j]==BURIED?'#':'.');
			}
			cout << '\n';
		}
	}
	bool TryPutPiece( int i, int j, CPiece::piece &p ){
		CPiece::piece &pce = p;

		for(int ci = i; ci <= i + pce.H; ++ci){
			for(int cj = j; cj <= j + pce.W; ++cj){
				if( m[ci][cj] == BURIED && pce.m[ci - i][cj - j] == PZL )
					return false;
			}
		}
		return true;
	}
	void PutPiece( int i, int j, CPiece::piece &p ){
		CPiece::piece &pce = p;

		for(int ci = i; ci <= i + pce.H; ++ci){
			for(int cj = j; cj <= j + pce.W; ++cj){
				if( m[ci][cj] == NONE && pce.m[ci - i][cj - j] == PZL )
					m[ci][cj] = BURIED;
			}
		}
	}
	bool Complete(){
		for(int i = 1; i <= H; ++i){
			for(int j = 1; j <= W; ++j){
				if( m[i][j] == NONE )
					return false;
			}
		}
		return true;
	}
	int CountEmptyCells(){
		int ret = 0;
		for(int i = 1; i <= H; ++i){
			for(int j = 1; j <= W; ++j){
				if( m[i][j] == NONE ) ++ret;
			}
		}
		return ret;
	}
};

class CPieces{
public:
	vector< CPiece > vPieces;
	bitset< MAX_PIECES > bUse;
	bitset< MAX_PIECES > bUsed;
};

bool DFS( CPuzzle &p, CPieces &ps ){
	int choose = NIL;

	for(unsigned int i = 0; i < ps.vPieces.size(); ++i){
		if( ps.bUse[i] && !ps.bUsed[i] ){
			choose = i;
			break;
		}
	}
	
	if( choose == NIL ){
		if( p.Complete() )
			return true;
		else
			return false;
	}

	for(int k = 0; k < 4; ++k){
		for(int i = 1; i <= p.H - ps.vPieces[choose].pr[k].H; ++i){
			for(int j = 1; j <= p.W - ps.vPieces[choose].pr[k].W; ++j){
				if( p.TryPutPiece( i, j, ps.vPieces[choose].pr[k] ) ){
					CPuzzle pzl_tmp = p;
					bool bResult;
					
					pzl_tmp.PutPiece( i, j, ps.vPieces[choose].pr[k] );
					ps.bUsed.set( choose );
					
					bResult = DFS( pzl_tmp, ps );
					if( bResult )
						return true;
					ps.bUsed.reset( choose );
				}
			}
		}
	}
	return false;
}

int main(){
	while( true ){
		int n;
		int p, pnum;
		CPuzzle pzl;
		CPieces ps;

		pzl.Initialize();

		cin >> pzl.H >> pzl.W;

		if( pzl.H == 0 && pzl.W == 0 )
			break;

		for(int i = 1; i <= pzl.H; ++i){
			string s;
			cin >> s;
			for(int j = 1; j <= pzl.W; ++j){
				if( s[j - 1] == '#' ) 
					pzl.m[ i ][ j ] = BURIED;
				else
					pzl.m[ i ][ j ] = NONE;
			}
		}

		cin >> n;
		for(int i = 0; i < n; ++i){
			CPiece tmp;
			
			cin >> tmp.pr[0].H >> tmp.pr[0].W;
			for(int j = 1; j <= tmp.pr[0].H; ++j){
				string s;
				cin >> s;
				for(int k = 1; k <= tmp.pr[0].W; ++k){
					if( s[k - 1] == '#' )
						tmp.pr[0].m[ j ][ k ] = PZL;
					else
						tmp.pr[0].m[ j ][ k ] = NONE;
				}
			}
			tmp.CreateRotations();
			ps.vPieces.push_back( tmp );
		}

		cin >> p;
		for(int i = 0; i < p; ++i){
			cin >> pnum;
			int cntPzl = 0;
			int cntPss = 0;
			CPuzzle pzl_tmp = pzl;
			CPieces ps_tmp;

			ps_tmp.bUse.reset();
			ps_tmp.bUsed.reset();
			for( int j = 0; j < pnum; ++j ){
				int t;
				cin >> t;
				ps_tmp.vPieces.push_back( ps.vPieces[t-1] );
				cntPss += ps.vPieces[t-1].CountPuzzleCells();
				ps_tmp.bUse.set( j );
			}
			cntPzl = pzl_tmp.CountEmptyCells();

			if( cntPzl > cntPss ){
				cout << "NO\n";
				continue;
			}

			/* ツパツズツδ仰づ個妥・ツつォツつ「ツ渉づ可青ョツ療ア */
			sort( ps_tmp.vPieces.begin(), ps_tmp.vPieces.end(), greater<CPiece>() );

			if( DFS( pzl_tmp, ps_tmp ) )
				cout << "YES\n";
			else
				cout << "NO\n";
		}
	}
	return 0;
}