#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <functional>
using namespace std;

#define N_MAX 15
#define S_MAX 10

int X, Y, n, M[S_MAX][S_MAX];
vector< pair<int, int> > k;
int ans;

struct Rect {
  int x, y, w, h;
};
Rect R[N_MAX];

bool isHit(int u) {
  for(int i = 0; i < n; ++i) {
    if(i == u) continue;
    if(R[u].x >= R[i].x + R[i].w) continue;
    if(R[u].y >= R[i].y + R[i].h) continue;
    if(R[i].x >= R[u].x + R[u].w) continue;
    if(R[i].y >= R[u].y + R[u].h) continue;
    return true;
  }
  return false;
}

void draw() {
  for(int i = 0; i < n; ++i) {
    for(int dx = 0; dx < R[i].w; ++dx) {
      for(int dy = 0; dy < R[i].h; ++dy) {
	M[R[i].y+dy][R[i].x+dx] = i+1;
      }
    }
  }
}

void rec(int p) {
  if(p == n) {
    draw();
    ans++;
    return;
  }

  int x = R[k[p].second].x;
  int y = R[k[p].second].y;

  for(int h = 1; h <= k[p].first; ++h) {
    if(k[p].first % h != 0) continue;
    int w = k[p].first / h;
    R[k[p].second].h = h;
    R[k[p].second].w = w;
    for(int ny = y-h+1; ny <= y; ++ny) {
      for(int nx = x-w+1; nx <= x; ++nx) {
	if(nx < 0 || nx + w > X) continue;
	if(ny < 0 || ny + h > Y) continue;
	R[k[p].second].x = nx;
	R[k[p].second].y = ny;
	if(!isHit(k[p].second)) rec(p+1);
	if(ans >= 2) return;
      }
    }
  }
  R[k[p].second].x = x;
  R[k[p].second].y = y;
  R[k[p].second].w = 1;
  R[k[p].second].h = 1;
}


void solve() {
  ans = 0;
  sort(k.begin(), k.end(), greater< pair<int, int> >());
  rec(0);
  if(ans == 1) {
    for(int i = 0; i < Y; ++i) {
      for(int j = 0; j < X; ++j) {
	if(j != 0) cout << " ";
	cout << M[i][j];
      }
      cout << endl;
    }
  } else {
    cout << "NA" << endl;
  }
}

main() {
  while((cin >> X >> Y >> n) && (X || Y || n)) {
    k.clear();
    for(int i = 0; i < n; ++i) {
      int b, s;
      cin >> b >> s;
      b--;
      k.push_back(pair<int, int>(s, b));
    }
    for(int i = 0; i < Y; ++i) {
      for(int j = 0; j < X; ++j) {
	cin >> M[i][j];
	if(M[i][j] != 0) {
	  int u = M[i][j] - 1;
	  R[u].x = j;
	  R[u].y = i;
	  R[u].w = 1;
	  R[u].h = 1;
	}
      }
    }
    solve();
  }
}