#include <iostream>
#include <vector>

using namespace std;

typedef vector<int> Vec;
typedef vector<Vec> Area;

class Solver {
  public:
    Area solve() {
      Area area(x, Vec(y, 0));
      vector<Area> answers;
      for (int i = 0; i < y; ++i) {
        for (int j = 0; j < x; ++j) {
          area[j][i] = s[j][i];
        }
      }
      search(area, answers);
      if (answers.size() != 1) {
        answers.resize(1);
        answers[0].clear();
      }
      return answers[0];
    }
    int x, y, n;
    int b[15], k[15];
    int s[10][10];
  private:
    void print_area(Area &area) {
      for (int i = 0; i < y; ++i) {
        for (int j = 0; j < x; ++j) {
          cerr << area[j][i] << " ";
        }
        cerr << endl;
      }
      cerr << "--------------------------------" << endl;
    }
    void search(Area &area, vector<Area> &answers, int id = 0) {
      //print_area(area);
      if (id == n) {
        answers.push_back(area);
        return;
      }
      for (int yi = 0; yi < y; ++yi) {
        for (int dy = 1; yi + dy <= y; ++dy) {
          int dx = k[id] / dy;
          if (k[id] % dy != 0) {
            continue;
          }
          for (int xi = 0; xi + dx <= x; ++xi) {
            bool is_valid = true;
            for (int i = yi; i < yi + dy and is_valid; ++i) {
              for (int j = xi; j < xi + dx; ++j) {
                if (area[j][i] != 0 and area[j][i] != b[id]) {
                  is_valid = false;
                  break;
                }
              }
            }
            if (not is_valid) {
              continue;
            }
            for (int i = yi; i < yi + dy; ++i) {
              for (int j = xi; j < xi + dx; ++j) {
                area[j][i] = b[id];
              }
            }
            search(area, answers, id + 1);
            if (answers.size() > 1) {
              return;
            }
            for (int i = yi; i < yi + dy; ++i) {
              for (int j = xi; j < xi + dx; ++j) {
                area[j][i] = s[j][i];
              }
            }
          }
        }
      }
    }
};

int main() {
  Solver s;
  Area answer;
  while (true) {
    cin >> s.x >> s.y >> s.n;
    for (int i = 0; i < s.n; ++i) {
      cin >> s.b[i] >> s.k[i];
    }
    for (int i = 0; i < s.y; ++i) {
      for (int j = 0; j < s.x; ++j) {
        cin >> s.s[j][i];
      }
    }
    answer = s.solve();
    if (answer.size() == 0) {
      cout << "NA" << endl;
    } else {
      for (int i = 0; i < s.y; ++i) {
        for (int j = 0; j < s.x; ++j) {
          cout << answer[j][i];
          if (j < s.x - 1) {
            cout << " ";
          } else {
            cout << endl;
          }
        }
      }
    }
  }
}