Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

H, W, D = map(int, input().split())
A = []
C = [0 for _ in range(H * W + 1)]
for h in range(H):
    A.append(list(map(int, input().split())))
for i in range(H):
    for j in range(W):
        C[A[i][j]] = (i, j)
Q = int(input())
for _ in range(Q):
    ans = 0
    l, r = map(int, input().split())
    p = (C[l][0], C[l][1])
    l += D
    while l <= r:
        n = (C[l][0], C[l][1])
        ans += abs(n[0] - p[0]) + abs(n[1] - p[1])
        p = n
        l += D
    print(ans)


### FIXED CODE ###

H, W, D = map(int, input().split())
A = []
C = [0 for _ in range(H * W + 1)]
for h in range(H):
    A.append(list(map(lambda x: int(x) - 1, input().split())))
for i in range(H):
    for j in range(W):
        C[A[i][j]] = (i, j)
h = [0 for _ in range(H * W)]
for i in range(D):
    s = 0
    for j in range(i, H * W, D):
        if j != i:
            n = (C[j][0], C[j][1])
            p = (C[j - D][0], C[j - D][1])
            s += abs(n[0] - p[0]) + abs(n[1] - p[1])
        h[j] = s
Q = int(input())
for _ in range(Q):
    l, r = map(lambda x: int(x) - 1, input().split())
    print(h[r] - h[l])
### BUGGY CODE ###

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [[int(i) for i in input().split()] for i in range(m)]

for i in range(m):
        a.sort()
        bc_temp = bc[i]
        for j in range(bc_temp[0]):
                if a[j] < bc_temp[1]:
                        a[j] = bc_temp[1]

print(sum(a)) 

### FIXED CODE ###

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [[int(i) for i in input().split()] for i in range(m)]

bc.sort(key = lambda x:x[1], reverse = True)

a.sort()

a_count = 0
for b, c in bc:
        if c > a[a_count]:
                for i in range(b):
                        if c > a[a_count]:
                                a[a_count] = c
                                a_count += 1    
                        else:
                                break
                        if a_count == n:
                                        break

        else:
                break
        if a_count == n:
                break        

print(sum(a))
### BUGGY CODE ###

N = int(input())
def f(n):
    count=0
    for x in range(1,n):
        for y in range(1,n):
            for z in range(1,n):
                if x*x+y*y+z*z+x*y+y*z+z*x==n:
                    count +=1
    print(count)
    
for i in range(1,N+1):
    f(i)

### FIXED CODE ###

a=[0]*int(input())
r=range(1,99)
for x in r:
    for y in r:
        for z in r:
            n=x*(x+y+z)+y*(y+z)+z*z-1
            if n<len(a):
                a[n]+=1
print(*a)
### BUGGY CODE ###

import os
import sys

import numpy as np


