#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <complex>
using namespace std;

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define EPS 1e-8
#define DEB 0

int w,h,n;
int field[16][16];
int check[16][16],output[16][16];
int ans;
struct state{
	int area,x,y;
} memo[16];

bool ok(int x, int y, int zx, int zy, int num){
	rep(i,zy){
		rep(j,zx){
			int tx = x + j;
			int ty = y + i;
			if( tx<0 || ty<0 || tx>=w || ty>=h || check[ty][tx]>0 )return false;
			if( field[ty][tx]!=0 && field[ty][tx]!=num )return false;
		}
	}
	return true;
}

void dfs(int cnt){
#if DEB
	printf("cnt:%d\n",cnt);
	rep(i,h){
		rep(j,w){
			printf("%d ",check[i][j]);
		}
		puts("");
	}
	puts("");
#endif
	
	if( cnt==n ){
		ans++;
		if( ans>=2 ) return;
		rep(i,h)rep(j,w)output[i][j]=check[i][j];

		rep(i,h)rep(j,w)if( check[i][j]==0 )ans=2;
		return;
	}

	for(int i=1; i<=memo[cnt].area; i++){
		if( memo[cnt].area%i!=0 )continue;
		int j = memo[cnt].area / i;
		int si = memo[cnt].y - i + 1;
		int sj = memo[cnt].x - j + 1;

#if DEB
		printf("(i,j)=(%d,%d),  (si,sj)=(%d,%d)\n",i,j,si,sj);
#endif		

		for(int ii=si; ii<=memo[cnt].y; ii++){
			if( ii<0 )continue;
			for(int jj=sj; jj<=memo[cnt].x; jj++){
				if( jj<0 )continue;
				if( ok(jj, ii, j, i, cnt+1) ){
#if DEB
					printf("isok:: jj:%d , ii:%d, j:%d, i:%d\n",jj,ii,j,i);
#endif 					
					rep(k,i)rep(l,j)check[ii+k][jj+l]=cnt+1;
					dfs(cnt+1);
					if( ans>=2 ) return;
					rep(k,i)rep(l,j)check[ii+k][jj+l]=0;
				}
			}
		}
	}
}
int main(){
	
	while(scanf("%d%d%d",&w,&h,&n),w|h|n){
		rep(i,16)rep(j,16)check[i][j]=output[i][j]=0;
		ans = 0;
		
		rep(i,n){
			int b,k; scanf("%d%d",&b,&k);
			memo[b-1].area = k;
		}
		rep(i,h)rep(j,w)scanf("%d",&field[i][j]);
		rep(i,h)rep(j,w)if( field[i][j]>0 ){
			memo[ field[i][j]-1 ].x = j;
			memo[ field[i][j]-1 ].y = i;
		}
		
		dfs(0);

		if( ans==1 ){
			rep(i,h){
				rep(j,w){
					if( j>0 )putchar(' ');
					printf("%d",output[i][j]);
				}
				puts("");
			}
		}else{
			puts("NA");
		}
	}
	
	return 0;
}