#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair <INT, INT> II;
typedef vector <II> VII;

template<class T> ostream& operator << ( ostream& os, vector<T> v ) {
    for ( typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i ) {
        os << *it_i << ", ";
    }
    return os;
}


const int SIZE = 11;
const int MAX_NUM = 16;
int w, h, n;
int T[SIZE][SIZE];
int TC[SIZE][SIZE];
int K[MAX_NUM]; 
II U[MAX_NUM]; 
int cnt;

void init() {
    cnt = 0;
    for ( int i = 0; i < h; ++ i ) {
        for ( int j = 0; j < w; ++ j ) {
            T[i][j] = 0;
        }
    }
}

void output() {
    for ( int i = 0; i < h; ++ i ) {
        for ( int j = 0; j < w; ++ j ) {
            cout << TC[i][j];
            if ( j + 1 < w ) cout << " ";
        }
        cout << endl;
    }
}

bool put( int k, int r0, int c0, int w0, int h0 ) {
    for ( int i = 0; i < h0; ++ i ) {
        for ( int j = 0; j < w0; ++ j ) {
            if ( r0+i >= h || c0+j >= w ) return false;
            if ( T[r0+i][c0+j] != 0 ) return false;
            T[r0+i][c0+j] = k + 1;
        }
    }
    return true;
}

void solve( int k ) {
    if ( k >= n ) {
        cnt ++;
        for ( int i = 0; i < h; ++ i ) {
            for ( int j = 0; j < w; ++ j ) {
                TC[i][j] = T[i][j];
            }
        }
        return;
    }
    
    int r = U[k].first;
    int c = U[k].second; 
    int Tcopy[SIZE][SIZE];
    for ( int i = 0; i < h; ++ i ) {
        for ( int j = 0; j < w; ++ j ) {
            Tcopy[i][j] = T[i][j];
        }
    }
    for ( int r0 = 0; r0 < h; ++ r0 ) {
        for ( int c0 = 0; c0 < w; ++ c0 ) {
            for ( int w0 = 1; w0 <= w; ++ w0 ) {
                for ( int h0 = 1; h0 <= h; ++ h0 ) {
                    if ( w0 * h0 != K[k] ) continue;
                    if ( r0 <= r && r < r0 + h0 && c0 <= c && c <= c0 + w0 ) {
                        for ( int i = 0; i < h; ++ i ) {
                            for ( int j = 0; j < w; ++ j ) {
                                T[i][j] = Tcopy[i][j];
                            }
                        }
                        if ( put( k, r0, c0, w0, h0 ) ) solve( k + 1 );
                        if ( cnt >= 2 ) return;
                    }
                }
            }
        }
    }
}

void input() {
    for ( int i = 0; i < n; ++ i ) {
        int b;
        cin >> b;
        b --;
        cin >> K[b];
    }
    for ( int i = 0; i < h; ++ i ) {
        for ( int j = 0; j < w; ++ j ) {
            int s;
            cin >> s;
            if ( s > 0 ) {
                -- s;
                U[s] = II( i, j );
            }
        }
    }
}

int main() {
    while ( cin >> w >> h >> n ) {
        if ( w == 0 && h == 0 && n == 0 ) break;
        init();
        input();
        solve( 0 );

        if ( cnt == 0 || cnt > 1 ) {
            cout << "NA" << endl;
        } else {
            output();
        }
    }
    return 0;
}