def solve(n,k,p,c):
    ans = -10**9
    for i in range(n):
        x = i
        t = k
        count = 0
        l = True
        sub = 0
        ln = 0
        while True:
            x = p[x] - 1
            if l:
                count += c[x]
                ln += 1
                if x == i:
                    if t > ln:
                        if count > 0:
                            sub += (t // ln - 1) * count
                            t %= ln
                            t += ln
                        else:
                            t = 0
                    l = False
            sub += c[x]
            t -= 1
            ans = max(sub, ans)
            if t < 1:
                break
    return ans


# >>> numba compile >>>
if sys.argv[-1]=='ONLINE_JUDGE':
  from numba.pycc import CC
  cc=CC('my_module')
  cc.export('solve','i8(i8,i8,i8[:],i8[:])')(main)
  cc.compile()
  exit()
#from my_module import main
# <<< numba compile <<<
(n, k), p, c = [np.int_(i.split()) for i in open(0)]
print(solve(n,k,p,c))


### FIXED CODE ###

(n, k), p, c = [[*map(int, i.split())] for i in open(0)]

def solve(x, t):
    visit = [0] * n
    visit[x] = 1
    loop = [0] * n
    count = 0
    ans = c[p[x] - 1]
    l = True
    sub = 0
    while True:
        x = p[x] - 1
        if l:
            if visit[x]:
                if loop[x]:
                    ln = sum(loop)
                    if t > l:
                        sub += (t//ln -1)*count*(count>0)
                        t %= ln
                        t += ln
                    l = False
                count += c[x]
                loop[x] = 1
            visit[x] = 1
        sub += c[x]
        t -= 1
        ans = max(sub, ans)
        if t < 1:
            return ans


print(max(solve(i, k) for i in range(n)))
### BUGGY CODE ###

import sys
from collections import deque
N, M = map(int, input().split())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]

A.sort(reverse=True)
B.sort(reverse=True)

if len(set(A))!=len(A) or len(set(B))!=len(B):
    print(0)
    sys.exit()

ans = 1
rows, columna = 0,0
for x in range(N*M, 0, -1):

    if x in A and x in B:
        rows += 1
        columna += 1

    elif x in A and not x in B:
        rows += 1
        ans = ans * (columna)

    elif x in B and not x in A:
        columna += 1
        ans = ans *(rows)

    else:
        ans = ans * (rows * columna -(N*M-x))

print(ans%(10**9+7))


### FIXED CODE ###

def solve():
    MOD = 10 ** 9 + 7
    N, M = map(int, input().split())
    A = set(map(int, input().split()))
    B = set(map(int, input().split()))
    
    row, columun = 0, 0
    ans = 1
    for i in range(N * M, 0, -1):
        in_A = i in A
        in_B = i in B
        if in_A and in_B:
            row += 1
            columun += 1
        elif in_A:
            row += 1
            ans *= columun
        elif in_B:
            columun += 1
            ans *= row
        else:
            ans *= row * columun - (N * M - i)
        ans %= MOD
    print(ans)


if __name__ == '__main__':
    solve()
### BUGGY CODE ###

import copy
n,m = map(int,input().split())
l = []
for i in range(m):
  l.append([int(i) for i in input().split()])
ll = []
for k in range(l[0][0],l[0][1]+1):
  ll.append(k)
for j in range(1,m):
  for i in ll:
    lll = copy.copy(ll)
    if i < l[j][0] or i > l[j][1]:
      lll.remove(i)
    ll = lll
print(len(ll))

### FIXED CODE ###

n,m = map(int,input().split())
l = []
for i in range(m):
  l.append([int(i) for i in input().split()])
maxL = l[0][0]
minR = l[0][1]
for j in range(1,m):
  if maxL <= l[j][0]:
    maxL = l[j][0]
  if minR >= l[j][1]:
    minR = l[j][1]
if maxL <= minR:
  print(minR - maxL + 1)
else:
  print(0)
### BUGGY CODE ###

from itertools import permutations
def warshall_floyd(n,e):
  INF=float("inf")
  g=[[INF]*n for _ in range(n)]
  for i in range(n):g[i][i]=0
  for a,b,c in e:g[a][b],g[b][a]=c,c
  for k in range(n):
    for i in range(n):
      for j in range(n):g[i][j]=min(g[i][k]+g[k][j],g[i][j])
  return g
n,m,rl=map(int,input().split());r,e=list(map(int,input().split())),[]
for i in range(m):x,y,z=map(int,input().split());e.append([x-1,y-1,z])
d=warshall_floyd(n,e)
a=float("inf")
for i in permutations(range(rl)):
  aa=0
  for x,y in zip(i,i[1:]):aa+=d[r[x]-1][r[y]-1]
  a=min(a,aa)
print(a)

### FIXED CODE ###

from itertools import permutations
n,m,rl=map(int,input().split());r=list(map(int,input().split()))
d=[[float("inf")for _ in range(n)]for _ in range(n)]
for i in range(m):
  x,y,z=map(int,input().split())
  d[x-1][y-1],d[y-1][x-1]=z,z
for i in range(n):d[i][i]=0
for k in range(n):
  for i in range(n):
    for j in range(n):d[i][j]=min(d[i][j],d[i][k]+d[k][j])
a=float("inf")
for i in permutations(r,rl):
  aa=0
  for j in range(rl-1):aa+=d[i[j]-1][i[j+1]-1]
  a=min(a,aa)
print(a)
### BUGGY CODE ###

from itertools import combinations, permutations
N = int(input())
#N,K = map(int,input().split())
#N = int(input())
V = list(map(int,input().split()))
s=0
S=[0]
for C in combinations(V,2):
    n=N
    v=V.copy()
    v.remove(C[0])
    v.remove(C[1])
    s=(C[0]+C[1])/2**(n-1)
    n-=2
    #print(C,s)
    for D in permutations(v):
        m=n
        k=s
        for i in range(m):

            k+=D[i]/(2**m)
            m-=1
        S.append(k)
#        print(k,D)
print(max(S))


### FIXED CODE ###

N = int(input())
#N,K = map(int,input().split())
#N = int(input())
V = list(map(int,input().split()))
V.sort()
N-=1
s=V[0]/(2**N)
V.pop(0)
for i in range(N):
    s+=V[i]/2**N
    N-=1

print(s)
### BUGGY CODE ###

N,K = map(int,input().split())
A = list(map(int,input().split()))
L = [1]
s = set()
s.add(1)
now = 1
for _ in range(N):
    if A[now - 1] in s:
        K -= L.index(A[now - 1])
        if K >= 0: K %= len(L) - L.index(A[now - 1])
        break
    L.append(A[now - 1])
    s.add(A[now - 1])
    now = A[now - 1]
print(L[K + L.index(A[now - 1])])
if len(L) == 1:
    while True:
        print("hoge")



### FIXED CODE ###

N,K = map(int,input().split())
A = list(map(int,input().split()))
L = [1]
s = set()
s.add(1)
now = 1
for _ in range(N):
    if A[now - 1] in s:
        K -= L.index(A[now - 1])
        if K >= 0: K %= len(L) - L.index(A[now - 1])
        break
    L.append(A[now - 1])
    s.add(A[now - 1])
    now = A[now - 1]
#print(L)
#print(K)
print(L[K + L.index(A[now - 1])])
### BUGGY CODE ###

from collections import deque
H, W = list(map(int, input().split()))
S = [list(input()) for _ in range(H)]

mv = {(1, 0), (-1, 0), (0, 1), (0, -1)}


def BFS(St):
    stk = deque([[St[0],St[1],0]])
    seen = [[False]*W for _ in range(H)]
    while len(stk) > 0:
        l = stk.pop()
        i = l[0]
        j = l[1]
        ct = l[2]
        seen[i][j] = True
        for k in mv:
            x = i+k[0]
            y = j+k[1]
            if 0 <= x < H and 0 <= y < W:
                if S[x][y] == '.' and seen[x][y] == False:
                    stk.appendleft([x, y, ct+1])
    return(ct)

ans = 0
# 各スタート地点に対して最大距離を出力すればいいですねって話

for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue
        ans=max(ans,BFS([i,j]))
print(ans)


### FIXED CODE ###

H, W = list(map(int, input().split()))
S =[list(input()) for _ in range(H)]
N=H*W
INF=float('inf')
L=[[INF]*N for _ in range(N)]
for i in range(N):
    h=i//W
    w=i%W
    # print(i,h,w)
    if S[h][w]=='.':
        L[i][i]=0
    if w>0 and S[h][w-1]=='.':
        L[i][i-1]=1
    if w<W-1 and S[h][w+1]=='.':
        L[i][i+1]=1
    if h>0 and S[h-1][w]=='.':
        L[i][i-W]=1
    if h<H-1 and S[h+1][w]=='.':
        L[i][i+W]=1
# print(L)

for i in range(N):
    via=L[i]
    if via[i]==INF:
        continue
    for j in range(N):
        st=L[j]
        if st[j]==INF:
            continue
        for k in range(N):
            L[j][k]=min(st[i]+via[k], st[k])
# print(L)
ans=0
for i in range(N):
    for j in range(N):
        if L[i][j]!=INF:
            ans=max(ans,L[i][j])
print(ans)
### BUGGY CODE ###

I = lambda: map(int, input().split())
n, k = I()
a = []
for _ in range(n):
    t, d = I()
    a += [d, t, 0],
a.sort(reverse = True)
b = 0
c = [0]*(n+1)
e = 0
for i in a[:k]:
    b += i[0]
    if c[i[1]] == 0: e += 1
    c[i[1]] += 1
    i[2] = 1
f = [b+e**2]
for i in range(k, n):
    g = a[i]
    if c[g[1]] > 0: continue
    for j in a[:i][::-1]:
        if j[2] > 0 and c[j[1]] > 1:
            b += g[0]-j[0]
            g[2] = 1
            j[2] = 0
            c[g[1]] += 1
            c[j[1]] -= 1
            e += 1
            f += b+e**2,
            break
print(max(f))

### FIXED CODE ###

mi = lambda: map(int, input().split())
n, k = mi()
sushis = [list(mi()) for _ in range(n)]
types = set()
sushis.sort(key = lambda x: x[1], reverse = True)
bp = tp = sp = 0
exts = []
for t, d in sushis[:k]:
    bp += d
    if t in types:
        exts += d,
    else:
        types.add(t)
        tp += 1
sp = bp+tp**2
for t, d in sushis[k:]:
    if not exts: break
    if t not in types:
        types.add(t)
        bp += d-exts.pop()
        tp += 1
        sp = max(sp, bp+tp**2)
print(sp)
### BUGGY CODE ###

x,y,z,k = map(int, raw_input().split())
a = map(int, raw_input().split())
b = map(int, raw_input().split())
c = map(int, raw_input().split())
s = []
for ka in a:
    for kb in b:
		for kc in c:
			s.append(ka+kb+kc)
for n in range(k):
	print(max(s))
	s.remove(max(s))
	

### FIXED CODE ###

x,y,z,k = map(int, raw_input().split())
a = map(int, raw_input().split())
b = map(int, raw_input().split())
c = map(int, raw_input().split())
s = []
a.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)
for ka in range(x):
    for kb in range(y):
			s.append(a[ka]+b[kb])
