#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int W, H, N; 
int b[16], k[16];
int s[10][10];
int ans[10][10];
int used[10][10];
int TX[20], TY[20];
class DFSError {};
void dfs(int idx){
  if(idx == N) {
    if(ans[0][0] != -1) throw DFSError();
    REP(y, H)REP(x, W) ans[y][x] = used[y][x];
    return;
  }
  /*
  printf("idx = %d\n", idx);
  REP(y, H){
    REP(x, W){
      cout << used[y][x];
      if(x != W - 1) cout << " ";
      else cout << endl;
    }
  }
  */
  int tx = TX[b[idx] - 1];
  int ty = TY[b[idx] - 1];
  //printf("idx = %d tx = %d ty = %d\n", idx, tx, ty);
  
  assert(idx < N);
  assert(b[idx] >= 1 && b[idx] <= N);
  assert(tx != -1);
  for(int h = 1; h <= k[idx]; h++) if(k[idx] % h == 0){
    int w = k[idx] / h;
    for(int by = 0; by > -h; by--) for(int bx = 0; bx > -w; bx--){
      int sx = tx + bx;
      int sy = ty + by;
      bool ok = true;
      for(int y = 0; y < h; y++) for(int x = 0; x < w; x++) {
        if(!valid(sx + x, sy + y, W, H) || used[sy + y][sx + x] != -1) ok = false;
      }
      if(ok){
        int tmp[10][10];
        memcpy(tmp, used, sizeof(used));
        for(int y = 0; y < h; y++) for(int x = 0; x < w; x++) {
          used[sy + y][sx + x] = b[idx];
        }
        dfs(idx + 1);
        memcpy(used, tmp, sizeof(used));
      }
    }
  }
}

int main(){
  while(cin >> W >> H >> N && (W | H | N)){
    REP(i, N) cin >> b[i] >> k[i];
    REP(y, H)REP(x, W) cin >> s[y][x];
    try{
      memset(used, -1, sizeof(used));
      memset(ans, -1, sizeof(ans));
      memset(TX, -1, sizeof(TX));
      memset(TY, -1, sizeof(TY));
      REP(y, H) REP(x, W) if(s[y][x] > 0) TX[s[y][x] - 1] = x, TY[s[y][x] - 1] = y;
      REP(i, N)if(TX[i] == -1){
        /*
        cerr << W << " " << H << " " << N << endl;
        REP(j, N) cerr << b[j] << " " << k[j] << endl;
        REP(y, H) { REP(x, W) cerr << s[y][x] << " "; cerr << endl; }
        assert("bord is dissapeared" == "");
        */
        throw 0;
      }
      dfs(0);
      if(ans[0][0] == -1) throw DFSError();
      REP(y, H){
        REP(x, W){
          cout << ans[y][x];
          if(x != W - 1) cout << " ";
          else cout << endl;
        }
      }
    }catch(...){
      cout << "NA" << endl;
    }
  }
  return 0;
}