#include<iostream>
#include<vector>
#include<bitset>
using namespace std;

int X, Y, n;
vector<vector<int>> s;
vector<vector<int>> answer;
vector<int> area;

// id, 左上, 右下
bool placeable(vector<vector<int>> field, int id, int y1, int x1, int y2, int x2) {
    bool ok = false;
    for(int i = y1; i <= y2; ++i) for(int j = x1; j <= x2; ++j) {
        if(field[i][j] && field[i][j] != id) return false;
        if(field[i][j] == id) ok = true;
    }
    return ok;
}
vector<vector<int>> place(vector<vector<int>> field, int id, int y1, int x1, int y2, int x2) {
    for(int i = y1; i <= y2; ++i) for(int j = x1; j <= x2; ++j) field[i][j] = id;
    return field;
}

bool in(int y, int x) {
    return 0 <= y && y < Y && 0 <= x && x < X;
}

int dfs(vector<vector<int>> field, bitset<11> used) {
    if(used.count() == n) {
        answer = field;
        return 1;
    }

    int y, x;
    y = x = -1;
    for(int i = 0; y == -1 && i < Y; ++i) for(int j = 0; x == -1 && j < X; ++j) if(!used[field[i][j]]) {
        y = i;
        x = j;
    }

    int result = 0;
    for(int id = 1; id <= n; ++id) {
        if(used[id]) continue;
        if(field[y][x] && field[y][x] != id) continue;
        int a = area[id];
        for(int t = 0; t < a; ++t) {
            int i = y + t;
            int j = x + a / (t + 1) - 1;
            if(!in(i, j)) continue;
            if(!placeable(field, id, y, x, i, j)) continue;
            result += dfs(place(field, id, y, x, i, j), used.set(id));
            used.reset(id);
        }
    }
    return result;
}

int main() {
    while(cin >> X >> Y >> n, X | Y | n) {
        area.clear();
        area.resize(n + 1);
        for(int i = 0; i < n; ++i) {
            int b, k;
            cin >> b >> k;
            area[b] = k;
        }
        s.clear();
        s.resize(Y, vector<int>(X));
        for(auto& y: s) for(auto& x: y) cin >> x;

        if(dfs(s, 0) == 1) {
            for(int i = 0; i < Y; ++i) {
                for(int j = 0; j < X; ++j) cout << (j ? " " : "") << answer[i][j];
                cout << endl;
            }
        } else {
            cout << "NA" << endl;
        }
    }
}