s.sort(reverse = True)
p = 3000
if(x * y < 3000):
	p = x * y
t = []
for n in range(p):
	for m in c:
		t.append(s[n] + m)
t.sort(reverse = True)
for n in range(k):
	print(t[n])
### BUGGY CODE ###

l,r = map(int,input().split())
mod_min = 2020
for i in range(l,r+1):
  for j in range(i+1,r+1):
    mod = (i*j) % 2019
    if mod <= mod_min:
      mod_min = mod
    else:
      pass

print(mod_min)
    

### FIXED CODE ###

l,r = map(int,input().split())
if r-l > 2017:
  print(0)
else:
  mod_min = 2019
  for i in range(l,r+1):
    for j in range(i+1,r+1):
      mod = (i*j)%2019
      if mod <= mod_min:
        mod_min = mod
      else:
        pass
  print(mod_min)
### BUGGY CODE ###

def solve():
    N = int(raw_input())
    S = raw_input() # str
    min_need_change = N
    for i in range(N):
        count = 0
        for j in range(0, i):
            if S[j] == 'W':
                count += 1
        for j in range(i+1, N):
            if S[j] == 'E':
                count += 1
        if count < min_need_change:
            min_need_change = count
    print str(min_need_change)

if __name__ == '__main__':
    solve()


