#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <complex>

using namespace std;

typedef complex<int> P;

vector< vector<int> > draw; int drawTimes;
void drawInit(int W, int H) {
	++W, ++H;

	drawTimes = 0;
	draw.resize(H);
	for (int i = 0; i < H; ++i) draw[i].resize(W);

	for (int y = 0; y < H; ++y)
		for (int x = 0; x < W; ++x)
			draw[y][x] = 0;
}
void drawSet(P p1, P p2) {
	++drawTimes;

	draw[p1.imag()][p1.real()] += 1;
	draw[p1.imag()][p2.real()] += -1;
	draw[p2.imag()][p1.real()] += -1;
	draw[p2.imag()][p2.real()] += 1;
}
void drawDo() {
	for (int y = 0; y < draw.size(); ++y)
		for (int x = 0; x < draw[0].size()-1; ++x)
			draw[y][x+1] += draw[y][x];

	for (int x = 0; x < draw[0].size(); ++x)
		for (int y = 0; y < draw.size()-1; ++y)
			draw[y+1][x] += draw[y][x];
}

int main() {
	int W, H, n;
	while (cin >> W >> H >> n, W || H || n) {
		vector<int> block(n);
		for (int i = 0; i < n; ++i) {
			int b, k; cin >> b >> k;
			block[b-1] = k;
		}

		vector< vector<int> > m(H, vector<int>(W));
		for (int y = 0; y < H; ++y)
			for (int x = 0; x < W; ++x)
				cin >> m[y][x];

		vector< vector<int> > df(n);
		for (int i = 0; i < n; ++i) {
			for (int j = 1; j*j <= block[i]; ++j) {
				if (block[i] % j == 0) {
					df[i].push_back( j );
					if (j != block[i] / j) df[i].push_back( block[i] / j );
				}
			}
		}

		vector< pair<P, P> > pos(n);
		for (int y = 0; y < H; ++y)
			for (int x = 0; x < W; ++x)
				if (m[y][x])
					pos[m[y][x]-1] = pair<P, P>( P(x, y), P(x, y) );

		for (int t = 0; t < W*H; ++t) {
			vector< vector<int> > state(H, vector<int>(W, 0));
			for (int i = 0; i < n; ++i) {
				drawInit(W, H);
				for (int j = 0; j < df[i].size(); ++j) {
					int w = df[i][j], h = block[i] / df[i][j];

					if (w <= W && h <= H) {
						P p1 = pos[i].first, p2 = pos[i].second;
						int x1 = p1.real(), y1 = p1.imag();
						int x2 = p2.real(), y2 = p2.imag();
						int posW = x2 - x1 + 1, posH = y2 - y1 + 1;

						if (w >= posW && h >= posH) {
							w -= posW, h -= posH;
							x1 = max(x1-w, 0),   y1 = max(y1-h, 0);
							x2 = min(x2+w+1, W), y2 = min(y2+h+1, H);
							drawSet( P(x1, y1), P(x2, y2) );
						}
					}
				}
				drawDo();
/*
			for (int y = 0; y < H; ++y) {
				for (int x = 0; x < W; ++x) {
					if (x) cout << " ";
					cout << draw[y][x];
				}
				cout << endl;
			}cout << endl;
*/
				int cnt = 0, fx = -1, fy = -1, lx = -1, ly = -1;
				for (int y = 0; y < H; ++y) {
					for (int x = 0; x < W; ++x) {
						if (draw[y][x] && !m[y][x]) {
							if (fx == -1) fx = x, fy = y;
							lx = x, ly = y;
							state[y][x] |= 1 << i;
						}
					}
				}
				if (cnt == block[i]) {
					for (int y = 0; y < H; ++y)
						for (int x = 0; x < W; ++x)
							if (draw[y][x]) state[y][x] = i+1;

					pos[i] = pair<P, P>( P(fx, fy), P(lx, ly) );
				}
			}

			for (int y = 0; y < H; ++y) {
				for (int x = 0; x < W; ++x) {
					if (!m[y][x] && state[y][x] && state[y][x] == (state[y][x] & -state[y][x])) {
						int b = 0;
						for (int k = 1; k != state[y][x]; k *= 2, ++b);

						P p1 = pos[b].first, p2 = pos[b].second;
						int minX = p1.real(), minY = p1.imag();
						int maxX = p2.real(), maxY = p2.imag();

						minX = min(minX, x), minY = min(minY, y);
						maxX = max(maxX, x), maxY = max(maxY, y);

						pos[b] = pair<P, P>( P(minX, minY), P(maxX, maxY) );

						for (int Y = minY; Y <= maxY; ++Y)
							for (int X = minX; X <= maxX; ++X)
								m[Y][X] = b+1;
					}
				}
			}
		}

		bool flag = true;
		for (int y = 0; y < H; ++y)
			for (int x = 0; x < W; ++x)
				if (m[y][x] == 0) flag = false;

		if (!flag) cout << "NA" << endl;
		else {
			for (int y = 0; y < H; ++y) {
				for (int x = 0; x < W; ++x) {
					if (x) cout << " ";
					cout << m[y][x];
				}
				cout << endl;
			}
		}
	}
}