#include<iostream>
#include<queue>
#include<vector>
using namespace std;
queue<pair<int, vector<vector<int> > > >Q;
vector<vector<int>>X, P; int H, W, N, V1[20], V2[20];
pair<bool, vector<vector<int> > > hantei(vector<vector<int> >a1, int X1, int Y1, int H1, int W1, int N1) {
	bool OK = true;
	for (int i = X1; i < X1 + H1; i++) {
		for (int j = Y1; j < Y1 + W1; j++) {
			if (i >= H || j >= W) { OK = false; continue; }
			if (a1[i][j] != 0 || (X[i][j] != V1[N1] && X[i][j] >= 1))OK = false;
			a1[i][j] = V1[N1];
		}
	}
	return make_pair(OK, a1);
}
int main() {
	while (true) {
		cin >> W >> H >> N; int D1 = 0; if (W == 0 && H == 0 && N == 0)break;
		for (int i = 0; i < N; i++) { cin >> V1[i] >> V2[i]; D1 += V2[i]; }
		for (int i = 0; i < H; i++) {
			vector<int>G, I; int a;
			for (int j = 0; j < W; j++) {
				cin >> a; G.push_back(a); I.push_back(0);
			}
			X.push_back(G); P.push_back(I);
		}
		if (D1 != W*H) { cout << "NA" << endl; continue; }
		Q.push(make_pair(0, P)); vector<vector<int> >res; int cnt = 0;
		while (!Q.empty()) {
			vector<vector<int>>K = Q.front().second;
			int L = Q.front().first; Q.pop();
			if (L == N) { cnt++; res = K; continue; }
			for (int i = 0; i < H; i++) {
				for (int j = 0; j < W; j++) {
					for (int k = 1; k <= V2[L]; k++) {
						if (V2[L] % k != 0)continue;
						pair<bool, vector<vector<int> > >res1 = hantei(K, i, j, k, V2[L] / k, L);
						if (res1.first == true) { Q.push(make_pair(L + 1, res1.second)); }
					}
				}
			}
		}
		if (cnt == 0 || cnt >= 2)cout << "NA" << endl;
		else {
			for (int i = 0; i < H; i++) {
				for (int j = 0; j < W; j++) {
					if (j >= 1) { cout << ' '; }cout << res[i][j];
				}
				cout << endl;
			}
		}
	}
	return 0;
}