Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
N,M,L = map(int,input().split())
ABC = [tuple(map(int,input().split())) for i in range(M)]
Q = int(input())
ST = [tuple(map(int,input().split())) for i in range(Q)]

INF = 10**20
g = [[INF]*N for _ in range(N)]
for a,b,c in ABC:
    a,b = a-1,b-1
    g[a][b] = g[b][a] = c
for i in range(N):
    g[i][i] = 0
for k in range(N):
    for i in range(N):
        for j in range(N):
            g[i][j] = min(g[i][j], g[i][k] + g[k][j])

h = [[N]*N for _ in range(N)]
for i in range(N-1):
    for j in range(i+1,N):
        if g[i][j] <= L:
            h[i][j] = h[j][i] = 1
for i in range(N):
    h[i][i] = 0
for k in range(N):
    for i in range(N):
        for j in range(N):
            h[i][j] = min(h[i][j], h[i][k] + h[k][j])

ans = []
for s,t in ST:
    s,t = s-1,t-1
    if h[s][t] == N:
        ans.append(-1)
    else:
        ans.append(h[s][t] - 1)
print(*ans, sep='\n')

Fixed submission:
import sys
input = sys.stdin.readline
N,M,L = map(int,input().split())
ABC = [tuple(map(int,input().split())) for i in range(M)]
Q = int(input())
ST = [tuple(map(int,input().split())) for i in range(Q)]

INF = float('inf')
dist1 = [[INF]*N for i in range(N)]
dist2 = [[INF]*N for i in range(N)]
for a,b,c in ABC:
    if c > L: continue
    a,b = a-1,b-1
    dist1[a][b] = dist1[b][a] = c
