#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<class T>
vector<T> divisor(T n){
    vector<T> ret;
    for(T i = 1; i * i <= n; i++) {
        if(n % i == 0) {
            ret.push_back(i);
            if(i * i != n) ret.push_back(n / i);
        }
    }
    sort(begin(ret), end(ret));
    return(ret);
}

void solve(int w, int h, int n){
    vector<int> ss(n+1);
    for (int i = 0; i < n; ++i) {
        int b, k;
        cin >> b >> k;
        ss[b] = k;
    }

    vector<vector<int>> G(h, vector<int>(w, 0));
    vector<int> ys(n+1), xs(n+1);
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            cin >> G[i][j];
            if(G[i][j]){
                ys[G[i][j]] = i;
                xs[G[i][j]] = j;
            }
        }
    }
    if(accumulate(ss.begin(),ss.end(), 0) != h*w){
        puts("NA");
        return;
    }
    vector<vector<int>> a(n+1);
    for (int i = 1; i <= n; ++i) {
        a[i] = divisor(ss[i]);
    }
    vector<int> used(n+1);
    int cnt = 0;
    stack<pair<int, int>> s;
    vector<vector<int>> ans;
    auto dfs = [&](int y, int x, auto &&f) -> bool {
        if(cnt >= 2) return 1;
        if(y == h) {
            if(cnt && G != ans) return 1;
            if(!cnt) {
                ans = G;
                cnt++;
            }
            return 0;
        }
        if(G[y][x]) {
            if(f(y+(x == w-1), (x+1)%w, f)) return 1;
        }
        for (int i = 1; i <= n; ++i) {
            if(!used[i]){
                used[i] = 1;
                for (auto &&sz : a[i]) {
                    if(y+sz > h ||  x+ss[i]/sz > w) continue;
                    int ok = 0;
                    for (int j = 0; j < sz; ++j) {
                        for (int k = 0; k < ss[i]/sz; ++k) {
                            if(G[y+j][x+k] == i){
                                ok = 1;
                            }else if(G[y+j][x+k]){
                                ok = 0;
                                j = k = 1000;
                            }
                        }
                    }
                    if(!ok) continue;
                    for (int j = 0; j < sz; ++j) {
                        for (int k = 0; k < ss[i]/sz; ++k) {
                            G[y+j][x+k] = i;
                        }
                    }
                    if(f(y+(x == w-1), (x+1)%w, f)) return 1;
                    for (int j = 0; j < sz; ++j) {
                        for (int k = 0; k < ss[i]/sz; ++k) {
                            if(ys[i] != y+j || xs[i] != x+k){
                                G[y+j][x+k] = 0;
                            }
                        }
                    }
                }
                used[i] = 0;
            }
        }
        return 0;
    };
    if(dfs(0, 0, dfs) || !cnt) puts("NA");
    else {
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                if(j) printf(" ");
                cout << ans[i][j];
            }
            puts("");
        }
    }
}

int main() {
    int w, h, n;
    while(cin >> w >> h >> n, w){
        solve(w, h, n);
    }
    return 0;
}
