Blank, Fill = 0, 1

class Piece
  def initialize(tile)
    @direction = 0
    @tile = Array.new(4)
    @tile[0] = tile
    h, w = tile.size, tile.first.size
    3.times do |d|
      @tile[d + 1] = Array.new(d.odd? ? h : w){Array.new((d.odd? ? w : h), 0)}
      @tile[d + 1].size.times do |i|
        @tile[d + 1].first.size.times do |j|
          @tile[d + 1][i][j] = @tile[d][j][(d.odd? ? h : w) - 1 - i]
        end
      end
    end
  end

  def h
    @tile[@direction].size
  end

  def w
    @tile[@direction].first.size
  end

  def tile(i, j)
    @tile[@direction][i][j]
  end

  def rotate
    @direction = (@direction + 1) % 4
  end

  def view
    4.times do |d|
      p "direction #{d}"
      @tile[d].each do |line|
        p line
      end
    end
  end
  def view1
      p "direction #{@direction}"
      @tile[@direction].each do |line|
        p line
      end
    end
end

def copy(target)
  Marshal.load(Marshal.dump(target))
end

def dfs(puzzle, piece)
# p piece
# p piece.size
# puzzle.each{|v|p v}
  return @memo if @memo[[puzzle, piece]]
  y, x = nil, nil
  @h.times do |i|
    @w.times do |j|
      next unless puzzle[i][j] == Blank
      y, x = i, j
      break
    end
    break if y
  end
  return (piece.empty? ? (@memo[[puzzle, piece]] = 1) : (@memo[[puzzle, piece]] = 0)) unless y

  piece.size.times do |index|
    p = copy(@piece[piece[index]])
    4.times do
      p.rotate
      dx = 0
      dx += 1 until p.tile(0, dx) == Fill
      next if x - dx < 0
      next if @h <= y + p.h
      next if @w <= x - dx + p.w
      _puzzle = copy(puzzle)
      ok = true
      p.h.times do |i|
        p.w.times do |j|
          _puzzle[y + i][x - dx + j] += p.tile(i, j)
          next if _puzzle[y + i][x - dx + j] == Fill
          next if _puzzle[y + i][x - dx + j] == Blank
          ok = false
          break
        end
        break unless ok
      end
      next unless ok
      _piece = copy(piece)
      _piece.delete_at(index)
      return (@memo[[puzzle, piece]] = 1) if dfs(_puzzle, _piece) == 1
    end
  end

  @memo[[puzzle, piece]] = 0
end

until (@h, @w = gets.split.map(&:to_i)).all?(&:zero?)
  puzzle = @h.times.map{gets.chomp.chars.map{|c| c == "#" ? Fill : Blank}}
  @piece = Array.new
  gets.to_i.times do
    h1, w1 = gets.split.map(&:to_i)
    @piece << Piece.new(h1.times.map{gets.chomp.chars.map{|c| c == "#" ? Fill : Blank}})
  end
  gets.to_i.times do
    @memo = Hash.new
    selected = gets.split.map(&:to_i)
    selected.shift
    puts dfs(copy(puzzle), selected.map{|i| i - 1}) == 1 ? "YES" : "NO"
  end
end