from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall


def solve():
    N, M, L = map(int, input().split())
    dist = [[0 for _ in range(N)] for _ in range(N)]
    for i in range(M):
        a, b, c = map(int, input().split())
        dist[a-1][b-1] = c
        dist[b-1][a-1] = c
    fw = floyd_warshall(csr_matrix(dist))

    ok = [[0 for _ in range(N)] for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if fw[i][j] <= L:
                ok[i][j] = 1
    ans = floyd_warshall(csr_matrix(ok))

    Q = int(input())
    for i in range(Q):
        s, t = map(lambda x: int(x)-1, input().split())
        if ans[s][t] < N:
            print(int(ans[s][t])-1)
        else:
            print(-1)


if __name__ == '__main__':
    solve() from scipy.sparse import csr_matrix from scipy.sparse.csgraph import floyd_warshall def solve():
    N, M, L = map(int, input().split())
    dist = [[0 for _ in range(N)] for _ in range(N)]
    for i in range(M):
        a, b, c = map(int, input().split())
        dist[a-1][b-1] = c
        dist[b-1][a-1] = c
    fw = floyd_warshall(csr_matrix(dist))

    ok = [[0 for _ in range(N)] for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if fw[i][j] <= L:
                ok[i][j] = 1
    ans = floyd_warshall(csr_matrix(ok))

    Q = int(input())
    for i in range(Q):
        s, t = map(lambda x: int(x)-1, input().split())
        if ans[s][t] < N:
            print(int(ans[s][t])-1)
        else:
            print(-1) solve N, M, L = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M L [0 for _ in range(N)] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [0 for _ in range(N)] dist = [[0 for _ in range(N)] for _ in range(N)] dist [[0 for _ in range(N)] for _ in range(N)] for i in range(M):
        a, b, c = map(int, input().split())
        dist[a-1][b-1] = c
        dist[b-1][a-1] = c
     i range(M) range M a, b, c = map(int, input().split()) a map(int, input().split()) map int input().split() ().split () input split b c dist[a-1][b-1] = c dist[a-1][b-1] [a-1] dist a-1 a 1 b-1 b 1 c dist[b-1][a-1] = c dist[b-1][a-1] [b-1] dist b-1 b 1 a-1 a 1 c fw = floyd_warshall(csr_matrix(dist)) fw floyd_warshall(csr_matrix(dist)) floyd_warshall csr_matrix(dist) csr_matrix dist [0 for _ in range(N)] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [0 for _ in range(N)] ok = [[0 for _ in range(N)] for _ in range(N)] ok [[0 for _ in range(N)] for _ in range(N)] for i in range(N):
        for j in range(N):
            if fw[i][j] <= L:
                ok[i][j] = 1
     i range(N) range N for j in range(N):
            if fw[i][j] <= L:
                ok[i][j] = 1
     j range(N) range N if fw[i][j] <= L:
                ok[i][j] = 1
     fw[i][j] <= L fw[i][j] [i] fw i j L ok[i][j] = 1 ok[i][j] [i] ok i j 1 ans = floyd_warshall(csr_matrix(ok)) ans floyd_warshall(csr_matrix(ok)) floyd_warshall csr_matrix(ok) csr_matrix ok Q = int(input()) Q int(input()) int input() input for i in range(Q):
        s, t = map(lambda x: int(x)-1, input().split())
        if ans[s][t] < N:
            print(int(ans[s][t])-1)
        else:
            print(-1) i range(Q) range Q s, t = map(lambda x: int(x)-1, input().split()) s map(lambda x: int(x)-1, input().split()) map lambda x: int(x)-1 int(x)-1 int(x) int x 1 x input().split() ().split () input split t if ans[s][t] < N:
            print(int(ans[s][t])-1)
        else:
            print(-1) ans[s][t] < N ans[s][t] [s] ans s t N print(int(ans[s][t])-1) print int(ans[s][t])-1 int(ans[s][t]) int ans[s][t] [s] ans s t 1 print(-1) print -1 if __name__ == '__main__':
    solve() __name__ == '__main__' __name__ '__main__' solve() solve def solve():
    N, M, L = map(int, input().split())
    dist = [[0 for _ in range(N)] for _ in range(N)]
    for i in range(M):
        a, b, c = map(int, input().split())
        dist[a-1][b-1] = c
        dist[b-1][a-1] = c
    fw = floyd_warshall(csr_matrix(dist))

    ok = [[0 for _ in range(N)] for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if fw[i][j] <= L:
                ok[i][j] = 1
    ans = floyd_warshall(csr_matrix(ok))

    Q = int(input())
    for i in range(Q):
        s, t = map(lambda x: int(x)-1, input().split())
        if ans[s][t] < N:
            print(int(ans[s][t])-1)
        else:
            print(-1) def solve():
    N, M, L = map(int, input().split())
    dist = [[0 for _ in range(N)] for _ in range(N)]
    for i in range(M):
        a, b, c = map(int, input().split())
        dist[a-1][b-1] = c
        dist[b-1][a-1] = c
    fw = floyd_warshall(csr_matrix(dist))

    ok = [[0 for _ in range(N)] for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if fw[i][j] <= L:
                ok[i][j] = 1
    ans = floyd_warshall(csr_matrix(ok))

    Q = int(input())
    for i in range(Q):
        s, t = map(lambda x: int(x)-1, input().split())
        if ans[s][t] < N:
            print(int(ans[s][t])-1)
        else:
            print(-1) solve
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall


def solve():
    N, M, L = map(int, input().split())
    dist = [[0 for _ in range(N)] for _ in range(N)]
    for i in range(M):
        a, b, c = map(int, input().split())
        dist[a-1][b-1] = c
        dist[b-1][a-1] = c
    fw = floyd_warshall(csr_matrix(dist))

    ok = [[0 for _ in range(N)] for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if fw[i][j] <= L:
                ok[i][j] = 1
    ans = floyd_warshall(csr_matrix(ok))

    Q = int(input())
    for i in range(Q):
        s, t = map(lambda x: int(x)-1, input().split())
        if ans[s][t] < N:
            print(int(ans[s][t])-1)
        else:
            print(-1)


if __name__ == '__main__':
    solve()