#include<iostream>
#include<cstring>
#include<cassert>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define repn(i, m, n) for (int i = m; i < int(n); ++i)

int bk[20];
int mp[20][20];
int res[20][20];
int result[20][20];
int xy[20][2];
int x, y ,n;

int solve(int used[], int bit) {
  /*
  rep (i, y) {
    rep (j, x) {
      cout << res[i][j] << " ";
    }
    cout << endl;
  }
  cout << endl;
  */
  if (bit == 0) {
    rep (i, y) rep (j, x) result[i][j] = res[i][j];
    return 1;
  }
  int xx, yy;
  rep (i, y) {
    if (__builtin_popcount(used[i]) == x) continue;
    yy = i;
    xx = __builtin_popcount((~used[i] & -~used[i]) - 1);
    break;
  }
  int r = 0;
  rep (i, 20) {
    if ((bit & 1 << i) == 0) continue;
    if (xy[i][1] < xx) continue;
    repn (j, xy[i][1] - xx + 1, x - xx + 1) {
      if (bk[i] % j > 0) continue;
      if (yy + bk[i] / j > y) continue;
      if (yy + bk[i] / j <= xy[i][0]) continue;
      int bb = (1 << (xx + j)) - (1 << xx);
      bool ok = true;
      rep (k, bk[i] / j) {
	if (used[yy + k] & bb) {
	  ok = false;
	  break;
	}
      }
      if (!ok) continue;
      int next[20];
      rep (k, y) {
	next[k] = used[k];
      }
      rep (k, bk[i] / j) {
	rep (l, j) {
	  res[yy + k][xx + l] = i;
	}
	next[yy + k] |= bb;
      }
      r += solve(next, bit ^ 1 << i);
      if (r > 1) return r;
    }
  }
  return r;
}

int main() {
  int used[20], sum;
  for (;;) {
    cin >> x >> y >> n;
    if (x == 0 && y == 0 && n == 0) break;
    sum = 0;
    memset(bk, -1, sizeof(bk));
    rep (i, n) {
      int b, k;
      cin >> b >> k;
      bk[b] = k;
      sum += k;
    }
    if (sum != x * y) {
      cout << "NA\n";
      continue;
    }
    memset(xy, -1, sizeof(xy));
    int bit = 0;
    rep (i, y) rep (j, x) {
      cin >> mp[i][j];
      if (mp[i][j] > 0) {
	xy[mp[i][j]][0] = i;
	xy[mp[i][j]][1] = j;
	bit |= 1 << mp[i][j];
      }
    }
    rep (i, 20) {
      assert(!(bk[i] > 0 && xy[i][0] < 0)); 
    }
    memset(used, 0, sizeof(used));
    int r = solve(used, bit);
    if (r == 1) {
      rep (i, y) {
	rep (j, x - 1) cout << result[i][j] << " ";
	cout << result[i][x - 1] << endl;
      }
    } else {
      cout << "NA\n";
    }
  }
  return 0;
}