Kris works in a large company "Blake Technologies". As a best engineer of the company he was assigned a task to develop a printer that will be able to print horizontal and vertical strips. First prototype is already built and Kris wants to tests it. He wants you to implement the program that checks the result of the printing.

Printer works with a rectangular sheet of paper of size n × m. Consider the list as a table consisting of n rows and m columns. Rows are numbered from top to bottom with integers from 1 to n, while columns are numbered from left to right with integers from 1 to m. Initially, all cells are painted in color 0.

Your program has to support two operations:   Paint all cells in row r_{i} in color a_{i};  Paint all cells in column c_{i} in color a_{i}. 

If during some operation i there is a cell that have already been painted, the color of this cell also changes to a_{i}.

Your program has to print the resulting table after k operation.


-----Input-----

The first line of the input contains three integers n, m and k (1  ≤  n,  m  ≤ 5000, n·m ≤ 100 000, 1 ≤ k ≤ 100 000) — the dimensions of the sheet and the number of operations, respectively.

Each of the next k lines contains the description of exactly one query:   1 r_{i} a_{i} (1 ≤ r_{i} ≤ n, 1 ≤ a_{i} ≤ 10^9), means that row r_{i} is painted in color a_{i};  2 c_{i} a_{i} (1 ≤ c_{i} ≤ m, 1 ≤ a_{i} ≤ 10^9), means that column c_{i} is painted in color a_{i}. 


-----Output-----

Print n lines containing m integers each — the resulting table after all operations are applied.


-----Examples-----
Input
3 3 3
1 1 3
2 2 1
1 2 2

Output
3 1 3 
2 2 2 
0 1 0 

Input
5 3 5
1 1 1
1 3 1
1 5 1
2 1 1
2 3 1

Output
1 1 1 
1 0 1 
1 1 1 
1 0 1 
1 1 1 



-----Note-----

The figure below shows all three operations for the first sample step by step. The cells that were painted on the corresponding step are marked gray.  [Image]

Difficulty: interview

Starter code:

Solutions:
#!/usr/bin/env python3

n, m, k = [int(x) for x in input().split()]

row = [(0, -1)] * n
col = [(0, -1)] * m
for i in range(0, k):
    t, num, color = [int(x) for x in input().split()]
    num -= 1
    if t == 1:
        row[num] = (color, i)
    else:
        assert t == 2
        col[num] = (color, i)

for r in row:
    for c in col:
        if c[1] > r[1]:
            print(c[0], end=' ')
        else:
            print(r[0], end=' ')
    print()



n, m, k = map(int, input().split())
lines = [[0, 0] for i in range(n)]
columns = [[0, 0] for i in range(m)]
for i in range(k):
    task, num, color = map(int, input().split())
    if task == 1:
        lines[num-1] = [color, i+1]
    else:
        columns[num-1] = [color, i+1]
for i in range(n):
    for j in range(m):
        if lines[i][1] > columns[j][1]:
            print(lines[i][0], end = ' ')
        else:
            print(columns[j][0], end = ' ')
        
    print('')


def main():
    from sys import stdin, stdout
    n, m, op = list(map(int, stdin.readline().split()))
    hor = dict()
    vert = dict()
    for i in range(op):
        a, b, c = list(map(int, input().split()))
        b -= 1
        if a == 1:
            hor[b] = (c, i)
        else:
            vert[b] = (c, i)
    for i in range(n):
        for j in range(m):
            if i not in hor:
                hor[i] = (0, -1)
            if j not in vert:
                vert[j] = (0, -1)
            if hor[i][1] > vert[j][1]:
                stdout.write(str(hor[i][0]) + ' ')
            else:
                stdout.write(str(vert[j][0]) + ' ')
        stdout.write('\n')
                

main()


# You lost the game.
n, m, k = map(int, input().split())
Q = [list(map(int, input().split())) for _ in range(k)]

T = [[0 for _ in range(m)] for _ in range(n)]
R = [[0,-1] for _ in range(n)]
C = [[0,-1] for _ in range(m)]

