#include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

typedef pair<int,int> ii;
struct Data { int h,w,index; };

int H,W,n,b[20],k[20],s[20][20],ans[20][20],buf[20][20];
vector<Data> edge[20][20];
int found;

bool isValid(int x,int y){ return 0 <= x && x < W && 0 <= y && y < H; }

inline void makeEdge(){
  rep(i,H) rep(j,W) edge[i][j].clear();
  rep(y,H) rep(x,W) {
    rep(i,n){
      for(int hy=1;hy<=k[i];hy++){ 
        if( k[i] % hy ) continue;
        int hx = k[i] / hy;
        bool failed = false, check = false;          
        for(int cy=y;cy<y+hy;cy++)for(int cx=x;cx<x+hx;cx++){
            if( !isValid(cx,cy) ) {
              failed = true;
              goto Skip;
            }
            if( !( s[cy][cx] == 0 || s[cy][cx] == b[i] ) ) {
              failed = true;
              goto Skip;
            }
            if( s[cy][cx] == b[i] ) check = true;
          }
        if( check ) edge[y][x].push_back((Data){hy,hx,b[i]});
      Skip:;
        
      }
    }
  }
  /*  
      rep(i,H) rep(j,W) {
      cout << "(" << j << "," << i << ") ::" << endl;
      rep(l,(int)edge[i][j].size()) cout << "(" << edge[i][j][l].w << "," << edge[i][j][l].h << "," << edge[i][j][l].index << ") ";
      cout << endl;
      }
  */
}

bool BackTracking(int cx,int cy){

  if( cx == W-1 && cy == H-1 && ans[cy][cx] != 0 ) {
    ++found;
    if( found == 1 ) rep(i,H) rep(j,W) buf[i][j] = ans[i][j];
    return found >= 2;
  }

  rep(i,(int)edge[cy][cx].size()){
    int h = edge[cy][cx][i].h, w = edge[cy][cx][i].w, index = edge[cy][cx][i].index;
    ii next;
    rep(y,h) rep(x,w) {
      //assert( isValid(x+cx,y+cy) );
      if( ans[y+cy][x+cx] != 0 ) {
        goto Skip;
      }
      ans[cy+y][cx+x] = index;
    }
    next = ii(-1,-1);
    rep(y,H) rep(x,W){
      if( ans[y][x] == 0 ) {
        next = ii(x,y);
        goto Skip;
      }
      if( x == W-1 && y == H-1 ) next = ii(W-1,H-1);
    }
  Skip:;
    if( next != ii(-1,-1) && BackTracking(next.first,next.second) ) return true;
    rep(y,h) rep(x,w) ans[cy+y][cx+x] = 0;
  }
  return false;
}

inline void compute(){
  makeEdge();
  rep(i,H) rep(j,W) ans[i][j] = 0;
  found = 0;
  if( !BackTracking(0,0) && found == 1 ) {
    rep(i,H) { rep(j,W) cout << buf[i][j]; cout << endl; } 
  } else puts("NA");
}

int main(){
  while( cin >> W >> H >> n, W|H|n ){
    int sum = 0;
    rep(i,n) {
      cin >> b[i] >> k[i];
      sum += k[i];
    }
    set<char> S;
    rep(i,H) rep(j,W){
      cin >> s[i][j];
      if( S.count(s[i][j]) && s[i][j] != 0 ) while(1);
      S.insert(s[i][j]);
    }
    if( sum != H*W ) {
      puts("NA");
      continue;
    }
    compute();
  }
  return 0;
}