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

f=lambda:map(int,input().split())
N,M=f()
N+=1;V=[-1]*N
Q=list(range(N))
F,B=eval('{i:[]for i in Q},'*2)
for i in'0'*M:x,y=f();F[x]+=[y];B[y]+=[x]
while Q:
 p=Q.pop();S=[1+V[q]for q in B[p]]
 if all(S)and V[p]<0:V[p]=max(S+[0]);Q+=F[p]
print(max(V))

### FIXED CODE ###

from collections import defaultdict
debug = print
def debug(*va): pass

def longest_by_topological_sort(N, nexts, prevs):
    visited = [-1] * N
    max_dist = 0
    que = list(range(N))
    while len(que):
        p = que.pop()
        if visited[p] == -1:
            if all(visited[q] != -1 for q in prevs[p]):
                dist = 0
                for q in prevs[p]:
                    dist = max(dist, visited[q] + 1)
                visited[p] = dist
                max_dist = max(max_dist, dist)
                for n in nexts[p]:
                    que.append(n)
    return max_dist

def test_separate():
    import time
    N = 10**5
    t0 = time.time()
    print(longest_by_topological_sort(N, defaultdict(list), defaultdict(list)))
    print('elapsed', time.time() - t0)

def test_fanout():
    import time
    N = 10**5
    nn, pp = defaultdict(list), defaultdict(list)
    for i in range(1, N):
        nn[0].append(i)
        pp[i].append(0)
    t0 = time.time()
    print(longest_by_topological_sort(N, nn, pp))
    print('elapsed', time.time() - t0)

def test_middle():
    import time
    N = 10**5
    nn, pp = defaultdict(list), defaultdict(list)
    for i in range(1, N):
        # fanout
        nn[0].append(i)
        pp[i].append(0)
        # fanin
        nn[i].append(1)
        pp[1].append(i)
    t0 = time.time()
    print(longest_by_topological_sort(N, nn, pp))
    print('elapsed', time.time() - t0)

if __name__ == '__main__':
    N, M = map(int, input().split())

    nextnodes = [defaultdict(list), defaultdict(list)]
    for i in range(M):
        x, y = map(int, input().split())
        nextnodes[0][x - 1].append(y - 1)
        nextnodes[1][y - 1].append(x - 1)

    max_dist = longest_by_topological_sort(N, nextnodes[0], nextnodes[1])
    print(max_dist)
### BUGGY CODE ###

from collections import Counter

N = int(input())
count = []

for _ in range(N):
    s = input()
    count.append(Counter(s))

ans = 0
for i in range(N):
    for j in range(i+1, N):
        if(count[i] == count[j]):
            ans += 1

print(ans)


### FIXED CODE ###

from collections import Counter
import json

N = int(input())
ss = []
c = []

for _ in range(N):
    s = input()
    ss.append("".join(sorted(s)))

sum = 0

