Buggy python submission (verdict: Time Limit Exceeded):
N,M,L = map(int,input().split())
INF = (500, 10**9)
dist = [[INF if i != j else (0,0) for i in range(N)] for j in range(N)]
edge = [[] for i in range(N)]
for i in range(M):
	A,B,C = map(int,input().split())
	if C <= L:
		edge[A-1].append((B-1, C))
		edge[B-1].append((A-1, C))

import heapq as hq
for i in range(N):
	searched = [False for j in range(N)]
	n = i
	while n != None:
		searched[n] = True
		for e in edge[n]:
			if searched[e[0]]:
				continue
			d = dist[i][n]
			n2, d2 = e[0], e[1]
			if d[1] + d2 > L:
				new_d = (d[0] + 1, d2)
			else:
				new_d = (d[0], d[1] + d2)
			dist[i][n2] = min(dist[i][n2], new_d)
		n = None
		m = INF
		for j in range(N):
			if not searched[j] and dist[i][j] < m:
				n = j
				m = dist[i][j]


Q = int(input())
for q in range(Q):
	s,t = map(int,input().split())
	print(dist[s-1][t-1][0] if dist[s-1][t-1] != INF else -1)


Fixed submission:
import sys
input = sys.stdin.readline

N,M,L = map(int,input().split())
ABC = [list(map(int,input().split())) for _ in [0]*M]
Q = int(input())
ST = [list(map(int,input().split())) for _ in [0]*Q]


INF = (L+1)*N
d = [[INF]*N for _ in [0]*N]
for i in range(N):
    d[i][i] = 0
for a,b,c in ABC:
    a-=1;b-=1
    d[a][b] = c
    d[b][a] = c

def Warshall_Floyd(d,N=None):
    if N==None : N = len(d)
    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])

Warshall_Floyd(d,N)

d2 = [None]*N
for i,row in enumerate(d):
    d2[i] = [1 if e<=L else INF for e in row]
    d2[i][i] = 0

Warshall_Floyd(d2,N)

for s,t in ST:
    s-=1;t-=1
    ans = (d2[s][t]%INF)-1
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
from collections import defaultdict


N, M, L = map(int, input().split())
graph = defaultdict(list)
for _ in range(M):
    a, b, c = map(int, input().split())
    graph[a].append([b, c])
    graph[b].append([a, c])

def search(s, t, L):
    queue = [(0, -L, s)]
    while queue:
        cost, fuel, cur = heapq.heappop(queue)
        if cur == t:
            return cost
        for nxt, c in graph[cur]:
            if fuel + c <= 0:
                heapq.heappush(queue, (cost, fuel + c, nxt))
                heapq.heappush(queue, (cost + 1, -L,  nxt))
    return -1


Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    print(search(s, t, L))

Fixed submission:
import sys
input = sys.stdin.readline

N,M,L = map(int,input().split())
ABC = [list(map(int,input().split())) for _ in [0]*M]
Q = int(input())
ST = [list(map(int,input().split())) for _ in [0]*Q]


INF = (L+1)*N
d = [[INF]*N for _ in [0]*N]
for i in range(N):
    d[i][i] = 0
for a,b,c in ABC:
    a-=1;b-=1
    d[a][b] = c
    d[b][a] = c

def Warshall_Floyd(d,N=None):
    if N==None : N = len(d)
    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])

Warshall_Floyd(d,N)

d2 = [None]*N
for i,row in enumerate(d):
    d2[i] = [1 if e<=L else INF for e in row]
    d2[i][i] = 0

Warshall_Floyd(d2,N)

for s,t in ST:
    s-=1;t-=1
    ans = (d2[s][t]%INF)-1
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
from collections import defaultdict


N, M, L = map(int, input().split())
graph = defaultdict(list)
for _ in range(M):
    a, b, c = map(int, input().split())
    graph[a].append([b, c])
    graph[b].append([a, c])

def search(s, t, L):
    queue = [(0, -L, s)]
    while queue:
        cost, fuel, cur = heapq.heappop(queue)
        if cur == t:
            return cost
        for nxt, c in graph[cur]:
            if fuel + c <= 0:
                heapq.heappush(queue, (cost, fuel + c, nxt))
                heapq.heappush(queue, (cost + 1, -L,  nxt))
    return -1


Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    print(search(s, t, L))

Fixed submission:
import sys
input = sys.stdin.readline

N,M,L = map(int,input().split())
ABC = [list(map(int,input().split())) for _ in [0]*M]
Q = int(input())
ST = [list(map(int,input().split())) for _ in [0]*Q]


INF = (L+1)*N
d = [[INF]*N for _ in [0]*N]
for i in range(N):
    d[i][i] = 0
for a,b,c in ABC:
    a-=1;b-=1
    d[a][b] = c
    d[b][a] = c

def Warshall_Floyd(d,N=None):
    if N==None : N = len(d)
    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])

Warshall_Floyd(d,N)

d2 = [None]*N
for i,row in enumerate(d):
    d2[i] = [1 if e<=L else INF for e in row]
    d2[i][i] = 0

Warshall_Floyd(d2,N)

for s,t in ST:
    s-=1;t-=1
    ans = (d2[s][t]%INF)-1
    print(ans)