#pragma comment (linker, "/STACK:256000000")

#define _USE_MATH_DEFINES
#define _CRT_NO_DEPRECEATE
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <utility>
#include <algorithm>
#include <functional>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <memory.h>
#include <sstream>
#include <cassert>
#include <ctime>
#include <complex>
#include <random>
#include <bitset>

using namespace std;

typedef unsigned int uint32;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double ldouble;
typedef pair<int, int> pii;
typedef pair<int64, int64> pll;
typedef pair<pii, pii> piiii;

#define pb push_back
#define sq(x) ((x)*(x))
#define tmin(x,y,z) (min(min((x),(y)),(z)))
#define rand32() (((unsigned int)(rand()) << 16) | (unsigned int)(rand()))
#define rand64() (((unsigned int64)(rand32()) << 16) | (unsigned int64)(rand32()))
#define bit(mask, b) ((mask >> b) & 1)
#define biton(mask, bit) (mask | (((unsigned int)(1)) << bit))
#define bitoff(mask, bit) (mask & (~(((unsigned int)(1)) << bit)))
#define bitputon(mask, bit) (mask |= (((unsigned int)(1)) << bit))
#define bitputoff(mask, bit) (mask &= (~(((unsigned int)(1)) << bit)))
#define FAIL() (*((int*)(0)))++
#define INF ((int)(1e9) + 1337)
#define LINF ((int64)(1e18))
#define EPS (1e-9)
#define PI (3.1415926535897932384626433832795)
#define y1 yy1
#define y0 yy0
#define j0 jj0
#define MOD 1000000007LL
#define HMOD 1234567891LL

//#define HMOD ((int64)(1e18) + 3LL)
//#ifdef _MY_DEBUG
//#define HMOD 1000000007
//#endif
#define MAX 2000000000
#define HBASE 127
const int MAXN = 5000;
int n, m, k;
int a[MAXN][MAXN];
//
void solve()
{
	scanf("%d %d %d", &n, &m, &k);
	for(int i = 0; i < k; ++i)
	{
		int t, x, b;
		scanf("%d %d %d", &t, &x, &b);
		x--;
		if (t == 1)
		{
			for(int j = 0; j < m; ++j)
				a[x][j] = b;
		}
		else
		{
			for(int j = 0; j < n; ++j)
				a[j][x] = b;
		}
	}
	for(int i = 0; i < n; ++i)
	{
		for(int j = 0; j < m; ++j)
		{
			printf("%d ", a[i][j]);
		}
		printf("\n");
	}
}

int main()
{
#ifdef _MY_DEBUG
    freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout);
#else
    //freopen(TASK ".in", "rt", stdin); freopen(TASK ".out", "wt", stdout);
#endif
    //stresstest(); return 0;
    srand(1313);
	solve();

    return 0;
}


#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <unordered_set>
typedef long long ll;
void main()
{
	std::ios::sync_with_stdio(false);
	std::unordered_map<std::string, std::string> dict;
	int n(0),m,k;std::cin>>n>>m>>k;
	int max = 1;
	int sheet[5001][5001];
	bool flag = true;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
			sheet[i][j] = 0;
	}
	for (int i = 0; i < k; i++)
	{
		int a;
		int op, color;
		std::cin >> a;
		if (a == 2)
		{
			std::cin >> op >> color;
			for (int j = 1; j <= n; j++)
				sheet[j][op] = color;
		}
		else
		{
			std::cin >> op >> color;
			for (int j = 1; j <= m; j++)
				sheet[op][j] = color;
		}
	}
	for (int i = 1; i <= n; i++)
	{
		for(int j = 1; j<=m;j++)
			std::cout << sheet[i][j] << " ";
		std::cout << "\n";
	}
}


#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <unordered_set>
typedef long long ll;
void main()
{
	std::ios::sync_with_stdio(false);
	std::unordered_map<std::string, std::string> dict;
	int n(0), m, k; std::cin >> n >> m >> k;
	int co[100001] = {0};
	int max = 1;
	int l[5001] = {0}; int c[5001] = {0};
	bool flag = true;
	for (int i = 1; i <= k; i++)
	{
		int a;
		int op, color;
		std::cin >> a;
		if (a == 2)
		{
			std::cin >> op >> color;
			co[i] = color;
			c[op] = i;
		}
		else
		{
			std::cin >> op >> color;
			co[i] = color;
			l[op] = i;
		}
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
			std::cout << co[std::max(l[i], c[j])] << " ";
		std::cout << "\n";
	}
}


# include <iostream>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cstring>
# include <cmath>
# include <stack>
# include <queue>
# include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <iterator>
# include <functional>
# include <bitset>
# include <climits>
# include <ctime>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define odd(x) (x&1)
typedef long long ll;
typedef long double ld;
const ll oo = 2 * 1000 * 1000 * 1000;
const int _cnt = 1000 * 1000;
const int _p = 1000 * 1000 * 1000 + 7;
ll o(ll x) { return x%_p; }
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) { return a*b / gcd(a, b); }
using namespace std;

void file_put() {
	freopen("filename.in", "r", stdin);
	freopen("filename.out", "w", stdout);
}

struct color {
	int no, c;
};

int x, n, m, k, y, t;
color r[10005], v[10005];

int main() {
	//file_put();

	scanf("%d%d%d", &n, &m, &k);
	for (int i = 1; i <= k; i++) {
		scanf("%d%d%d", &t, &x, &y);
		if (t == 1) {
			r[x].c = y;
			r[x].no = i;
		}
		if (t == 2) {
			v[x].c = y;
			v[x].no = i;
		}
	}
	for (int i = 1; i <= n; i++){
		for (int j=1; j<=m-1; j++) printf("%d ",(r[i].no>v[j].no) ? r[i].c : v[j].c);
		printf("%d\n", (r[i].no>v[m].no) ? r[i].c : v[m].c);
	}

	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<math.h>

int row[5001][2], column[5001][2];

int main() {
    int n, m, k, a, b,c;
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 1; i <= k; i++) {
        scanf("%d%d%d", &a, &b, &c);
        if (a==1) {
            row[b][0] = c;
            row[b][1] = i;
        }
        else {
            column[b][0] = c;
            column[b][1] = i;
        }
    }
    for (int i = 1; i <= n ; i++) {
        for (int j = 1; j <= m; j++) {
            if(row[i][1] > column[j][1]) {
                printf("%d ",row[i][0]);
            }else {
                printf("%d ",column[j][0]);
            }
        }
        printf("\n");
        }
    return 0;
    }


#include<stdio.h>
#include<algorithm>
#include<math.h>

int map[5001][5001];

int main() {
    int n, m, k, a, b,c;
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 1; i <= k; i++) {
        scanf("%d%d%d", &a, &b, &c);
        if (a==1) {
            for (int j = 1; j <= m; j++) {
                map[b][j] = c;
            }
        }
        else {
            for (int j = 1; j <= n; j++) {
                map[j][b] = c;
            }
        }
    }
    for (int i = 1; i <= n ; i++) {
        for (int j = 1; j <= m; j++) {
            printf("%d ", map[i][j]);
        }
                    printf("\n");
        }
    return 0;
    }


#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int main() {

	int n, m, k;
	int** a;
	cin >> n >> m >> k;

	a = new int*[n];
	for (int i = 0; i < n; i++) {
		a[i] = new int[m];
		for (int j = 0; j < m; j++)
			a[i][j] = 0;
	}

	for (int que = 0; que < k; que++) {
		int type, num, col;
		cin >> type >> num >> col;
		if (type == 1) {
			for (int i = 0; i < m; i++)
				a[num - 1][i] = col;
		}
		else {
			for (int i = 0; i < n; i++)
				a[i][num - 1] = col;
		}
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++)
			cout << a[i][j] << " ";
		cout << endl;
	}
	cin >> n;

	return 0;
}


#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int main() {

	int n, m, k;
	int** a;
	cin >> n >> m >> k;

	a = new int*[n];
	for (int i = 0; i < n; i++) {
		a[i] = new int[m];
		for (int j = 0; j < m; j++)
			a[i][j] = 0;
	}

	int** que = new int*[3];
	que[0] = new int[k];
	que[1] = new int[k];
	que[2] = new int[k];

	bool* col = new bool[n];
	bool* row = new bool[m];
	for (int i = 0; i < n; i++)
		col[i] = true;
	for (int i = 0; i < m; i++)
		row[i] = true;

	for (int i = 0; i < k; i++)
		cin >> que[0][i] >> que[1][i] >> que[2][i];

	int colC = n;
	int rowC = m;
	for (int i = k - 1; i >= 0 && colC != 0 && rowC != 0; i--) {
		if (que[0][i] == 1) {
			if (col[que[1][i] - 1]) {
				col[que[1][i] - 1] = false;
				colC--;
				for (int j = 0; j < m; j++)
					if (row[j])
						a[que[1][i] - 1][j] = que[2][i];
			}
		}
		else {
			if (row[que[1][i] - 1]) {
				row[que[1][i] - 1] = false;
				rowC--;
				for (int j = 0; j < n; j++)
					if (col[j])
						a[j][que[1][i] - 1] = que[2][i];
			}
		}
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++)
			cout << a[i][j] << " ";
		cout << endl;
	}
	cin >> n;

	return 0;
}


#include <cstdio>
#include <algorithm>
#define MAX 5001
#define MAXK 100001
using namespace std;

int i, j, n, m, k;
int type, p;
int l[MAX], c[MAX], x[MAXK];

int main(void)
{
  scanf("%d %d %d",&n,&m,&k);
  for (i = 1; i <= k; i++)
  {
    scanf("%d %d %d",&type,&p,&x[i]);
    if (type == 1) l[p] = i; else c[p] = i;
  }

  for (i = 1; i <= n; i++)
  {
    for (j = 1; j <= m; j++)
      printf("%d ", x[max(l[i],c[j])]);
     printf("\n");
  }
  return 0;
}


#include<iostream>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
int a[5555][5555],n,m,k;
int main(){
    cin>>n>>m>>k;
    for(int i=0; i<k; ++i){
            int h,t,b;
            cin>>h>>t>>b;
            if(h==1){for(int j=1; j<=m; ++j){a[t][j]=b;}}
            else if(h==2){for(int j=1; j<=n; ++j){a[j][t]=b;}}
            }
    for(int i=1; i<=n; ++i){
            for(int j=1; j<=m; ++j){
                    cout<<a[i][j]<<" ";
                    }
                    cout<<endl;
            }
    return 0;
    }


#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int n,m,k;
int t,p;
int l[5555], c[5555], x[111111];
int main() {
	cin>>n>>m>>k;
    x[0]=0;
    for (int i=1; i<=k; ++i) {
        cin>>t>>p>>x[i];
        if (t==1) l[p]=i;
        else c[p]=i;
    }
    for (int i=1; i<=n; ++i) {
        for (int j=1; j<=m; ++j){
            cout<<x[max(l[i],c[j])]<<" ";
        }
        cout<<endl;
    }

    return 0;
}


#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int n,m,k;
int t,p;
int l[5555], c[5555], x[111111];
int main() {
	cin>>n>>m>>k;
    x[0]=0;
    for (int i=1; i<=k; ++i) {
        cin>>t>>p>>x[i];
        if (t==1) l[p]=i;
        else c[p]=i;
    }
    for (int i=1; i<=n; ++i) {
        for (int j=1; j<=m; ++j){
            cout<<x[max(l[i],c[i])];
        }
        cout<<endl;
    }

    return 0;
}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#include<set>
using namespace std;
string s1, s2;
void main() {
	int x,j,y,n, m, k, a[10001] = { 0 }, b[10001] = { 0 }, a1[10001] = { 0 }, b1[10001] = { 0 };
	cin >> n >> m >> k;
	for(int i = 1; i <= k; i++) {
		cin >> x >> j >> y;
		if (x == 1) {
			a[j] = y;
			a1[j] = i;
		}
		else {
			b[j] = y;
			b1[j] = i;
		}
	}
	for (int i = 1; i <= n; i++) {
		for (j = 1; j <= m; j++)
			if (a1[i] > b1[j])
				cout << a[i] << ' ';
			else cout << b[j] << ' ';
				cout << endl;
	}
	//system("pause");
}


