Buggy python submission (verdict: Time Limit Exceeded):
N, M, L = map(int,input().split(' '))
Ms=[]
inf = 10**11
for i in range(N+1):
    #for j in range(N):
    Ms.append([inf]*(N+1) )
#print(d)
#print(d[1][1])
for i in range(M):
    a,b,c = map(int,input().split(' '))
    #print(a,b,c)
    Ms[a][b] = c
    Ms[b][a] = c
    #print(Ms)
Q = int(input())

def warshall_floyd(Ms):
    for k in range(1,len(Ms)):
        for i in range(1,len(Ms)):
            for j in range(1,len(Ms)):
                Ms[i][j]=min(Ms[i][j],Ms[i][k]+Ms[k][j])
    return Ms

Ms = warshall_floyd(Ms)
#print(Ms)
#print("Ms",Ms[3][2],Ms[2][3])
Ls = Ms.copy()
for i in range(N+1):
    for j in range(N+1):
        if Ls[i][j] <= L:
            Ls[i][j] = 1
        else:
            Ls[i][j]=inf
#print("Ls",Ls[3][2])
Ls = warshall_floyd(Ls)
#print(Ls)
for i in range(Q):
    s,t = map(int,input().split(' '))
    if Ls[s][t] == inf:
        print(-1)
    else:
        print(Ls[s][t]-1)



Fixed submission:
N, M, L = map(int,input().split(' '))
Ms=[]
inf = 10**11
for i in range(N+1):
    #for j in range(N):
    Ms.append([inf]*(N+1) )
#print(d)
#print(d[1][1])
for i in range(M):
    a,b,c = map(int,input().split(' '))
    #print(a,b,c)
    Ms[a][b] = c
    Ms[b][a] = c
    #print(Ms)
Q = int(input())

def warshall_floyd(Ms):
    for k in range(1,len(Ms)):
        for i in range(1,len(Ms)):
            for j in range(1,len(Ms)):
                Ms[i][j]=min(Ms[i][j],Ms[i][k]+Ms[k][j])
    return Ms

Ms = warshall_floyd(Ms)
#print(Ms)
#print("Ms",Ms[3][2],Ms[2][3])
#Ls = Ms.copy()
Ls = Ms
for i in range(N+1):
    for j in range(N+1):
        if Ls[i][j] <= L:
            Ls[i][j] = 1
        else:
            Ls[i][j]=inf
#print("Ls",Ls[3][2])
Ls = warshall_floyd(Ls)
#print(Ls)
for i in range(Q):
    s,t = map(int,input().split(' '))
    if Ls[s][t] == inf:
        print(-1)
    else:
        print(Ls[s][t]-1)

Buggy python submission (verdict: Runtime Error):
N, M, L = map(int,input().split(' '))
Ms=[]
inf = 10**11
for i in range(N+1):
    #for j in range(N):
    Ms.append([inf]*(N+1) )
#print(d)
#print(d[1][1])
for i in range(M):
    a,b,c = map(int,input().split(' '))
    #print(a,b,c)
    Ms[a][b] = c
    Ms[b][a] = c
    #print(Ms)
Q = int(input())

def warshall_floyd(Ms):
    for k in range(1,len(Ms)):
        for i in range(1,len(Ms)):
            for j in range(1,len(Ms)):
                Ms[i][j]=min(Ms[i][j],Ms[i][k]+Ms[k][j])
    return Ms

Ms = warshall_floyd(Ms)
#print(Ms)
#print("Ms",Ms[3][2],Ms[2][3])
Ls = Ms.copy()
for i in range(N+1):
    for j in range(N+1):
        if Ls[i][j] <= L:
            Ls[i][j] = 1
        else:
            Ls[i][j]=inf
#print("Ls",Ls[3][2])
Ls = warshall_floyd(Ls)
#print(Ls)
for i in range(Q):
    s,t = map(int,input().split(' '))
    if Ls[s][t] == inf:
        print(-1)
    else:
        print(Ls[s][t]-1)



Fixed submission:
N, M, L = map(int,input().split(' '))
Ms=[]
inf = 10**11
for i in range(N+1):
    #for j in range(N):
    Ms.append([inf]*(N+1) )
#print(d)
#print(d[1][1])
for i in range(M):
    a,b,c = map(int,input().split(' '))
    #print(a,b,c)
    Ms[a][b] = c
    Ms[b][a] = c
    #print(Ms)
Q = int(input())

def warshall_floyd(Ms):
    for k in range(1,len(Ms)):
        for i in range(1,len(Ms)):
            for j in range(1,len(Ms)):
                Ms[i][j]=min(Ms[i][j],Ms[i][k]+Ms[k][j])
    return Ms

Ms = warshall_floyd(Ms)
#print(Ms)
#print("Ms",Ms[3][2],Ms[2][3])
#Ls = Ms.copy()
Ls = Ms
for i in range(N+1):
    for j in range(N+1):
        if Ls[i][j] <= L:
            Ls[i][j] = 1
        else:
            Ls[i][j]=inf
#print("Ls",Ls[3][2])
Ls = warshall_floyd(Ls)
#print(Ls)
for i in range(Q):
    s,t = map(int,input().split(' '))
    if Ls[s][t] == inf:
        print(-1)
    else:
        print(Ls[s][t]-1)