### FIXED CODE ###

def solve():
    N = int(raw_input())
    S = raw_input() # str
    count = 0
    for i in range(1, N): # count for leader is the 1st person case
        if S[i] == 'E':
            count += 1
    min_need_change = count
    for i in range(1,N): # count for leader is the 2nd, ... (N + 1)th person cases
        if S[i] == 'E':
            count -= 1
        if S[i - 1] == 'W':
            count += 1
        if count < min_need_change:
            min_need_change = count
    print str(min_need_change)

if __name__ == '__main__':
    solve()
### BUGGY CODE ###

N = int(input())
L = list(map(int, input().split()))

ans = 0
for i in range(1,2**N):
  bin_line = str(bin(i))
  bin_line = bin_line.replace('0b', '')
  if len(bin_line) < N:
    zero = '0'*(N-len(bin_line))
    bin_line = zero + bin_line
  count = 0
  line = []
  for j in range(N):
    if bin_line[j] == '1':
      count += 1
      line.append(L[j])
  if count == 3 and len(set(line)) == 3:
    if line[0]+line[1] > line[2] and line[0]+line[2] > line[1] and line[1]+line[2]>line[0]:
      ans += 1
print(ans)
    
                 

### FIXED CODE ###

N = int(input())
L = list(map(int, input().split()))

