Buggy python submission (verdict: Time Limit Exceeded):
def froyd(n, d):
    for k in range(n):
        for s in range(n):
            ds = d[s]
            for t in range(n):
                ds[t] = min(ds[t], ds[k] + d[k][t])


def main():
    N, M, L = [int(x) for x in input().split()]
    INF = float("inf")
    A = [[] for _ in range(N)]
    B = [[] for _ in range(N)]
    for i in range(N):
        A[i] = [INF] * N
        A[i][i] = 0
        B[i] = [INF] * N
        B[i][i] = 0
    for i in range(M):
        a, b, c = [int(x) for x in input().split()]
        A[a - 1][b - 1] = c
        A[b - 1][a - 1] = c
    froyd(N, A)
    # print(A)
    for s in range(N):
        for t in range(N):
            B[s][t] = 0 if s == t else 1 if A[s][t] <= L else INF
    # print(B)
    froyd(N, B)
    # print(B)

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


main()


Fixed submission:
def froyd(n, d):
    for k in range(n):
        for s in range(n):
            ds = d[s]
            for t in range(n):
                ds[t] = min(ds[t], ds[k] + d[k][t])


def main():
    N, M, L = [int(x) for x in input().split()]
    INF = float("inf")
    A = [[] for _ in range(N)]
    B = [[] for _ in range(N)]
    for i in range(N):
        A[i] = [INF] * N
        A[i][i] = 0
        B[i] = [INF] * N
        B[i][i] = 0
    for i in range(M):
        a, b, c = [int(x) for x in input().split()]
        A[a - 1][b - 1] = c
        A[b - 1][a - 1] = c
    froyd(N, A)
    # print(A)
    for s in range(N):
        for t in range(N):
            B[s][t] = 0 if s == t else 1 if A[s][t] <= L else INF
    # print(B)
    froyd(N, B)
    # print(B)

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


main()

Buggy python submission (verdict: Time Limit Exceeded):
def froyd(n, d):
    for k in range(n):
        for s in range(n):
            # ds = d[s]
            for t in range(n):
                d[s][t] = min(d[s][t], d[s][k] + d[k][t])


def main():
    N, M, L = [int(x) for x in input().split()]
    INF = float("inf")
    A = [[] for _ in range(N)]
    B = [[] for _ in range(N)]
    for i in range(N):
        A[i] = [INF] * N
        A[i][i] = 0
        B[i] = [INF] * N
        B[i][i] = 0
    for i in range(M):
        a, b, c = [int(x) for x in input().split()]
        A[a - 1][b - 1] = c
        A[b - 1][a - 1] = c
    froyd(N, A)
    # print(A)
    for s in range(N):
        for t in range(N):
            B[s][t] = 0 if s == t else 1 if A[s][t] <= L else INF
    # print(B)
    froyd(N, B)
    # print(B)

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


main()


Fixed submission:
def froyd(n, d):
    for k in range(n):
        for s in range(n):
            ds = d[s]
            for t in range(n):
                ds[t] = min(ds[t], ds[k] + d[k][t])


def main():
    N, M, L = [int(x) for x in input().split()]
    INF = float("inf")
    A = [[] for _ in range(N)]
    B = [[] for _ in range(N)]
    for i in range(N):
        A[i] = [INF] * N
        A[i][i] = 0
        B[i] = [INF] * N
        B[i][i] = 0
    for i in range(M):
        a, b, c = [int(x) for x in input().split()]
        A[a - 1][b - 1] = c
        A[b - 1][a - 1] = c
    froyd(N, A)
    # print(A)
    for s in range(N):
        for t in range(N):
            B[s][t] = 0 if s == t else 1 if A[s][t] <= L else INF
    # print(B)
    froyd(N, B)
    # print(B)

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


main()