#dame datta...
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix
def main():
    N, M, L = map(int, input().split())
    l = [[0] * N for _ in range(N)]
    for _ in range(M):
        a, b, c = map(int, input().split())
        if c > L:
            continue
        l[a-1][b-1] = c
        l[b-1][a-1] = c
    G = csr_matrix(l)
    d = floyd_warshall(G)
    l = [[0] * N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if d[i][j] <= L:
                l[i][j] = 1
    G = csr_matrix(l)
    d = floyd_warshall(G)
    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        x = d[s][t]
        if x != float('inf'):
            print(int(x)-1)
        else:
            print(-1)
main() from scipy.sparse.csgraph import floyd_warshall from scipy.sparse import csr_matrix def main():
    N, M, L = map(int, input().split())
    l = [[0] * N for _ in range(N)]
    for _ in range(M):
        a, b, c = map(int, input().split())
        if c > L:
            continue
        l[a-1][b-1] = c
        l[b-1][a-1] = c
    G = csr_matrix(l)
    d = floyd_warshall(G)
    l = [[0] * N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if d[i][j] <= L:
                l[i][j] = 1
    G = csr_matrix(l)
    d = floyd_warshall(G)
    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        x = d[s][t]
        if x != float('inf'):
            print(int(x)-1)
        else:
            print(-1) main N, M, L = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M L [0] * N for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [0] * N [0] 0 N l = [[0] * N for _ in range(N)] l [[0] * N for _ in range(N)] for _ in range(M):
        a, b, c = map(int, input().split())
        if c > L:
            continue
        l[a-1][b-1] = c
        l[b-1][a-1] = c
     _ 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 if c > L:
            continue
         c > L c L continue l[a-1][b-1] = c l[a-1][b-1] [a-1] l a-1 a 1 b-1 b 1 c l[b-1][a-1] = c l[b-1][a-1] [b-1] l b-1 b 1 a-1 a 1 c G = csr_matrix(l) G csr_matrix(l) csr_matrix l d = floyd_warshall(G) d floyd_warshall(G) floyd_warshall G [0] * N for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [0] * N [0] 0 N l = [[0] * N for _ in range(N)] l [[0] * N for _ in range(N)] for i in range(N):
        for j in range(N):
            if d[i][j] <= L:
                l[i][j] = 1
     i range(N) range N for j in range(N):
            if d[i][j] <= L:
                l[i][j] = 1
     j range(N) range N if d[i][j] <= L:
                l[i][j] = 1
     d[i][j] <= L d[i][j] [i] d i j L l[i][j] = 1 l[i][j] [i] l i j 1 G = csr_matrix(l) G csr_matrix(l) csr_matrix l d = floyd_warshall(G) d floyd_warshall(G) floyd_warshall G Q = int(input()) Q int(input()) int input() input for _ in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        x = d[s][t]
        if x != float('inf'):
            print(int(x)-1)
        else:
            print(-1) _ range(Q) range Q s, t = map(int, input().split()) s map(int, input().split()) map int input().split() ().split () input split t s -= 1 s 1 t -= 1 t 1 x = d[s][t] x d[s][t] [s] d s t if x != float('inf'):
            print(int(x)-1)
        else:
            print(-1) x != float('inf') x float('inf') float 'inf' print(int(x)-1) print int(x)-1 int(x) int x 1 print(-1) print -1 main() main def main():
    N, M, L = map(int, input().split())
    l = [[0] * N for _ in range(N)]
    for _ in range(M):
        a, b, c = map(int, input().split())
        if c > L:
            continue
        l[a-1][b-1] = c
        l[b-1][a-1] = c
    G = csr_matrix(l)
    d = floyd_warshall(G)
    l = [[0] * N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if d[i][j] <= L:
                l[i][j] = 1
    G = csr_matrix(l)
    d = floyd_warshall(G)
    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        x = d[s][t]
        if x != float('inf'):
            print(int(x)-1)
        else:
            print(-1) def main():
    N, M, L = map(int, input().split())
    l = [[0] * N for _ in range(N)]
    for _ in range(M):
        a, b, c = map(int, input().split())
        if c > L:
            continue
        l[a-1][b-1] = c
        l[b-1][a-1] = c
    G = csr_matrix(l)
    d = floyd_warshall(G)
    l = [[0] * N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if d[i][j] <= L:
                l[i][j] = 1
    G = csr_matrix(l)
    d = floyd_warshall(G)
    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        x = d[s][t]
        if x != float('inf'):
            print(int(x)-1)
        else:
            print(-1) main
#dame datta...
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix
def main():
    N, M, L = map(int, input().split())
    l = [[0] * N for _ in range(N)]
    for _ in range(M):
        a, b, c = map(int, input().split())
        if c > L:
            continue
        l[a-1][b-1] = c
        l[b-1][a-1] = c
    G = csr_matrix(l)
    d = floyd_warshall(G)
    l = [[0] * N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if d[i][j] <= L:
                l[i][j] = 1
    G = csr_matrix(l)
    d = floyd_warshall(G)
    Q = int(input())
    for _ in range(Q):
        s, t = map(int, input().split())
        s -= 1
        t -= 1
        x = d[s][t]
        if x != float('inf'):
            print(int(x)-1)
        else:
            print(-1)
main()