"""for i in range(n):
    for j in range(m):
        for l in range(k-1,-1,-1):
            if (Q[l][0] == 1 and Q[l][1] == i+1) or (Q[l][0] == 2 and Q[l][1] == j+1):
                T[i][j] = Q[l][2]
                break"""

for i in range(k):
    q = Q[i]
    if q[0] == 1:
        R[q[1]-1] = [q[2],i]
    else:
        C[q[1]-1] = [q[2],i]

for i in range(n):
    for j in range(m):
        if R[i][1] > C[j][1]:
            T[i][j] = R[i][0]
        else:
            T[i][j] = C[j][0]

for i in range(n):
    for j in range(m):
        print(T[i][j],end=" ")
    print()

    


n, m, k = [int(x) for x in input().split()]
r = [(0, -1) for i in range(n)]
c = [(0, -1)for i in range(m)]

for i in range(k):
    ax, crd, clr = [int(x) for x in input().split()]
    if ax == 1: r[crd - 1] = (clr, i)
    else:       c[crd - 1] = (clr, i) # ax === 2
for i in range(n):
    s = []
    for j in range(m):
        clr = r[i][0] if r[i][1] > c[j][1] else c[j][0]
        s.append(str(clr))
    print(" ".join(s))


def main():
    n, m, k = map(int, input().split())
    B1 = [[0, 0, 0, 0] for i in range(n)]
    B2 = [[0, 0, 0, 0] for i in range(m)]
    for i in range(k):
        a, b, c = map(int, input().split())
        b -= 1
        if a == 1:
            B1[b] = [i, a, b, c]
        else:
            B2[b] = [i, a, b, c]

    C = sorted(B1 + B2, key = lambda x: x[0])

    A = [[0] * m for i in range(n)]
        
    for i in range(len(C)):
        x, a, b, c = C[i]
        if a == 1:
            A[b] = [c] * m
        elif a == 2:
            for j in range(n):
                A[j][b] = c
    for row in A:
        print(' '.join(map(str, row)))


main()

n,m,k = map(int,input().split())
t = [[0]*m for i in range(n)]
used = set()
a = []
for i in range(k):
    q, b, c = tuple(map(int,input().split()))
    b = b - 1
    a.append((q, b, c))

a = a[::-1]
for i in range(k):
    if a[i][0:2] not in used:
        if a[i][0] == 1:
            for s in range(m):
                if t[a[i][1]][s] == 0:
                    t[a[i][1]][s] = a[i][2]
        else:
            for s in range(n):
                if t[s][a[i][1]] == 0:
                    t[s][a[i][1]] = a[i][2]
        used.add(a[i][0:2])
for i in range(n):
    for j in range(m):
        print(t[i][j], end = ' ')
    print()
                

n, m , k = map(int, input().split())
a = [(0, -1)] * n
b = [(0, -1)] * m
for i in range(k):
    t, pl, col = map(int, input().split())
    if t == 1:
        a[pl - 1] = (col, i)
    else:
        b[pl - 1] = (col, i)
for i in range(n):
    for j in range(m):
        if a[i][1] > b[j][1]:
            print(a[i][0], end=' ')
        else:
            print(b[j][0], end=' ')
    print()        


n, m, k = map(int, input().split())
stol = [[0, -1] for i in range(m)]
strok = [[0, -1] for i in range(n)]
for i in range(k):
    c, ri, ai = map(int, input().split())
    if c == 1:
        strok[ri - 1][0] = ai
        strok[ri - 1][1] = i
    else:
        stol[ri - 1][0] = ai
        stol[ri - 1][1] = i
data = [[0 for i in range(m)] for j in range(n)]
for i in range(n):
    for j in range(m):
        if strok[i][1] > stol[j][1]:
            data[i][j] = strok[i][0]
        else:
            data[i][j] = stol[j][0]
for i in range(n):
    print(*data[i])