#include<stdio.h>
#include<iostream>
#include<string>
#include<cstring>
#include <iomanip>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<set>
#include<bitset>
#include<map>
#include<queue>
#include<math.h>
#include <time.h>
using namespace std;
//set<int>::iterator it;
#define mdlo 1000000007
#define maxERR 0.00000002
//#define minVal -10000000000000
#define maxBits 40
#define pi 3.1415926535897932384626433832795
#define INF 999999999999
#define lim 10000007
typedef long long ll;
long long mInv[41];
long long fact[41];
long long factInv[41];
map<long,long>mymap;
ll heights[5];
ll segTree[3];
long nums[5005];
long g[5005];
//ll dp[2][5002][5002];
vector<long>primes;
vector<long>badPrimes;
long funcVal[1000006];
ll N;
set<string>dictionary;
set<string>::iterator it;
pair<set<string>::iterator,bool> ret;
typedef multiset<long> numset;
ll gcd(ll a,ll b){
    if(b==0)
        return a;
    if(a%b==0)
        return b;
    return gcd(b,a%b);
}
long long getmoduloInv(long long n){
    if(n==1)
        return 1;
    if(mInv[n]>0)
        return mInv[n];
    long long m=(-1*mdlo)/n;
    m+=mdlo;
    m*=getmoduloInv(mdlo%n);
    mInv[n]=(m%mdlo);
    return mInv[n];
}
vector< long> get_primes(unsigned long maxN){

    char *sieve;
    sieve = new char[maxN/8+1];
    // Fill sieve with 1
    long m=(maxN/8)+1;
    for(long long i=0;i<m;i++)
        sieve[i]=255;
    for(unsigned long x = 2; x <= maxN; x++)
        if(sieve[x/8] & (0x01 << (x % 8))){
            primes.push_back(x);
            // Is prime. Mark multiplicates.
            for(unsigned long j = 2*x; j <= maxN; j += x)
                sieve[j/8] &= ~(0x01 << (j % 8));
        }
    delete[] sieve;
    return primes;
}
ll getPow(ll b,ll p){
	if(b<2||p==1)
		return b;
	if(p==0)
		return 1;
	ll val=getPow(b,p/2);
	val*=val;
	val%=mdlo;
	if(p%2==1){
		val*=b;
		val%=mdlo;
	}
	return val;
}
void buildSegTree(long node,long b,long e){
    if(b==e)
        segTree[node]=b;
    else{
        long mid=(b+e)/2;
        buildSegTree(node*2,b,mid);
        buildSegTree(1+node*2,mid+1,e);
        if(heights[segTree[node*2]]<=heights[segTree[1+node*2]])
            segTree[node]=segTree[node*2];
        else
            segTree[node]=segTree[1+node*2];
    }
}
long long getMin(long node,long segBeg,long segEnd,long qBeg,long qEnd){
    if(segEnd<qBeg||segBeg>qEnd)
        return N+1;
    if(segBeg>=qBeg&&segEnd<=qEnd)
        return segTree[node];
    long mid=(segBeg+segEnd)/2;
    long fh=getMin(node*2,segBeg,mid,qBeg,qEnd);
    long sh=getMin(1+node*2,mid+1,segEnd,qBeg,qEnd);
    if(heights[sh]<heights[fh])
        return sh;
    return fh;
}
string genMinString(ll val){
	long len=val/9,firstDigit=val%9;
	string s="";
	if(firstDigit>0){
		char ch='0'+firstDigit;
		s=ch+s;
	}
	for(long i=0;i<len;i++)
		s=s+"9";
	return s;

}
typedef vector<long>bigInteger;
long maxBuild;
bigInteger mult(bigInteger A,bigInteger B){
    long len1=A.size(),len2=B.size();
    bigInteger C;
    for(long i=0;i<len1;i++){
        long carry=0;
        for(long j=0;j<len2;j++){
            long pos=i+j;
            long val=carry+(A[i]*B[j]);
            carry=val/10;
            val=val%10;
            if(pos>=C.size()){
                C.push_back(val);
            }
            else{
                C[pos]+=val;
                carry=C[pos]/10;
                C[pos]=C[pos]%10;
            }
        }
        if(carry>0)
            C.push_back(carry);
    }
    return C;
}
bigInteger getVal(long power){
    bigInteger C;
    C.push_back(5);
    if(power==1)
        return C;
    bigInteger B=getVal(power/2);
    B=mult(B,B);
    if(power%2==1)
        B=mult(B,C);
    return B;
}
long getZeros(long fives){
    long ans=0;
    while(fives>0){
        ans+=fives;
        fives/=5;
    }
    return ans;
}
ll getPow(long zeros){
    long val=((zeros*4)/5)-1;
    while(getZeros(val)<zeros)
        val++;
    if(getZeros(val)==zeros)
        return val;
    return -1;
}
ll rowVal[5050],colVal[5050];
ll rowAlter[5050],colAlter[5050];
int main(void){
 // freopen ("E:/inputSmall.txt","r",stdin);
 // freopen ("E:/outputSmall2.txt","w",stdout);
    ll test_cases=1,l,r,seed=1,maxVal;
	//memset(dp,0,sizeof(dp));
	vector<string>originalStrings;
	ll curNum;
	ll m,n,k;
	ll ans=0;
//	get_primes(32000);
  for(long T=0;T<test_cases;T++){
	   cin>>n>>m>>k;
	   long rv,cv,av;
	   memset(rowVal,0,sizeof(rowVal));
	   memset(colVal,0,sizeof(colVal));
	   memset(rowAlter,-1,sizeof(rowAlter));
	   memset(colAlter,-1,sizeof(colAlter));
	   for(long i=0;i<k;i++){
        scanf("%ld",&av);
        scanf("%ld",&rv);
        rv--;
        scanf("%ld",&cv);
        if(av==1){
            rowVal[rv]=cv;
            rowAlter[rv]=i;
        }
        else{
            colVal[rv]=cv;
            colAlter[rv]=i;
        }
	   }
	   for(long i=0;i<n;i++){
        for(long j=0;j<m;j++){
            if(rowAlter[i]>colAlter[j])
                printf("%ld ",rowVal[i]);
            else
                printf("%ld ",colVal[j]);
        }
        cout<<endl;
	   }

        //cout<<ans<<endl;
   }
    return 0;
}
/*ll cumDist1[100005];
ll cumDist2[100005];
vector<ll>distFrom1;
vector<ll>distFrom2;
void initFact(){
    fact[0]=1;
    factInv[0]=1;
    for(ll i=1;i<0;i++){
        factInv[i]=(factInv[i-1]*getmoduloInv(i))%mdlo;
        fact[i]=(fact[i-1]*i)%mdlo;
    }
}
bool isLeapyear(long year){
    if(year%4==0&&(year%100!=0||year%400==0))
        return true;
    return false;
}
ll ds,ms,ys,de,me,ye,dc,mc,yc,day;
void next13th(){
    if(ds<13){
        ds=13;
        return;
    }
    ms++;
    ds=13;
    if(ms>12)
    {
        ms=1;
        ys++;
    }
}
    long months[]={31,28,31,30,31,30,31,31,30,31,30,31};
    long dayBeforeMonth[12];
bool validDate(){
    if(ys>ye)
        return false;
    if(ys<ye)
        return true;
    if(ms>me)
        return false;
    if(ms<me)
        return true;
    if(ds>de)
        return false;
    return true;
}
long getDay(long d,long m,long y){
    long day=((y-1900)*365+(y-1900)/4-(y-1900)/100+(y-1600)/400+dayBeforeMonth[m-1]+d+2)%7;
    if(y%4==0&&m<3){
        if(y%400==0||y%100!=0)
            day=(day+6)%7;
    }
    return day;
}
bool iscurDayFriday(){
    if(getDay(ds,ms,ys)==0)
        return true;
    return false;
}
void dijkstra(long startPoint){
    long m;
    if(startPoint==1)
        m=0;
    else
        m=1;
    distMatrix[m][startPoint]=0;
    priority_queue<edge, vector<edge>,greater <edge>>pq;
    pq.push(edge(0,startPoint));
    while(!pq.empty()){
        edge top=pq.top();
        pq.pop();
        ll d= top.first,u=top.second;
        if(distMatrix[m][u]==d){
            for(long i=0;i<graph[u].size();i++){
                ll v=graph[u][i].second;
                ll ec=graph[u][i].first;
                if(distMatrix[m][v]>distMatrix[m][u]+ec){
                    distMatrix[m][v]=distMatrix[m][u]+ec;
                    pq.push(edge(distMatrix[m][v],v));
                }
            }
        }
    }
}
ll vals[1];
ll dp[1];
ll totalSum[1];
long long mInv[1];
long long fact[1];
long long factInv[1];

//long long pow10[18];
long long getmoduloInv(long long n){
    if(n==1)
        return 1;
    if(mInv[n]>0)
        return mInv[n];
    long long m=(-1*mdlo)/n;
    m+=mdlo;
    m*=getmoduloInv(mdlo%n);
    mInv[n]=(m%mdlo);
    return mInv[n];
}
long long onNthbit(long pos,long val){
    return val|pwr2[pos];
}
bool isNthbitOn(long long pos,long long val){
    return (val&pwr2[pos])>0;
}

seg segTree[5];
long N;
void buildSumSegmentTree(long long b,long long e,long long node){
    if(b==e)
    {
         segTree[node].first=vals[b];
         return;
    }
    long long mid=(b+e)/2;
    buildSumSegmentTree(b,mid,node*2);
    buildSumSegmentTree(mid+1,e,1+(node*2));
    segTree[node].first=segTree[2*node].first+segTree[1+(node*2)].first;
}
long long getSum(long long qStart,long long qEnd,long long sStart,long long sEnd,long long node,prop p){

    long k=sEnd-sStart,n;
    for(long i=0;i<p.size();i++){
        segTree[node].second.push_back(p[i]);
        n=p[i];
        segTree[node].first+=(((n+k)*(n+k+1)*(2*n+2*k+1)-(n*(n-1)*(2*n-1)))/6+((n+k)*(n+k+1)-n*(n-1)))/2;
        }
    if(qEnd<sStart||qStart>sEnd)
    {
        return 0;
    }
    if(sStart>=qStart&&sEnd<=qEnd)
    {
         return segTree[node].first;
    }
    long long sMid=(sStart+sEnd)/2;
    prop l;
    for(long i=0;segTree[node].second.size();i++)
        l.push_back(segTree[node].second[i]+sMid+1-sStart);
    return getSum(qStart,qEnd,sStart,sMid,2*node,segTree[node].second)+getSum(qStart,qEnd,sMid+1,sEnd,1+(2*node),l);
}
long long update(ll node, long long sStart,ll sEnd,long upStart,long upEnd){
    if(upEnd<sStart||upStart>sEnd)
        return 0;
    long k=sEnd-sStart,n=sStart+1-upStart;
    if(upStart<=sStart&&upEnd>=sEnd){
        segTree[node].second.push_back(n);
        segTree[node].first+=(((n+k)*(n+k+1)*(2*n+2*k+1))/6);
        segTree[node].first-=(((n*(n-1)*(2*n-1)))/6);
        segTree[node].first+=(((n+k)*(n+k+1)-n*(n-1)))/2;
        return 0;
    }
    ll sMid=(sStart+sEnd)/2;
    update(node*2,sStart,sMid,upStart,upEnd);
    update(1+(node*2),sMid+1,sEnd,upStart,upEnd);
    segTree[node].first=segTree[2*node].first+segTree[1+(node*2)].first;

    for(long i=0;i<segTree[node].second.size();i++){
        n=segTree[node].second[i];
        segTree[node].first+=(((n+k)*(n+k+1)*(2*n+2*k+1)-(n*(n-1)*(2*n-1)))/6+((n+k)*(n+k+1)-n*(n-1)))/2;
    }
    return segTree[node].first;
}
void update(long x,long y){
    long len=y-x+1,add=0;
    for(long i=0;i<len;i++){
        add+=(i+1)*2;
        update(1,0,N-1,x+i,add+vals[x+i]);
    }
}
long getlen(long long num){
    long len=0;
    while(num>0){
        num/=10;
        len++;
    }
    return len;
}
encryptedLine lineConverter(string s){
    encryptedLine el;
    ll val=0,len=s.size();
    for(long i=0;i<len;i++){
        if(s[i]==' '){
            el.push_back(val);
            val=0;
        }
        else{
            val*=26;
            val+=(s[i]-'a');
        }
    }
    el.push_back(val);
    return el;
}
long getPalinDrome(long num){
    long a=num%10;
    long b=(num/10)%10;
    long c=(num/100)%10;
    return num*1000+a*100+b*10+c;
}

vector<unsigned long> get_primes(unsigned long max){
    vector<unsigned long> primes;
    char *sieve;
    sieve = new char[max/8+1];
    // Fill sieve with 1
    long m=(max/8)+1;
    for(long long i=0;i<m;i++)
        sieve[i]=255;
    for(unsigned long x = 2; x <= max; x++)
        if(sieve[x/8] & (0x01 << (x % 8))){
            primes.push_back(x);
            // Is prime. Mark multiplicates.
            for(unsigned long j = 2*x; j <= max; j += x)
                sieve[j/8] &= ~(0x01 << (j % 8));
        }
    delete[] sieve;
    return primes;
}string add(string a,string b){
    int l1=a.size();
    int l2=b.size();
    while(l1<l2){
        a="0"+a;
        l1++;
    }
    while(l2<l1){
        b="0"+b;
        l2++;
    }
    string s;
    int carry=0;
    for(long i=l2-1;i>-1;i--){
        int d=a[i]+b[i]+carry-2*'0';
        carry=d/10;
        a[i]='0'+(d%10);
    }
    if(carry)
        a="1"+a;
    return a;
}
long long mInv[1010];
long long fact[1000];
long long factInv[1000];
long long getmoduloInv(long long n){
    if(n==1)
        return 1;
    if(mInv[n]>0)
        return mInv[n];
    long long m=(-1*mdlo)/n;
    m+=mdlo;
    m*=getmoduloInv(mdlo%n);
    mInv[n]=(m%mdlo);
    return mInv[n];

}

long dp[200010];
vector<weightedPos>points;
vector<weightedPos>modifiedPositions;
vector<long long>heights;
long long segTree[530010];
long N,X,W;
void buildSegTree(long node,long b,long e){
    if(b==e)
        segTree[node]=b;
    else{
        long mid=(b+e)/2;
        buildSegTree(node*2,b,mid);
        buildSegTree(1+node*2,mid+1,e);
        if(heights[segTree[node*2]]<=heights[segTree[1+node*2]])
            segTree[node]=segTree[node*2];
        else
            segTree[node]=segTree[1+node*2];
    }
}
long long getMin(long node,long segBeg,long segEnd,long qBeg,long qEnd){
    if(segEnd<qBeg||segBeg>qEnd)
        return N;
    if(segBeg>=qBeg&&segEnd<=qEnd)
        return segTree[node];
    long mid=(segBeg+segEnd)/2;
    long fh=getMin(node*2,segBeg,mid,qBeg,qEnd);
    long sh=getMin(1+node*2,mid+1,segEnd,qBeg,qEnd);
    if(heights[sh]<heights[fh])
        return sh;
    return fh;
}
long long getMinPos(long node,long segBeg,long segEnd,long qBeg,long qEnd){
    if(segEnd<qBeg||segBeg<qEnd)
        return 0;
    if(segBeg>=qBeg&&segEnd<=qEnd)
        return segTree[node];
    long mid=(segBeg+segEnd)/2;
    long fh=getMin(node*2,segBeg,mid,qBeg,qEnd);
    long sh=getMin(node*2,mid+1,segEnd,qBeg,qEnd);
    return min(fh,sh);
}
long getPos(long val){
    long low=0,high=modifiedPositions.size()-1;
    long mid=(low+high)/2;
    while(low<high){
        if(modifiedPositions[mid].first<val)
            low=mid+1;
        else
            high=mid-1;
        mid=(low+high)/2;
    }
    if(mid>0)
        mid--;
    while(mid<modifiedPositions.size()&&modifiedPositions[mid].first<val)
        mid++;
    return mid;
}
long getMaxGroup(long index){
    if(dp[index]>-1)
        return dp[index];
    long pos=getPos(points[index].first+points[index].second);
    if(pos>=N)
        return 1;
    long nextPoint=getMin(1,0,N-1,pos,N-1);
    dp[index]=1+getMaxGroup(nextPoint);
    return dp[index];
}
*/


