from scipy.sparse.csgraph import floyd_warshall



N,M,L=list(map(int,input().split()))



A=[[10**12 for i in range(N+1)]for j in range(N+1)]

B=[[10**12 for i in range(N+1)]for j in range(N+1)]

for i in range(M):

    a,b,c=list(map(int,input().split()))

    if c<=L:

        A[a][b]=c

        A[b][a]=c

        B[a][b]=1

        B[b][a]=1



#print(A)





A= floyd_warshall(A,directed = False)

#print(A)

for i in range(N+1):

    for j in range(N+1):

        if A[i][j]<=L:

            B[i][j]=1

"""for c in range(1,N+1):

    for a in range(1,N+1):

        for b in range(1,N+1):

            A[a][b]=min(A[a][b],A[a][c]+A[c][b])

            if A[a][b]<=L:

                B[a][b]=1"""

#print(B)



B= floyd_warshall(B,directed = False)

"""for c in range(1,N+1):

    for a in range(1,N+1):

        for b in range(1,N+1):

            B[a][b]=min(B[a][b],B[a][c]+B[c][b])"""



#print(B)

Q=int(eval(input()))

for i in range(Q):

    s,t=list(map(int,input().split()))

    if B[s][t]==10**12:

        print((-1))

    else:

        print((int(B[s][t]-1)))
import sys

input = sys.stdin.readline

N,M,L=list(map(int,input().split()))



A=[[10**12 for i in range(N+1)]for j in range(N+1)]

B=[[10**12 for i in range(N+1)]for j in range(N+1)]

for i in range(M):

    a,b,c=list(map(int,input().split()))

    if c<=L:

        A[a][b]=c

        A[b][a]=c

        B[a][b]=1

        B[b][a]=1

for i in range(N+1):

    A[i][i]=0

    B[i][i]=0   

#print(A)



for c in range(1,N+1):

    for a in range(1,N+1):

        for b in range(1,N+1):

            A[a][b]=min(A[a][b],A[a][c]+A[c][b])

            if A[a][b]<=L:

                B[a][b]=1

#print(A)

for c in range(1,N+1):

    for a in range(1,N+1):

        for b in range(1,N+1):

            B[a][b]=min(B[a][b],B[a][c]+B[c][b])



#print(B)

Q=int(eval(input()))

for i in range(Q):

    s,t=list(map(int,input().split()))

    if B[s][t]==10**12:

        print((-1))

    else:

        print((B[s][t]-1))

from scipy.sparse.csgraph import floyd_warshall

N, M, L = list(map(int, input().split()))
A = [[10**12 for i in range(N + 1)] for j in range(N + 1)]
B = [[10**12 for i in range(N + 1)] for j in range(N + 1)]
for i in range(M):
    a, b, c = list(map(int, input().split()))
    if c <= L:
        A[a][b] = c
        A[b][a] = c
        B[a][b] = 1
        B[b][a] = 1
# print(A)
A = floyd_warshall(A, directed=False)
# print(A)
for i in range(N + 1):
    for j in range(N + 1):
        if A[i][j] <= L:
            B[i][j] = 1
"""for c in range(1,N+1):
    for a in range(1,N+1):
        for b in range(1,N+1):
            A[a][b]=min(A[a][b],A[a][c]+A[c][b])
            if A[a][b]<=L:
                B[a][b]=1"""
# print(B)
B = floyd_warshall(B, directed=False)
"""for c in range(1,N+1):
    for a in range(1,N+1):
        for b in range(1,N+1):
            B[a][b]=min(B[a][b],B[a][c]+B[c][b])"""
# print(B)
Q = int(eval(input()))
for i in range(Q):
    s, t = list(map(int, input().split()))
    if B[s][t] == 10**12:
        print((-1))
    else:
        print((int(B[s][t] - 1)))

import sys

input = sys.stdin.readline
N, M, L = list(map(int, input().split()))
A = [[10**12 for i in range(N + 1)] for j in range(N + 1)]
B = [[10**12 for i in range(N + 1)] for j in range(N + 1)]
for i in range(M):
    a, b, c = list(map(int, input().split()))
    if c <= L:
        A[a][b] = c
        A[b][a] = c
        B[a][b] = 1
        B[b][a] = 1
for i in range(N + 1):
    A[i][i] = 0
    B[i][i] = 0
# print(A)
for c in range(1, N + 1):
    for a in range(1, N + 1):
        for b in range(1, N + 1):
            A[a][b] = min(A[a][b], A[a][c] + A[c][b])
            if A[a][b] <= L:
                B[a][b] = 1
# print(A)
for c in range(1, N + 1):
    for a in range(1, N + 1):
        for b in range(1, N + 1):
            B[a][b] = min(B[a][b], B[a][c] + B[c][b])
# print(B)
Q = int(eval(input()))
for i in range(Q):
    s, t = list(map(int, input().split()))
    if B[s][t] == 10**12:
        print((-1))
    else:
        print((B[s][t] - 1))

-from scipy.sparse.csgraph import floyd_warshall +import sys +input = sys.stdin.readline +for i in range(N + 1): +    A[i][i] = 0 +    B[i][i] = 0 -A = floyd_warshall(A, directed=False) +for c in range(1, N + 1): +    for a in range(1, N + 1): +        for b in range(1, N + 1): +            A[a][b] = min(A[a][b], A[a][c] + A[c][b]) +            if A[a][b] <= L: +                B[a][b] = 1 -for i in range(N + 1): -    for j in range(N + 1): -        if A[i][j] <= L: -            B[i][j] = 1 -"""for c in range(1,N+1): -    for a in range(1,N+1): -        for b in range(1,N+1): -            A[a][b]=min(A[a][b],A[a][c]+A[c][b]) -            if A[a][b]<=L: -                B[a][b]=1""" -# print(B) -B = floyd_warshall(B, directed=False) -"""for c in range(1,N+1): -    for a in range(1,N+1): -        for b in range(1,N+1): -            B[a][b]=min(B[a][b],B[a][c]+B[c][b])""" +for c in range(1, N + 1): +    for a in range(1, N + 1): +        for b in range(1, N + 1): +            B[a][b] = min(B[a][b], B[a][c] + B[c][b]) -        print((int(B[s][t] - 1))) +        print((B[s][t] - 1))