### subroutines

def fit?(b, x1, y1, x2, y2, grnds)
  for y in (y1..y2)
    for x in (x1..x2)
      return false if grnds[y][x] > 0 && grnds[y][x] != b
    end
  end
  true
end

def divk(k)
  for i in (1..k)
    if k % i == 0
      yield k / i, i
    end
  end
end

def gen_rects(b, k, x, y, grnds, w, h)
  rects = []

  divk(k) do |w0, h0|
    next if w0 > w || h0 > h

    for y0 in (0...h0)
      y1 = y - y0
      y2 = y + h0 - 1 - y0
      next if y1 < 0 || y2 >= h

      for x0 in (0...w0)
        x1 = x - x0
        x2 = x + w0 - 1 - x0
        next if x1 < 0 || x2 >= w

        if fit?(b, x1, y1, x2, y2, grnds)
          rects << [x1, y1, x2, y2]
        end
      end
    end
  end

  rects
end

def fill_rect(b, x1, y1, x2, y2, grnds)
  for y in (y1..y2)
    for x in (x1..x2)
      grnds[y][x] = b
    end
  end
end

def place_rec(id, n, buyers, grnds)
  if id >= n
    return [1, grnds.map{|hl| hl.map{|b| b}}]
  end

  count = 0
  gans = nil

  bi = buyers[id]
  for r in bi[4]
    if fit?(bi[0], *r, grnds)
      fill_rect(bi[0], *r, grnds)
      count0, gans0 = place_rec(id + 1, n, buyers, grnds)
      fill_rect(0, *r, grnds)
      grnds[bi[3]][bi[2]] = bi[0]

      count += count0
      if count > 1
        return [count, nil]
      elsif count0 == 1
        gans = gans0
      end
    end
  end

  [count, gans]
end

### main

while true
  w, h, n = gets.strip.split(' ').map{|s| s.to_i}
  break if w + h + n == 0

  buyers = []
  ksum = 0

  n.times.each do
    b, k = gets.strip.split(' ').map{|s| s.to_i}
    buyers[b - 1] = [b, k]
    ksum += k
  end

  if ksum != w * h
    h.times.each{gets}
    puts 'NA'
    next
  end

  grnds = h.times.map{gets.strip.split(' ').map{|s| s.to_i}}

  for y in (0...h)
    for x in (0...w)
      b = grnds[y][x]
      if b > 0
        b0 = buyers[b - 1]
        b0[2] = x
        b0[3] = y
        b0[4] = gen_rects(b0[0], b0[1], x, y, grnds, w, h)
      end
    end
  end
  #p grnds
  #p buyers

  count, gans = place_rec(0, n, buyers, grnds)

  if count != 1
    puts 'NA'
  else
    for hl in gans
      puts hl.join(' ')
    end
  end
end