#include<stdio.h>
#include<iostream>
#include<string>
#include<cstring>
#include <iomanip>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<set>
#include<bitset>
#include<map>
#include<queue>
#include<math.h>
#include <time.h>
using namespace std;
//set<int>::iterator it;
#define mdlo 1000000007
#define maxERR 0.00000002
//#define minVal -10000000000000
#define maxBits 40
#define pi 3.1415926535897932384626433832795
#define INF 999999999999
#define lim 10000007
typedef long long ll;
long long mInv[41];
long long fact[41];
long long factInv[41];
map<long,long>mymap;
ll heights[5];
ll segTree[3];
long nums[5005];
long g[5005];
//ll dp[2][5002][5002];
vector<long>primes;
vector<long>badPrimes;
long funcVal[1000006];
ll N;
set<string>dictionary;
set<string>::iterator it;
pair<set<string>::iterator,bool> ret;
typedef multiset<long> numset;
ll gcd(ll a,ll b){
    if(b==0)
        return a;
    if(a%b==0)
        return b;
    return gcd(b,a%b);
}
long long getmoduloInv(long long n){
    if(n==1)
        return 1;
    if(mInv[n]>0)
        return mInv[n];
    long long m=(-1*mdlo)/n;
    m+=mdlo;
    m*=getmoduloInv(mdlo%n);
    mInv[n]=(m%mdlo);
    return mInv[n];
}
vector< long> get_primes(unsigned long maxN){

    char *sieve;
    sieve = new char[maxN/8+1];
    // Fill sieve with 1
    long m=(maxN/8)+1;
    for(long long i=0;i<m;i++)
        sieve[i]=255;
    for(unsigned long x = 2; x <= maxN; x++)
        if(sieve[x/8] & (0x01 << (x % 8))){
            primes.push_back(x);
            // Is prime. Mark multiplicates.
            for(unsigned long j = 2*x; j <= maxN; j += x)
                sieve[j/8] &= ~(0x01 << (j % 8));
        }
    delete[] sieve;
    return primes;
}
ll getPow(ll b,ll p){
	if(b<2||p==1)
		return b;
	if(p==0)
		return 1;
	ll val=getPow(b,p/2);
	val*=val;
	val%=mdlo;
	if(p%2==1){
		val*=b;
		val%=mdlo;
	}
	return val;
}
void buildSegTree(long node,long b,long e){
    if(b==e)
        segTree[node]=b;
    else{
        long mid=(b+e)/2;
        buildSegTree(node*2,b,mid);
        buildSegTree(1+node*2,mid+1,e);
        if(heights[segTree[node*2]]<=heights[segTree[1+node*2]])
            segTree[node]=segTree[node*2];
        else
            segTree[node]=segTree[1+node*2];
    }
}
long long getMin(long node,long segBeg,long segEnd,long qBeg,long qEnd){
    if(segEnd<qBeg||segBeg>qEnd)
        return N+1;
    if(segBeg>=qBeg&&segEnd<=qEnd)
        return segTree[node];
    long mid=(segBeg+segEnd)/2;
    long fh=getMin(node*2,segBeg,mid,qBeg,qEnd);
    long sh=getMin(1+node*2,mid+1,segEnd,qBeg,qEnd);
    if(heights[sh]<heights[fh])
        return sh;
    return fh;
}
string genMinString(ll val){
	long len=val/9,firstDigit=val%9;
	string s="";
	if(firstDigit>0){
		char ch='0'+firstDigit;
		s=ch+s;
	}
	for(long i=0;i<len;i++)
		s=s+"9";
	return s;

}
typedef vector<long>bigInteger;
long maxBuild;
bigInteger mult(bigInteger A,bigInteger B){
    long len1=A.size(),len2=B.size();
    bigInteger C;
    for(long i=0;i<len1;i++){
        long carry=0;
        for(long j=0;j<len2;j++){
            long pos=i+j;
            long val=carry+(A[i]*B[j]);
            carry=val/10;
            val=val%10;
            if(pos>=C.size()){
                C.push_back(val);
            }
            else{
                C[pos]+=val;
                carry=C[pos]/10;
                C[pos]=C[pos]%10;
            }
        }
        if(carry>0)
            C.push_back(carry);
    }
    return C;
}
bigInteger getVal(long power){
    bigInteger C;
    C.push_back(5);
    if(power==1)
        return C;
    bigInteger B=getVal(power/2);
    B=mult(B,B);
    if(power%2==1)
        B=mult(B,C);
    return B;
}
long getZeros(long fives){
    long ans=0;
    while(fives>0){
        ans+=fives;
        fives/=5;
    }
    return ans;
}
ll getPow(long zeros){
    long val=((zeros*4)/5)-1;
    while(getZeros(val)<zeros)
        val++;
    if(getZeros(val)==zeros)
        return val;
    return -1;
}
ll rowVal[5050],colVal[5050];
ll rowAlter[5050],colAlter[5050];
int main(void){
 // freopen ("E:/inputSmall.txt","r",stdin);
 // freopen ("E:/outputSmall2.txt","w",stdout);
    ll test_cases=1,l,r,seed=1,maxVal;
	//memset(dp,0,sizeof(dp));
	vector<string>originalStrings;
	ll curNum;
	ll m,n,k;
	ll ans=0;
//	get_primes(32000);
  for(long T=0;T<test_cases;T++){
	   cin>>n>>m>>k;
	   long rv,cv,av;
	   memset(rowVal,0,sizeof(rowVal));
	   memset(colVal,0,sizeof(colVal));
	   memset(rowAlter,-1,sizeof(rowAlter));
	   memset(colAlter,-1,sizeof(colAlter));
	   for(long i=0;i<k;i++){
        scanf("%ld",&av);
        scanf("%ld",&rv);
        rv--;
        scanf("%ld",&cv);
        if(av==1){
            rowVal[rv]=cv;
            rowAlter[rv]=i;
        }
        else{
            colVal[rv]=cv;
            colAlter[rv]=i;
        }
	   }
	   for(long i=0;i<n;i++){
        for(long j=0;j<m;j++){
            if(rowAlter[i]>colAlter[j])
                printf("%ld ",rowVal[i]);
            else
                printf("%ld ",colVal[j]);
        }
        cout<<endl;
	   }

        cout<<ans<<endl;
   }
    return 0;
}
/*ll cumDist1[100005];
ll cumDist2[100005];
vector<ll>distFrom1;
vector<ll>distFrom2;
void initFact(){
    fact[0]=1;
    factInv[0]=1;
    for(ll i=1;i<0;i++){
        factInv[i]=(factInv[i-1]*getmoduloInv(i))%mdlo;
        fact[i]=(fact[i-1]*i)%mdlo;
    }
}
bool isLeapyear(long year){
    if(year%4==0&&(year%100!=0||year%400==0))
        return true;
    return false;
}
ll ds,ms,ys,de,me,ye,dc,mc,yc,day;
void next13th(){
    if(ds<13){
        ds=13;
        return;
    }
    ms++;
    ds=13;
    if(ms>12)
    {
        ms=1;
        ys++;
    }
}
    long months[]={31,28,31,30,31,30,31,31,30,31,30,31};
    long dayBeforeMonth[12];
bool validDate(){
    if(ys>ye)
        return false;
    if(ys<ye)
        return true;
    if(ms>me)
        return false;
    if(ms<me)
        return true;
    if(ds>de)
        return false;
    return true;
}
long getDay(long d,long m,long y){
    long day=((y-1900)*365+(y-1900)/4-(y-1900)/100+(y-1600)/400+dayBeforeMonth[m-1]+d+2)%7;
    if(y%4==0&&m<3){
        if(y%400==0||y%100!=0)
            day=(day+6)%7;
    }
    return day;
}
bool iscurDayFriday(){
    if(getDay(ds,ms,ys)==0)
        return true;
    return false;
}
void dijkstra(long startPoint){
    long m;
    if(startPoint==1)
        m=0;
    else
        m=1;
    distMatrix[m][startPoint]=0;
    priority_queue<edge, vector<edge>,greater <edge>>pq;
    pq.push(edge(0,startPoint));
    while(!pq.empty()){
        edge top=pq.top();
        pq.pop();
        ll d= top.first,u=top.second;
        if(distMatrix[m][u]==d){
            for(long i=0;i<graph[u].size();i++){
                ll v=graph[u][i].second;
                ll ec=graph[u][i].first;
                if(distMatrix[m][v]>distMatrix[m][u]+ec){
                    distMatrix[m][v]=distMatrix[m][u]+ec;
                    pq.push(edge(distMatrix[m][v],v));
                }
            }
        }
    }
}
ll vals[1];
ll dp[1];
ll totalSum[1];
long long mInv[1];
long long fact[1];
long long factInv[1];

//long long pow10[18];
long long getmoduloInv(long long n){
    if(n==1)
        return 1;
    if(mInv[n]>0)
        return mInv[n];
    long long m=(-1*mdlo)/n;
    m+=mdlo;
    m*=getmoduloInv(mdlo%n);
    mInv[n]=(m%mdlo);
    return mInv[n];
}
long long onNthbit(long pos,long val){
    return val|pwr2[pos];
}
bool isNthbitOn(long long pos,long long val){
    return (val&pwr2[pos])>0;
}

seg segTree[5];
long N;
void buildSumSegmentTree(long long b,long long e,long long node){
    if(b==e)
    {
         segTree[node].first=vals[b];
         return;
    }
    long long mid=(b+e)/2;
    buildSumSegmentTree(b,mid,node*2);
    buildSumSegmentTree(mid+1,e,1+(node*2));
    segTree[node].first=segTree[2*node].first+segTree[1+(node*2)].first;
}
long long getSum(long long qStart,long long qEnd,long long sStart,long long sEnd,long long node,prop p){

    long k=sEnd-sStart,n;
    for(long i=0;i<p.size();i++){
        segTree[node].second.push_back(p[i]);
        n=p[i];
        segTree[node].first+=(((n+k)*(n+k+1)*(2*n+2*k+1)-(n*(n-1)*(2*n-1)))/6+((n+k)*(n+k+1)-n*(n-1)))/2;
        }
    if(qEnd<sStart||qStart>sEnd)
    {
        return 0;
    }
    if(sStart>=qStart&&sEnd<=qEnd)
    {
         return segTree[node].first;
    }
    long long sMid=(sStart+sEnd)/2;
    prop l;
    for(long i=0;segTree[node].second.size();i++)
        l.push_back(segTree[node].second[i]+sMid+1-sStart);
    return getSum(qStart,qEnd,sStart,sMid,2*node,segTree[node].second)+getSum(qStart,qEnd,sMid+1,sEnd,1+(2*node),l);
}
long long update(ll node, long long sStart,ll sEnd,long upStart,long upEnd){
    if(upEnd<sStart||upStart>sEnd)
        return 0;
    long k=sEnd-sStart,n=sStart+1-upStart;
    if(upStart<=sStart&&upEnd>=sEnd){
        segTree[node].second.push_back(n);
        segTree[node].first+=(((n+k)*(n+k+1)*(2*n+2*k+1))/6);
        segTree[node].first-=(((n*(n-1)*(2*n-1)))/6);
        segTree[node].first+=(((n+k)*(n+k+1)-n*(n-1)))/2;
        return 0;
    }
    ll sMid=(sStart+sEnd)/2;
    update(node*2,sStart,sMid,upStart,upEnd);
    update(1+(node*2),sMid+1,sEnd,upStart,upEnd);
    segTree[node].first=segTree[2*node].first+segTree[1+(node*2)].first;

    for(long i=0;i<segTree[node].second.size();i++){
        n=segTree[node].second[i];
        segTree[node].first+=(((n+k)*(n+k+1)*(2*n+2*k+1)-(n*(n-1)*(2*n-1)))/6+((n+k)*(n+k+1)-n*(n-1)))/2;
    }
    return segTree[node].first;
}
void update(long x,long y){
    long len=y-x+1,add=0;
    for(long i=0;i<len;i++){
        add+=(i+1)*2;
        update(1,0,N-1,x+i,add+vals[x+i]);
    }
}
long getlen(long long num){
    long len=0;
    while(num>0){
        num/=10;
        len++;
    }
    return len;
}
encryptedLine lineConverter(string s){
    encryptedLine el;
    ll val=0,len=s.size();
    for(long i=0;i<len;i++){
        if(s[i]==' '){
            el.push_back(val);
            val=0;
        }
        else{
            val*=26;
            val+=(s[i]-'a');
        }
    }
    el.push_back(val);
    return el;
}
long getPalinDrome(long num){
    long a=num%10;
    long b=(num/10)%10;
    long c=(num/100)%10;
    return num*1000+a*100+b*10+c;
}

vector<unsigned long> get_primes(unsigned long max){
    vector<unsigned long> primes;
    char *sieve;
    sieve = new char[max/8+1];
    // Fill sieve with 1
    long m=(max/8)+1;
    for(long long i=0;i<m;i++)
        sieve[i]=255;
    for(unsigned long x = 2; x <= max; x++)
        if(sieve[x/8] & (0x01 << (x % 8))){
            primes.push_back(x);
            // Is prime. Mark multiplicates.
            for(unsigned long j = 2*x; j <= max; j += x)
                sieve[j/8] &= ~(0x01 << (j % 8));
        }
    delete[] sieve;
    return primes;
}string add(string a,string b){
    int l1=a.size();
    int l2=b.size();
    while(l1<l2){
        a="0"+a;
        l1++;
    }
    while(l2<l1){
        b="0"+b;
        l2++;
    }
    string s;
    int carry=0;
    for(long i=l2-1;i>-1;i--){
        int d=a[i]+b[i]+carry-2*'0';
        carry=d/10;
        a[i]='0'+(d%10);
    }
    if(carry)
        a="1"+a;
    return a;
}
long long mInv[1010];
long long fact[1000];
long long factInv[1000];
long long getmoduloInv(long long n){
    if(n==1)
        return 1;
    if(mInv[n]>0)
        return mInv[n];
    long long m=(-1*mdlo)/n;
    m+=mdlo;
    m*=getmoduloInv(mdlo%n);
    mInv[n]=(m%mdlo);
    return mInv[n];

}

long dp[200010];
vector<weightedPos>points;
vector<weightedPos>modifiedPositions;
vector<long long>heights;
long long segTree[530010];
long N,X,W;
void buildSegTree(long node,long b,long e){
    if(b==e)
        segTree[node]=b;
    else{
        long mid=(b+e)/2;
        buildSegTree(node*2,b,mid);
        buildSegTree(1+node*2,mid+1,e);
        if(heights[segTree[node*2]]<=heights[segTree[1+node*2]])
            segTree[node]=segTree[node*2];
        else
            segTree[node]=segTree[1+node*2];
    }
}
long long getMin(long node,long segBeg,long segEnd,long qBeg,long qEnd){
    if(segEnd<qBeg||segBeg>qEnd)
        return N;
    if(segBeg>=qBeg&&segEnd<=qEnd)
        return segTree[node];
    long mid=(segBeg+segEnd)/2;
    long fh=getMin(node*2,segBeg,mid,qBeg,qEnd);
    long sh=getMin(1+node*2,mid+1,segEnd,qBeg,qEnd);
    if(heights[sh]<heights[fh])
        return sh;
    return fh;
}
long long getMinPos(long node,long segBeg,long segEnd,long qBeg,long qEnd){
    if(segEnd<qBeg||segBeg<qEnd)
        return 0;
    if(segBeg>=qBeg&&segEnd<=qEnd)
        return segTree[node];
    long mid=(segBeg+segEnd)/2;
    long fh=getMin(node*2,segBeg,mid,qBeg,qEnd);
    long sh=getMin(node*2,mid+1,segEnd,qBeg,qEnd);
    return min(fh,sh);
}
long getPos(long val){
    long low=0,high=modifiedPositions.size()-1;
    long mid=(low+high)/2;
    while(low<high){
        if(modifiedPositions[mid].first<val)
            low=mid+1;
        else
            high=mid-1;
        mid=(low+high)/2;
    }
    if(mid>0)
        mid--;
    while(mid<modifiedPositions.size()&&modifiedPositions[mid].first<val)
        mid++;
    return mid;
}
long getMaxGroup(long index){
    if(dp[index]>-1)
        return dp[index];
    long pos=getPos(points[index].first+points[index].second);
    if(pos>=N)
        return 1;
    long nextPoint=getMin(1,0,N-1,pos,N-1);
    dp[index]=1+getMaxGroup(nextPoint);
    return dp[index];
}
*/