n,m,k = map(int,input().split())
columns = [[0]*2 for i in range(n)]
strokes = [[0]*2 for i in range(m)]
for i in range(n):
    columns[i][1] = -1
for i in range(m):
    strokes[i][1] = -1
for i in range(k):
    num,rci,ai = map(int,input().split())
    if num == 2:
        strokes[rci-1][0] = ai
        strokes[rci-1][1] = i
    else:
        columns[rci-1][0] = ai
        columns[rci-1][1] = i
nm = [[[0]*2 for i in range(m)] for i in range(n)]
for i in range(n):
    for j in range(m):
        if columns[i][1] >= nm[i][j][1]:
            nm[i][j][0] = columns[i][0]
            nm[i][j][1] = columns[i][1]
        if strokes[j][1] >= nm[i][j][1]:
            nm[i][j][0] = strokes[j][0]
            nm[i][j][1] = strokes[j][1]
        
for i in range(n):
    for j in range(m):
        print(nm[i][j][0],end=" ")
    print()


n, m, k = list(map(int, input().split()))

rows = [[0, 0] for i in range(n)]
cols = [[0, 0] for i in range(m)]
for i in range(k):
    typ, p, x = list(map(int, input().split()))
    if typ == 1:
        rows[p-1] = [x, i+1]
    elif typ == 2:
        cols[p-1] = [x, i+1]
    else:
        print('WTF?!')

for i in range(n):
    line = []
    for j in range(m):
        if rows[i][1] > cols[j][1]:
            line.append(rows[i][0])
        else:
            line.append(cols[j][0])
    print(' '.join(map(str, line)))


# fin = open('input.txt', 'r')
# fout = open('output.txt', 'w')

n, m, k = map(int, input().split())

l = [[0, 0] for i in range(n + 1)]
c = [[0, 0] for i in range(m + 1)]

for i in range(k):
    dim, ind, col = map(int, input().split())
    ind -= 1
    if dim == 1:
        l[ind][0] = col
        l[ind][1] = i
    else:
        c[ind][0] = col
        c[ind][1] = i

for i in range(n):
    for j in range(m):
        if l[i][1] > c[j][1]:
            print(l[i][0], end=" ")
        elif l[i][1] < c[j][1]:
            print(c[j][0], end=" ")
        else:
            if l[i][0] != 0:
                print(l[i][0], end=" ")
            else:
                print(c[j][0], end=" ")

    print()


n, m, k = [int(i) for i in input().split()]
## n rows, m columns, k queries

printops = []
for i in range(k):
    printops.append([int(i) for i in input().split()])

def pm():
    for r in matrix:
        for i in r:
            print(i, end=' ')
        print()

row_status = [True] * n
col_status = [True] * m

matrix = [[0]*m for i in range(n)]
for i in range(k):
    j = k - i - 1
    rc_indicator, rc, color = printops[j]
    rc -= 1

    # print()

    # pm()

    if rc_indicator == 1:
        if row_status[rc]:
            for c in range(m):
                if col_status[c]:
                    matrix[rc][c] = color
            row_status[rc] = False
    else:
        if col_status[rc]:
            for r in range(n):
                if row_status[r]:
                   matrix[r][rc] = color
            col_status[rc] = False

pm()


INP = lambda : list(map(int, input().split()))
n,m,k = INP()
instructions = {
}
A = [[0 for i in range(m)] for j in range(n)]
for i in range(k):
    inp = INP()
    key = (inp[0],inp[1])
    val = [i,inp[2]]
    instructions[key] = val

for item in sorted(list(instructions.items()), key=lambda item: item[1][0]):
    key = item[0]
    val = item[1]
    if key[0] == 1:
        A[key[1]-1] = [val[1] for i in range(m)]
    else:
        for j in range(n):
            A[j][key[1]-1] = val[1]

for row in A:
    print(" ".join(map(str,row)))


def fill_column(num, color):
    nonlocal canvas
    for i in range(n):
        canvas[i][num] = color

def fill_row(num, color):
    nonlocal canvas
    for i in range(m):
        canvas[num][i] = color

