Buggy python submission (verdict: Time Limit Exceeded):
def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    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())
d = [[10**12 for i in range(n)] for i in range(n)]
for i in range(m):
    ai, bi, ci = map(int, input().split())
    d[ai-1][bi-1] = ci
    d[bi-1][ai-1] = ci
for i in range(n):
    d[i][i] = 0 #自身のところに行くコストは０

d = warshall_floyd(d)
for i in range(n):
    for j in range(n):
        if d[i][j] <= l:
            d[i][j] = 1
        else:
            d[i][j] = 10**12

d = warshall_floyd(d)

q = int(input())
for i in range(q):
    si, ti = map(int, input().split())
    if d[si-1][ti-1] >= 10**12:
        print(-1)
    else:
        print(d[si-1][ti-1]-1)

Fixed submission:
def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    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())
d = [[10**12 for i in range(n)] for i in range(n)]
for i in range(m):
    ai, bi, ci = map(int, input().split())
    d[ai-1][bi-1] = ci
    d[bi-1][ai-1] = ci
for i in range(n):
    d[i][i] = 0 #自身のところに行くコストは０

d = warshall_floyd(d)
for i in range(n):
    for j in range(n):
        if d[i][j] <= l:
            d[i][j] = 1
        else:
            d[i][j] = 10**12

d = warshall_floyd(d)

q = int(input())
for i in range(q):
    si, ti = map(int, input().split())
    if d[si-1][ti-1] >= 10**12:
        print(-1)
    else:
        print(d[si-1][ti-1]-1)

Buggy python submission (verdict: Time Limit Exceeded):
def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    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())
d = [[float("inf") for i in range(n)] for i in range(n)]
for i in range(m):
    ai, bi, ci = map(int, input().split())
    d[ai-1][bi-1] = ci
    d[bi-1][ai-1] = ci
for i in range(n):
    d[i][i] = 0 #自身のところに行くコストは０

d = warshall_floyd(d)
for i in range(n):
    for j in range(n):
        if d[i][j] <= l:
            d[i][j] = 1
        else:
            d[i][j] = float("inf")

d = warshall_floyd(d)

q = int(input())
for i in range(q):
    si, ti = map(int, input().split())
    if d[si-1][ti-1] == float('inf'):
        print(-1)
    else:
        print(d[si-1][ti-1]-1)

Fixed submission:
def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    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())
d = [[10**12 for i in range(n)] for i in range(n)]
for i in range(m):
    ai, bi, ci = map(int, input().split())
    d[ai-1][bi-1] = ci
    d[bi-1][ai-1] = ci
for i in range(n):
    d[i][i] = 0 #自身のところに行くコストは０

d = warshall_floyd(d)
for i in range(n):
    for j in range(n):
        if d[i][j] <= l:
            d[i][j] = 1
        else:
            d[i][j] = 10**12

d = warshall_floyd(d)

q = int(input())
for i in range(q):
    si, ti = map(int, input().split())
    if d[si-1][ti-1] >= 10**12:
        print(-1)
    else:
        print(d[si-1][ti-1]-1)

Buggy python submission (verdict: Time Limit Exceeded):
def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    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())
d = [[float("inf") for i in range(n)] for i in range(n)]
for i in range(m):
    ai, bi, ci = map(int, input().split())
    d[ai-1][bi-1] = ci
    d[bi-1][ai-1] = ci
for i in range(n):
    d[i][i] = 0 #自身のところに行くコストは０

d = warshall_floyd(d)
for i in range(n):
    for j in range(n):
        if d[i][j] <= l:
            d[i][j] = 1
        else:
            d[i][j] = float("inf")

d = warshall_floyd(d)

q = int(input())
for i in range(q):
    si, ti = map(int, input().split())
    if d[si-1][ti-1] == float('inf'):
        print(-1)
    else:
        print(d[si-1][ti-1]-1)

Fixed submission:
def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    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())
d = [[10**12 for i in range(n)] for i in range(n)]
for i in range(m):
    ai, bi, ci = map(int, input().split())
    d[ai-1][bi-1] = ci
    d[bi-1][ai-1] = ci
for i in range(n):
    d[i][i] = 0 #自身のところに行くコストは０

d = warshall_floyd(d)
for i in range(n):
    for j in range(n):
        if d[i][j] <= l:
            d[i][j] = 1
        else:
            d[i][j] = 10**12

d = warshall_floyd(d)

q = int(input())
for i in range(q):
    si, ti = map(int, input().split())
    if d[si-1][ti-1] >= 10**12:
        print(-1)
    else:
        print(d[si-1][ti-1]-1)