#define _USE_MATH_DEFINES
#include <iostream>
#include <complex>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <cmath>
#include <math.h>
#include <numeric>
#include <list>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <climits>
#include <set>
#include <memory.h>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <map>
#include <cassert>
#include <time.h>
#include <ctime>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int , P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 29;
const double EPS = 1e-10;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)
#define repD(i, n) for(int i = n; i >= 0; i--)

int cnt;
int field[100][100];
int check[100][100];
int res[100][100];
bool used[20];
int K[20];
int X, Y, n;

void solve(int y, int x){
	if(y == Y){
		cnt++;
		rep(i, Y)rep(j, X) res[i][j] = check[i][j];
	}else{
		if(check[y][x] >= 0){
			if(x == X-1) solve(y+1, 0);
			else solve(y, x+1);
		}else{
			int dy = Y - y;
			int dx = X- x;
			bool ok;
			rep2(i, 1, dy + 1){
				rep2(j, 1, dx + 1){
					ok = true;
					int id = -1;
					rep(k, i){
						rep(l, j){
							if(check[y+k][x+l] >= 0) ok = false;
							if(field[y+k][x+l] > 0){
								if(!used[field[y+k][x+l]] && (id == -1 || field[y+k][x+l] == id)){
									id = field[y+k][x+l];
								}else ok = false;
							}
						}
					}
					if(!ok || id == -1 || K[id] != i * j) continue;
					rep(k,i)rep(l, j) check[y+k][x+l] = id;
					used[id] = true;
					solve(y,x);
					used[id] = false;
					rep(k,i)rep(l, j) check[y+k][x+l] = -1;
				}
			}
		}
	}
}



int main(){
	while(cin >> X >> Y >> n && (X || Y || n)){
		int total = 0;
	
		rep(i, n){
			int b;
			cin >> b;
			cin >> K[b];
			total += K[b];
		}
		if(total != X * Y){
			cout << "NA" << endl;
			continue;
		}
		rep(i, Y)rep(j, X){
			cin >> field[i][j];
		}
		memset(check, -1, sizeof(check));
		memset(used, false, sizeof(used));

		cnt = 0;
		solve(0, 0);
		if(cnt != 1) cout << "NA" << endl;
		else{
			rep(i, Y){
				rep(j,X){
					if(j != 0) cout << " ";
					cout << res[i][j];
				}
				cout << endl;
			}
		}
	}
	return 0;
}