for i , v in Counter(ss).items():
    sum += (v*(v-1)//2)
print(sum)
### BUGGY CODE ###

N,K=map(int,input().split())
h=list(map(int,input().split()))
ans=[0]*N
ans[1]=abs(h[1]-h[0])
for i in range(2,N):
    ans[i]=10**10
    ans[i]= min(abs(h[i]-h[j])+ans[j] for j in range(max(0,i-K),i))
print(ans[-1])

### FIXED CODE ###

def main():
    N,K = map(int,input().split())
    h = list(map(int,input().split()))
    dp = [0]*N
    for i in range(1,N):
        dp[i]= min(abs(h[i]-h[j])+dp[j] for j in range(max(0,i-K),i))
    print(dp[-1])
main()
### BUGGY CODE ###

import sys
input = sys.stdin.readline

def main():
    n = int(input())
    s = input()
    R = []
    G = []
    B = []

    for i in range(n):
        if s[i] == "R":
            R.append(i + 1)
        elif s[i] == "G":
            G.append(i + 1)
        else:
            B.append(i + 1)

    res = 0
    for r in R:
        for g in G:
            for b in B:
                x = max(r, g, b)
                y = min(r, g, b)
                z = r + g + b - x - y
                if x + y != z * 2:
                    res += 1
    print(res)
if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
input = sys.stdin.readline

def main():
    n = int(input())
    s = input()
    R = 0
    G = 0
    B = 0

    for i in range(n):
        if s[i] == "R":
            R += 1
        elif s[i] == "G":
            G += 1
        else:
            B += 1
            
    res = R*G*B
    for i in range(n - 2):
        for j in range(i + 1, i + 1 + (n - 1 - i) // 2):
            k = j + (j - i)
            if s[i] != s[j] != s[k] != s[i]:
                res -= 1
    print(res)
if __name__ == '__main__':
    main()
### BUGGY CODE ###

def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(1,n+1):
        for i in range(1,n+1):
            for j in range(1,n+1):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d
  
  
n,m,r = map(int,input().split()) 
lists=list(map(int,input().split()))
d = [[float("inf") for i in range(n+1)] for i in range(n+1)] 

for i in range(m):
    x,y,z = map(int,input().split())
    d[x][y] = z
    d[y][x] = z
for i in range(n+1):
    d[i][i] = 0 #自身のところに行くコストは０
dislist=warshall_floyd(d)

import itertools

U=list(itertools.permutations(lists))

mini=10**100
for some in U:
  dd=0
  for j in range(r-1):
    if dislist[some[j]][some[j+1]]!="inf":
      dd+=dislist[some[j]][some[j+1]]
    else:
      break
  mini=min(mini,dd)
print(mini)
   	

### FIXED CODE ###

def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(1,n+1):
        for i in range(1,n+1):
            for j in range(1,n+1):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d
  
  
n,m,r = map(int,input().split()) 
lists=list(map(int,input().split()))
d = [[float("inf") for i in range(n+1)] for i in range(n+1)] 

for i in range(m):
    x,y,z = map(int,input().split())
    d[x][y] = z
    d[y][x] = z
for i in range(n+1):
    d[i][i] = 0 #自身のところに行くコストは０
dislist=warshall_floyd(d)

import itertools

U=list(itertools.permutations(lists))

mini=10**100
for some in U:
  dd=0
  for j in range(r-1):
    if dislist[some[j]][some[j+1]]!=float("inf"):
      dd+=dislist[some[j]][some[j+1]]
    else:
      break
  mini=min(mini,dd)
print(mini)
### BUGGY CODE ###

# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**9)

N = int(input())

graph = [[] for i in range(N)]

for i in range(N-1):
    a,b = map(int,input().split())
    graph[a-1].append((b,i))
    graph[b-1].append((a,i))
    
ans = [0]*(N-1)
ten_col =[[] for i in range(N)]

def dfs(start,now):
    new_col = 1
    for (future,i) in graph[now-1]:
        if future == start:
            pass
        else:
            if new_col in ten_col[now-1]:
                new_col += 1
            ten_col[now-1].append(new_col)
            ten_col[future-1].append(new_col)
            ans[i] = new_col
            new_col += 1

            dfs(now,future)
            
dfs(-1,1)
print(max(ans))
for i in ans:
    print(i)

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**9)

N = int(input())

graph = [[] for i in range(N)]

for i in range(N-1):
    a,b = map(int,input().split())
    graph[a-1].append((b,i))
    graph[b-1].append((a,i))
    
ans = [0]*(N-1)

def dfs(past,now,edge_col):
    new_col = 1
    for (future,i) in graph[now-1]:
        if future == past:
            pass
        else:
            if new_col == edge_col:
                new_col += 1
            ans[i] = new_col
            dfs(now,future,new_col)
            new_col += 1
            
dfs(-1,1,-1)

print(max(ans))
for i in ans:
    print(i)
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))

ans = 1
for i in range(n) :
    ans *= a[i]

