Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
N, M, L = map(int, input().split())
V = {i: [] for i in range(1,N+1)}
for _ in range(M):
    a, b, c = map(int, input().split())
    V[a].append((b, c))
    V[b].append((a, c))
INF = float('inf')
VD = {i: {i: INF for i in range(1,N+1)} for i in range(1,N+1)}
for i, e in V.items():
    for (j, c) in e:
        VD[i][j] = c
        VD[j][i] = c
for i in range(1,N+1):
    VD[i][i] = 0

for k in range(1,N+1):
    for i in range(1,N+1):
        for j in range(1,N+1):
            if VD[i][j] > VD[i][k] + VD[k][j]:
                VD[i][j] = VD[i][k] + VD[k][j]

W = {i: [] for i in range(1,N+1)}
for i, v in VD.items():
    for j, d in v.items():
        if d <= L:
            W[i].append(j)
            W[j].append(i)

WD = {i: {i: INF for i in range(1,N+1)} for i in range(1,N+1)}
for i, e in W.items():
    for j in e:
        WD[i][j] = 1
        WD[j][i] = 1
for i in range(1,N+1):
    WD[i][i] = 0

for k in range(1,N+1):
    for i in range(1,N+1):
        for j in range(1,N+1):
            if WD[i][j] > WD[i][k] + WD[k][j]:
                WD[i][j] = WD[i][k] + WD[k][j]

Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    print(WD[s][t]-1 if WD[s][t] < INF else -1)


Fixed submission:
import sys
input = sys.stdin.readline
N, M, L = map(int, input().split())
INF = float('inf')
VD = [[INF] * N for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    VD[a][b] = c
    VD[b][a] = c
for i in range(N):
    VD[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            if VD[i][j] > VD[i][k] + VD[k][j]:
                VD[i][j] = VD[i][k] + VD[k][j]

WD = [[INF] * N for _ in range(N)]
for i in range(N):
    WD[i][i] = 0
    for j in range(i+1,N):
        d = VD[i][j]
        if d <= L:
            WD[i][j] = 1
            WD[j][i] = 1

for k in range(N):
    for i in range(N):
        for j in range(N):
            if WD[i][j] > WD[i][k] + WD[k][j]:
                WD[i][j] = WD[i][k] + WD[k][j]

Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    print(WD[s-1][t-1]-1 if WD[s-1][t-1] < INF else -1)

Buggy python submission (verdict: Time Limit Exceeded):
N, M, L = map(int, input().split())
V = {i: [] for i in range(1,N+1)}
for _ in range(M):
    a, b, c = map(int, input().split())
    V[a].append((b, c))
    V[b].append((a, c))
INF = float('inf')
VD = {i: {i: INF for i in range(1,N+1)} for i in range(1,N+1)}
for i, e in V.items():
    for (j, c) in e:
        VD[i][j] = c
        VD[j][i] = c
for i in range(1,N+1):
    VD[i][i] = 0

for k in range(1,N+1):
    for i in range(1,N+1):
        for j in range(1,N+1):
            if VD[i][j] > VD[i][k] + VD[k][j]:
                VD[i][j] = VD[i][k] + VD[k][j]

W = {i: [] for i in range(1,N+1)}
for i, v in VD.items():
    for j, d in v.items():
        if d <= L:
            W[i].append(j)
            W[j].append(i)

WD = {i: {i: INF for i in range(1,N+1)} for i in range(1,N+1)}
for i, e in W.items():
    for j in e:
        WD[i][j] = 1
        WD[j][i] = 1
for i in range(1,N+1):
    WD[i][i] = 0

for k in range(1,N+1):
    for i in range(1,N+1):
        for j in range(1,N+1):
            if WD[i][j] > WD[i][k] + WD[k][j]:
                WD[i][j] = WD[i][k] + WD[k][j]

Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    print(WD[s][t]-1 if WD[s][t] < INF else -1)


Fixed submission:
import sys
input = sys.stdin.readline
N, M, L = map(int, input().split())
INF = float('inf')
VD = [[INF] * N for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    VD[a][b] = c
    VD[b][a] = c
for i in range(N):
    VD[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            if VD[i][j] > VD[i][k] + VD[k][j]:
                VD[i][j] = VD[i][k] + VD[k][j]

WD = [[INF] * N for _ in range(N)]
for i in range(N):
    WD[i][i] = 0
    for j in range(i+1,N):
        d = VD[i][j]
        if d <= L:
            WD[i][j] = 1
            WD[j][i] = 1

for k in range(N):
    for i in range(N):
        for j in range(N):
            if WD[i][j] > WD[i][k] + WD[k][j]:
                WD[i][j] = WD[i][k] + WD[k][j]

Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    print(WD[s-1][t-1]-1 if WD[s-1][t-1] < INF else -1)