#include <iostream>
using namespace std;
int main()
{
 unsigned int n,m,k;
 cin >>n>>m>>k;
 unsigned int **mas=new unsigned int* [n];
 for (int i=0; i<n; i++)
   mas[i]=new unsigned int [m];
 for(int i=0; i<n; i++)
   for(int j=0; j<n; j++)
    mas[i][j]=0;
 for (int i=0; i<k; i++)
   {
	char a;
	unsigned int b,c;
	cin >>a>>b>>c;
	if(a=='1')
	 for(int j=0; j<m; j++)
	  mas[b-1][j]=c;
	else
	 for(int j=0; j<n; j++)
	  mas[j][b-1]=c;
   }
  for(int i=0; i<n; i++)
   {
   for(int j=0; j<m; j++)
    cout<<mas[i][j]<<' ';
	cout <<endl;
   }
  delete [] mas;
 return 0;
}


#include <iostream>
using namespace std;
int main()
{
 unsigned int n,m,k;
 cin >>n>>m>>k;
 unsigned int **mas=new unsigned int* [3];
 for (int i=0; i<3; i++)
   mas[i]=new unsigned int [k];
   for(int j=0; j<k; j++)
    cin >>mas[0][j]>>mas[1][j]>>mas[2][j];
 for (int i=0; i<n; i++)
   {
	unsigned int *boop=new unsigned int [m];
	for(int j=m-1;j>=0;j--)
	 for(int z=k-1;z>=0;z--)
	  if ((mas[1][z]==i+1&&mas[0][z]==1)||(mas[1][z]==j+1&&mas[0][z]==2))
	  {
	  	boop[j]=mas[2][z];
	  	break;
	  }
	  else
	  {
	  	boop[j]=0;
	  }
	for (int j=0; j<m; j++)
	 cout <<boop[j]<<' ';
	cout<<endl;
	delete [] boop;
   }
  delete [] mas;
 return 0;
}


#include <iostream>
using namespace std;
int main()
{
 unsigned int n,m,k;
 cin >>n>>m>>k;
 unsigned int **mas=new unsigned int* [3];
 for (int i=0; i<3; i++)
   mas[i]=new unsigned int [k];
   for(int j=0; j<k; j++)
    cin >>mas[0][j]>>mas[1][j]>>mas[2][j];
 for (int i=0; i<n; i++)
   {
	unsigned int *boop=new unsigned int [m];
	for(int j=m-1;j>=0;j--)
	 for(int z=k-1;z>=0;z--)
	  if ((mas[1][z]==i+1&&mas[0][z]==1)||(mas[1][z]==j+1&&mas[0][z]==2))
	  {
	  	boop[j]=mas[2][z];
	  	 break;
	  }
	for (int j=0; j<m; j++)
	 cout <<boop[j]<<' ';
	cout<<endl;
	delete [] boop;
   }
  delete [] mas;
 return 0;
}


#include <iostream>
using namespace std;
int main()
{
 unsigned int n,m,k;
 cin >>n>>m>>k;
 unsigned int **mas=new unsigned int* [n];
 for (int i=0; i<m; i++)
   mas[i]=new unsigned int [m];
 for(int i=0; i<n; i++)
   for(int j=0; j<n; j++)
    mas[i][j]=0;
 for (int i=0; i<k; i++)
   {
	char a;
	unsigned int b,c;
	cin >>a>>b>>c;
	if(a=='1')
	 for(int j=0; j<m; j++)
	  mas[b-1][j]=c;
	else
	 for(int j=0; j<n; j++)
	  mas[j][b-1]=c;
   }
  for(int i=0; i<n; i++)
   {
   for(int j=0; j<n; j++)
    cout<<mas[i][j]<<' ';
	cout <<endl;
   }
 return 0;
}


#include <iostream>
using namespace std;
int main()
{
 unsigned int n,m,k;
 cin >>n>>m>>k;
 unsigned int **mas=new unsigned int* [n];
 for (int i=0; i<n; i++)
   mas[i]=new unsigned int [m];
 for(int i=0; i<n; i++)
   for(int j=0; j<n; j++)
    mas[i][j]=0;
 for (int i=0; i<k; i++)
   {
	char a;
	unsigned int b,c;
	cin >>a>>b>>c;
	if(a=='1')
	 for(int j=0; j<n; j++)
	  mas[b-1][j]=c;
	else
	 for(int j=0; j<m; j++)
	  mas[j][b-1]=c;
   }
  for(int i=0; i<n; i++)
   {
   for(int j=0; j<m; j++)
    cout<<mas[i][j]<<' ';
	cout <<endl;
   }
 return 0;
}


#include <iostream>
using namespace std;
int main()
{
 unsigned int n,m,k;
 cin >>n>>m>>k;
 unsigned int **mas=new unsigned int* [3];
 for (int i=0; i<3; i++)
   mas[i]=new unsigned int [k];
   for(int j=0; j<k; j++)
    cin >>mas[0][j]>>mas[1][j]>>mas[2][j];
 for (int i=0; i<n; i++)
   {
	unsigned int *boop=new unsigned int [m];
	for(int j=m-1;j>=0;j--)
	 for(int z=k-1;z>=0;z--)
	  if ((mas[1][z]==i+1&&mas[0][z]==1)||(mas[1][z]==j+1&&mas[0][z]==2))
	  {
	  	boop[j]=mas[2][z];
	  	break;
	  }
	for (int j=0; j<m; j++)
	 cout <<boop[j]<<' ';
	cout<<endl;
	delete [] boop;
   }
  delete [] mas;
 return 0;
}


#include <iostream>
using namespace std;
int main()
{
 unsigned int n,m,k;
 cin >>n>>m>>k;
 unsigned int **mas=new unsigned int* [3];
 for (int i=0; i<3; i++)
   mas[i]=new unsigned int [k];
   for(int j=0; j<k; j++)
    cin >>mas[0][j]>>mas[1][j]>>mas[2][j];
 for (int i=0; i<n; i++)
   {
	for(int j=0;j<m;j++)
	 for(int z=k-1;z>=0;z--)
	  if ((mas[1][z]==i+1&&mas[0][z]==1)||(mas[1][z]==j+1&&mas[0][z]==2))
	  {
	  	cout <<mas[2][z]<<' ';
	  	break;
	  }
	  else
	  {
	    if (z==0)
	    {
		 cout <<0<<' ';
	  	 break;
		}
	  }
	cout<<endl;
   }
  delete [] mas;
 return 0;
}


#include <iostream>
using namespace std;
int main()
{
 unsigned int n,m,k;
 cin >>n>>m>>k;
 unsigned int **mas=new unsigned int* [n];
 for (int i=0; i<n; i++)
   mas[i]=new unsigned int [m];
 for(int i=0; i<n; i++)
   for(int j=0; j<m; j++)
    mas[i][j]=0;
 for (int i=0; i<k; i++)
   {
	unsigned int a,b,c;
	cin >>a>>b>>c;
	if(a==1)
	 for(int j=0; j<m; j++)
	  mas[b-1][j]=c;
	else
	 for(int j=0; j<n; j++)
	  mas[j][b-1]=c;
   }
  for(int i=0; i<n; i++)
   {
   for(int j=0; j<m; j++)
    cout<<mas[i][j]<<' ';
	cout <<endl;
   }
  delete [] mas;
 return 0;
}


#include <iostream>
using namespace std;
int main()
{
 unsigned int n,m,k;
 cin >>n>>m>>k;
 unsigned int **mas=new unsigned int* [n];
 for (int i=0; i<n; i++)
   mas[i]=new unsigned int [m];
 for(int i=0; i<n; i++)
   for(int j=0; j<n; j++)
    mas[i][j]=0;
 for (int i=0; i<k; i++)
   {
	unsigned int a,b,c;
	cin >>a>>b>>c;
	if(a==1)
	 for(int j=0; j<m; j++)
	  mas[b-1][j]=c;
	else
	 for(int j=0; j<n; j++)
	  mas[j][b-1]=c;
   }
  for(int i=0; i<n; i++)
   {
   for(int j=0; j<m; j++)
    cout<<mas[i][j]<<' ';
	cout <<endl;
   }
  delete [] mas;
 return 0;
}


#include <iostream>
using namespace std;
int main()
{
 unsigned int n,m,k;
 cin >>n>>m>>k;
 unsigned int **mas=new unsigned int* [n];
 for (int i=0; i<n; i++)
   mas[i]=new unsigned int [m];
 for(int i=0; i<n; i++)
   for(int j=0; j<n; j++)
    mas[i][j]=0;
 for (int i=0; i<k; i++)
   {
	char a;
	unsigned int b,c;
	cin >>a>>b>>c;
	if(a=='1')
	 for(int j=0; j<m; j++)
	  mas[b-1][j]=c;
	else
	 for(int j=0; j<n; j++)
	  mas[j][b-1]=c;
   }
  for(int i=0; i<n; i++)
   {
   for(int j=0; j<m; j++)
    cout<<mas[i][j]<<' ';
	cout <<endl;
   }
 return 0;
}


#include <iostream>
using namespace std;
int main()
{
 const int MAXN = 1e5+5;
 pair<int, int> rows[MAXN], couls[MAXN];
 unsigned int n,m,k;
 cin >>n>>m>>k;
 for (int i=0; i<n; i++)
  {
   rows[i].second=0;
   rows[i].first=-10;
  }
 for (int j=0; j<m; j++)
 {
  couls[j].second=0;
  couls[j].first=-10;
 }
 for(int j=0; j<k; j++)
  {
   unsigned int a,r;
   unsigned char c;
   cin >>c >>r >>a;
   if (c=='1')
    {
	 rows[r-1].first=j;
     rows[r-1].second=a;
    }
   else
    {
     couls[r-1].first=j;
     couls[r-1].second=a;
	}
  }
 for (int i=0; i<n; i++)
 {
  for (int j=0; j<m; j++)
   if (rows[i].first>couls[j].first)
    cout <<rows[i].second<<' ';
   else
    cout <<couls[j].second<<' ';
  cout <<endl;
 }
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
#define maxn 5002
int a[maxn][maxn];
int n;
int m,k;
int op,l,color;
typedef struct{
int op,l,color;
int flag;
}T;
T b[100005];
int check[3][5200];
int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
     memset(a,0,sizeof(a));
    for(int i=0;i<k;i++)
    {
        scanf("%d%d%d",&op,&l,&color);
        b[i].op=op;
        b[i].l=l;
        b[i].color=color;
        b[i].flag=1;
        if(check[op][l]!=0)
        {
            int id=check[op][l];
            b[id].flag=0;
        }
        check[op][l]=i;
    }
    for(int i=0;i<k;i++)
    {
        if(b[i].flag==0)continue;
        op=b[i].op;
        l=b[i].l;
        color=b[i].color;
        if(op==1)
        {
            for(int i=0;i<m;i++)
             a[l-1][i]=color;
        }
        if(op==2)
        {
            for(int i=0;i<n;i++)
            a[i][l-1]=color;
        }
    }
    for(int i=0;i<n;i++)
    {
    printf("%d",a[i][0]);
    for(int j=1;j<m;j++)
     printf(" %d",a[i][j]);
     printf("\n");
    }
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
#define maxn 5002
int a[maxn][maxn];
int n;
int m,k;
int op,l,color;
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    memset(a,0,sizeof(a));
    while(k--)
    {
        scanf("%d%d%d",&op,&l,&color);
        if(op==1)
        {
            for(int i=0;i<m;i++)
             a[l-1][i]=color;
        }
        if(op==2)
        {
            for(int i=0;i<n;i++)
            a[i][l-1]=color;
        }
    }
    for(int i=0;i<n;i++)
    {
    printf("%d",a[i][0]);
    for(int j=1;j<m;j++)
     printf(" %d",a[i][j]);
     printf("\n");
    }
    return 0;
}


#include <stdio.h>
#define MAX 5010
int row[MAX];
int row_c[MAX];
int col[MAX];
int col_c[MAX];
int map[MAX][MAX];

int main()
{
	int n, m, k;
	scanf("%d%d%d", &n, &m, &k);

	int i, j;
	int t, r, c;
	for (i = 1; i <= n; i++)
	{
		scanf("%d%d%d", &t, &r, &c);
		if (t == 1)
		{
			row[r] = c;
			row_c[r] = i;
		}
		if (t == 2)
		{
			col[r] = c;
			col_c[r] = i;
		}
	}
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			if (row_c[i] > col_c[j])
				map[i][j] = row[i];
			else
				map[i][j] = col[j];
			printf("%d ", map[i][j]);
		}
		printf("\n");
	}
	return 0;
}


#include <stdio.h>
#define MAX 5010
int arr[MAX][MAX];

int main()
{
	int n, m, k;
	scanf("%d%d%d", &n, &m, &k);

	int i, j;
	int t, r, c;
	for (i = 1; i <= k; i++)
	{
		scanf("%d%d%d", &t, &r, &c);
		if (t == 1)
		{
			for (j = 1; j <= m; j++)
				arr[r][j] = c;
		}
		else
		{
			for (j = 1; j <= n; j++)
				arr[j][r] = c;
		}
	}
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
			printf("%d ", arr[i][j]);
		printf("\n");
	}
	return 0;
}


#include <stdio.h>
#define MAX 5010
int map[MAX][MAX];
int arr[10010];
int que[10010];
int check[10010];

