#include <iostream> 
#include <sstream> 
#include <iomanip> 
#include <algorithm> 
#include <cmath> 
#include <string> 
#include <vector> 
#include <list> 
#include <queue> 
#include <stack> 
#include <set> 
#include <map> 
#include <bitset> 
#include <numeric> 
#include <climits> 
#include <cfloat> 
using namespace std;

int w, h;
vector<int> area;
vector<vector<int> > sum;

int retNum;
vector<string> ret;

void solve(int y, int x, vector<string>& used)
{
    if(x > w){
        ++ y;
        x = 1;
    }
    if(y > h){
        ++ retNum;
        ret = used;
        return;
    }

    if(used[y][x] != '0'){
        solve(y, x+1, used);
        return;
    }

    for(int y1=y; y1<=h; ++y1){
        for(int x1=x; x1<=w; ++x1){
            if(used[y1][x1] != '0'){
                w = x1 - 1;
                break;
            }

            bitset<16> bs = sum[y1][x1] & (~sum[y1][x-1]) & (~sum[y-1][x1]);
            if(bs.count() != 1)
                continue;

            int k = 0;
            while(!bs[k])
                ++ k;
            if(area[k] != (y1 - y + 1) * (x1 - x + 1))
                continue;

            bool ng = false;
            vector<string> used1 = used;
            for(int i=y; i<=y1; ++i){
                for(int j=x; j<=x1; ++j){
                    if(used1[i][j] != '0')
                        ng = true;
                    used1[i][j] = k + '0';
                }
            }
            if(!ng)
                solve(y, x+1, used1);
        }
    }
}

int main()
{
    for(;;){
        int n;
        cin >> w >> h >> n;
        if(w == 0)
            return 0;

        area.assign(n+1, 0);
        for(int i=0; i<n; ++i){
            int b, k;
            cin >> b >> k;
            area[b] = k;
        }

        sum.assign(h+1, vector<int>(w+1, 0));
        for(int y=1; y<=h; ++y){
            int tmp = 0;
            for(int x=1; x<=w; ++x){
                int a;
                cin >> a;
                if(a > 0)
                    tmp |= (1 << a);
                sum[y][x] = sum[y-1][x] | tmp;
            }
        }

        retNum = 0;
        vector<string> s(h+1, string(w+1, '0'));
        solve(1, 1, s);

        if(retNum != 1){
            cout << "NA" << endl;
        }else{
            for(int i=1; i<=h; ++i){
                cout << ret[i][1];
                for(int j=2; j<=w; ++j)
                    cout << ' ' << ret[i][j];
                cout << endl;
            }
        }
    }
}