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

int W,H,n;
vector<int> number;

int solve = 0;

struct Rect{
	int x1,y1,x2,y2; 
	Rect(int a,int b,int c,int d){x1 = a , y1 = b , x2 = c, y2 = d;};
};
vector< vector<Rect> > data(15);
vector< vector<int> > output(10,vector<int>(10));
vector< vector<int> > bt(10,vector<int>(10));

void dfs(int pos){
	if(solve > 2)return;

	if(pos == n){
		output = bt;
		solve++;
		return;
	}
	vector< vector<int> > prev = bt;
	
	rep(i,data[pos].size()){
		Rect t = data[pos][i];
		bool flag = true;
		for(int i=t.y1;i<=t.y2;i++){
			for(int j=t.x1;j<=t.x2;j++){
				if(bt[i][j] != -1){
					flag = false;
				}
				bt[i][j] = pos;
			}
		}
		if(flag){
			dfs(pos+1);
		}
		bt = prev;
	}
	return;
}
int main(){
	while(cin >> W >> H >> n , n){
		rep(i,10)rep(j,10)bt[i][j] = -1;
		rep(i,15)data[i].clear();
		vector<int> k(n);
		number.resize(n);
		rep(i,n)cin >> number[i] >> k[i];
		vector< vector<int> > map(H,vector<int>(W,0));
		rep(i,H)rep(j,W)cin >> map[i][j];
		rep(i,H)rep(j,W){
			for(int x=j;x<W;x++){
				for(int y=i;y<H;y++){
					int num = -1 , cnt = 0;
					bool flag = true;
					for(int X=j;X<=x;X++){
						for(int Y=i;Y<=y;Y++){
							if(map[Y][X] != 0){
								cnt++;
								num = find(number.begin(),number.end(),map[Y][X])-number.begin();
							}
						}
					}
					int tx = x-j+1, ty = y-i+1;
					if(cnt != 1 || tx * ty != k[num])continue;
					data[num].push_back(Rect(j,i,x,y));
				}
			}
		}
		solve = 0;
		dfs(0);
		if(solve == 1){
			//cout << W << " " << H << endl;
			rep(i,H){
					rep(j,W)
						cout << (j?" ":"") << number[output[i][j]];
					cout << endl;
			}
		}else{
			cout << "NA" << endl;
		}
	}
}