ans = 0
for i in range(0,N-2):
  for j in range(i+1,N-1):
    for k in range(j+1,N):
      if L[i] != L[j] and L[i] != L[k] and L[j] != L[k] and L[i]+L[j] > L[k] and L[i]+L[k] > L[j] and L[j]+L[k] > L[i]:
        ans += 1
print(ans)
### BUGGY CODE ###

n, m, q = map(int, input().split())
lis = []
ris = []
for i in range(m):
    li, ri = map(int, input().split())
    lis.append(li)
    ris.append(ri)
pis = []
qis = []

for i in range(q):
    pi, qi = map(int, input().split())
    pis.append(pi)
    qis.append(qi)

for (pi, qi) in zip(pis, qis):
    total = 0
    for (li, ri) in zip(lis, ris):
        if pi <= li and ri <= qi:
            total += 1
    print(total)




### FIXED CODE ###

n, m, qq = map(int, input().split())
lr = [[0] * n for _ in range(n)]
for _ in range(m):
    l, r = map(int, input().split())
    lr[l - 1][r - 1] += 1

for i in range(n):
    for j in range(n - 1):
        lr[i][j + 1] += lr[i][j]
for j in range(n):
    for i in range(n-1, 0, -1):
        lr[i - 1][j] += lr[i][j]

for _ in range(qq):
    p, q = map(int, input().split())
    if q == n:
        print(lr[p - 1][q - 1])
    else:
        print(lr[p - 1][q - 1] - lr[q][q - 1])
### BUGGY CODE ###

def resolve():
    N = int(input())
    S = input()
    cnt = {'R': [], 'B': [], 'G': []}
    indices_set = {'R': set(cnt['R']), 'G': set(cnt['G']), 'B': set(cnt['B'])}
    for i, c in enumerate(S):
        cnt[c].append(i)
    from itertools import permutations
    import bisect
    ans = 0
    for (ci, cj, ck) in permutations(['R', 'B', 'G']):
        for i in cnt[ci]:
            for ind_j in range(bisect.bisect_left(cnt[cj], i), len(cnt[cj])):
                j = cnt[cj][ind_j]
                ans += len(cnt[ck]) - bisect.bisect_left(cnt[ck], j)
                if j + (j - i) in cnt[ck]:
                    ans -= 1
    print(ans)
resolve()

### FIXED CODE ###

from collections import Counter

N = int(input())
s = input()
cnt = Counter(s)
R = cnt.get('R', 0)
G = cnt.get('G', 0)
B = cnt.get('B', 0)
ans = R * G * B
for i in range(N-1):
  for j in range(i+1, N):
    if s[i] != s[j] and 2*j-i < N and s[i] != s[2*j-i] and s[j] != s[2*j-i]:
      ans -= 1
print(ans)
### BUGGY CODE ###

import functools
"""最大公約数"""
def gcd(a,b):
    import fractions
    return fractions.gcd(a,b)

"""最小公倍数"""
def lcm(a,b):
    f = gcd(a,b)
    return a*b//f

def m(nums):
    return functools.reduce(lcm, nums)

