#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>

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

using namespace std;

int h,w,n;
map<int,int> ma;
map<int,pair<int,int> > p;
vector<vector<int> > v;

bool can(int i,int j,int ny,int nx)
{
	if(0 <= i && ny <= h && 0 <= j && nx <= w)
	{
		return true;
	}
	else return false;
}

bool canSet(int num,int y,int x,int y2,int x2)
{
	bool flag = true;

	REP(i,y,y2)
	{
		REP(j,x,x2)
		{
			if(v[i][j] != 0)
			{ 
				flag = false;
			}
		}
	}

	return flag;
}

void set(int num,int y,int x,int y2,int x2)
{
	REP(i,y,y2)
	{
		REP(j,x,x2)
		{
			v[i][j] = num;
		}
	}
}

int cnt = 0;
vector<vector<int> > v2;

void dfs(int num)
{
	if(num == 0)
	{
		if(cnt == 0)
		{
			bool flag = true;
			rep(i,h)
			{
				rep(j,w)
				{
					if(v[i][j] == 0) flag = false;
				}
			}

			if(flag)
			{
				v2.resize(h);
				rep(i,h) v2[i].resize(w);
				rep(i,h) rep(j,w) v2[i][j] = v[i][j];
			
				cnt++;
			}
		}
		else
		{
			bool flag = true;

			rep(i,h)
			{
				rep(j,w)
				{
					if(v[i][j] == 0) flag = false;
				}
			}

			if(flag)
			{
				cnt++;
			}
		}
	}
	else
	{
		REP(y,1,ma[num]+1)
		{
			if(ma[num]%y == 0)
			{
				int x = ma[num]/y;
				REP(i,p[num].first-y+1,p[num].first+1)
				{
					REP(j,p[num].second-x+1,p[num].second+1)
					{
						int ny = i+y;
						int nx = j+x;

						if(can(i,j,ny,nx))
						{
							v[p[num].first][p[num].second] = 0;
							if(canSet(num,i,j,ny,nx))
							{
								set(num,i,j,ny,nx);
								dfs(num-1);
								set(0,i,j,ny,nx);
							}
							v[p[num].first][p[num].second] = num;
						}
					}
				}
			}
		}
	}
}

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

		v.resize(h);
		rep(i,h) v[i].clear();

		ma.clear();
		p.clear();
		cnt = 0;

		rep(i,n)
		{
			int a,b;
			cin >> a >> b;
			ma[a] = b;
		}

		v.resize(n);
		rep(i,h)
		{
			rep(j,w)
			{
				int temp;
				cin >> temp;
				v[i].push_back(temp);

				if(1 <= temp && temp <= 15)
				{
					p[temp] = pair<int,int>(i,j);
				}
			}
		}

		dfs(n);

		if(cnt == 1)
		{
			rep(i,h)
			{
				rep(j,w)
				{
					cout << v2[i][j];
					if(j != w-1) cout << " ";
				}
				cout << endl;
			}
		}
		else
		{
			cout << "NA" << endl;
		}

	}
	return 0;
}