int main()
{
	int n, m, k;
	scanf("%d%d%d", &n, &m, &k);

	int i, j;
	int t, r, c;
	int count = 0;
	for (i = 1; i <= k; i++)
	{
		scanf("%d%d%d", &t, &r, &c);
		if (t == 1)
		{
			count++;
			que[count] = r;
			arr[r] = c;
			check[r] += 1;
		}
		else
		{
			count++;
			que[count] = r + n;
			arr[r + n] = c;
			check[r + n] += 1;
		}
	}
	count = 0;
	for (i = 1; i <= n; i++)
	{
		count++;
		if (check[que[count]] != 1)
		{
			check[que[count]]--;
			continue;
		}
		if (que[count] <= n)
		{
			for (j = 1; j <= m; j++)
				map[que[count]][j] = arr[que[count]];
		}
		else
		{
			for (j = 1; j <= n; j++)
				map[j][que[count] - n] = arr[que[count]];
		}
	}
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
			printf("%d ", map[i][j]);
		printf("\n");
	}
	return 0;
}


#include <stdio.h>
#define MAX 5010
int row[MAX];
int row_c[MAX];
int col[MAX];
int col_c[MAX];
int map[MAX][MAX];

int main()
{
	int n, m, k;
	scanf("%d%d%d", &n, &m, &k);

	int i, j;
	int t, r, c;
	for (i = 1; i <= k; i++)
	{
		scanf("%d%d%d", &t, &r, &c);
		if (t == 1)
		{
			row[r] = c;
			row_c[r] = i;
		}
		if (t == 2)
		{
			col[r] = c;
			col_c[r] = i;
		}
	}
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			if (row_c[i] > col_c[j])
				map[i][j] = row[i];
			else
				map[i][j] = col[j];
			printf("%d ", map[i][j]);
		}
		printf("\n");
	}
	return 0;
}


#include <stdio.h>
#define MAX 5010
int map[MAX][MAX];
int arr[10010];
int que[10010];
int check[10010];

int main()
{
	int n, m, k;
	scanf("%d%d%d", &n, &m, &k);

	int i, j;
	int t, r, c;
	int count = 0;
	for (i = 1; i <= k; i++)
	{
		scanf("%d%d%d", &t, &r, &c);
		if (t == 1)
		{
			count++;
			que[count] = r;
			arr[r] = c;
			check[r] += 1;
		}
		else
		{
			count++;
			que[count] = r + n;
			arr[r + n] = c;
			check[r + n] += 1;
		}
	}
	count = 0;
	for (i = 1; i <= n; i++)
	{
		count++;
		if (check[que[count]] > 1)
		{
			check[que[count]]--;
			continue;
		}
		if (que[count] <= n)
		{
			for (j = 1; j <= m; j++)
				map[que[count]][j] = arr[que[count]];
		}
		else
		{
			for (j = 1; j <= n; j++)
				map[j][que[count] - n] = arr[que[count]];
		}
	}
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
			printf("%d ", map[i][j]);
		printf("\n");
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;

const int maxx = 5000 + 10;
int N,M;
struct node{
    int color;
    int k;
}rc[2][maxx];
int k;///op数

int new_max(node x,node y){
    if(x.k > y.k){
        return x.color;
    }else{
        return y.color;
    }
}
int main( ){
    cin>>N>>M>>k;
    int op,op_count,color;
    for(int i = 0;i < k; ++i){
        rc[1][op_count].color = 0;
        rc[1][op_count].k = 0;
        rc[0][op_count].color = 0;
        rc[0][op_count].k = 0;
    }
    for(int i = 1;i <= k; ++i){
        cin>>op>>op_count>>color;
        rc[op-1][op_count].color = color;
        rc[op-1][op_count].k = i;
    }
    for(int i = 1;i <= N; ++i){
        for(int  j = 1;j <= M; ++j){
            printf("%d",new_max(rc[0][i],rc[1][j]));
            if(j!=M) printf(" ");
        }
        //printf("\n");
    }
    return 0;
}


#include <iostream>
#include <cstdio>
using namespace std;
int a[5010][5010];
int main(int argc, char *argv[]) {
	int n,m,k,num,op,x;
	while (cin >> n >> m >> k) {
		for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			a[i][j]=0;
		for(int i=1;i<=k;i++){
			cin >> op >> x >> num ;
			if(op==1) {
				for(int j=1;j<=m;j++) a[x][j]=num;
			}
			else {
				for(int j=1;j<=n;j++) a[j][x]=num;
			}
		}
		for(int i=1;i<=n;i++) {
			for(int j=1;j<=m;j++)
				cout << a[i][j]<<" ";
			cout <<"\n";
		}

	}
	return 0;
}


// Print Check
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 5005;
int num[maxn][maxn];
int ma[2][maxn];
int ro[maxn], ca[maxn];
int main ()
{
	 int n, m, k;
	 cin>>n>>m>>k;
	 int a, b, c;
	 for(int i = 1; i <= k; i++){
				cin>>a>>b>>c;
				if(a == 1){
				 ro[b] = c;
				 ma[0][b] = i;
				}
				else{
					ca[b] = c;
					ma[1][b] = i;
				}
	 }
	 for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
				int a = 0;
				if(ma[0][i] > ma[1][j]) a = ro[i];
				else a = ca[j];
				printf("%d%c",a, j == m?'\n':' ');
		}
	 }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
	int n,m,k,order,l,color,i,j;
	cin>>n>>m>>k;
	vector<pair<int,int>> R(n,make_pair(-1,0)),C(m,make_pair(-1,0));
	vector<vector<int>> answer(n,vector<int>(m,0));
	for (i=0;i<k;++i)
	{
		cin>>order>>l>>color;
		if (order==1)
		{
			R[l-1].first=i;
			R[l-1].second=color;
		}
		else
		{
			C[l-1].first=i;
			C[l-1].second=color;
		}
	}
	for (i=0;i<n;++i)
	{
		for (j=0;j<m;++j)
		{
			if(R[i].first>C[j].first)
				answer[i][j]=R[i].second;
			else  answer[i][j]=C[j].second;
		}
	}
	for (i=0;i<n;++i)
	{
		copy(answer[i].begin(),answer[i].end(),ostream_iterator<int>(cout," "));
		cout<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
using namespace std;
int main(void)
{
	int i,n,counter=0,helper=0,first=0;
	bool flag=false;
	string S;
	list<int> L1,L2;
	list<int>::iterator it,prev;
	map<int,int,greater<int>> M;
	cin>>S;
	n=S.length();
	for (i=0;i<n;i++)
	{
		if (S[i]=='(')
			L1.push_back(i);
		else if (S[i]==')')
		{
			if (!L1.empty())
				L1.pop_back();
			else if (L1.empty())
				L2.push_back(i);
		}
	}
	L1.merge(L2);
	L1.push_front(-1);
	L1.push_back(n);
	it=L1.begin();
	prev=it;
	++it;
	for (it;it!=L1.end();)
	{
		M[*it-*prev-1]++;
		prev=it;
		it++;
	}
	if (!M.empty() && M.begin()->first>=2)
		 cout<<M.begin()->first<<" "<<M.begin()->second<<endl;
	else cout<<"0 1\n"<<endl;
	return 0;
}


#include<iostream>;
#include<vector>;
using namespace std;

class command;
void columnCommand(unsigned short location, long value);
void rowCommand(unsigned short location, long value);
void printFinished();
void initate();
void runCommands();

class command
{
public:
	long value;
	short mode;
	unsigned short location;

	command()
	{
		cin >> mode;
		cin >> location;
		cin >> value;
		location--;
	}

	void runCommand()
	{
		if (mode == 1)
			rowCommand(location, value);
		else if (mode == 2)
			columnCommand(location, value);
	}

};

vector<vector<long>> matrix;
vector<command> commandsList;

int main()
{
	initate();
	runCommands();
	printFinished();
	return 0;
}

void initate()
{
	unsigned short rows;
	unsigned short columns;
	unsigned int commands;
	vector<long> tempVect;

	cin >> rows;
	cin >> columns;
	cin >> commands;

	for (int i = 0; i < rows; i++)
	{
		tempVect.clear();
		for (int k = 0; k < columns; k++)
		{
			tempVect.push_back(0);
		}
		matrix.push_back(tempVect);
	}

	for (int j = 0; j < commands; j++)
	{
		commandsList.push_back(command());
	}
}

void runCommands()
{
	vector<unsigned short> rowsUsed;
	vector<unsigned short> columnsUsed;
	bool useThisOne;

	for (int i = commandsList.size() - 1; i >= 0; i--)
	{
		useThisOne = true;
		if (commandsList[i].mode == 1)
		{
			for each (unsigned short location in rowsUsed)
			{
				if (commandsList[i].location == location)
				{
					useThisOne = false;
					break;
				}
			}
			if(useThisOne)
			{
				rowsUsed.push_back(commandsList[i].location);
				commandsList[i].runCommand();
			}
		}
		else if (commandsList[i].mode == 2)
		{
			for each (unsigned short location in columnsUsed)
			{
				if (commandsList[i].location == location)
				{
					useThisOne = false;
					break;
				}
			}
			if (useThisOne)
			{
				columnsUsed.push_back(commandsList[i].location);
				commandsList[i].runCommand();
			}
		}
	}
}

void rowCommand(unsigned short location, long value)
{
	for (int i = 0; i < matrix[location].size(); i++)
	{
		matrix[location][i] = value;
	}
}

void columnCommand(unsigned short location, long value)
{
	for (int i = 0; i < matrix.size(); i++)
	{
		matrix[i][location] = value;
	}
}

void printFinished()
{
	for (int i = 0; i < matrix.size(); i++)
	{
		for (int k = 0; k < matrix[i].size(); k++)
		{
			cout << matrix[i][k] << ' ';
		}
		cout << endl;
	}
}


#include<iostream>;
#include<vector>;
using namespace std;

class command;
void columnCommand(unsigned short location, long value);
void rowCommand(unsigned short location, long value);
void printFinished();
void initate();
void runCommands();

class command
{
public:
	long value;
	short mode;
	unsigned short location;

	command()
	{
		cin >> mode;
		cin >> location;
		cin >> value;
		location--;
	}

	void runCommand()
	{
		if (mode == 1)
			rowCommand(location, value);
		else if (mode == 2)
			columnCommand(location, value);
	}

};

vector<vector<long>> matrix;
vector<command> commandsList;

int main()
{
	initate();
	runCommands();
	printFinished();
	return 0;
}

void initate()
{
	unsigned short rows;
	unsigned short columns;
	unsigned int commands;
	vector<long> tempVect;

	cin >> rows;
	cin >> columns;
	cin >> commands;

	for (int i = 0; i < rows; i++)
	{
		tempVect.clear();
		for (int k = 0; k < columns; k++)
		{
			tempVect.push_back(0);
		}
		matrix.push_back(tempVect);
	}

	for (int j = 0; j < commands; j++)
	{
		commandsList.push_back(command());
	}
}

void runCommands()
{
	vector<unsigned short> rowsUsed;
	vector<unsigned short> columnsUsed;
	bool unique;

	for (int i = commandsList.size() - 1; i >= 0; i--)
	{
		unique = true;
		if (commandsList[i].mode == 1)
		{
			for each (unsigned short location in rowsUsed)
			{
				if (commandsList[i].location == location)
				{
					unique = false;
					commandsList.erase(commandsList.begin()+i);
					break;
				}
			}
			if(unique)
			{
				rowsUsed.push_back(commandsList[i].location);
			}
		}
		else if (commandsList[i].mode == 2)
		{
			for each (unsigned short location in columnsUsed)
			{
				if (commandsList[i].location == location)
				{
					unique = false;
					commandsList.erase(commandsList.begin() + i);
					break;
				}
			}
			if (unique)
			{
				columnsUsed.push_back(commandsList[i].location);
			}
		}
	}
	for each(command comm in commandsList)
	{
		comm.runCommand();
	}
}

void rowCommand(unsigned short location, long value)
{
	for (int i = 0; i < matrix[location].size(); i++)
	{
		matrix[location][i] = value;
	}
}

void columnCommand(unsigned short location, long value)
{
	for (int i = 0; i < matrix.size(); i++)
	{
		matrix[i][location] = value;
	}
}

void printFinished()
{
	for (int i = 0; i < matrix.size(); i++)
	{
		for (int k = 0; k < matrix[i].size(); k++)
		{
			cout << matrix[i][k] << ' ';
		}
		cout << endl;
	}
}


#include<iostream>;
#include<vector>;
using namespace std;

void readCommand();
void columnCommand(unsigned short location, int value);
void rowCommand(unsigned short location, int value);
void printFinished();
void initate();

vector<vector<int>> matrix;

int main()
{
	initate();
	printFinished();
	return 0;
}

void initate()
{
	unsigned short rows;
	unsigned short columns;
	unsigned int commands;
	vector<int> tempVect;

	cin >> rows;
	cin >> columns;
	cin >> commands;

	for (int i = 0; i < columns; i++)
	{
		tempVect.clear();
		for (int k = 0; k < rows; k++)
		{
			tempVect.push_back(0);
		}
		matrix.push_back(tempVect);
	}

	for (int j = 0; j < commands; j++)
	{
		readCommand();
	}
}

void readCommand()
{
	short command;
	unsigned short location;
	int value;

	cin >> command;
	cin >> location;
	cin >> value;
	location--;

	if (command == 1)
		rowCommand(location, value);
	if (command == 2)
		columnCommand(location, value);
}

void rowCommand(unsigned short location, int value)
{
	for (int i = 0; i < matrix[location].size(); i++)
	{
		matrix[location][i] = value;
	}
}

void columnCommand(unsigned short location, int value)
{
	for (int i = 0; i < matrix.size(); i++)
	{
		matrix[i][location] = value;
	}
}

void printFinished()
{
	for (int i = 0; i < matrix.size(); i++)
	{
		for (int k = 0; k < matrix[i].size(); k++)
		{
			cout << matrix[i][k] << ' ';
		}
		cout << endl;
	}
}


#include<iostream>;
#include<vector>;
using namespace std;

void readCommand();
void columnCommand(unsigned short location, int value);
void rowCommand(unsigned short location, int value);
void printFinished();
void initate();

vector<vector<long>> matrix;

int main()
{
	initate();
	printFinished();
	return 0;
}

void initate()
{
	unsigned short rows;
	unsigned short columns;
	unsigned int commands;
	vector<long> tempVect;

	cin >> rows;
	cin >> columns;
	cin >> commands;

	for (int i = 0; i < rows; i++)
	{
		tempVect.clear();
		for (int k = 0; k < columns; k++)
		{
			tempVect.push_back(0);
		}
		matrix.push_back(tempVect);
	}

	for (int j = 0; j < commands; j++)
	{
		readCommand();
	}
}

void readCommand()
{
	short command;
	unsigned short location;
	int value;

	cin >> command;
	cin >> location;
	cin >> value;
	location--;

	if (command == 1)
		rowCommand(location, value);
	if (command == 2)
		columnCommand(location, value);
}

void rowCommand(unsigned short location, int value)
{
	for (int i = 0; i < matrix[location].size(); i++)
	{
		matrix[location][i] = value;
	}
}

void columnCommand(unsigned short location, int value)
{
	for (int i = 0; i < matrix.size(); i++)
	{
		matrix[i][location] = value;
	}
}

void printFinished()
{
	for (int i = 0; i < matrix.size(); i++)
	{
		for (int k = 0; k < matrix[i].size(); k++)
		{
			cout << matrix[i][k] << ' ';
		}
		cout << endl;
	}
}


#include<iostream>;
#include<vector>;
using namespace std;

void readCommand();
void columnCommand(unsigned short location, int value);
void rowCommand(unsigned short location, int value);
void printFinished();
void initate();

vector<vector<int>> matrix;

int main()
{
	initate();
	printFinished();
	return 0;
}

void initate()
{
	unsigned short rows;
	unsigned short columns;
	unsigned int commands;
	vector<int> tempVect;

	cin >> rows;
	cin >> columns;
	cin >> commands;

	for (int i = 0; i < rows; i++)
	{
		tempVect.clear();
		for (int k = 0; k < columns; k++)
		{
			tempVect.push_back(0);
		}
		matrix.push_back(tempVect);
	}

	for (int j = 0; j < commands; j++)
	{
		readCommand();
	}
}

void readCommand()
{
	short command;
	unsigned short location;
	int value;

	cin >> command;
	cin >> location;
	cin >> value;
	location--;

	if (command == 1)
		rowCommand(location, value);
	if (command == 2)
		columnCommand(location, value);
}

void rowCommand(unsigned short location, int value)
{
	for (int i = 0; i < matrix[location].size(); i++)
	{
		matrix[location][i] = value;
	}
}

void columnCommand(unsigned short location, int value)
{
	for (int i = 0; i < matrix.size(); i++)
	{
		matrix[i][location] = value;
	}
}

void printFinished()
{
	for (int i = 0; i < matrix.size(); i++)
	{
		for (int k = 0; k < matrix[i].size(); k++)
		{
			cout << matrix[i][k] << ' ';
		}
		cout << endl;
	}
}


#include <cstdio>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <cstring>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <set>
#include <ctime>

using namespace std;
typedef long long ll;
const ll INF = 1e14;
const int MAXN = 300000 + 5;

int n, m, k, num[100005];
int main(){
    int row[5005], col[5005];
    while(~scanf("%d%d%d", &n, &m, &k)){
        memset(row, 0, sizeof row);
        memset(col, 0, sizeof col);
        num[0] = 0;
        for(int i=1; i<=k; i++){
            int op, x;
            scanf("%d%d%d", &op, &x, &num[i]);
            if(op==1) row[x] = i;
            else col[x] = i;
        }
        for(int i=1; i<=n; i++){
            for(int j=1; j<=m; j++){
                if(row[i] > col[j]) printf("%d", num[row[i]]);
                else if(row[i] <= col[j]) printf("%d", num[col[j]]);
                if(j!=m) printf(" ");
                else puts("");
            }
        }
    }
    return 0;
}


#include <cstdio>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <cstring>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <set>
#include <ctime>

using namespace std;
typedef long long ll;
const ll INF = 1e14;
const int MAXN = 300000 + 5;

int n, m, k;
int main(){
    int row[5005], col[5005], num[5005];
    while(~scanf("%d%d%d", &n, &m, &k)){
        memset(row, 0, sizeof row);
        memset(col, 0, sizeof col);
        for(int i=1; i<=k; i++){
            int op, rl;
            scanf("%d%d%d", &op, &rl, &num[i]);
            if(op==1) row[rl] = i;
            else col[rl] = i;
        }
        for(int i=1; i<=n; i++){
            for(int j=1; j<=m; j++){
                if(row[i] > col[j]) printf("%d", num[row[i]]);
                else if(row[i] <= col[j]) printf("%d", num[col[j]]);
                if(j!=m) printf(" ");
                else puts("");
            }
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <memory.h>
#include <stack>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;

int row[5001] = {};
int col[5001] = {};

int main()
{

	memset(row,-1,sizeof(row));
	memset(col,-1,sizeof(col));

	int n, m, k;
	cin >> n >> m >> k;

	vector<pair<pair<int, int>, int>> q;

	q.resize(k);

	vector<pair<pair<int, int>, int>> res;

	for (int i = 0; i < k; ++i)
	{
		int x, num, a;
		cin >> x >> num >> a;
		--num;
		q.push_back(make_pair(make_pair(x, num), a));
	}

	reverse(q.begin(), q.end());

	for (int i = 0; i < k; ++i) {

		if (q[i].first.first == 1) //row
		{
			if (row[q[i].first.second] == -1)
			{
				row[q[i].first.second] = q[i].second;
				res.push_back(make_pair(make_pair(q[i].first.first,q[i].first.second),q[i].second));
			}

		}

		else     //column
		{

			if (col[q[i].first.second] == -1)
			{
				col[q[i].first.second] = q[i].second;
				res.push_back(make_pair(make_pair(q[i].first.first, q[i].first.second), q[i].second));
			}
		}
	}

	vector<vector<int>> g;
	g.resize(n,vector<int>(m,0));

	for (int i = res.size() - 1; i >= 0;--i)
	{
		if (res[i].first.first == 1)
		{
			for (int j = 0; j < m; ++j)
			{

				g[res[i].first.second][j] = res[i].second;
			}
		}

		else
		{

			for (int j = 0; j < n; ++j)
			{
				g[j][res[i].first.second] = res[i].second;
			}

		}
	}

	cout << endl;

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j)
			cout << g[i][j] << " ";
		cout << endl;

	}

	return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <memory.h>
#include <stack>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;

int row[5001] = {};
int col[5001] = {};

int main()
{

	memset(row,-1,sizeof(row));
	memset(col,-1,sizeof(col));

	int n, m, k;
	cin >> n >> m >> k;

	vector<pair<pair<int, int>, int>> q;

	q.resize(k);

	vector<pair<pair<int, int>, int>> res;

	for (int i = 0; i < k; ++i)
	{
		int x, num, a;
		cin >> x >> num >> a;
		--num;
		q.push_back(make_pair(make_pair(x, num), a));
	}

	reverse(q.begin(), q.end());

	for (int i = 0; i < k; ++i) {

		if (q[i].first.first == 1) //row
		{
			if (row[q[i].first.second] == -1)
			{
				row[q[i].first.first] = q[i].second;
				res.push_back(make_pair(make_pair(q[i].first.first,q[i].first.second),q[i].second));
			}

		}

		else     //column
		{

			if (col[q[i].first.second] == -1)
			{
				col[q[i].first.second] = q[i].second;
				res.push_back(make_pair(make_pair(q[i].first.first, q[i].first.second), q[i].second));
			}
		}
	}

	vector<vector<int>> g;
	g.resize(n,vector<int>(m,0));

	for (int i = res.size() - 1; i >= 0;--i)
	{
		if (res[i].first.first == 1)
		{
			for (int j = 0; j < m; ++j)
			{

				g[res[i].first.second][j] = res[i].second;
			}
		}

		else
		{

			for (int j = 0; j < n; ++j)
			{
				g[j][res[i].first.second] = res[i].second;
			}

		}
	}

	cout << endl;

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j)
			cout << g[i][j] << " ";
		cout << endl;

	}

	return 0;
}


#include "stdafx.h"
#include <iostream>
using namespace std;
long long unsigned a[5001][5001];
int main()
{
	long long unsigned n,m,k,r,c,i,j,op,color;
	cin >> n;
	cin >> m;
	cin >> k;
	for (j = 1; j <= k; j++)
	{
		cin >> op;
		if (op == 1)
		{
			cin >> r;
			cin >> color;
			for (i = 1; i <= m; i++)
				a[r][i] = color;
		}
		else
		{
			cin >> c;
			cin >> color;
			for (i = 1; i <= n; i++)
				a[i][c] = color;
		}
	}
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
			cout << a[i][j]<<" ";
		cout << endl;
	}

}


