#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
	int H, W, N;
	while (cin >> W >> H >> N, N) {
		vector<int> S(N);
		int sum = 0;
		for (int i = 0; i < N; ++i) {
			int b, x;
			cin >> b >> x; --b;
			S[b] = x;
			sum += x;
		}
		vector<vector<int> > C(H, vector<int>(W));
		for (int i = 0; i < H; ++i) {
			for (int j = 0; j < W; ++j) {
				cin >> C[i][j]; --C[i][j];
			}
		}
		vector<vector<int> > curstate(H, vector<int>(W, -1));
		vector<vector<vector<int> > > sols;
		function<void(int)> dfs = [&](int bit) {
			if (bit == 0) {
				sols.push_back(curstate);
				return;
			}
			int x = -1, y = -1;
			for (int i = 0; i < H && x == -1; ++i) {
				for (int j = 0; j < W && y == -1; ++j) {
					if (curstate[i][j] == -1) {
						x = i, y = j;
					}
				}
			}
			for (int i = 0; i < N; ++i) {
				if (!((bit >> i) & 1)) continue;
				for (int j = 1; j <= S[i]; ++j) {
					if (S[i] % j != 0) continue;
					int h = j, w = S[i] / j;
					if (x + h > H || y + w > W) continue;
					bool f1 = true, f2 = false;
					for (int k = 0; k < h; ++k) {
						for (int l = 0; l < w; ++l) {
							if (C[x + k][y + l] == i) f2 = true;
							else if (C[x + k][y + l] != -1 || curstate[x + k][y + l] != -1) f1 = false;
						}
					}
					if (f1 && f2) {
						for (int k = 0; k < h; ++k) {
							for (int l = 0; l < w; ++l) {
								curstate[x + k][y + l] = i;
							}
						}
						dfs(bit - (1 << i));
						for (int k = 0; k < h; ++k) {
							for (int l = 0; l < w; ++l) {
								curstate[x + k][y + l] = -1;
							}
						}
					}
				}
			}
		};
		if (sum == H * W) {
			dfs((1 << N) - 1);
		}
		if (sols.size() != 1) {
			cout << "NA" << endl;
		}
		else {
			for (int i = 0; i < H; ++i) {
				for (int j = 0; j < W; ++j) {
					if (j) cout << ' ';
					cout << sols[0][i][j] + 1;
				}
				cout << endl;
			}
		}
	}
	return 0;
}
