#include <iostream>
#include <vector>
#include <cstring>
#include <map>
using namespace std;

int X, Y, n;
int id[10], k[10];
int sx[11], sy[11];
int bd[10][10];

vector<pair<int,int> > area[101];
int ans[10][10];

int dfs(int u) {
	if (u == n) {
		memcpy(ans, bd, sizeof ans);
		return 1;
	}
	int h, w, tx, ty, lx, rx, uy, dy;
	tx = sx[id[u]];
	ty = sy[id[u]];
	bd[ty][tx] = 0;
	int ret = 0;
	for (int i=0; i<(int)area[k[u]].size(); ++i) {
		h = area[k[u]][i].first;
		w = area[k[u]][i].second;
		lx = max(tx-w+1, 0);
		rx = min(tx+w-1, X-1);
		uy = max(ty-h+1, 0);
		dy = min(ty+h-1, Y-1);
		if (rx-lx+1 < w || dy-uy+1 < h) continue;
		for (int y=uy; y+h-1<=dy; ++y) {
			for (int x=lx; x+w-1<=rx; ++x) {
				bool ok = true;
				for (int y2=y; y2<y+h && ok; ++y2) {
					for (int x2=x; x2<x+w; ++x2) {
						if (bd[y2][x2] != 0) {
							ok = false;
							break;
						}
					}
				}
				if (ok) {
					for (int y2=y; y2<y+h; ++y2) {
						for (int x2=x; x2<x+w; ++x2) {
							bd[y2][x2] = id[u];
						}
					}
					ret += dfs(u+1);
					for (int y2=y; y2<y+h; ++y2) {
						for (int x2=x; x2<x+w; ++x2) {
							bd[y2][x2] = 0;
						}
					}
					if (2 <= ret) return 2;
				}
			}
		}
	}
	bd[ty][tx] = id[u];
	return ret;
}

int main() {
	for (int i=1; i<=10; ++i) {
		for (int j=1; j<=10; ++j) {
			area[i*j].push_back(make_pair(i, j));
		}
	}
	while (cin >> X >> Y >> n, X) {
		int sum = 0;
		for (int i=0; i<n; ++i) {
			cin >> id[i] >> k[i];
			sum += k[i];
		}
		for (int i=0; i<Y; ++i) {
			for (int j=0; j<X; ++j) {
				cin >> bd[i][j];
				if (bd[i][j] != 0) {
					sx[bd[i][j]] = j;
					sy[bd[i][j]] = i;
				}
			}
		}
		int res = dfs(0);
		if (X * Y != sum || res == 0 || 2 <= res) {
			cout << "NA\n";
		} else {
			for (int i=0; i<Y; ++i) {
				for (int j=0; j<X; ++j) {
					cout << (j == 0 ? "" : " ") << ans[i][j];
				}
				cout << endl;
			}
		}
	}
	return 0;
}