#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int INF = 1 << 28;

class Piece
{
private:
	int h, w;
	char g[20][20];
	vector<int> bit;

public:
	Piece() { };

	bool readFromStdin(bool cutflag = true)
	{
		cin >> h >> w;
		if (h == 0 &&  w == 0)
			return false;

		for (int y = 0; y < h; ++y)
		{
			for (int x = 0; x < w; ++x)
			{
				char c;
				cin >> c;
				g[y][x] = c;
			}
		}

		if (cutflag)
			reduce();

		makeBitVector();
		return true;
	}

	void reduce()
	{
		int ly, lx, hy, hx;
		int x, y;
		for (x = 0; x < w; ++x)
		{
			for (y = 0; y < h; ++y)
			{
				if (g[y][x] == '#')
					break;
			}			

			if (y < h)
				break;
		}

		lx = x;
		for (x = w - 1; x >= 0; --x)
		{
			for (y = 0; y < h; ++y)
			{
				if (g[y][x] == '#')
					break;
			}			

			if (y < h)
				break;
		}

		hx = x;
		for (y = 0; y < h; ++y)
		{
			for (x = 0; x < w; ++x)
			{
				if (g[y][x] == '#')
					break;
			}			

			if (x < w)
				break;
		}

		ly = y;
		for (y = h - 1; y >= 0; --y)
		{
			for (x = 0; x < w; ++x)
			{
				if (g[y][x] == '#')
					break;
			}			

			if (x < w)
				break;
		}

		hy = y;

		h = hy - ly + 1;
		w = hx - lx + 1;
		for (int y = 0; y < h; ++y)
		{
			for (int x = 0; x < w; ++x)
				g[y][x] = g[ly + y][lx + x];
		}
	}

	void makeBitVector()
	{
		bit.clear();
		for (int y = 0; y < h; ++y)
		{
			int b = 0;
			for (int x = 0; x < w; ++x)
				b |= ((g[y][x] == '#') ? 1 : 0) << x;

			bit.push_back(b);
		}
	}

	Piece rotate() const
	{
		Piece p;
		p.h = w;
		p.w = h;

		for (int y = 0; y < p.h; ++y)
		{
			for (int x = 0; x < p.w; ++x)
				p.g[y][x] = g[h - 1 - x][y];
		}

		p.makeBitVector();
		return p;
	}

	bool ok(const Piece &p, int by, int bx) const
	{
		if (by < 0 || by + p.h > h)
			return false;

		if (bx < 0 || bx + p.w > w)
			return false;

		for (int y = 0; y < p.h; ++y)
		{
			if (bit[by + y] & (p.bit[y] << bx))
				return false;
		}

		return true;
	}

	void bury(const Piece &p, int by, int bx)
	{
		for (int y = 0; y < p.h; ++y)
			bit[by + y] |= p.bit[y] << bx;
	}

	void erase(const Piece &p, int by, int bx)
	{
		for (int y = 0; y < p.h; ++y)
			bit[by + y] &= ~(p.bit[y] << bx);
	}

	int getH() const { return h; }
	int getW() const { return w; }
	int getSize() const { return h * w; }
	bool getDot(int y, int j) const { return (bit[y] >> j) & 1; }

	int getCount() const
	{
		int res = 0;
		for (int y = 0; y < h; ++y)
		{
			for (int x = 0; x < w; ++x)
				res += (g[y][x] == '#') ? 1 : 0;
		}

		return res;
	}

	void show() const
	{
		for (int y = 0; y < h; ++y)
		{
			for (int x = 0; x < w; ++x)
				cout << g[y][x];

			cout << endl;
		}
	}
};

const int MAX_N = 10;
int H, W;
Piece base;
int n;
Piece piece[MAX_N][4];
int t[MAX_N];

bool
rec(int s1, int e1)
{
	if (s1 == e1)
		return true;

	{
		Piece b = base;
		for (int by = 0; by < H; ++by)
		{
			for (int bx = 0; bx < W; ++bx)
			{
				if (!b.getDot(by, bx))
				{
					for (int i = s1; i != e1; ++i)
					{
						for (int a = 0; a < 4; ++a)
						{
							const Piece &p = piece[t[i]][a];
							int h = p.getH();
							int w = p.getW();
							for (int y = max(0, by - h + 1); y <= by && y + h <= H; ++y)
							{
								for (int x = max(0, bx - w + 1); x <= bx && x + w <= W; ++x)
								{
									if (base.ok(p, y, x))
										b.bury(p, y, x);
								}
							}
						}
					}
				}

				if (!b.getDot(by, bx))
					return false;
			}
		}
	}

	for (int a = 0; a < 4; ++a)
	{
		const Piece &p = piece[t[s1]][a];
		int h = p.getH();
		int w = p.getW();
		for (int y = 0; y + h <= H; ++y)
		{
			for (int x = 0; x + w <= W; ++x)
			{
				if (base.ok(p, y, x))
				{
					base.bury(p, y, x);
					bool res = rec(s1 + 1, e1);
					base.erase(p, y, x);
					if (res)
						return true;
				}
			}
		}
	}

	return false;
}

int
main(int argc, char **argv)
{
	int k;

	while (base.readFromStdin(false))
	{
		H = base.getH();
		W = base.getW();
		cin >> n;
		for (int s = 0; s < n; ++s)
		{
			piece[s][0].readFromStdin();
			for (int a = 1; a < 4; ++a)
				piece[s][a] = piece[s][a - 1].rotate();
		}

		int p;
		cin >> p;
		while(p--)
		{
			{
				cin >> k;
				vector<pair<int,int> > tmp(k);
				for (int i = 0; i < k; ++i)
				{
					cin >> tmp[i].second;
					tmp[i].second--;
					tmp[i].first = -piece[tmp[i].second][0].getSize();
				}

				sort(tmp.begin(), tmp.end());
				for (int i = 0; i < k; ++i)
					t[i] = tmp[i].second;
			}

			int sum = 0;
			for (int i = 0; i < k; ++i)
				sum += piece[t[i]][0].getCount();

			if (sum == base.getSize() - base.getCount() && rec(0, k))
				cout << "YES" << endl;
			else
				cout << "NO" << endl;
		}
	}

	return 0;
}