if ans > 10**18 :
    print(-1)
else :
    print(ans)


### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
if 0 in a :
    ans  = 0
    print(ans)
else :
    ans = 1
    for i in range(n):
        ans *= a[i]
        if ans > 10**18 :
            print(-1)
            break
if ans <= 10**18 and ans != 0 :
    print(ans)
### BUGGY CODE ###

def main():
  s = input()
  mod = 10**9+7
  dp = [0]*13
  dp[0] = 1
  digit = 1
  for index, i in enumerate(s[::-1], start=1):
    nextDP = [0]*13
    if i == "?":
      for k in range(10):
        for j in range(13):
          nextDP[(digit * k + j)%13] += dp[j]
          nextDP[(digit * k + j)%13] %= mod
    else:
      for j in range(13):
        nextDP[(digit * int(i) + j)%13] += dp[j]
        nextDP[(digit * int(i) + j)%13] %= mod
    dp = nextDP
    digit *= 10
  print(dp[5])

if __name__ == '__main__':
  main()

### FIXED CODE ###

def main():
  s = input()
  mod = 10**9+7
  dp = [0]*13
  dp[0] = 1
  digit = 1
  for i in range(len(s)):
    nextDP = [0]*13
    if s[i] == "?":
      for k in range(10):
        for j in range(13):
          t = (k + j * 10)%13
          nextDP[t] = (dp[j] + nextDP[t]) % mod
    else:
      si = int(s[i])
      for j in range(13):
        t = (si + j * 10)%13
        nextDP[t] = (dp[j] + nextDP[t]) % mod
    dp = nextDP
    digit *= 10
  print(dp[5])

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

