import numpy as np
import sys
from scipy.sparse.csgraph import floyd_warshall


def use_fast_io():
    global input
    import io
    import sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline


use_fast_io()


def solve():
    n, m, le = map(int, input().split())
    g1 = (le + 1) * np.zeros((n, n))
    for _ in range(m):
        a, b, c = map(int, input().split())
        if c <= le:
            g1[a - 1, b - 1] = c
            g1[b - 1, a - 1] = c

    fw1 = floyd_warshall(g1, directed=False)

    g2 = 1000 * np.zeros((n, n))
    g2[fw1 <= le] = 1

    fw2 = floyd_warshall(g2, directed=False)

    q = int(input())
    res_list = [-1] * q
    for i in range(q):
        s, t = map(int, input().split())
        res = fw2[s - 1, t - 1]
        if res <= n:
            res_list[i] = int(res - 1)

    for r in res_list:
        print(r)


if __name__ == "__main__":
    solve() import numpy as np numpy import sys sys from scipy.sparse.csgraph import floyd_warshall def use_fast_io():
    global input
    import io
    import sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline use_fast_io global input import io io import sys sys f = io.BytesIO(sys.stdin.buffer.read()) f io.BytesIO(sys.stdin.buffer.read()) io.BytesIO io BytesIO sys.stdin.buffer.read() sys.stdin.buffer.read sys.stdin.buffer sys.stdin sys stdin buffer read input = f.readline input f.readline f readline use_fast_io() use_fast_io def solve():
    n, m, le = map(int, input().split())
    g1 = (le + 1) * np.zeros((n, n))
    for _ in range(m):
        a, b, c = map(int, input().split())
        if c <= le:
            g1[a - 1, b - 1] = c
            g1[b - 1, a - 1] = c

    fw1 = floyd_warshall(g1, directed=False)

    g2 = 1000 * np.zeros((n, n))
    g2[fw1 <= le] = 1

    fw2 = floyd_warshall(g2, directed=False)

    q = int(input())
    res_list = [-1] * q
    for i in range(q):
        s, t = map(int, input().split())
        res = fw2[s - 1, t - 1]
        if res <= n:
            res_list[i] = int(res - 1)

    for r in res_list:
        print(r) solve n, m, le = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split m le g1 = (le + 1) * np.zeros((n, n)) g1 (le + 1) * np.zeros((n, n)) le + 1 le 1 np.zeros((n, n)) np.zeros np zeros (n, n) n n for _ in range(m):
        a, b, c = map(int, input().split())
        if c <= le:
            g1[a - 1, b - 1] = c
            g1[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 <= le:
            g1[a - 1, b - 1] = c
            g1[b - 1, a - 1] = c

     c <= le c le g1[a - 1, b - 1] = c g1[a - 1, b - 1] g1 a - 1 a 1 c g1[b - 1, a - 1] = c g1[b - 1, a - 1] g1 b - 1 b 1 c fw1 = floyd_warshall(g1, directed=False) fw1 floyd_warshall(g1, directed=False) floyd_warshall g1 directed=False directed False g2 = 1000 * np.zeros((n, n)) g2 1000 * np.zeros((n, n)) 1000 np.zeros((n, n)) np.zeros np zeros (n, n) n n g2[fw1 <= le] = 1 g2[fw1 <= le] g2 fw1 <= le fw1 le 1 fw2 = floyd_warshall(g2, directed=False) fw2 floyd_warshall(g2, directed=False) floyd_warshall g2 directed=False directed False q = int(input()) q int(input()) int input() input res_list = [-1] * q res_list [-1] * q [-1] -1 q for i in range(q):
        s, t = map(int, input().split())
        res = fw2[s - 1, t - 1]
        if res <= n:
            res_list[i] = int(res - 1)

     i range(q) range q s, t = map(int, input().split()) s map(int, input().split()) map int input().split() ().split () input split t res = fw2[s - 1, t - 1] res fw2[s - 1, t - 1] fw2 s - 1 s 1 if res <= n:
            res_list[i] = int(res - 1)

     res <= n res n res_list[i] = int(res - 1) res_list[i] res_list i int(res - 1) int res - 1 res 1 for r in res_list:
        print(r) r res_list print(r) print r if __name__ == "__main__":
    solve() __name__ == "__main__" __name__ "__main__" solve() solve def solve():
    n, m, le = map(int, input().split())
    g1 = (le + 1) * np.zeros((n, n))
    for _ in range(m):
        a, b, c = map(int, input().split())
        if c <= le:
            g1[a - 1, b - 1] = c
            g1[b - 1, a - 1] = c

    fw1 = floyd_warshall(g1, directed=False)

    g2 = 1000 * np.zeros((n, n))
    g2[fw1 <= le] = 1

    fw2 = floyd_warshall(g2, directed=False)

    q = int(input())
    res_list = [-1] * q
    for i in range(q):
        s, t = map(int, input().split())
        res = fw2[s - 1, t - 1]
        if res <= n:
            res_list[i] = int(res - 1)

    for r in res_list:
        print(r) def solve():
    n, m, le = map(int, input().split())
    g1 = (le + 1) * np.zeros((n, n))
    for _ in range(m):
        a, b, c = map(int, input().split())
        if c <= le:
            g1[a - 1, b - 1] = c
            g1[b - 1, a - 1] = c

    fw1 = floyd_warshall(g1, directed=False)

    g2 = 1000 * np.zeros((n, n))
    g2[fw1 <= le] = 1

    fw2 = floyd_warshall(g2, directed=False)

    q = int(input())
    res_list = [-1] * q
    for i in range(q):
        s, t = map(int, input().split())
        res = fw2[s - 1, t - 1]
        if res <= n:
            res_list[i] = int(res - 1)

    for r in res_list:
        print(r) solve def use_fast_io():
    global input
    import io
    import sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline def use_fast_io():
    global input
    import io
    import sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline use_fast_io
import numpy as np
import sys
from scipy.sparse.csgraph import floyd_warshall


def use_fast_io():
    global input
    import io
    import sys
    f = io.BytesIO(sys.stdin.buffer.read())
    input = f.readline


use_fast_io()


def solve():
    n, m, le = map(int, input().split())
    g1 = (le + 1) * np.zeros((n, n))
    for _ in range(m):
        a, b, c = map(int, input().split())
        if c <= le:
            g1[a - 1, b - 1] = c
            g1[b - 1, a - 1] = c

    fw1 = floyd_warshall(g1, directed=False)

    g2 = 1000 * np.zeros((n, n))
    g2[fw1 <= le] = 1

    fw2 = floyd_warshall(g2, directed=False)

    q = int(input())
    res_list = [-1] * q
    for i in range(q):
        s, t = map(int, input().split())
        res = fw2[s - 1, t - 1]
        if res <= n:
            res_list[i] = int(res - 1)

    for r in res_list:
        print(r)


if __name__ == "__main__":
    solve()