#include<iostream>
#include<algorithm>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)

#define PUT 0
#define REMOVE 1
#define CHECK_PLANE 2

int cnt;
int m[10][10];
int ans[10][10];

bool is_origin[10][10];
bool make_rect(int now,int x,int y,int width,int height,int flag,
	       int r,int c){
  rep(i,height){
    rep(j,width){
      if ( flag == PUT )m[y+i][j+x]=now;
      else if ( flag == REMOVE && is_origin[y+i][x+j] == false){
	m[y+i][j+x]=0;
      }else if (flag == CHECK_PLANE){
	if ( m[y+i][j+x]!=0 && m[y+i][j+x]!=now)return false;
      }
    }
  }
  return true;
}

void solve(int num,int now,int *number,int *x,int *y,int *size,int r,int c){
  if ( now == num){
    cnt++;
    rep(i,r)rep(j,c)ans[i][j]=m[i][j];
    return;
  }

  if ( cnt > 1)return;
  
  int cur=number[now];
  REP(k,1,size[now]+1){
    if ( size[now]%k != 0)continue;
    int h=k,w=size[now]/k;
    int hstart=max(0,y[now]-h+1),wstart=max(0,x[now]-w+1);
    REP(i,hstart,y[now]+1 && i+h <=r){
      REP(j,wstart,x[now]+1 && j+w <=c){
	if ( make_rect(cur,j,i,w,h,CHECK_PLANE,r,c) ){
	  make_rect(cur,j,i,w,h,PUT,r,c);
	  solve(num,now+1,number,x,y,size,r,c);
	  make_rect(cur,j,i,w,h,REMOVE,r,c);
	}
      }
    }
  }
}


main(){
  int c,r,n;
  while(cin>>c>>r>>n && c){
    pair<int,int> in[n];
    int x[n],y[n],size[n],tmp[100+1],num[100+1];
    rep(i,n)cin>>in[i].second>>in[i].first;
    sort(in,in+n,greater<pair<int,int> >());
    rep(i,n)tmp[in[i].second]=i,size[i]=in[i].first,num[i]=in[i].second;
    rep(i,r){
      rep(j,c){
	cin>>m[i][j];
	if ( m[i][j] != 0){
	  x[tmp[m[i][j]]]=j;
	  y[tmp[m[i][j]]]=i;
	  is_origin[i][j]=true;
	}
	is_origin[i][j]=false;
      }
    }
    cnt = 0;

    solve(n,0,num,x,y,size,r,c);
    if ( cnt == 1){
      rep(i,r){
	rep(j,c){
	  if ( j)cout << ' ';
	  cout <<ans[i][j];
	}
	cout << endl;
      }
    }else cout << "NA"<<endl;
  }
}