(n, m, k) = map(int, input().split())
canvas = [[0 for j in range(m)] for i in range(n)]
(T, num, color) = (0, 0, 0)
a = []
T1 = [-1 for i in range(n)]
T2 = [-1 for j in range(m)]
for i in range(k):
    (T, num, color) = map(int, input().split())
    a.append((T, num - 1, color))
    if T == 1:
        T1[num - 1] = i
    else:
        T2[num - 1] = i

for i in range(k):
    if a[i][0] == 1:
        if T1[a[i][1]] == i:
            fill_row(a[i][1], a[i][2])
    else:
        if T2[a[i][1]] == i:
            fill_column(a[i][1], a[i][2])

for i in range(n):
    for j in range(m):
        print(canvas[i][j], end = ' ')
    print()

    
    


n, m, k = map(int, input().split())
canvas = [['0']*m for i in range(n)]
comands = [input().split() for i in range(k)]
colored_rows = set()
colored_columns = set()
important_comands = []
while comands and len(colored_rows) != n and len(colored_columns) != m:
    comand = comands.pop()
    if comand[0] == '1':
        if comand[1] not in colored_rows:
            important_comands.append(comand)
            colored_rows.add(comand[1])
    else:
        if comand[1] not in colored_columns:
            important_comands.append(comand)
            colored_columns.add(comand[1])
for comand in important_comands[-1::-1]:
    mark, number, color = comand
    if mark == '1':
        canvas[int(number)-1] = [color]*m
    else:
        for i in range(n):
            canvas[i][int(number)-1] = color
for i in range(n):
    print(" ".join(canvas[i]))

mx = []
ops = []
nx=list(map(int,input().split()))
n=nx[0]
m=nx[1]
k=nx[2]
flagn=[(-1,-1,i) for i in range (0,n)]
flagm=[(-1,-1,i) for i in range (0,m)]
for i in range (0,n):
    mx.append([0]*m)
for i in range (0,k):
    temp=(list(map(int,input().split())))
    if temp[0]==1:
        flagn[temp[1]-1]=(i,temp[2],temp[1]-1)
    else:
        flagm[temp[1]-1]=(i,temp[2],temp[1]-1)
flagn.sort(key=lambda x:x[0])
flagm.sort(key=lambda x:x[0])
pntn=0
pntm=0
for i in range (0,n+m):
    if ((pntn<n)&(pntm<m)):
        if flagn[pntn][0]<=flagm[pntm][0]:
            if (flagn[pntn][0]!=-1):
                for j in range (0,m):
                    mx[flagn[pntn][2]][j]=flagn[pntn][1]
            i+=1
            pntn+=1
        else:
            if flagm[pntm][0]!=-1:
                for j in range (0,n):
                    mx[j][flagm[pntm][2]]=flagm[pntm][1]
            i+=1
            pntm+=1
    else:
        if (pntn<n):
            if (flagn[pntn][0]!=-1):
                for j in range (0,m):
                    mx[flagn[pntn][2]][j]=flagn[pntn][1]
            i+=1
            pntn+=1
        if (pntm<m):
            if flagm[pntm][0]!=-1:
                for j in range (0,n):
                    mx[j][flagm[pntm][2]]=flagm[pntm][1]
            i+=1
            pntm+=1      
for i in range (0,n):
    for j in range (0,m):
        print (mx[i][j],end=' ')
    print()


import sys
n, m, k = [int(x) for x in input().split()]
lines, columns = [(0, 0, i, 1) for i in range(n)], [(0, 0, i, 2) for i in range(m)]
for i, line in enumerate(sys.stdin):
    t, r, c = [int(x) for x in line.split()]
    if t == 1:
        lines[r-1] = (i, c, r-1, 1)
    else:
        columns[r-1] = (i, c, r-1, 2)
strings = lines + columns
strings.sort()
matrix = [[0] * m for i in range(n)]
for i, c, r, t in strings:
    if t == 1:
        matrix[r] = [c for i in range(m)]
    else:
        for j in range(n):
            matrix[j][r] = c

