#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }


int n, w, h;
int have[16];
pint pos[16];
int field[16][16], answer[16][16];


bool zero(int id, int x1, int y1, int x2, int y2)
{
  REP(x, x1, x2+1) REP(y, y1, y2+1) if(field[x][y] != 0 && field[x][y] != id) return false;

  return true;
}

int rec(int id)
{
  if(n < id){
    rep(x, w) rep(y, h) answer[x][y] = field[x][y];
    return 1;
  }

  int res = 0;
  
  for(int x1 = pos[id].first; 0 <= x1; x1--) {
    for(int y1 = pos[id].second; 0 <= y1; y1--) {
      REP(x2, pos[id].first, w) REP(y2, pos[id].second, h) if((x2 - x1 + 1) * (y2 - y1 + 1) == have[id]) {
	if(!zero(id, x1, y1, x2, y2)) break;
	
	for(int x = x1; x <= x2; x++) for(int y = y1; y <= y2; y++) field[x][y] = id;

	res += rec(id + 1);

	for(int x = x1; x <= x2; x++) for(int y = y1; y <= y2; y++) if(mp(x, y) != pos[id]) field[x][y] = 0;
      }
    }
  }

  return res;
}

int main()
{
  while(cin >> w >> h >> n && w){

    rep(i, n){
      int b, k;
      cin >> b >> k;
      have[b] = k;
    }

    rep(y, h) rep(x, w){
      cin >> field[x][y];
      pos[field[x][y]] = mp(x, y);
    }

    if(rec(1) == 1) rep(y, h) rep(x, w) cout << answer[x][y] << (x + 1 == w ? "\n" : " ");
    else cout << "NA" << endl;
  }
}