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

struct Info{
	int x,y,w,h;
	Info(int x,int y,int w,int h) : x(x) , y(y) , w(w) , h(h) {}
	Info(){x=y=w=h=-99999;}
};

map<int,vector<Info> > info;

int c[11][11];
int ok = 0;
int current[11][11];
int answer[11][11];

int dfs(map<int,vector<Info> >::iterator it){
	if( ok >= 2 ) return 0;
	if( it == info.end()){
		int bad = 0;
		for(int i = 0 ; i < H ; i++){
			for(int j = 0 ; j < W ; j++){
				if( current[i][j] == 0 ) bad = true;
			}
		}
		if( bad ) return 0;
		ok++;
		rep(i,11)rep(j,11) answer[i][j] = current[i][j];
		return 0;
	}else{
		vector<Info> &v = it->second;
		for(int i = 0 ; i < v.size() ; i++){
			int chk = 1;
			for(int y = 0 ; y < v[i].h ; y++){
				for(int x = 0 ; x < v[i].w ; x++){
					chk &= current[v[i].y+y][v[i].x+x] == 0;
				}
			}
			if( !chk ) continue;
			for(int y = 0 ; y < v[i].h ; y++){
				for(int x = 0 ; x < v[i].w ; x++){
					current[v[i].y+y][v[i].x+x] = it->first;
				}
			}
			it++;
			dfs(it);
			it--;
			for(int y = 0 ; y < v[i].h ; y++){
				for(int x = 0 ; x < v[i].w ; x++){
					current[v[i].y+y][v[i].x+x] = 0;
				}
			}
		}
		return 0;
	}
}
int main(){
	while(cin >> W >> H >> n && W){
		info.clear();
		for(int i = 0 ; i < 11 ; i++)
			for(int j = 0 ; j < 11 ; j++)
				answer[i][j] = current[i][j] = 0;
		for(int i = 0 ; i < n ; i++){	
			int a,b;
			cin >> a >> b;
			area[a] = b;
		}
		rep(i,H)rep(j,W) cin >> c[i][j];
		for(int sx = 1 ; sx <= W ; sx++){
			for(int sy = 1 ; sy <= H ; sy++){
				int cnt = 0 , number = 0;
				for(int i = 0 ; i <= H - sy ; i++){
					for(int j = 0 ; j <= W - sx ; j++){
						int cnt = 0 , number;
						for(int k = 0 ; k < sy ; k++){
							for(int l = 0 ; l < sx ; l++){
								int C = c[i+k][j+l];
								if( C != 0 ){
									cnt++;
									number = C;
								}
							}
						}
						if( cnt == 1 && area[number] == sx * sy ){
							info[number].push_back(Info(j,i,sx,sy));
						}
					}
				}
			}
		}
		ok = 0;
		dfs(info.begin());
		if( ok != 1 ){
			cout << "NA" << endl;
		}else{
			rep(i,H){
				rep(j,W) cout << (j?" ":"") << answer[i][j];
				cout << endl;
			}
		}
	}
}