#include <iostream>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX_SIZE = 20;

struct Fit
{
	Fit( int _x, int _y, char _d ) : x( _x ), y( _y ), d( _d ){}
	int x, y;
	char d;
};

class Board
{
public:
	Board( int _w, int _h ) : w( _w ), h( _h ){
		memset( map, 0, sizeof(map) );
	}

	bool isFill() const
	{
		for( int i = 0; i < h; ++i ){
			for( int j = 0; j < w; ++j ){
				if( !map[ i ][ j ] ){
					return false;
				}
			}
		}
		return true;
	}

	int w, h;
	bool map[ MAX_SIZE ][ MAX_SIZE ];
};

class Piece
{
public:
	Piece() : h( 0 ), w( 0 ), numFitPosition( 0 ), setPattern( false )
	{
		memset( pattern, 0, sizeof(pattern) );
	}

	void makeRotationPattern()
	{
		if( !setPattern ){
			memset( pattern2, 0, sizeof(pattern2) );
			setPattern = true;
			for( int i = 0; i < h; ++i ){
				for( int j = 0; j < w; ++j ){
					pattern2[ 0 ][ i ][ j ] 
					= pattern2[ 1 ][ j ][ h - i - 1 ]
					= pattern2[ 2 ][ h - i - 1 ][ w - j - 1 ]
					= pattern2[ 3 ][ w - j - 1 ][ i ]
					= pattern[ i ][ j ];
				}
			}
		}
	}

	void findFitosition( Board& board )
	{
		if( h <= board.h && w <= board.w ){
			for( int i = 0; i < board.h - h + 1; ++i ){
				for( int j = 0; j < board.w - w + 1; ++j ){
					bool notFitN = false, notFitS = false;
					for( int y = 0; y < h; ++y ){
						for( int x = 0; x < w; ++x ){
							if( pattern[ y ][ x ] && board.map[ i + y ][ j + x ] ){
								notFitN = true;
							}
							if( pattern[ h - y - 1 ][ w - x - 1 ] && board.map[ i + y ][ j + x ] ){
								notFitS = true;
							}
							if( notFitN && notFitS ) break;
						}
						if( notFitN && notFitS ) break;
					}
					if( !notFitN ){
						fitPosition.push_back( Fit( j, i, 'N' ) );
					}
					if( !notFitS ){
						fitPosition.push_back( Fit( j, i, 'S' ) );
					}
				}
			}
		}
		if( w <= board.h && h <= board.w ){
			for( int i = 0; i < board.h - w + 1; ++i ){
				for( int j = 0; j < board.w - h + 1; ++j ){
					bool notFitE = false, notFitW = false;
					for( int y = 0; y < h; ++y ){
						for( int x = 0; x < w; ++x ){
							if( pattern[ y ][ x ] && board.map[ i + x ][ j + h - y - 1 ] ){
								notFitE = true;
							}
							if( pattern[ h - y - 1 ][ w - x - 1 ] && board.map[ i + x ][ j + h - y - 1 ] ){
								notFitW = true;
							}
							if( notFitE && notFitW ) break;
						}
						if( notFitE && notFitW ) break;
					}
					if( !notFitE ){
						fitPosition.push_back( Fit( j, i, 'E' ) );
					}
					if( !notFitW ){
						fitPosition.push_back( Fit( j, i, 'W' ) );
					}
				}
			}
		}
		numFitPosition = fitPosition.size();
	}

	bool put( Board& board, Fit& fit )
	{
		int index, hh, ww;
		switch( fit.d ){
			case 'N': index = 0; hh = h, ww = w; break;
			case 'E': index = 1; hh = w, ww = h; break;
			case 'S': index = 2; hh = h, ww = w; break;
			case 'W': index = 3; hh = w, ww = h; break;
		}

		for( int i = 0; i < hh; ++i ){
			for( int j = 0; j < ww; ++j ){
				if( pattern2[ index ][ i ][ j ] && board.map[ fit.y + i ][ fit.x + j ] ){
					return false;
				}
			}
		}

		for( int i = 0; i < hh; ++i ){
			for( int j = 0; j < ww; ++j ){
				if( pattern2[ index ][ i ][ j ] ){
					board.map[ fit.y + i ][ fit.x + j ] = true;
				}
			}
		}

		return true;
	}

	void remove( Board& board, Fit& fit )
	{
		int index, hh, ww;
		switch( fit.d ){
			case 'N': index = 0; hh = h, ww = w; break;
			case 'E': index = 1; hh = w, ww = h; break;
			case 'S': index = 2; hh = h, ww = w; break;
			case 'W': index = 3; hh = w, ww = h; break;
		}

		for( int i = 0; i < hh; ++i ){
			for( int j = 0; j < ww; ++j ){
				if( pattern2[ index ][ i ][ j ] ){
					board.map[ fit.y + i ][ fit.x + j ] = false;
				}
			}
		}
	}

	bool operator<( const Piece& p ) const
	{
		return numFitPosition < p.numFitPosition;
	}

	int h, w;
	bool pattern[ MAX_SIZE ][ MAX_SIZE ];
	bool pattern2[ 4 ][ MAX_SIZE ][ MAX_SIZE ];
	bool setPattern;
	vector<Fit> fitPosition;
	int numFitPosition;

};

bool isCorrect( Board& board, vector<Piece> list, int index )
{
	if( index == list.size() ){
		return board.isFill();
	}

	Board boardcpy = board;
	Piece piece = list[ index ];
	for( int i = 0; i < piece.numFitPosition; ++i ){
		if( piece.put( boardcpy, piece.fitPosition[ i ] ) ){
			if( isCorrect( boardcpy, list, index + 1 ) ){
				return true;
			}
			piece.remove( boardcpy, piece.fitPosition[ i ] );
		}
	}
	return false;
}

int main()
{
	int h, w;

	while( cin >> h >> w && !( h == 0 && w == 0 ) ){
		Board board( w, h );

		for( int i = 0; i < h; ++i ){
			for( int j = 0; j < w; ++j ){
				char c;
				cin >> c;
				board.map[ i ][ j ] = c == '#';
			}
		}

		vector<Piece> pieceList;
		int numPiece;
		cin >> numPiece;
		for( int i = 0; i < numPiece; ++i ){
			Piece p;
			cin >> p.h >> p.w;
			for( int y = 0; y < p.h; ++y ){
				for( int x = 0; x < p.w; ++x ){
					char c;
					cin >> c;
					p.pattern[ y ][ x ] = c == '#';
				}
			}
			p.makeRotationPattern();
			pieceList.push_back( p );
		}
		for( int i = 0; i < pieceList.size(); ++i ){
			pieceList.at( i ).findFitosition( board );
		}

		int numPlayer;
		cin >> numPlayer;
		for( int i = 0; i < numPlayer; ++i ){
			int numChosen;
			cin >> numChosen;
			vector<Piece> chosenList;
			for( int j = 0; j < numChosen; ++j ){
				int index;
				cin >> index;
				chosenList.push_back( pieceList.at( index - 1 ) );
			}
			sort( chosenList.begin(), chosenList.end() );
			cout << ( isCorrect( board, chosenList, 0 ) ? "YES" : "NO" ) << endl;
		}
	}
	return 0;
}