for i in range(N):
    dist1[i][i] = 0
    dist2[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist1[i][k] + dist1[k][j]
            if dist1[i][j] <= d: continue
            dist1[i][j] = d
for i in range(N):
    for j in range(N):
        if i==j: continue
        if dist1[i][j] <= L:
            dist2[i][j] = 1
for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist2[i][k] + dist2[k][j]
            if dist2[i][j] <= d: continue
            dist2[i][j] = d
ans = []
for s,t in ST:
    d = dist2[s-1][t-1]
    if d == INF:
        ans.append(-1)
    else:
        ans.append(d-1)
print(*ans, sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
N,M,L = map(int,input().split())
ABC = [tuple(map(int,input().split())) for i in range(M)]
Q = int(input())
ST = [tuple(map(int,input().split())) for i in range(Q)]

INF = float('inf')
dist1 = [[INF]*N for i in range(N)]
dist2 = [[INF]*N for i in range(N)]
for a,b,c in ABC:
    if c > L: continue
    a,b = a-1,b-1
    dist1[a][b] = dist1[b][a] = c
for i in range(N):
    dist1[i][i] = 0
    dist2[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            dist1[i][j] = min(dist1[i][j], dist1[i][k] + dist1[k][j])
for i in range(N):
    for j in range(N):
        if i==j: continue
        if dist1[i][j] <= L:
            dist2[i][j] = 1
for k in range(N):
    for i in range(N):
        for j in range(N):
            dist2[i][j] = min(dist2[i][j], dist2[i][k] + dist2[k][j])

ans = []
for s,t in ST:
    d = dist2[s-1][t-1]
    if d == INF:
        ans.append(-1)
    else:
        ans.append(d-1)
print(*ans, sep='\n')

Fixed submission:
import sys
input = sys.stdin.readline
N,M,L = map(int,input().split())
ABC = [tuple(map(int,input().split())) for i in range(M)]
Q = int(input())
ST = [tuple(map(int,input().split())) for i in range(Q)]

INF = float('inf')
dist1 = [[INF]*N for i in range(N)]
dist2 = [[INF]*N for i in range(N)]
for a,b,c in ABC:
    if c > L: continue
    a,b = a-1,b-1
    dist1[a][b] = dist1[b][a] = c
for i in range(N):
    dist1[i][i] = 0
    dist2[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist1[i][k] + dist1[k][j]
            if dist1[i][j] <= d: continue
            dist1[i][j] = d
for i in range(N):
    for j in range(N):
        if i==j: continue
        if dist1[i][j] <= L:
            dist2[i][j] = 1
for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist2[i][k] + dist2[k][j]
            if dist2[i][j] <= d: continue
            dist2[i][j] = d
ans = []
for s,t in ST:
    d = dist2[s-1][t-1]
    if d == INF:
        ans.append(-1)
    else:
        ans.append(d-1)
print(*ans, sep='\n')

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N,M,L = map(int,input().split())
ABC = [tuple(map(int,input().split())) for i in range(M)]
Q = int(input())
ST = [tuple(map(int,input().split())) for i in range(Q)]

INF = float('inf')
dist = [[INF]*N for i in range(N)]
st = set()
for a,b,c in ABC:
    if c > L: continue
    a,b = a-1,b-1
    dist[a][b] = dist[b][a] = c
    st.add((a,b))
    st.add((b,a))
for i in range(N):
    dist[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            if dist[i][k] == INF or dist[k][j] == INF: continue
            a,x = divmod(dist[i][k],L)
            b,y = divmod(dist[k][j],L)
            if x+y > L:
                dist[i][j] = min(dist[i][j], (a+b+1)*L + min(x,y))
            else:
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
            print(i,j,dist[i][j])

ans = []
for s,t in ST:
    s,t = s-1,t-1
    if (s,t) in st:
        ans.append(0)
    elif dist[s][t] == INF:
        ans.append(-1)
    else:
        ans.append(dist[s][t])//L))
print(*ans, sep='\n')

Fixed submission:
import sys
input = sys.stdin.readline
N,M,L = map(int,input().split())
ABC = [tuple(map(int,input().split())) for i in range(M)]
Q = int(input())
ST = [tuple(map(int,input().split())) for i in range(Q)]

INF = float('inf')
dist1 = [[INF]*N for i in range(N)]
dist2 = [[INF]*N for i in range(N)]
for a,b,c in ABC:
    if c > L: continue
    a,b = a-1,b-1
    dist1[a][b] = dist1[b][a] = c
for i in range(N):
    dist1[i][i] = 0
    dist2[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist1[i][k] + dist1[k][j]
            if dist1[i][j] <= d: continue
            dist1[i][j] = d
for i in range(N):
    for j in range(N):
        if i==j: continue
        if dist1[i][j] <= L:
            dist2[i][j] = 1
for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist2[i][k] + dist2[k][j]
            if dist2[i][j] <= d: continue
            dist2[i][j] = d
ans = []
for s,t in ST:
    d = dist2[s-1][t-1]
    if d == INF:
        ans.append(-1)
    else:
        ans.append(d-1)
print(*ans, sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
import heapq,sys
input = sys.stdin.readline
N,M,L = map(int,input().split())
ABC = [tuple(map(int,input().split())) for i in range(M)]
Q = int(input())
ST = [tuple(map(int,input().split())) for i in range(Q)]

es = [[] for i in range(N)]
for a,b,c in ABC:
    if c > L: continue
    a,b = a-1,b-1
    es[a].append((b,c))
    es[b].append((a,c))

INF = float('inf')
dist = [[INF]*N for i in range(N)]
for i in range(N):
    dist[i][i] = 0

for i in range(N):
    hq = [i]
    heapq.heapify(hq)
    while hq:
        v = heapq.heappop(hq)
        k,j = divmod(v,N)
        k,l = divmod(k,L)
        for to,c in es[j]:
            if l+c > L:
                newcost = (k+1)*L + c
            else:
                newcost = k*L + l + c
            if newcost < dist[i][to]:
                dist[i][to] = newcost
                heapq.heappush(hq, newcost*N + to)

ans = []
for s,t in ST:
    s,t = s-1,t-1
    if dist[s][t] == INF:
        ans.append(-1)
    else:
        ans.append((dist[s][t]-1)//L)
print(*ans, sep='\n')

Fixed submission:
import sys
input = sys.stdin.readline
N,M,L = map(int,input().split())
ABC = [tuple(map(int,input().split())) for i in range(M)]
Q = int(input())
ST = [tuple(map(int,input().split())) for i in range(Q)]

INF = float('inf')
dist1 = [[INF]*N for i in range(N)]
dist2 = [[INF]*N for i in range(N)]
for a,b,c in ABC:
    if c > L: continue
    a,b = a-1,b-1
    dist1[a][b] = dist1[b][a] = c
for i in range(N):
    dist1[i][i] = 0
    dist2[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist1[i][k] + dist1[k][j]
            if dist1[i][j] <= d: continue
            dist1[i][j] = d
for i in range(N):
    for j in range(N):
        if i==j: continue
        if dist1[i][j] <= L:
            dist2[i][j] = 1
for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist2[i][k] + dist2[k][j]
            if dist2[i][j] <= d: continue
            dist2[i][j] = d
ans = []
for s,t in ST:
    d = dist2[s-1][t-1]
    if d == INF:
        ans.append(-1)
    else:
        ans.append(d-1)
print(*ans, sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N,M,L = map(int,input().split())
ABC = [tuple(map(int,input().split())) for i in range(M)]
Q = int(input())
ST = [tuple(map(int,input().split())) for i in range(Q)]

es = [[] for i in range(N)]
for a,b,c in ABC:
    if c > L: continue
    a,b = a-1,b-1
    es[a].append((b,c))
    es[b].append((a,c))

INF = float('inf')
dist = [[INF]*N for i in range(N)]
for i in range(N):
    dist[i][i] = 0

for i in range(N):
    hq = [i]
    heapq.heapify(hq)
    while hq:
        v = heapq.heappop(hq)
        k,j = divmod(v,N)
        k,l = divmod(k,L)
        for to,c in es[j]:
            if l+c > L:
                newcost = (k+1)*L + c
            else:
                newcost = k*L + l + c
            if newcost < dist[i][to]:
                dist[i][to] = newcost
                heapq.heappush(hq, newcost*N + to)

for s,t in ST:
    s,t = s-1,t-1
    if dist[s][t] == INF:
        print(-1)
    else:
        print((dist[s][t]-1)//L)

Fixed submission:
import sys
input = sys.stdin.readline
N,M,L = map(int,input().split())
ABC = [tuple(map(int,input().split())) for i in range(M)]
Q = int(input())
ST = [tuple(map(int,input().split())) for i in range(Q)]

INF = float('inf')
dist1 = [[INF]*N for i in range(N)]
dist2 = [[INF]*N for i in range(N)]
for a,b,c in ABC:
    if c > L: continue
    a,b = a-1,b-1
    dist1[a][b] = dist1[b][a] = c
for i in range(N):
    dist1[i][i] = 0
    dist2[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist1[i][k] + dist1[k][j]
            if dist1[i][j] <= d: continue
            dist1[i][j] = d
for i in range(N):
    for j in range(N):
        if i==j: continue
        if dist1[i][j] <= L:
            dist2[i][j] = 1
for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist2[i][k] + dist2[k][j]
            if dist2[i][j] <= d: continue
            dist2[i][j] = d
ans = []
for s,t in ST:
    d = dist2[s-1][t-1]
    if d == INF:
        ans.append(-1)
    else:
        ans.append(d-1)
print(*ans, sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N,M,L = map(int,input().split())
ABC = [tuple(map(int,input().split())) for i in range(M)]
Q = int(input())
ST = [tuple(map(int,input().split())) for i in range(Q)]

es = [[] for i in range(N)]
for a,b,c in ABC:
    if c > L: continue
    a,b = a-1,b-1
    es[a].append((b,c))
    es[b].append((a,c))

INF = float('inf')
dist = [[INF]*N for i in range(N)]
for i in range(N):
    dist[i][i] = 0

for i in range(N):
    hq = [(0,i)]
    heapq.heapify(hq)
    while hq:
        k,j = heapq.heappop(hq)
        k,l = divmod(k,L)
        for to,c in es[j]:
            if l+c > L:
                newcost = (k+1)*L + c
            else:
                newcost = k*L + l + c
            if newcost < dist[i][to]:
                dist[i][to] = newcost
                heapq.heappush(hq, (newcost, to))

for s,t in ST:
    s,t = s-1,t-1
    if dist[s][t] == INF:
        print(-1)
    else:
        print((dist[s][t]-1)//L)

Fixed submission:
import sys
input = sys.stdin.readline
N,M,L = map(int,input().split())
ABC = [tuple(map(int,input().split())) for i in range(M)]
Q = int(input())
ST = [tuple(map(int,input().split())) for i in range(Q)]

INF = float('inf')
dist1 = [[INF]*N for i in range(N)]
dist2 = [[INF]*N for i in range(N)]
for a,b,c in ABC:
    if c > L: continue
    a,b = a-1,b-1
    dist1[a][b] = dist1[b][a] = c
for i in range(N):
    dist1[i][i] = 0
    dist2[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist1[i][k] + dist1[k][j]
            if dist1[i][j] <= d: continue
            dist1[i][j] = d
for i in range(N):
    for j in range(N):
        if i==j: continue
        if dist1[i][j] <= L:
            dist2[i][j] = 1
for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist2[i][k] + dist2[k][j]
            if dist2[i][j] <= d: continue
            dist2[i][j] = d
ans = []
for s,t in ST:
    d = dist2[s-1][t-1]
    if d == INF:
        ans.append(-1)
    else:
        ans.append(d-1)
print(*ans, sep='\n')