#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a) = min((a), (b)))
#define chmax(a, b) ((a) = max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
const double eps = 1e-10;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

int x, y, n;
int ans;
vector<vector<int>> fians(20, vector<int>(20));
vector<vector<P>> divi(20);
vector<P> larger;

void dfs(int depth, vector<vector<int>> &s){
    if(depth == n){
        ans++;
        fians = s;
        return ;
    }

    if(1 < ans){
        return ;
    }

    int K = larger[depth].first;
    int B = larger[depth].second;

    for(auto i:divi[B]){
        int h = i.first;
        int w = i.second;

        for(int j=0; j+h-1<y; j++){
            for(int k=0; k+w-1<x; k++){
                int l = k;
                int r = k + w - 1;
                int u = j;
                int d = j + h - 1;

                bool exist = false;
                bool valid = true;
                auto cs = s;
                for(int i1=u; i1<=d; i1++){
                    for(int i2=l; i2<=r; i2++){
                        if(cs[i1][i2] == B){
                            exist = true;
                        }
                        else if(cs[i1][i2] != 0){
                            valid = false;
                            goto END;
                        }
                    }
                }

                END:;

                if(exist && valid){
                    for(int i1=u; i1<=d; i1++){
                        for(int i2=l; i2<=r; i2++){
                            cs[i1][i2] = B;
                        }
                    }

                    dfs(depth + 1, cs);
                }
            }
        }
    }

    return ;
}

int main(){
    while(1){
        ans = 0;
        larger.clear();
        for(int i=0; i<20; i++){
            divi[i].clear();
        }

        cin>>x>>y>>n;
        if(x == 0) return 0;
        
        int sum = 0;
        for(int i=0; i<n; i++){
            int b, k; cin>>b>>k;
            larger.emplace_back(k, b);
            sum += k;
            set<P> st;
            for(int j=1; j*j<=k; j++){
                if(k % j == 0){
                    st.emplace(j, k / j);
                    st.emplace(k / j, j);
                }
            }

            for(auto j:st){
                divi[b].emplace_back(j.first, j.second);
            }
        }

        sort(larger.begin(), larger.end(), greater<P>());

        vector<vector<int>> s(y, vector<int>(x));
        for(int i=0; i<y; i++){
            for(int j=0; j<x; j++){
                cin>>s[i][j];
            }
        }

        if(x * y != sum){
            cout << "NA" << endl;
            continue;
        }

        dfs(0, s);

        if(ans == 1){
            for(int i=0; i<y; i++){
                for(int j=0; j<x; j++){
                    cout << fians[i][j] << " \n"[j == x-1];
                }
            }
        }
        else{
            cout << "NA" << endl;
        }
    }
}


Measured memory_bytes: 3132