#include <iostream>
using namespace std;
struct info
{
    long long unsigned color;
    unsigned step;
};
info R[5001],C[5001];
int main()
{
    unsigned m,n,i,j,k,op;
    cin>>m>>n>>k;
    for(j=1;j<=k;j++)
    {
        cin>>op;
        if(op==1)
        {
            cin>>i;
            R[i].step=j;
            cin>>R[i].color;
        }
        else
        {
            cin>>i;
            C[i].step=j;
            cin>>C[i].color;
        }
    }
    for(i=1;i<=m;i++){
        for(j=1;j<=n;j++)
        {
            if(R[i].step>C[j].step)
                cout<<R[i].color<<" ";
            else cout<<C[j].color<<" ";
        }
        cout<<endl;
    }
}


#include <iostream>
using namespace std;
long long unsigned a[5001][5001];
int main()
{
	long long unsigned n,m,k,r,c,i,j,op,color;
	cin >> n;
	cin >> m;
	cin >> k;
	for (j = 1; j <= k; j++)
	{
		cin >> op;
		if (op == 1)
		{
			cin >> r;
			cin >> color;
			for (i = 1; i <= m; i++)
				a[r][i] = color;
		}
		else
		{
			cin >> c;
			cin >> color;
			for (i = 1; i <= n; i++)
				a[i][c] = color;
		}
	}
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
			cout << a[i][j]<<" ";
		cout << endl;
	}

}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <string>

using namespace std;

int a[5010][5010];
int n, m, k;

