#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int w, h, n;
int space[100];
int iniField[20][20];
int field[110][20][20];
int ansField[20][20];
int used[20];

void PrintField(int field[20][20]) {
  REP(y, h) {
    REP(x, w) {
      if (x != 0) { putchar(' '); }
      printf("%d", field[y][x]);
    }
    puts("");
  }
}

int calc(int x, int y) {
  int depth = y * w + x;
  int ndepth = depth + 1;
  if (y == h) {
    memcpy(ansField, field[depth], sizeof(field[depth]));
    return 1;
  }
  if (x == w) { return calc(0, y + 1); }
  if (field[depth][y][x] != -1) {
    memcpy(field[ndepth], field[depth], sizeof(field[depth]));
    return calc(x + 1, y);
  }
  int ret = 0;
  REP(i, n) {
    if (used[i]) { continue; }
    used[i] = i;
    FOREQ(hh, 1, space[i]) {
      memcpy(field[ndepth], field[depth], sizeof(field[depth]));
      int ww = space[i] / hh;
      bool exist = false;
      if (hh * ww != space[i]) { continue; }
      REP(yy, hh) {
        REP(xx, ww) {
          if (field[ndepth][yy + y][xx + x] != -1 ||
              (iniField[yy + y][xx + x] != 0 &&
               iniField[yy + y][xx + x] != i + 1)) { goto next; }
          exist |= iniField[yy + y][xx + x] == i + 1;
          field[ndepth][yy + y][xx + x] = i + 1;
        }
      }
      if (!exist) { goto next; }
      ret += calc(x + 1, y);
      if (ret >= 2) { return ret; }
next:;
    }
    used[i] = 0;
  }
  return ret;
}

int main() {
  while (scanf("%d %d %d", &w, &h, &n), w|h|n) {
    MEMSET(used, 0);
    MEMSET(iniField, 0x0f);
    REP(i, n) {
      int f, v;
      scanf("%d %d", &f, &v);
      space[f - 1] = v;
    }
    REP(y, h) {
      REP(x, w) {
        scanf("%d", &iniField[y][x]);
      }
    }
    MEMSET(field, -1);
    if (calc(0, 0) == 1) {
      PrintField(ansField);
    } else {
      puts("NA");
    }
  }
}