#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reps(i,0,n)

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;

int w, h, n;
int area[15];
vector<pii> size[15];
pii flag[15];
int field[10][10];
int tmp[10][10];
int ans[10][10];
bool found;

void dfs(int y, int x, int d, int a[])
{
	if(d == n){
		if(!found){
			rep(i, h) rep(j, w)
				ans[i][j] = tmp[i][j];
			found = true;
		}
		else{
			ans[0][0] = -1;
		}
		return;
	}
	
	while(tmp[y][x]){
		++x;
		if(x == w){
			x = 0;
			++y;
		}
	}
	reps(k, d, n){
		swap(a[d], a[k]);
		int minh = flag[a[d]].first - y + 1;
		int minw = flag[a[d]].second - x + 1;
		if(0<=minh && 0<=minw){
			int maxh = h-y;
			int maxw = w-x;
			rep(l, size[a[d]].size()){
				int th = size[a[d]][l].first;
				int tw = size[a[d]][l].second;
				if(th<minh || maxh<th || tw<minw || maxw<tw)
					continue;
				rep(i, th) rep(j, tw){
					if(tmp[y+i][x+j] != 0 || (field[y+i][x+j] != 0 && field[y+i][x+j] != a[d]+1))
						goto end;
				}
				rep(i, th) rep(j, tw){
					tmp[y+i][x+j] = a[d] + 1;
				}
				dfs(y, x, d+1, a);
				rep(i, th) rep(j, tw){
					tmp[y+i][x+j] = 0;
				}
end:;
			}
		}
		swap(a[d], a[k]);
	}
}

int main()
{
	while(scanf("%d%d%d", &w, &h, &n), n){
		ans[0][0] = -1;
		rep(i, h) 
			fill(tmp[i], tmp[i]+w, 0);
		found = false;

		rep(i, n){
			int b, k;
			scanf("%d%d", &b, &k);
			--b;
			area[b] = k;
			size[b].clear();
			reps(y, 1, h+1){
				if(k%y == 0){
					int x = k / y;
					if(x <= w)
						size[b].push_back(pii(y, x));
				}
			}
		}
		rep(i, h) rep(j, w){
			scanf("%d", &field[i][j]);
			if(field[i][j] != 0)
				flag[field[i][j]-1] = pii(i, j);
		}
		int a[15];
		rep(i, n)
			a[i] = i;
		dfs(0, 0, 0, a);

		if(ans[0][0] == -1)
			puts("NA");
		else{
			rep(i, h) rep(j, w)
				printf("%d%c", ans[i][j], j==w-1?'\n':' ');
		}
	}
	return 0;
}