for i in range(n):
    sys.stdout.write(' '.join(map(str, matrix[i])) + '\n')


n, m, q = list(map(int,input().split()))
timeR, timeC, color = [0] * n, [0] * m, [0] * (q + 1)

for query in range(1, q + 1):
	typeQ, posQ, color[query] = list(map(int, input().split()))
	if typeQ == 1:
		timeR[posQ - 1] = query
	else:
		timeC[posQ - 1] = query

for i in range(n):
	out = str()
	for j in range(m):
		out += str(color[max(timeR[i], timeC[j])])
		out += " "
	print(out)


import sys
n, m, k = [int(x) for x in input().split()]
L, C = [(-1, 0) for i in range(n)], [(-1, 0) for i in range(m)]
for i, line in enumerate(sys.stdin):
    t, r, c = [int(x) for x in line.split()]
    if t == 1:
        L[r-1] = (i, c)
    else:
        C[r-1] = (i, c)

for i in range(n):
    sys.stdout.write(' '.join(str(L[i][1]) if L[i][0] > C[j][0] else str(C[j][1]) for j in range(m)) + '\n')


import sys
n, m, k = [int(x) for x in input().split()]
L, C = [(-1, 0) for i in range(n)], [(-1, 0) for i in range(m)]
for i, line in enumerate(sys.stdin):
    t, r, c = [int(x) for x in line.split()]
    if t == 1:
        L[r-1] = (i, c)
    else:
        C[r-1] = (i, c)

matrix = [[max(L[i], C[j])[1] for j in range(m)] for i in range(n)]

for i in range(n):
    sys.stdout.write(' '.join(map(str, matrix[i])) + '\n')


n,m,k=map(int,input().split())
c=[(0,0)]*(m+1)
r=[(0,0)]*(n+1)
for i in range(k):
    q,a,b=map(int,input().split())
    if q==1: r[a]=(i+1,b)
    else: c[a]=(i+1,b)
for i in range(n):
    s=""
    for j in range(m):
        t=r[i+1][1]
        if c[j+1][0]>r[i+1][0]: t=c[j+1][1]
        s+=str(t)+' '
    print(s)

n,m,k=map(int,input().split())
c=[(0,0)]*(m+1)
r=[(0,0)]*(n+1)
for i in range(k):
    q,a,b=map(int,input().split())
    if q==1: r[a]=(i+1,b)
    else: c[a]=(i+1,b)
for i in range(n):
    s=[]
    for j in range(m):
        t=r[i+1][1]
        if c[j+1][0]>r[i+1][0]: t=c[j+1][1]
        s+=[str(t)]
    print(' '.join(s))

n, m, k = map(int, input().split())
A = [0] * n
for i in range(n):
    A[i] = [0,0]
B = [0] * m
for i in range(m):
    B[i] = [0,0]
for i in range(k):
    per = input().split()
    if per[0] == '1':
        A[int(per[1])-1] = [int(per[2]), i+1]
    else:
        B[int(per[1])-1] = [int(per[2]), i+1]
ans = [0] * n
for i in range(n):
    ans[i] = [0] * m

for i in range(n):
    for j in range(m):
        
        if A[i][1] > B[j][1]:
            ans[i][j] = A[i][0]
        else:
            ans[i][j] = B[j][0]

for i in ans:
    print(' '.join(map(str,i)))

n, m, k = list(map(int, input().split(' ')))

row, col = [(0,0)]*n, [(0,0)]*m
for i in range(k):
    t, r, a = list(map(int, input().split(' ')))
    if t == 1:
        row[r-1] = (a, i+1)
    elif t == 2:
        col[r-1] = (a, i+1)

sheet = [[0]*m for i in range(n)]
for i in range(n):
    for j in range(m):
        if row[i][1] >= col[j][1]:
            sheet[i][j] = row[i][0]
        else:
            sheet[i][j] = col[j][0]
    print(' '.join(map(str, sheet[i])))