#include <bits/stdc++.h>
using namespace std;

int X, Y, n;
int area[16];
int used[10][10];
int ok[10][10];
int app[10][10];
int T[16];

int cnt;

bool check(int sy, int sx, int ey, int ex, int k)
{
	int t = T[k];
	for (int i = sy; i < ey; i++){
		for (int j = sx; j < ex; j++){
			if (used[i][j] != 0) return false;
			if (app[i][j] != 0 && app[i][j] != k) return false;
			if (app[i][j] == k) t--;
		}
	}
	return t == 0;
}

void Fill(int sy, int sx, int ey, int ex, int k)
{
	//if (k) printf("%d %d %d %d %d\n", k, sy, sx, ey, ex);
	
	for (int i = sy; i < ey; i++){
		for (int j = sx; j < ex; j++){
			used[i][j] = k;
		}
	}
}

void search(int k)
{
	if (k > n){
		cnt++;
		memcpy((int*)ok, (int*)used, sizeof(ok));
		
		/*for (int i = 0; i < Y; i++){
			for (int j = 0; j < X; j++){
				printf("%d ", ok[i][j]);
			}
			puts("");
		}*/
		
		return;
	}
	for (int i = 0; i < Y; i++){
		for (int j = 0; j < X; j++){
			for (int h = 1; i + h <= Y; h++){
				for (int w = 1; j + w <= X; w++){
					if (area[k] != h * w) continue;
					if (!check(i, j, i + h, j + w, k)) continue;
					Fill(i, j, i + h, j + w, k);
					search(k + 1);
					Fill(i, j, i + h, j + w, 0);
				}
			}
		}
	}
}

void solve()
{
	cnt = 0;
	search(1);
	if (cnt != 1) puts("NA");
	else {
		for (int i = 0; i < Y; i++){
			for (int j = 0; j < X; j++){
				if (j) printf(" ");
				printf("%d", ok[i][j]);
			}
			puts("");
		}
	}
}

int main()
{
	while (scanf("%d %d %d", &X, &Y, &n), X){
		memset(T, 0, sizeof(T));
		
		for (int i = 0; i < n; i++){
			int b, k;
			scanf("%d %d", &b, &k);
			area[b] = k;
		}
		for (int i = 0; i < Y; i++){
			for (int j = 0; j < X; j++){
				scanf("%d", &app[i][j]);
				T[app[i][j]]++;
			}
		}
		solve();
	}
}