def resolve():
    N=int(input())
    X=list(map(int,input().split()))
    Y=sorted(X)
    Z=[(Y[:i]+Y[i+1:])[N//2-1] for i in range(N)]

    for i in range(N):
        print(Z[Y.index(X[i])])




resolve()


### FIXED CODE ###

def resolve():
    N=int(input())
    X=list(map(int,input().split()))
    Y=sorted(X)
    l=Y[N // 2-1]
    r=Y[N//2]


    for i in X:
        if i<r:
            print(r)
        else:
            print(l)






resolve()
### BUGGY CODE ###

from collections import defaultdict


N, K = map(int, input().split())
p = list(map(int, input().split()))
pd = defaultdict(int)

kitai = []
for i in p:
    if not pd[i]:
        ex = (1 + i) / 2
        kitai.append(ex)
    else:
        kitai.append(pd[i])

ans = 0
for i in range(N):
    if i + (K-1) >= N:
        break
    else:
        ans = max(ans, sum(kitai[i:i+K]))

print(ans)

### FIXED CODE ###

from collections import defaultdict


N, K = map(int, input().split())
p = list(map(int, input().split()))
pd = defaultdict(int)

kitai = []
for i in p:
    if not pd[i]:
        ex = (1 + i) / 2
        kitai.append(ex)
    else:
        kitai.append(pd[i])

# 累積和
rui = []
for i, j in enumerate(kitai):
    if i == 0:
        rui.append(j)
    else:
        rui.append(rui[i-1] + j)

ans = 0
for i, j in enumerate(rui):
    if i + (K-1) >= N:
        continue
    else:
        if i == 0:
            ans = rui[i+K-1]
        else:
            ans = max(ans, rui[i+K-1] - rui[i-1])

print(ans)
### BUGGY CODE ###

n, q = map(int, input().split())
ab = sorted([list(map(int, input().split())) for i in range(n-1)])
px = [list(map(int, input().split())) for i in range(q)]
p = [0 for i in range(n)]
for i in px:
  p[i[0] - 1] += i[1]
s = ""
for i in ab:
  p[i[1]-1] += p[i[0]-1]
print(" ".join(map(str, p)))

### FIXED CODE ###

n, q = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(n-1)]
ab.sort()
p = [0]*n
for i in range(q):
  a, b = map(int, input().split())
  p[a - 1] += b
s = ""
for i in ab:
  p[i[1]-1] += p[i[0]-1]
print(*p)
### BUGGY CODE ###

import heapq


def small_multiple(k):
    # kの倍数で各桁の総数が最も小さくなるような、各桁の総数
    dist = [float("inf")] * k
    q = []
    # kで割った余りが1になるような最小の数は1
    heapq.heappush(q, (1, 1))

    while q:
        n, c = heapq.heappop(q)
        if dist[n] <= c:
            continue
        dist[n] = c

        if dist[(n + 1) % k] > c + 1:
            heapq.heappush(q, ((n + 1) % k, c + 1))
        if dist[(n * 10) % k] > c:
            heapq.heappush(q, ((n * 10) % k, c))

    print(dist[0])


def main():
    k = int(input())
    small_multiple(k)


if __name__ == "__main__":
    main()


### FIXED CODE ###

import heapq


def small_multiple(k):
    # kの倍数で各桁の総数が最も小さくなるような、各桁の総数
    dist = [float("inf")] * k
    q = []
    # kで割った余りが1になるような最小の数は1
    heapq.heappush(q, (1, 1))

    while q:
        n, c = heapq.heappop(q)
        if dist[n] <= c:
            continue
        dist[n] = c

        if dist[(n + 1) % k] > c + 1:
            heapq.heappush(q, ((n + 1) % k, c + 1))
        if dist[(n * 10) % k] > c:
            heapq.heappush(q, ((n * 10) % k, c))

    print(dist[0])


def main():
    k = int(input())
    small_multiple(k)


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

import sys

n = int(input())
list_input = list(map(int, sys.stdin.readline().strip().split()))

a = 1
for i in list_input:
    a *= i

if a > pow(10, 18):
    print(-1)
else:
    print(a)


### FIXED CODE ###

import sys

n = int(input())
list_input = list(map(int, sys.stdin.readline().strip().split()))

if 0 in list_input:
    print(0)
    exit()

a = 1
for i in list_input:
    a *= i
    if a > pow(10, 18):
        break


if a > pow(10, 18):
    print(-1)
else:
    print(a)
### BUGGY CODE ###

n = int(input())
A = []
for i in range(n):
    rs = list(map(int, input().split()))
    rs = [r-1 for r in rs]
    A.append(rs)

cnts = [0] * n
days = 1
while True:
    tekis = []
    # print("Day",days, cnts)
    for cnt,a in zip(cnts,A):
        if cnt == n-1: tekis.append(-1)
        else: tekis.append(a[cnt])
    # 敵の敵が自分だったらマッチング成立
    flg = False
    matched = [False] * n
    for i,teki in enumerate(tekis):
        if matched[teki] or teki==-1:continue
        if tekis[teki] == i:
            cnts[i] += 1
            matched[i] = True
            cnts[teki] += 1
            matched[teki] = True
            flg = True
            # print("{}vs{}".format(i,teki))
    if all([cnt == n-1 for cnt in cnts]):
        print(days)
        exit()  
    if not flg:
        print(-1)
        exit()
    days += 1

### FIXED CODE ###

import sys
input = sys.stdin.readline
from collections import defaultdict, deque
n = int(input())
X = 10000
INF = 1 << 50
edges = defaultdict(list)
edges_r = defaultdict(list)
def topological_sort(G):

    cnt_in = defaultdict(int)
    for k in G.keys():
        for v in G[k]:
            cnt_in[v] += 1
    # print(cnt_in)
    res = []
    # 入次数==0
    q = deque([i for i in list(G.keys()) if cnt_in[i]==0])
    while len(q) > 0:
        v = q.popleft()
        res.append(v)
        for next_v in G[v]:
            # 入次数を下げていく
            cnt_in[next_v] -= 1
            # 入次数が0に残ったところを次に追加
            if cnt_in[next_v] == 0:
                q.append(next_v)
    # トポソ不可能！
    if len(G) != len(res):
        print(-1)
        exit()
    return res

for i in range(n):
    r = list(map(int, input().split()))
    # 初期
    mn = min(i, r[0]-1)
    mx = max(i, r[0]-1)
    prev = mn * n + mx
    for e in r[1:]:
        e -= 1
        mn = min(i,e)
        mx = max(i,e)
        now = mn*n+mx
        edges[prev].append(now)
        edges_r[now].append(prev)
        prev = now
vs = topological_sort(edges)

# トポソの順番で経路長を入れていく
used = [0] * n**2
# for V in vs:
#     if used[V]: continue
#     used[V] = 1
#     q = deque([V])
#     while len(q) > 0:
#         v = q.popleft()
#         for u in edges[v]: 
#             if used[u] <= used[v]:
#                 q.append(u)
#                 used[u] = max(used[u], used[v] + 1)

for V in vs:
    if used[V]:continue
    used[V] = 1
    mx = 1
    for b in edges_r[V]:
        mx = max(mx, used[b]+1)
    used[V] = mx
# print(used)
print(max(used))
### BUGGY CODE ###

N, W = list(map(int, input().split()))
W += 1
A = [[0] * W for _ in range(N)]
wi, vi = list(map(int, input().split()))

for w in range(W):
    A[0][w] = 0 if w < wi else vi

for i in range(1, N):
    wi, vi = list(map(int, input().split()))
    for w in range(W):
        A[i][w] = max([
            A[i-1][w_] + vi for w_ in range(w - wi + 1)
        ] + [A[i-1][w]])

print(max(A[-1]))

### FIXED CODE ###

N, W = list(map(int, input().split()))
W += 1
A = [0] * W
wi, vi = list(map(int, input().split()))
A[wi] = vi

for _ in range(N-1):
    wi, vi = list(map(int, input().split()))
    tmp = [0] * W

    for w in range(W):
        if w-wi < 0:
            tmp[w] = A[w]
        else:
            tmp[w] = max(A[w-wi] + vi, A[w])

    A = tmp

print(max(A))
### BUGGY CODE ###

n = int(input())
l = list(map(int, input().split()))

l = l+l
xl = []
for i in range(n):
    a = 0
    m = False
    for j in range(i, n+i):
        if m is False:
            a += l[j]
            m = True
        else:
            a -= l[j]
            m = False
    xl.append(a)
    
print(" ".join(map(str, xl)))

### FIXED CODE ###

n = int(input())
l = list(map(int, input().split()))

l = l+l
xl = []
for i in range(2):
    a = 0
    m = False
    for j in range(i, n+i):
        if m is False:
            a += l[j]
            m = True
        else:
            a -= l[j]
            m = False
    xl.append(a)
    
for i in range(2, n):
    xl.append(xl[-2] - l[i-2]*2 + l[i-1]*2)
    
print(" ".join(map(str, xl)))
### BUGGY CODE ###

from collections import defaultdict,deque
import math
def gcd(a,b):
    return b if not a%b else gcd(b,a%b)
def lcm(a,b):
    return (a*b)//gcd(a,b)

def main():
    n = int(input())
    A=[int(i) for i in input().split()]
    res = 0
    m = 0
    for i in range(n):
        l_m = A[0]
        r_m = A[n-1]
        if(i!=0 and i!=n-1):
            for j in range(1,i):
                l_m = gcd(l_m,A[j])

            for j in range(n-1,i,-1):
                r_m = gcd(r_m,A[j])
            res = max(res,gcd(l_m,r_m))
        else:
            if(i==0):
                for j in range(1,n-1):
                    r_m = gcd(r_m,A[j])
                res = max(res,r_m)
            else:
                for j in range(1,n-1):
                    l_m = gcd(l_m,A[j])
                res = max(res,l_m)
    print(res)



if __name__ == '__main__':
    main()


### FIXED CODE ###

from collections import defaultdict,deque
import math
def gcd(a,b):
    return b if not a%b else gcd(b,a%b)
def lcm(a,b):
    return (a*b)//gcd(a,b)

def main():
    n = int(input())
    A=[int(i) for i in input().split()]
    res = 0
    m = 0
    l_g = [0,A[0]]
    r_g = [0,A[-1]]
    ind = 2
    for i in A[1:]:
        l_g.append(gcd(l_g[ind-1],i))
        ind += 1
    ind = 2
    for i in range(n-2,-1,-1):
        r_g.append(gcd(r_g[ind-1],A[i]))
        ind += 1
    r_g = r_g[::-1]
    r_g = r_g[1:]

    for i in range(len(r_g)):
        if(r_g[i]!=0 and l_g[i]!=0):
            res = max(res, gcd(r_g[i],l_g[i]))
        else:
            res = max(r_g[i],l_g[i],res)
    print(res)

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

def main():
    n = int(input())
    a = sorted(list(map(int,input().split())))
    for i in range(n-1):
        if a[i]!=0:
            flg = 0
            for j in range(i+1,n):
                if a[i]==a[j]:
                    flg=1
                    a[j] = 0
                elif a[j]%a[i]==0:
                    a[j] = 0
            if flg==1:
                a[i] = 0
    ans = 0
    for i in range(n):
        if a[i]!=0:
            ans+=1
    print(ans)

if __name__ == "__main__":
    main()


### FIXED CODE ###

def main():
    n = int(input())
    a = sorted(list(map(int,input().split())))
    dp = [-1 for i in range(a[-1])]
    for i in range(n):
        if dp[a[i]-1]==-1:
            dp[a[i]-1]=1
            for j in range(2,a[-1]//a[i]+1):
                dp[a[i]*j-1]=0
        elif dp[a[i]-1]==1:
            dp[a[i]-1]=0
    ans = 0
    for i in range(len(dp)):
        if dp[i]==1:
            ans += 1
    print(ans)

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

n = int(input())

li = [int(input()) for i in range(n)]

for i in range(n):
    if(i != 0):
        p1 = max(li[0:i])
    else:
        p1 = 0
    if(i != n-1):
        p2 = max(li[i+1:n])
    else:
        p2 = 0
    
    #print("{}, {}, {}".format(p1,p2,max(p1,p2)))
    print(max(p1,p2))

### FIXED CODE ###

n = int(input())

li = [int(input()) for i in range(n)]
ls = sorted(li, reverse = True)

m1 = ls[0]
m2 = ls[1]
"""
for i in range(0,n):
    if(ls[i] != m1):
        m2 = ls[i]
        break
"""

for i in li:
    print(m1 if(i != m1) else m2)
### BUGGY CODE ###

N, M = map(int, input().split())
AB = []
for i in range(N):
    AB.append(list(map(int, input().split())))
    
AB.sort(key=lambda x:x[0])

kane = 0
hon = 0
for i in range(N):
    if hon + AB[i][1] < M:
        hon += AB[i][1]
        kane += AB[i][0] * AB[i][1]
        continue
    elif hon + AB[i][1] == M:
        hon += AB[i][1]
        kane += AB[i][0] * AB[i][1]
        print(kane)
        break
    else:
        for j in range(AB[i][1]):
            hon += 1
            kane += AB[i][0]
            if hon == M:
                print(kane)
                break

### FIXED CODE ###

N, M = map(int, input().split())
AB = []
for i in range(N):
    AB.append(list(map(int, input().split())))
    
AB.sort(key=lambda x:x[0])

kane = 0
hon = 0
for i in range(N):
    if hon + AB[i][1] < M:
        hon += AB[i][1]
        kane += AB[i][0] * AB[i][1]
        continue
    elif hon + AB[i][1] == M:
        hon += AB[i][1]
        kane += AB[i][0] * AB[i][1]
        print(kane)
        break
    else:
        kane += (M - hon) * AB[i][0]
        print(kane)
        break
### BUGGY CODE ###

def dijkstra(s,n,cost,l):
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        for i in range(n):
            if (not used[i]) and (v == -1):
                v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
            break
        used[v] = True
        dist_v = d[v]
        for j in range(n):
            weight = cost[v][j]
            alt = dist_v + weight
            if (dist_v - 1) // l != (alt - 1) // l and dist_v != 0:
                alt = ((dist_v - 1) // l + 1) * l + weight         
            d[j] = min(d[j],alt)
    return d

n,m,l = map(int, input().split())
abc = [list(map(int, input().split())) for i in range(m)]
q = int(input())
st = [list(map(int, input().split())) for i in range(q)]
path = [[float("inf") for i in range(n)] for i in range(n)]
ans_list = []
for i in abc:
  a,b,cost = i[0]-1, i[1]-1, i[2]
  if cost <= l:
    path[a][b] = cost
    path[b][a] = cost
for s in range(n):
  ans_list.append(dijkstra(s,n,path,l))
for i in st:
  s, t = i[0] - 1, i[1] - 1
  ans = ans_list[s][t]
  if ans == float("inf"):
    ans = -1
  else:
    ans = (ans_list[s][t] - 1) // l
  print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline

def dijkstra(s,n,cost,l):
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        for i in range(n):
            if (not used[i]) and (v == -1):
                v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
            break
        used[v] = True
        dist_v = d[v]
        for j, weight in enumerate(cost[v]):
            alt = dist_v + weight
            r = (dist_v - 1) // l
            if r != ((alt - 1) // l) and dist_v != 0:
                alt = (r + 1) * l + weight         
            d[j] = min(d[j],alt)
    return d

n,m,l = map(int, input().split())
path = [[float("inf") for i in range(n)] for i in range(n)]
ans_list = []
for i in range(m):
  a,b,cost = map(int, input().split())
  a -= 1
  b -= 1
  if cost <= l:
    path[a][b] = cost
    path[b][a] = cost
for s in range(n):
  ans_list.append(dijkstra(s,n,path,l))
q = int(input())

for i in range(q):
  s,t = map(int, input().split())
  ans = ans_list[s-1][t-1]
  if ans == float("inf"):
    ans = -1
  else:
    ans = (ans - 1) // l
  print(ans)
### BUGGY CODE ###

s=input()
l=len(s)
q=int(input())
cnt=1

for _ in range(q):
	a=list(input().split())
	if len(a)==1:
		cnt*=(-1)
	else:
		if cnt==1:
			if a[1]=="1":
				s=a[2]+s
			else:
				s=s+a[2]
		else:
			if a[1]=="1":
				s=s+a[2]
			else:
				s=a[2]+s
				
if cnt==-1:
	s=s[::-1]
	
print(s)

### FIXED CODE ###

s=input()
l=len(s)
q=int(input())
cnt=1
l=str()
r=str()

for _ in range(q):
	a=list(input().split())
	if a[0]=="1":
		cnt*=(-1)
	else:
		if int(a[1])*cnt==1 or int(a[1])*cnt==-2:
			l+=a[2]
		else:
			r+=a[2]				

s=l[::-1]+s+r
if cnt==-1:
	s=s[::-1] 	

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

S = str(input())
# 余りのリスト
counts = [0] * 2019
n, d = 0, 1

for s in S[::-1]:
    n += int(s) * d
    d *= 10
    n %= 2019
    counts[n] += 1
ans = counts[0]
for count in counts:
    ans += count * (count - 1) // 2
print(ans)


### FIXED CODE ###

S = str(input())
# 余りのリスト
counts = [0] * 2019
n, d = 0, 1

for s in S[::-1]:
    n += int(s) * d
    d *= 10
    n %= 2019
    d %= 2019
    counts[n] += 1
ans = counts[0]
for count in counts:
    ans += count * (count - 1) // 2
print(ans)