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

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int w[15];
int b[15];
int m[10][10];
int rev[16];
int res[10][10];
int ans[10][10];
vector<int> div0[15];
bool found;
bool ok;
int x,y,n;
int xx[15];
int yy[15];

void solve(int c){
  /*
  printf("solve(%d): b=%d w=%d\n",c,b[c],w[c]);
  REP(i,y) { REP(j,x) printf("%d ",res[i][j]); puts(""); } puts("");
  REP(i,y) { REP(j,x) printf("%d ",m[i][j]); puts(""); } puts("");
  */

  if(c == n)
    if(found){
      ok = false;
      return;
    }else{
      found = true;
      ok = true;
      REP(i,y) REP(j,x) ans[i][j] = res[i][j];
      return;
    }

  FOR(it,div0[c]){
    int width  = *it;
    int height = w[c] / width;

    int xxx = xx[c];
    int yyy = yy[c];

    for(int i = -height + 1; i < height; i++){
      for(int j = -width + 1; j < width; j++){
        if(xxx - j < 0 || yyy - i < 0) continue;
        int XX = xxx - j + width - 1;
        int YY = yyy - i + height - 1;
        if(XX >= x || YY >= y) continue;
        //printf("X=%d, Y=%d, (%d,%d)\n",xxx-j,yyy-i,i,j);
        REP(ii,height) REP(jj,width){
          int X = xxx - j + jj;
          int Y = yyy - i + ii;

          if(res[Y][X] == 0 && (m[Y][X] == 0 || m[Y][X] == c+1)){
            res[Y][X] = c + 1;
          }else{
            REP(iii,ii+1) REP(jjj,width)
              if(res[yyy-i+iii][xxx-j+jjj] == c+1)
                res[yyy-i+iii][xxx-j+jjj] = 0;
            j+=(width - jj - 1);
            goto next;
          }
        }
        solve(c+1);
        REP(ii,height) REP(jj,width){
          int X = xxx - j + jj;
          int Y = yyy - i + ii;
          res[Y][X] = 0;
        }
        //printf("solve(%d)(backed): b=%d w=%d (%d,%d)\n",c,b[c],w[c],i,j);
        if(found && !ok) return;
      next:;
        continue;
      }
    }
  }
}

int main(){
  while(true){
    x = getInt();
    y = getInt();
    n = getInt();

    if(x + y + n == 0) break;

    REP(i,n){
      b[i] = getInt();
      rev[b[i]] = i+1;
      w[i] = getInt();

      div0[i] = vector<int>();
      rep(j,1,w[i]+1) if(w[i] % j == 0)
        div0[i].push_back(j);
    }

    REP(i,y) REP(j,x){
      m[i][j] = getInt();
      if(m[i][j]){
        m[i][j] = rev[m[i][j]];
        yy[m[i][j]-1] = i;
        xx[m[i][j]-1] = j;
      }
    }

    found = false;
    solve(0);

    if(found && ok)
      REP(i,y) REP(j,x){
        int t = ans[i][j];
        if(t != 0) t = b[t-1];
        printf("%d%c",ans[i][j],(j==x-1?'\n':' '));
      }
    else
      puts("NA");
  }
  return 0;
}

Measured memory_bytes: 884