#include <iostream>
using namespace std;

int X, Y, N;
int k[16];
int s[10][10];
int t[10][10];
int u;
int v[10][10];

bool input() {
  cin >> X >> Y >> N;
  if (!X && !Y && !N) {
    return false;
  }

  for (int i = 0; i < N; ++i) {
    int b;
    cin >> b;
    cin >> k[b];
  }
  for (int i = 0; i < Y; ++i) {
    for (int j = 0; j < X; ++j) {
      cin >> s[i][j];
    }
  }

  return true;
}

void paint(int id, int y1, int x1, int y2, int x2) {
  for (int i = y1; i <= y2; ++i) {
    for (int j = x1; j <= x2; ++j) {
      t[i][j] = id;
    }
  }
  u += (y2 - y1 + 1) * (x2 - x1 + 1);
}

void unpaint(int y1, int x1, int y2, int x2) {
  for (int i = y1; i <= y2; ++i) {
    for (int j = x1; j <= x2; ++j) {
      t[i][j] = 0;
    }
  }
  u -= (y2 - y1 + 1) * (x2 - x1 + 1);
}

void find_vacant(int &r, int &c) {
  for (int i = 0; i < Y; ++i) {
    for (int j = 0; j < X; ++j) {
      if (!t[i][j]) {
        r = i;
        c = j;
        return;
      }
    }
  }
}

void print_ans() {
  for (int i = 0; i < Y; ++i) {
    for (int j = 0; j < X; ++j) {
      if (!j) {
        cout << v[i][j];
      } else {
        cout << " " << v[i][j];
      }
    }
    cout << endl;
  }
}

int dfs(int p, int q) {
  if (u == X * Y) {
    for (int i = 0; i < Y; ++i) {
      for (int j = 0; j < X; ++j) {
        v[i][j] = t[i][j];
      }
    }
    return 1;
  }

  int z = 0;
  for (int i = p; i < Y; ++i) {
    for (int j = q; j < X; ++j) {
      if (!t[i][j] && !s[i][j]) {
        continue;
      }
      if (t[i][j] > 0) {
        break;
      }
      int id = s[i][j];
      for (int f = i; f < Y; ++f) {
        int h = f - p + 1;
        for (int g = j; g < X; ++g) {
          if (t[f][g] > 0 || (s[f][g] != id && s[f][g])) {
            break;
          }
          int w = g - q + 1;
          int a = h * w;
          if (k[id] > a) {
            continue;
          }
          if (k[id] < a) {
            break;
          }
          paint(id, p, q, f, g);
          int np, nq;
          find_vacant(np, nq);
          z += dfs(np, nq);
          unpaint(p, q, f, g);
        }
      }
      break;
    }
  }
  return z;
}

void solve() {
  for (int i = 0; i < Y; ++i) {
    for (int j = 0; j < X; ++j) {
      t[i][j] = 0;
    }
  }
  u = 0;
  int z = dfs(0, 0);
  if (z != 1) {
    cout << "NA" << endl;
  } else {
    print_ans();
  }
}

int main() {
  while (input()) {
    solve();
  }
  return 0;
}