def main():
    n = int(input())
    A = list(map(int,input().split()))
    mod = 10**9+7
    l = m(A)
    res = 0
    for a in A:
        res += l//a
        res %= mod
    print(res%mod)

if __name__ == '__main__':
    main()

### FIXED CODE ###

import functools
"""最大公約数"""
def gcd(a,b):
    import fractions
    return fractions.gcd(a,b)

"""最小公倍数"""
def lcm(a,b):
    f = gcd(a,b)
    return a*b//f

def m(nums):
    return functools.reduce(lcm, nums)

def main():
    n = int(input())
    A = list(map(int,input().split()))
    mod = 10**9+7
    l = m(A)
    res = 0
    print(sum(l// i for i in A) % mod)

if __name__ == '__main__':
    main()
### BUGGY CODE ###

import bisect

A, B, Q = map(int,input().split())
INF = 30000000000
s = [-1*INF]
t = [-1*INF]
for _ in range(A):
    s.append(int(input()))
s.append(INF)
for _ in range(B):
    t.append(int(input()))
t.append(INF)
for _ in range(Q):
    x = int(input())
    s_index = bisect.bisect_left(s,x)
    t_index = bisect.bisect_left(t,x)
    s_l = x - s[s_index-1]
    s_r = s[s_index] - x
    t_l = x - t[t_index-1]
    t_r = t[t_index] - x
    print(min(max(s_l,t_l), s_l + t_r*2, s_r + t_l*2, s_l*2 + t_r, s_r*2 + t_l, max(s_r,t_r)))

### FIXED CODE ###

from bisect import bisect_left
A, B, Q = map(int,input().split())
INF = 2 * 10 ** 10
S = [-INF]
T = [-INF]
for _ in range(A):
    S.append(int(input()))
S.append(INF)
for _ in range(B):
    T.append(int(input()))
T.append(INF)
for _ in range(Q):
    x = int(input())
    s = bisect_left(S, x)
    t = bisect_left(T, x)
    ans = min(max(S[s], T[t]) - x, x - min(S[s-1], T[t-1]), 2 * S[s] - T[t-1] - x, 2 * T[t] - S[s-1] - x, T[t] + x - 2 * S[s-1], S[s] + x - 2 * T[t-1])
    print(ans)
### BUGGY CODE ###

#-*- coding:utf-8 -*-
import copy
import fractions
from functools import reduce

N = int(input())
A = list(map(int, input().split()))

gcd_list =[]

for i, a in enumerate(A):
    A_except_a = copy.deepcopy(A)
    A_except_a.pop(i)
    
    gcd = reduce(fractions.gcd, A_except_a)
    gcd_list.append(gcd)

print(max(gcd_list))

### FIXED CODE ###

import fractions
from functools import reduce

N = int(input())
a_list = list(map(int, input().split()))

m1 = [0] * (N + 1)
m2 = [0] * (N + 1)
for i in range(1, N + 1):
  m1[i] = fractions.gcd(m1[i - 1], a_list[i - 1])
  m2[N - i] = fractions.gcd(m2[N - i + 1], a_list[N - i])

m = [0] * N
for i in range(N):
  m[i] = fractions.gcd(m1[i], m2[i + 1])

print(max(m))
### BUGGY CODE ###

import fractions
k = int(input())

l = [[-1 for _ in range(k+1)] for _ in range(k+1)]

sum = 0
for x1 in range(1,k+1):
  for x2 in range(1,k+1):
    for x3 in range(1,k+1):
      for i in range(1, 2):
        if l[x1][x2] == -1:
          ans = fractions.gcd(x1,x2)
        else:
          ans = l[x1][x2]
        if l[ans][x3] == -1:
          ans = fractions.gcd(ans,x3)
        else:
          ans = l[ans][x3]
        
        sum += ans
 
print(sum)

### FIXED CODE ###

import fractions
k = int(input())

l = [[-1 for _ in range(k+1)] for _ in range(k+1)]

for i in range(1,k+1):
  for j in range(1,k+1):
     if l[i][j] == -1:
        ans = fractions.gcd(i,j)
        l[i][j] = ans
        l[i][i] = ans
sum = 0
#1種類
for i in range(1,k+1):
  sum += i
#２種類
for i in range(1,k):
  for j in range(i+1,k+1):
    sum += l[i][j]*6

#3種類
for i in range(1,k-1):
  for j in range(i+1,k):
    for m in range(j+1,k+1):
      sum += l[l[i][j]][m]*6

print(sum)
### BUGGY CODE ###

import math
from functools import reduce


def gcd(*numbers):
    return reduce(math.gcd, numbers)


def gcd_list(numbers):
    return reduce(math.gcd, numbers)


K = int(input())

sums = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            sums += gcd(a, b, c)

print(sums)

### FIXED CODE ###

import math
import itertools
from functools import reduce


def gcd(*numbers):
    return reduce(math.gcd, numbers)


def gcd_list(numbers):
    return reduce(math.gcd, numbers)


K = int(input())

sums = 0
# for a, b, c in itertools.permutations(range(1, K+1), 3):
for a, b, c in itertools.product(range(1, K+1), repeat=3):
    sums += gcd(a, b, c)
# for a in range(1, K+1):
#     for b in range(1, K+1):
#         for c in range(1, K+1):
#             sums += gcd(a, b, c)

print(sums)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**7)

n, q = map(int,input().split())
g = [[] for _ in range(n+1)]
for  _ in range(n-1):
    a,b = map(int,input().split())
    g[a].append(b)
    g[b].append(a)
points = [0]*(n+1)
for _ in range(q):
    p, x = map(int,input().split())
    points[p] += x
# print(g)
# print(points)
def dfs(e,preve):
    for nexte in g[e]:
        # print(nexte)
        if nexte == preve:
            continue
        
        points[nexte] = points[nexte] + points[e]
        dfs(nexte,e)
dfs(1,-1)
print(*points[1:])



### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)

def input():
    return sys.stdin.readline()[:-1]

n, q = map(int,input().split())
g = [[] for _ in range(n+1)]
for  _ in range(n-1):
    a,b = map(int,input().split())
    g[a].append(b)
    g[b].append(a)
points = [0]*(n+1)
for _ in range(q):
    p, x = map(int,input().split())
    points[p] += x
# print(g)
# print(points)
def dfs(e,preve = -1):
    for nexte in g[e]:
        # print(nexte)
        if nexte == preve:
            continue
        points[nexte] = points[nexte] + points[e]
        dfs(nexte,e)
dfs(1)
print(*points[1:])
### BUGGY CODE ###

from collections import deque
v,n=map(int,input().split())
n+=v-1
es=[[int(x)for x in input().split()]for _ in range(n)]
outs={}
ins={}
for v1,v2 in es:
    outs[v1]=outs.get(v1,[])+[v2]
    ins[v2]=ins.get(v2,0)+1
q=deque(v1 for v1 in range(v)if not ins.get(v1,0))
res=[]
p=[0]*-~v
while q:
    v1=q.popleft()
    res.append(v1)
    for v2 in outs.get(v1,[]):
        ins[v2]-=1
        if ins[v2]==0:
            q.append(v2)
            p[v2]=v1
for t in p[1:]:print(t)

### FIXED CODE ###

from collections import defaultdict, deque

v, n = map(int, input().split())
n += v -1
es = [[int(x) for x in input().split()] for _ in range(n)]

outs = defaultdict(list)
ins = defaultdict(int)
for v1, v2 in es:
    outs[v1].append(v2)
    ins[v2] += 1

q = deque(v1 for v1 in range(v) if ins[v1] == 0)
res = []
f = [0]*(v+1)
while q:
    v1 = q.popleft()
    res.append(v1)
    for v2 in outs[v1]:
        ins[v2] -= 1
        if ins[v2] == 0:
            q.append(v2)
            f[v2] = v1
for t in f[1:]:
    print(t)