#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <string>
#include <map>
#define FOR(i,b,a) for(int i=(b);i<=(a);i++)
#define REP(i,a) for(int i=0;i<(a);i++)
using namespace std;
typedef pair<int,int> pr;
const int INF=1<<30;
int maps[20][20],px[20],py[20];
int ans[20][20],msize[20];
int X,Y,n,b,k,counts=0;
void search(int p){
	if(p==n+1){
		REP(i,20)
			REP(j,20)
				ans[i][j]=maps[i][j];
		counts++;
		return ;
	}

	FOR(i,1,X){
		int x=i,y=msize[p]/i;
		if(x*y==msize[p]){
			FOR(j,max(1,py[p]-y+1),min(Y-y+1,py[p]))
				FOR(k,max(1,px[p]-x+1),min(X-x+1,px[p])){
					bool able=true;
					REP(ii,y)
						REP(jj,x)
							if(maps[ii+j][jj+k]!=p && maps[ii+j][jj+k]!=0)
								able=false;
					if(able){
						REP(ii,y)
							REP(jj,x)
								maps[ii+j][jj+k]=p;
							//	cout << j << k << endl;
						search(p+1);
						REP(ii,y)
							REP(jj,x)
								maps[ii+j][jj+k]=0;
						maps[py[p]][px[p]]=p;
					}
				}
		}
	}
}
int main() {
	while(cin >> X >> Y >>n && X){
	counts=0;
	FOR(i,1,n){
	cin >> b>>k;
	msize[b]=k;
	}
	FOR(i,1,Y)
		FOR(j,1,X){
			cin >> maps[i][j];
			if(maps[i][j]){
				px[maps[i][j]]=j;
				py[maps[i][j]]=i;
			}
			
		}
	search(1);
	if(counts!=1)
		cout << "NA" << endl;
	else
		FOR(i,1,Y){
			FOR(j,1,X)
				if(j==X)
					cout <<ans[i][j];
				else
					cout <<ans[i][j]<<" ";
			cout << endl;
		}
	// your code goes here
	}
	return 0;
}