int main()
{
	while (scanf("%d%d%d", &n, &m, &k) != EOF)
	{
		memset(a, 0, sizeof(a));
		int type, pos, num;
		while (k--)
		{
			scanf("%d%d%d", &type, &pos, &num);
			if (type == 1)
			{
				for (int i = 1;i <= m;i++)
					a[pos][i] = num;
			}
			else
			{
				for (int i = 1;i <= n;i++)
					a[i][pos] = num;
			}
		}
		for (int i = 1;i <= n;i++)
		{
			for (int j = 1;j <= m;j++)
				printf("%d ", a[i][j]);
			printf("\n");
		}
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <string>

using namespace std;

int a[5010][5010];
int n, m, k;

int r[5010][2], c[5010][2];

int main()
{
	while (scanf("%d%d%d", &n, &m, &k) != EOF)
	{
		memset(a, 0, sizeof(a));
		memset(r, 0, sizeof(r));
		memset(c, 0, sizeof(c));

		int type, pos, num;
		for (int i = 1;i <= k;i++)
		{
			scanf("%d%d%d", &type, &pos, &num);
			if (type == 1)
			{
				r[pos][0] = num;
				r[pos][1] = i;
			}
			else
			{
				c[pos][0] = num;
				c[pos][1] = i;
			}
		}

		for (int i = 1;i <= n;i++)
		{
			for (int j = 1;j <= m;j++)
			{
				if (r[i][1] >= c[j][1])
					printf("%d ", r[i][0]);
				else
					printf("%d ", c[j][0]);
			}
			printf("\n");
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

#define MAXN 5000

int main()
{
	int n, m, k, viTri, mau, viTriCot[MAXN], viTriHang[MAXN], mauCot[MAXN] = { 0 }, mauHang[MAXN] = { 0 }, type;
	cin >> n >> m >> k;
	for (int i = 0; i < k; i++)
	{
		cin >> type >> viTri >> mau;
		if (type == 1)
		{
			viTriHang[viTri-1] = i;
			mauHang[viTri-1] = mau;
		}
		else
		{
			viTriCot[viTri-1] = i;
			mauCot[viTri-1] = mau;
		}
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			if (viTriHang[i] > viTriCot[j])
				cout << mauHang[i] << " ";
			else
				cout << mauCot[j] << " ";
		cout << endl;
	}
}


#include <iostream>
#include <algorithm>
using namespace std;

#define MAXN 5000

int main()
{
	int n, m, k, viTri, mau, viTriCot[MAXN] , viTriHang[MAXN] , mauCot[MAXN] = { 0 }, mauHang[MAXN] = { 0 }, type;
	cin >> n >> m >> k;
	for (int i = 0; i < n; i++)
		viTriHang[i] = -1;
	for (int i = 0; i < m; i++)
		viTriCot[i] = -1;
	for (int i = 0; i < k; i++)
	{
		cin >> type >> viTri >> mau;
		if (type == 1)
		{
			viTriHang[viTri-1] = i;
			mauHang[viTri-1] = mau;
		}
		else
		{
			viTriCot[viTri-1] = i;
			mauCot[viTri-1] = mau;
		}
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			if (viTriHang[i] > viTriCot[j])
				cout << mauHang[i] << " ";
			else
				cout << mauCot[j] << " ";
		cout << endl;
	}
}


#include <iomanip>
#include <iostream>
#include <functional>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <string>
using namespace std;
int v[100000];

int main(){
	int n, m, k;
	cin >> n >> m >> k;
	int r[5000] = { -1 }; long long a[5000]; int c[5000] = { -1 };
		for (int i = 0; i < k; i++){
			int z;
			cin >> z;
			if (z == 1){
				cin >> r[i];
				c[i] = -1;
			}
			else {
				cin >> c[i];
				r[i] = -1;
			}
			cin >> a[i];
	}
		long long omar[5000] = { 0 }, baker[5000] = { 0 };
		int jde[5000] = { 0 }, lorno[5000] = { 0 };
		int lora = 0;
		for (int i = k - 1; i >= 0; i--){
			//cout << c[i] << " " << r[i] << " " << i << "\n";
			int fafi = r[i];
			lora++;
			if (r[i] != -1){
				if (omar[fafi] == 0){
					omar[fafi] = a[i];
					jde[fafi] = i+1;
					//cout << omar[fafi] << " " << fafi << "r\n";
				}
			}
			fafi = c[i];
			if (c[i] != -1){

				if (baker[fafi] == 0 && c[i] != -1){
					baker[fafi] = a[i];
					lorno[fafi] = i+1;
					//cout << baker[fafi] << " " << fafi << "c\n";
				}

			}
		}

		for (int i = 1; i <= n; i++){

			for (int j = 1; j <= m; j++){
				if (lorno[j] + jde[i] == 0)cout << 0;
				else if (lorno[j] > jde[i])cout << baker[j];
				else cout << omar[i];
				cout << " ";
			} cout << "\n";

		}

}


#include <iomanip>
#include <iostream>
#include <functional>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <string>
using namespace std;
int v[100000];

const int fuck = 150000;
int main(){
	int n, m, k;
	cin >> n >> m >> k;
	int r[fuck] = { -1 }; long long a[fuck]; int c[fuck] = { -1 };
		for (int i = 0; i < k; i++){
			int z;
			cin >> z;
			if (z == 1){
				cin >> r[i];
				c[i] = -1;
			}
			else {
				cin >> c[i];
				r[i] = -1;
			}
			cin >> a[i];
	}
		long long omar[fuck] = { 0 }, baker[fuck] = { 0 };
		int jde[fuck] = { 0 }, lorno[fuck] = { 0 };
		int lora = 0;
		for (int i = k - 1; i >= 0; i--){
			//cout << c[i] << " " << r[i] << " " << i << "\n";
			int fafi = r[i];
			lora++;
			if (r[i] != -1){
				if (omar[fafi] == 0){
					omar[fafi] = a[i];
					jde[fafi] = i+1;
					//cout << omar[fafi] << " " << fafi << "r\n";
				}
			}
			fafi = c[i];
			if (c[i] != -1){

				if (baker[fafi] == 0 && c[i] != -1){
					baker[fafi] = a[i];
					lorno[fafi] = i+1;
					//cout << baker[fafi] << " " << fafi << "c\n";
				}

			}
		}

		for (int i = 1; i <= n; i++){

			for (int j = 1; j <= m; j++){
				if (lorno[j] + jde[i] == 0)cout << 0;
				else if (lorno[j] > jde[i])cout << baker[j];
				else cout << omar[i];
				cout << " ";
			} cout << "\n";

		}

}


 #include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>

using namespace std;
const int N=100001;
#define M 101
struct node{
	long long int a,b;
};
node ang[N];
node anb[N];
bool cmp1(node x,node y){
  return x.a<y.a;
}
bool cmp2(int a,int b){
	return a>b;
}
int t;
int cmp(int b[1005])
{
    for(int i=0; i<t; i++)
    {
        if(b[i]!=0)
            return 1;
    }
    return 0;
}

int main()
{
	int n,m,k;
	long long int a[2][5001];
	long long int ro[5001];
	long long int col[5001];

	long long int flag,rowcolum,num;
	memset(ro,0,sizeof(ro));
	memset(col,0,sizeof(col));
	cin>>n>>m>>k;
	for (int i = 1; i <= k; i++)
	{
		cin>>flag>>rowcolum>>num;
		if (flag==1)
		{
			a[0][rowcolum]=i;
			ro[rowcolum]=num;
		}else
		{
			a[1][rowcolum]=i;
			col[rowcolum]=num;
		}
	}
	for(int i = 1; i <= n; i++){
    for(int j = 1; j <= m; j++){
        long long int x = 0;
        if(a[0][i] > a[1][j]) x = ro[i];
		else x = col[j];
		cout<<x<<" ";
    }
	cout<<endl;
   }

	cin>>n;
	return 0;
}


 #include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>

using namespace std;
const int N=100001;
#define M 101
struct node{
	long long int a,b;
};
node ang[N];
node anb[N];
bool cmp1(node x,node y){
  return x.a<y.a;
}
bool cmp2(int a,int b){
	return a>b;
}
int t;
int cmp(int b[1005])
{
    for(int i=0; i<t; i++)
    {
        if(b[i]!=0)
            return 1;
    }
    return 0;
}

int main()
{
	int n,m,k;
	long long int a[2][5001];
	long long int ro[5001];
	long long int col[5001];

	long long int flag,rowcolum,num;
	memset(ro,0,sizeof(ro));
	memset(col,0,sizeof(col));
	cin>>n>>m>>k;
	for (int i = 1; i <= k; i++)
	{
		cin>>flag>>rowcolum>>num;
		if (flag==1)
		{
			a[0][rowcolum]=i;
			ro[rowcolum]=num;
		}else
		{
			a[1][rowcolum]=i;
			col[rowcolum]=num;
		}
	}
	for(int i = 1; i <= n; i++){
    for(int j = 1; j <= m; j++){
        int x = 0;
        if(a[0][i] > a[1][j]) x = ro[i];
		else x = col[j];
		cout<<x<<" ";
    }
	cout<<endl;
   }

	cin>>n;
	return 0;
}


 #include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>

using namespace std;
const int N=100001;
#define M 101
struct node{
	long long int a,b;
};
node ang[N];
node anb[N];
bool cmp1(node x,node y){
  return x.a<y.a;
}
bool cmp2(int a,int b){
	return a>b;
}
int t;
int cmp(int b[1005])
{
    for(int i=0; i<t; i++)
    {
        if(b[i]!=0)
            return 1;
    }
    return 0;
}

int main()
{
	int n,m,k;
	long long int a[2][5001];
	long long int ro[5001];
	long long int col[5001];

	int flag,rowcolum,num;
	memset(ro,0,sizeof(ro));
	memset(col,0,sizeof(col));
	cin>>n>>m>>k;
	for (int i = 1; i <= k; i++)
	{
		cin>>flag>>rowcolum>>num;
		if (flag==1)
		{
			a[0][rowcolum]=i;
			ro[rowcolum]=num;
		}else
		{
			a[1][rowcolum]=i;
			col[rowcolum]=num;
		}
	}
	for(int i = 1; i <= n; i++){
    for(int j = 1; j <= m; j++){
        int x = 0;
        if(a[0][i] > a[1][j]) x = ro[i];
		else x = col[j];
		cout<<x<<" ";
    }
	cout<<endl;
   }

	cin>>n;
	return 0;
}


 #include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>

using namespace std;
const int N=100001;
#define M 101
struct node{
	long long int a,b;
};
node ang[N];
node anb[N];
bool cmp1(node x,node y){
  return x.a<y.a;
}
bool cmp2(int a,int b){
	return a>b;
}
int t;
int cmp(int b[1005])
{
    for(int i=0; i<t; i++)
    {
        if(b[i]!=0)
            return 1;
    }
    return 0;
}

int main()
{
	int n,m,k;
	long long int a[2][5005];
	long long int ro[5005];
	long long int col[5005];

	long long int flag,rowcolum,num;
	memset(ro,0,sizeof(ro));
	memset(col,0,sizeof(col));
	cin>>n>>m>>k;
	for (int i = 1; i <= k; i++)
	{
		cin>>flag>>rowcolum>>num;
		if (flag==1)
		{
			a[0][rowcolum]=i;
			ro[rowcolum]=num;
		}else
		{
			a[1][rowcolum]=i;
			col[rowcolum]=num;
		}
	}
	for(int i = 1; i <= n; i++){
    for(int j = 1; j <= m; j++){
        long long int x = 0;
        if(a[0][i] > a[1][j]) x = ro[i];
		else if(a[0][i]<a[1][j]) x = col[j];
		cout<<x<<" ";
    }
	cout<<endl;
   }

	cin>>n;
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;

int main (void)
{
    int num[5005][5005];
   int ma[2][5005];
   int ro[5005], col[5005];
   int n, m, k;
   cin>>n>>m>>k;
   int a, b, c;
   for(int i = 1; i <= k; i++){
        cin>>a>>b>>c;
        if(a == 1){
         ro[b] = c;
         ma[0][b] = i;
        }
        else{
          col[b] = c;
          ma[1][b] = i;
        }
   }
   for(int i = 1; i <= n; i++){
    for(int j = 1; j <= m; j++){
        int a = 0;
        if(ma[0][i] > ma[1][j]) a = ro[i];
        else a = col[j];
        printf("%d%c",a, j == m?'\n':' ');
    }
   }
   cin>>n;
}


 #include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>

using namespace std;
const int N=100001;
#define M 101
struct node{
	long long int a,b;
};
node ang[N];
node anb[N];
bool cmp1(node x,node y){
  return x.a<y.a;
}
bool cmp2(int a,int b){
	return a>b;
}
int t;
int cmp(int b[1005])
{
    for(int i=0; i<t; i++)
    {
        if(b[i]!=0)
            return 1;
    }
    return 0;
}

int main()
{
	int n,m,k;
	long long int b[5001][5001];

	long long int flag,rowcolum,num;

	cin>>n>>m>>k;
	memset(b,0,sizeof(b));
	for (int i = 0; i < k; i++)
	{
		cin>>flag>>rowcolum>>num;
		if (flag==1)
		{
			for (int i = 0; i <m ; i++)
			{
				b[rowcolum-1][i]=num;
			}
		}else
		{
			for (int i = 0; i < n; i++)
			{
				b[i][rowcolum-1]=num;
			}
		}

	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cout<<b[i][j]<<" ";
		}
		cout<<endl;

	}

	cin>>n;
	return 0;
}


 #include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>

using namespace std;
const int N=100001;
#define M 101
struct node{
	long long int a,b;
};
node ang[N];
node anb[N];
bool cmp1(node x,node y){
  return x.a<y.a;
}
bool cmp2(int a,int b){
	return a>b;
}
int t;
int cmp(int b[1005])
{
    for(int i=0; i<t; i++)
    {
        if(b[i]!=0)
            return 1;
    }
    return 0;
}

int main()
{
	int n,m,k;
	long long int a[2][5005];
	long long int ro[5005];
	long long int col[5005];

	long long int flag,rowcolum,num;
	memset(ro,0,sizeof(ro));
	memset(col,0,sizeof(col));
	memset(a,0,sizeof(a));
	cin>>n>>m>>k;
	for (int i = 1; i <= k; i++)
	{
		cin>>flag>>rowcolum>>num;
		if (flag==1)
		{
			a[0][rowcolum]=i;
			ro[rowcolum]=num;
		}else
		{
			a[1][rowcolum]=i;
			col[rowcolum]=num;
		}
	}
	for(int i = 1; i <= n; i++){
    for(int j = 1; j <= m; j++){
        long long int x = 0;
        if(a[0][i] > a[1][j]) x = ro[i];
		else if(a[0][i]<a[1][j]) x = col[j];
		cout<<x<<" ";
    }
	cout<<endl;
   }

	cin>>n;
	return 0;
}


#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#define ll long long
using namespace std;
ll n, m,k;
int main()
{
	cin >> n >> m>>k;
	map<ll, pair<ll, ll>>s,t;
	for (int i = 1; i <= k; i++)
	{
		ll a, b, c;
		cin >> a >> b >> c;
		if (a == 1)
		{
			s[b] = make_pair(c,i);
		}
		else
		{
			t[b] = make_pair(c, i);
		}
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			if (s[i].second > t[j].second)
			{
				cout << s[i].first;
			}
			else
			{
				cout << t[j].first;
			}
			cout << " ";
		}
		cout << endl;
	}
	//system("pause");
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{

	long long d, b, c,n, m, k, a[5010][5010];
	cin >> n>> m>> k;
	memset(a, 0, sizeof(a));

	for (int i = 0; i < k; i++)
	{
		cin >> d >> b >> c;
		if (d == 1)
		{
			for (int j = 0; j < m; j++)
			{
				a[b-1][j] = c;
			}
		}
		else if (d==2)
		{
			for (int j = 0; j < n; j++)
			{
				a[j][b-1] = c;
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{

				cout << a[i][j] << " ";

		}

			cout << endl;

	}

	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int n, m, k,a[500][500],d,b,c;
	cin >> n>> m>> k;
	memset(a, 0, sizeof(a));

	for (int i = 0; i < k; i++)
	{
		cin >> d >> b >> c;
		if (d == 1)
		{
			for (int j = 0; j < m; j++)
			{
				a[b-1][j] = c;
			}
		}
		else if (d==2)
		{
			for (int j = 0; j < n; j++)
			{
				a[j][b-1] = c;
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (j == m - 1)
			{
				cout << a[i][j] ;
			}
			else
			{
				cout << a[i][j] << " ";
			}
		}
		if (i != n - 1)
		{
			cout << endl;
		}
	}
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int n, m, k,a[5001][5001],d,b,c;
	cin >> n>> m>> k;
	memset(a, 0, sizeof(a));

	for (int i = 0; i < k; i++)
	{
		cin >> d >> b >> c;
		if (d == 1)
		{
			for (int j = 0; j < m; j++)
			{
				a[b-1][j] = c;
			}
		}
		else
		{
			for (int j = 0; j < n; j++)
			{
				a[j][b-1] = c;
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cout << a[i][j] << " ";
		}
		cout << endl;
	}

	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{

	long long d, b, c,n, m, k, a[5010][5010];
	cin >> n>> m>> k;
	memset(a, 0, sizeof(a));

	for (int i = 0; i < k; i++)
	{
		cin >> d >> b >> c;
		if (d == 1)
		{
			for (int j = 0; j < m; j++)
			{
				a[b-1][j] = c;
			}
		}
		else if (d==2)
		{
			for (int j = 0; j < n; j++)
			{
				a[j][b-1] = c;
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{

				cout << a[i][j] << " ";

		}

			cout << endl;

	}
	system("pause");
	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<map>
#include<stack>
#include<queue>
using namespace std;
struct nn
{
	int f, d, p = 0;

};
int main()
{
	int n, k, m, h[5010][5010];
	memset(h, 0, sizeof(h));
	nn a[100010];
	map<pair<int, int>, int> x;
	queue <nn> u;
	cin >> n >> m >> k;
	for (int i = 0; i < k; i++)
	{
		cin >> a[i].f >> a[i].d >> a[i].p;
		x[make_pair(a[i].f, a[i].d)]++;
		u.push(a[i]);
	}
	while (!u.empty())
	{
		if (x[make_pair(u.front().f, u.front().d)] >= 1)
		{
			if (u.front().f == 1)
			{
				for (int j = 0; j < m; j++)
				{
					h[u.front().d - 1][j] = u.front().p;
				}
			}
			else
			{
				for (int j = 0; j < n; j++)
				{
					h[j][u.front().d - 1] = u.front().p;
				}
			}
			x[make_pair(u.front().d, u.front().f)] = -1;
		}
		u.pop();
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cout << h[i][j] << " ";
		}
		cout << endl;
	}

	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int n, m, k,a[500][500],d,b,c;
	cin >> n>> m>> k;
	memset(a, 0, sizeof(a));
	for (int i = 0; i < k; i++)
	{
		cin >> d >> b >> c;
		if (d == 1)
		{
			for (int j = 0; j < n; j++)
			{
				a[b-1][j] = c;
			}
		}
		else
		{
			for (int j = 0; j < m; j++)
			{
				a[j][b-1] = c;
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cout << a[i][j] << " ";
		}
		cout << endl;
	}

	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<map>
#include<stack>
#include<queue>
using namespace std;
class mynn
{
public:
	int f, d, p;

};
int main()
{
	int n, k, m, h[5010][5010];
	memset(h, 0, sizeof(h));
	mynn a[100010];
	map<pair<int, int>, int> x;
	queue <mynn> u;
	cin >> n >> m >> k;
	for (int i = 0; i < k; i++)
	{
		cin >> a[i].f >> a[i].d >> a[i].p;
		x[make_pair(a[i].f, a[i].d)]++;
		u.push(a[i]);
	}
	while (!u.empty())
	{
		if (x[make_pair(u.front().f, u.front().d)] >= 1)
		{
			if (u.front().f == 1)
			{
				for (int j = 0; j < m; j++)
				{
					h[u.front().d - 1][j] = u.front().p;
				}
			}
			else
			{
				for (int j = 0; j < n; j++)
				{
					h[j][u.front().d - 1] = u.front().p;
				}
			}
			x[make_pair(u.front().d, u.front().f)] = -1;
		}
		u.pop();
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cout << h[i][j] << " ";
		}
		cout << endl;
	}

	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<map>
#include<stack>
#include<queue>
using namespace std;
struct nn
{
	int f, d, p = 0;

};
int main()
{
	int n, k, m, h[510][110];
	memset(h, 0, sizeof(h));

	nn a[1010];
	map<pair<int, int>, int> x;
	queue <nn> u;
	cin >> n >> m >> k;
	for (int i = 0; i < k; i++)
	{
		cin >> a[i].f >> a[i].d >> a[i].p;
		x[make_pair(a[i].f, a[i].d)]++;
		u.push(a[i]);
	}
	while (!u.empty())
	{
		if (x[make_pair(u.front().f, u.front().d)] >= 1)
		{
			if (u.front().f == 1)
			{
				for (int j = 0; j < m; j++)
				{
					h[u.front().d - 1][j] = u.front().p;
				}
			}
			else
			{
				for (int j = 0; j < n; j++)
				{
					h[j][u.front().d - 1] = u.front().p;
				}
			}
			x[make_pair(u.front().d, u.front().f)] = -1;
		}
		u.pop();
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cout << h[i][j] << " ";
		}
		cout << endl;
	}

	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
bool comp(const int a, const int b)
{
	return a > b;
}
int main()
{
	int n, m,a[1000000],b,c;
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	for (int i = 0; i < m; i++)
	{
		cin >> b>> c;
		if (b == 1)
		{
			sort(a, a + c);
		}
		else
		{
			sort(a , a+c,comp);
		}
	}
	for (int i = 0; i < n; i++)
	{
		if (i == n - 1)
		{
			cout << a[i];
		}
		else
		{

			cout << a[i] << " ";
		}
	}

	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<map>
#include<stack>
#include<queue>
using namespace std;
class mynn
{
public:
	int f, d, p;

};
int main()
{
	int n, k, m, h[5010][5010];
	memset(h, 0, sizeof(h));
	mynn a[100010];
	map<pair<int, int>, int> x;
	stack <mynn> u,v;
	cin >> n >> m >> k;
	for (int i = 0; i < k; i++)
	{
		cin >> a[i].f >> a[i].d >> a[i].p;
		x[make_pair(a[i].f, a[i].d)]++;
		u.push(a[i]);
	}
	while (!u.empty())
	{
		if (x[make_pair(u.top().f, u.top().d)] >= 1)
		{

			v.push(u.top());
			x[make_pair(u.top().d, u.top().f)] = -1;
			u.pop();
		}

	}
	while (!v.empty())
	{
			if (v.top().f == 1)
			{
				for (int j = 0; j < m; j++)
				{
					h[v.top().d - 1][j] = v.top().p;
				}
			}
			else
			{
				for (int j = 0; j < n; j++)
				{
					h[j][v.top().d - 1] = v.top().p;
				}
			}
		    v.pop();
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cout << h[i][j] << " ";
		}
		cout << endl;
	}
	//system("pause");
	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<map>
#include<stack>
#include<queue>
using namespace std;
struct nn
{
	int f, d, p = 0;

};
int main()
{
	int n, k, m, h[5010][1010];
	memset(h, 0, sizeof(h));

	nn a[100010];
	map<pair<int, int>, int> x;
	queue <nn> u;
	cin >> n >> m >> k;
	for (int i = 0; i < k; i++)
	{
		cin >> a[i].f >> a[i].d >> a[i].p;
		x[make_pair(a[i].f, a[i].d)]++;
		u.push(a[i]);
	}
	while (!u.empty())
	{
		if (x[make_pair(u.front().f, u.front().d)] >= 1)
		{
			if (u.front().f == 1)
			{
				for (int j = 0; j < m; j++)
				{
					h[u.front().d - 1][j] = u.front().p;
				}
			}
			else
			{
				for (int j = 0; j < n; j++)
				{
					h[j][u.front().d - 1] = u.front().p;
				}
			}
			x[make_pair(u.front().d, u.front().f)] = -1;
		}
		u.pop();
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cout << h[i][j] << " ";
		}
		cout << endl;
	}

	return 0;
}


//phoenix

#include <bits/stdc++.h>
using namespace std;

#define spdio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define pp pop_back()
#define pf push_front
#define ppf pop_front()
#define ff first
#define ss second
#define mp make_pair
#define fr(i,j,n) for(ll i=j; i<n; i++)
#define rfr(i,j,n) for(ll i=n-1; i>=j; i--)

typedef long double ld;
typedef long long int ll;

//getline(cin,string);
const ll inf = (ll) 1e18 + 19;
const ll mod = (ll) 1e9  +  7;

void cazz() {
    ll t=1,i,j,n;
    ll m,x,y;
    cin>>n>>m>>x>>y;
    ll a[n],b[m],k;
    vector <pair<ll,ll> > v;
    fr(i,0,n) cin>>a[i];
    fr(i,0,m) cin>>b[i];
    sort(a,a+n);
    sort(b,b+m);
    k=0;
    fr(i,0,m) {
        fr(j,k,n) {
            if(a[j]-x<=b[i] && a[j]+y>=b[i]) {
                v.pb({j+1,i+1});
                k=j+1;
                break;
            }
            if(a[j]+y>b[i]) break;
        }
    }
    cout<<v.size()<<"\n";
    fr(i,0,v.size()) cout<<v[i].ff<<' '<<v[i].ss<<"\n";
}

int main() {
    spdio;
	cazz();
	return 0;
}


//phoenix

#include <bits/stdc++.h>
using namespace std;

#define spdio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define pp pop_back()
#define pf push_front
#define ppf pop_front()
#define ff first
#define ss second
#define mp make_pair
#define fr(i,j,n) for(ll i=j; i<n; i++)
#define rfr(i,j,n) for(ll i=n-1; i>=j; i--)

typedef long double ld;
typedef long long int ll;

//getline(cin,string);
const ll inf = (ll) 1e18 + 19;
const ll mod = (ll) 1e9  +  7;

void cazz() {
    ll t=1,i,j,n;
    ll m,x,y;
    cin>>n>>m>>x>>y;
    ll a[n],b[m],k;
    vector <pair<ll,ll> > v;
    fr(i,0,n) cin>>a[i];
    fr(i,0,m) cin>>b[i];
    bool vis[m]={};
    ll p=0,q=m,ind,l,r;
    fr(i,0,n) {
        ind=-1;
        l=p,r=q;
        while(l<=r) {
            ll mid=(l+r)/2;
            if(a[i]-x<=b[mid] && a[i]+y>=b[mid]) {
                ind=mid;
                break;
            }
            else if(a[i]-x>b[mid]) r=mid-1;
            else if(a[i]+y<b[mid]) l=mid+1;
        }
        if(ind!=-1) {
            fr(j,p,ind+1) {
                if(!vis[j] && a[i]-x<=b[j] && a[i]+y>=b[j]) {
                    v.pb({i+1,j+1});
                    vis[j]=1;
                    p=j;//cout<<p<<' ';//cout<<a[j]<<' '<<b[i]<<"\n";
                    break;
                }
            }
        }
    }
    cout<<v.size()<<"\n";
    fr(i,0,v.size()) cout<<v[i].ff<<' '<<v[i].ss<<"\n";
}

int main() {
    spdio;
	cazz();
	return 0;
}


//phoenix

#include <bits/stdc++.h>
using namespace std;

#define spdio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define pp pop_back()
#define pf push_front
#define ppf pop_front()
#define ff first
#define ss second
#define mp make_pair
#define fr(i,j,n) for(ll i=j; i<n; i++)
#define rfr(i,j,n) for(ll i=n-1; i>=j; i--)

typedef long double ld;
typedef long long int ll;

//getline(cin,string);
const ll inf = (ll) 1e18 + 19;
const ll mod = (ll) 1e9  +  7;

void cazz() {
    ll t=1,i,j,n;
    ll m,x,y;
    cin>>n>>m>>x>>y;
    ll a[n],b[m],k;
    vector <pair<ll,ll> > v;
    fr(i,0,n) cin>>a[i];
    fr(i,0,m) cin>>b[i];
    sort(a,a+n);
    sort(b,b+m);
    k=0;
    fr(i,0,m) {
        fr(j,k,n) {
            if(a[j]-x<=b[i] && a[j]+y>=b[i]) {
                v.pb({j+1,i+1});
                k=i+1;
                break;
            }
        }
    }
    cout<<v.size()<<"\n";
    fr(i,0,v.size()) cout<<v[i].ff<<' '<<v[i].ss<<"\n";
}

int main() {
    spdio;
	cazz();
	return 0;
}


//phoenix

#include <bits/stdc++.h>
using namespace std;

#define spdio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define pp pop_back()
#define pf push_front
#define ppf pop_front()
#define ff first
#define ss second
#define mp make_pair
#define fr(i,j,n) for(ll i=j; i<n; i++)
#define rfr(i,j,n) for(ll i=n-1; i>=j; i--)

typedef long double ld;
typedef long long int ll;

//getline(cin,string);
const ll inf = (ll) 1e18 + 19;
const ll mod = (ll) 1e9  +  7;

void cazz() {
    ll t=1,i,j,n;
    ll m,x,y;
    cin>>n>>m>>x>>y;
    ll a[n],b[m],k;
    vector <pair<ll,ll> > v;
    fr(i,0,n) cin>>a[i];
    fr(i,0,m) cin>>b[i];
    bool vis[m]={};
    ll p=0,q=m,ind,l,r;
    fr(i,0,n) {
        ind=-1;
        l=p,r=q;
        while(l<=r) {
            ll mid=(l+r)/2;
            if(a[i]-x<=b[mid] && a[i]+y>=b[mid]) {
                ind=mid;
                break;
            }
            else if(a[i]-x>b[mid]) l=mid+1;
            else if(a[i]+y<b[mid]) r=mid-1;
        }
        if(ind!=-1) {
            fr(j,p,ind+1) {
                if(!vis[j] && a[i]-x<=b[j] && a[i]+y>=b[j]) {
                    v.pb({i+1,j+1});
                    vis[j]=1;
                    p=j+1;//cout<<a[i]<<' '<<b[j]<<"\n";
                    break;
                }
            }
        }
    }
    cout<<v.size()<<"\n";
    fr(i,0,v.size()) cout<<v[i].ff<<' '<<v[i].ss<<"\n";
}

int main() {
    spdio;
	cazz();
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<exception>
using namespace std;
typedef long long ll;
typedef pair<long long int,long long int> P;
typedef pair<long long int,P> P1;
typedef pair<P,P> P2;
typedef map<long long int,long long int> m;
#define pu push
#define pb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())

int main(){
	ll n,m,x,y;
	cin>>n>>m>>x>>y;
	ll a[n];
	ll b[m];
	rep(i,n) cin>>a[i];
	rep(i,m) cin>>b[i];
	ll i=0,j=0;
	std::vector<P> v;
	while(i<n && j<m){
		if(b[j]>=(a[i]-x) && b[j]<=(a[i]+y)){
			v.pb(mp(i,j));
			i++;
			j++;
		}
		else{
			if(a[i]<b[j]) i++;
			else j++;
		}
	}
	cout<<v.size()<<endl;
	rep(i,v.size()){
		cout<<v[i].first+1<<" "<<v[i].second+1<<endl;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<exception>
using namespace std;
typedef long long ll;
typedef pair<long long int,long long int> P;
typedef pair<long long int,P> P1;
typedef pair<P,P> P2;
typedef map<long long int,long long int> m;
#define pu push
#define pb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())

int main(){
	ll n,m,x,y;
	cin>>n>>m>>x>>y;
	ll a[n];
	ll b[m];
	rep(i,n) cin>>a[i];
	rep(i,m) cin>>b[i];
	ll i=0,j=0;
	std::vector<P> v;
	while(i<n && j<m){
		if(b[j]>=(a[i]-x) && b[j]<=(a[i]+y)){
			v.pb(mp(i,j));
			i++;
			j++;
		}
		else{
			i++;
		}
	}
	cout<<v.size()<<endl;
	rep(i,v.size()){
		cout<<v[i].first+1<<" "<<v[i].second+1<<endl;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<exception>
using namespace std;
typedef long long ll;
typedef pair<long long int,long long int> P;
typedef pair<long long int,P> P1;
typedef pair<P,P> P2;
typedef map<long long int,long long int> m;
#define pu push
#define pb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())

int main(){
	ll n,m,x,y;
	cin>>n>>m>>x>>y;
	ll a[n];
	ll b[m];
	rep(i,n) cin>>a[i];
	rep(i,m) cin>>b[i];
	ll i=0,j=0;
	std::vector<P> v;
	while(i<n && j<m){
		if(a[i]>=(b[j]-x) && a[i]<=(b[j]+y)){
			v.pb(mp(i,j));
			i++;
			j++;
		}
		else{
			i++;
		}
	}
	cout<<v.size()<<endl;
	rep(i,v.size()){
		cout<<v[i].first+1<<" "<<v[i].second+1<<endl;
	}
}


#include <bits/stdc++.h>
#define FOR(a, b) for(int i = a ; i < b; i++)
#define FORI(i, a, b) for(i = a; i < b ; i++)
#define all(x) x.begin(), x.end()
using namespace std;
int main() {
    int n, m, x, y;
    cin >> n >> m >> x >> y;
    vector<int> A(n), B(m);
    FOR(0, n) {
        cin >> A[i];
    }
    FOR(0, m) {
        cin >> B[i];
    }
    vector<pair<int, int> > ans;
    for(int i = 0 , j = 0; i < n && j < m; i++) {
        while(j < m and B[j] < A[i] - x)++j;
        if(j >= m)break;
        if(B[j] <= A[i] + y) {
            ans.push_back({i + 1, j + 1});
            ++j;
        }
    }
    cout << ans.size() << endl;
    for(auto it : ans) {
        cout << it.first << " " << it.second << endl;
    }
    return 0;
}