#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
#define int long long
using namespace std;

template <typename X, typename T>
auto vectors(X x, T a) {
	return vector<T>(x, a);
}

template <typename X, typename Y, typename Z, typename... Zs>
auto vectors(X x, Y y, Z z, Zs... zs) {
	auto cont = vectors(y, z, zs...);
	return vector<decltype(cont)>(x, cont);
}

template<typename T>
ostream& operator << (ostream& os, vector<T>& v){
	rep(i,v.size()){ os << v[i] << (i == v.size() - 1 ? "" : " "); } return os;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& v){
	for(T& x: v){ is >> x; } return is;
}

int H, W, n;
pair<int,int> bk[15];
map<int,pair<int, int>> p;
vector<vector<int>> ans;

/*void dfs(vector<vector<int>> b, int h, int cnt){
	if(cnt == n){
		if(ans[0][0] == -1){
			ans = b;
		}else{
			ans[0][0] = -2;
		}
		return;
	}

	if(h == H) h = 0;
	int w;
	rep(i,W){
		if(b[h][i] == 0){
			w = i;
			break;
		}
	}
	cout << h << ' ' << w << endl;

	rep(i,H - h){
		rep(j,W - w){
			int id = b[i + h][j + w];
			show(id)
			if(id != 0 and (i + 1) * (j + 1) == bk[id]){
				show("JI")
				rep(k,h) rep(l,w) b[k][l] = id;

				dfs(b, h + i + 1, cnt + 1);

				rep(k,h) rep(l,w) b[k][l] = 0;
				b[i + h][j + w] = id;
			}
		}
	}
}*/

bool paint(vector<vector<int>>& s, int y, int x, int h, int w, int b){
	//cout << "left up " << y << ' ' << x << endl;
	//rep(i,H) { rep(j,W){ cout << s[i][j]; } cout << endl; }

	rep(i,h) rep(j,w){
		if(s[i + y][j + x] != 0 and s[i + y][j + x] != b) return false;
	}
	rep(i,h) rep(j,w){
		s[i + y][j + x] = b;
	}
	//rep(i,H) { rep(j,W){ cout << s[i][j]; } cout << endl; }
	return true;
}

void reset(vector<vector<int>>& s, int y, int x, int h, int w){
	rep(i,h) rep(j,w){
		s[i + y][j + x] = 0;
	}
}

bool check(vector<vector<int>>& s){
	rep(i,H) rep(j,W) if(s[i][j] == 0) return false;
	return true;
}

void dfs(vector<vector<int>> s, int idx){
	if(idx == n){

		if(check(s)){
			//show("OK")
	//rep(i,H) { rep(j,W){ cout << s[i][j]; } cout << endl; }
			if(ans[0][0] == -1) ans = s;
			else ans[0][0] = -2;
		}
		return;
	}

	int k, b;
	tie(k,b) = bk[idx];
	//cout <<"kb " <<  k << ' ' << b << endl;

	range(h,1,H + 1){
		if(k % h != 0) continue;
		int w = k / h;

		int y, x;
		tie(y,x) = p[b];

		//cout << "h  w  " << h << ' ' << w << endl;
		//cout << "id p  " << y << ' ' << x << endl;
		//cout << y - h + 1 << ' ' << y << endl;
		for(int i = max<int>(0, y - h + 1); i <= y and i + h - 1 < H; i++){
			for(int j = max<int>(0, x - w + 1); j <= x and j + w - 1 < W; j++){
				//cout << "i j " << i << ' ' << j << endl;
				if(not paint(s, i, j, h, w, b)) continue;

				dfs(s, idx + 1);

				reset(s, i, j, h, w);
				s[y][x] = b;
			}
		}
	}
}

signed main(){
	while(cin >> W >> H >> n, W){
		rep(i,n){
			int b, k;
			cin >> b >> k;
			bk[i] = make_pair(k,b);
		}
		sort(bk, bk + n, greater<pair<int, int>>());

		vector<vector<int>> s(H, vector<int>(W));
		ans = s;
		rep(i,H) rep(j,W) {
			cin >> s[i][j];
			if(s[i][j] != 0){
				p[s[i][j]] = make_pair(i,j);
			}
		}

		ans[0][0] = -1;
		dfs(s, 0);

		if(ans[0][0] > 0){
			rep(i,H){
				rep(j,W){
					if(j) cout << ' ';
					cout << ans[i][j];
				}
				cout << endl;
			}
		}else{
			cout << "NA" << endl;
		}
	}

}

