Buggy python submission (verdict: Time Limit Exceeded):
N,M,L = map(int, input().split())
RL=[list(map(int, input().split())) for _ in range(M)]
Q=int(input())
QL=[list(map(int, input().split())) for _ in range(Q)]
inf=float('inf')
fuel=[[inf]*N for _ in range(N)]
dist=[[inf]*N for _ in range(N)]
for i in range(N):
    fuel[i][i]=0
for I in RL:
    fuel[I[0]-1][I[1]-1]=fuel[I[1]-1][I[0]-1]=I[2]
for k in range(N):
    for i in range(N):
        for j in range(N):
            fuel[i][j]=min(fuel[i][j],fuel[i][k]+fuel[k][j])
for i in range(N):
    for j in range(N):
        if fuel[i][j] <= L:
            dist[i][j]=1
for i in range(N):
    dist[i][i]=0
for k in range(N):
    for i in range(N):
        for j in range(N):
            dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j])
for i in range(Q):
    print(dist[QL[i][0]-1][QL[i][1]-1]-1 if dist[QL[i][0]-1][QL[i][1]-1] != inf else -1)


Fixed submission:
def w(d):
    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])
    return d
N,M,L = map(int, input().split())
inf=float('inf')
fuel=[[inf]*N for _ in range(N)]
for i in range(N):
    fuel[i][i]=0
for _ in range(M):
    a,b,c=map(int,input().split())
    fuel[a-1][b-1]=fuel[b-1][a-1]=c
Q=int(input())
ST=[list(map(int, input().split())) for _ in range(Q)]
fuel = w(fuel)
for i in range(N):
    for j in range(N):
        if 0 < fuel[i][j] <= L:
            fuel[i][j]=1
        elif fuel[i][j] != 0:
            fuel[i][j] = inf
fuel = w(fuel)
for st in ST:
    print(fuel[st[0]-1][st[1]-1]-1 if fuel[st[0]-1][st[1]-1] != inf else -1)

Buggy python submission (verdict: Time Limit Exceeded):
N,M,L = map(int, input().split())
RL=[list(map(int, input().split())) for _ in range(M)]
Q=int(input())
QL=[list(map(int, input().split())) for _ in range(Q)]
inf=float('inf')
fuel=[[inf]*N for _ in range(N)]
dist=[[inf]*N for _ in range(N)]
for i in range(N):
    fuel[i][i]=dist[i][i]=0
for I in RL:
    fuel[I[0]-1][I[1]-1]=fuel[I[1]-1][I[0]-1]=I[2]
for k in range(N):
    for i in range(N):
        for j in range(N):
            fuel[i][j]=min(fuel[i][j],fuel[i][k]+fuel[k][j])
for i in range(N):
    for j in range(N):
        if fuel[i][j] <= L:
            dist[i][j]=min(dist[i][j],1)
for k in range(N):
    for i in range(N):
        for j in range(N):
            dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j])
for i in range(Q):
    print(dist[QL[i][0]-1][QL[i][1]-1]-1 if dist[QL[i][0]-1][QL[i][1]-1] != inf else -1)


Fixed submission:
def w(d):
    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])
    return d
N,M,L = map(int, input().split())
inf=float('inf')
fuel=[[inf]*N for _ in range(N)]
for i in range(N):
    fuel[i][i]=0
for _ in range(M):
    a,b,c=map(int,input().split())
    fuel[a-1][b-1]=fuel[b-1][a-1]=c
Q=int(input())
ST=[list(map(int, input().split())) for _ in range(Q)]
fuel = w(fuel)
for i in range(N):
    for j in range(N):
        if 0 < fuel[i][j] <= L:
            fuel[i][j]=1
        elif fuel[i][j] != 0:
            fuel[i][j] = inf
fuel = w(fuel)
for st in ST:
    print(fuel[st[0]-1][st[1]-1]-1 if fuel[st[0]-1][st[1]-1] != inf else -1)

Buggy python submission (verdict: Time Limit Exceeded):
N,M,L = map(int, input().split())
RL=[list(map(int, input().split())) for _ in range(M)]
Q=int(input())
QL=[list(map(int, input().split())) for _ in range(Q)]
inf=float('inf')
fuel=[[inf]*N for _ in range(N)]
dist=[[inf]*N for _ in range(N)]
for i in range(N):
    fuel[i][i]=dist[i][i]=0
for I in RL:
    fuel[I[0]-1][I[1]-1]=fuel[I[1]-1][I[0]-1]=I[2]
for k in range(N):
    for i in range(N):
        for j in range(N):
            fuel[i][j]=min(fuel[i][j],fuel[i][k]+fuel[k][j])
for i in range(N):
    for j in range(N):
        if fuel[i][j] <= L:
            dist[i][j]=min(dist[i][j],1)
for k in range(N):
    for i in range(N):
        for j in range(N):
            dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j])
for i in range(Q):
    print(dist[QL[i][0]-1][QL[i][1]-1]-1 if dist[QL[i][0]-1][QL[i][1]-1] != inf else -1)


Fixed submission:
def w(d):
    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])
    return d
N,M,L = map(int, input().split())
inf=float('inf')
fuel=[[inf]*N for _ in range(N)]
for i in range(N):
    fuel[i][i]=0
for _ in range(M):
    a,b,c=map(int,input().split())
    fuel[a-1][b-1]=fuel[b-1][a-1]=c
Q=int(input())
ST=[list(map(int, input().split())) for _ in range(Q)]
fuel = w(fuel)
for i in range(N):
    for j in range(N):
        if 0 < fuel[i][j] <= L:
            fuel[i][j]=1
        elif fuel[i][j] != 0:
            fuel[i][j] = inf
fuel = w(fuel)
for st in ST:
    print(fuel[st[0]-1][st[1]-1]-1 if fuel[st[0]-1][st[1]-1] != inf else -1)