def resolve():
    from scipy.sparse.csgraph import floyd_warshall
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, m, l = map(int, input().split())
    inf = 10 ** 20
    ar = [[0] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        ar[a - 1][b - 1] = c
    x = floyd_warshall(ar, directed=False)
    br = [[0] * n for _ in range(n)]
    for i in range(n):
        for j in range(i + 1, n):
            if x[i, j] <= l:
                br[i][j] = 1
    y = floyd_warshall(br, directed=False)
    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        print(int(y[s - 1, t - 1]) - 1 if y[s - 1, t - 1] < inf else -1)


if __name__ == "__main__":
    resolve() def resolve():
    from scipy.sparse.csgraph import floyd_warshall
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, m, l = map(int, input().split())
    inf = 10 ** 20
    ar = [[0] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        ar[a - 1][b - 1] = c
    x = floyd_warshall(ar, directed=False)
    br = [[0] * n for _ in range(n)]
    for i in range(n):
        for j in range(i + 1, n):
            if x[i, j] <= l:
                br[i][j] = 1
    y = floyd_warshall(br, directed=False)
    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        print(int(y[s - 1, t - 1]) - 1 if y[s - 1, t - 1] < inf else -1) resolve from scipy.sparse.csgraph import floyd_warshall import sys sys def input(): return sys.stdin.readline().rstrip()
     input n, m, l = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split m l inf = 10 ** 20 inf 10 ** 20 10 20 [0] * n for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [0] * n [0] 0 n ar = [[0] * n for _ in range(n)] ar [[0] * n for _ in range(n)] for _ in range(m):
        a, b, c = map(int, input().split())
        ar[a - 1][b - 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 ar[a - 1][b - 1] = c ar[a - 1][b - 1] [a - 1] ar a - 1 a 1 b - 1 b 1 c x = floyd_warshall(ar, directed=False) x floyd_warshall(ar, directed=False) floyd_warshall ar directed=False directed False [0] * n for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [0] * n [0] 0 n br = [[0] * n for _ in range(n)] br [[0] * n for _ in range(n)] for i in range(n):
        for j in range(i + 1, n):
            if x[i, j] <= l:
                br[i][j] = 1
     i range(n) range n for j in range(i + 1, n):
            if x[i, j] <= l:
                br[i][j] = 1
     j range(i + 1, n) range i + 1 i 1 n if x[i, j] <= l:
                br[i][j] = 1
     x[i, j] <= l x[i, j] x i l br[i][j] = 1 br[i][j] [i] br i j 1 y = floyd_warshall(br, directed=False) y floyd_warshall(br, directed=False) floyd_warshall br directed=False directed False q = int(input()) q int(input()) int input() input for _ in range(q):
        s, t = map(int, input().split())
        print(int(y[s - 1, t - 1]) - 1 if y[s - 1, t - 1] < inf else -1) _ range(q) range q s, t = map(int, input().split()) s map(int, input().split()) map int input().split() ().split () input split t print(int(y[s - 1, t - 1]) - 1 if y[s - 1, t - 1] < inf else -1) print int(y[s - 1, t - 1]) - 1 if y[s - 1, t - 1] < inf else -1 y[s - 1, t - 1] < inf y[s - 1, t - 1] y s - 1 s 1 inf int(y[s - 1, t - 1]) - 1 int(y[s - 1, t - 1]) int y[s - 1, t - 1] y s - 1 s 1 1 -1 if __name__ == "__main__":
    resolve() __name__ == "__main__" __name__ "__main__" resolve() resolve def resolve():
    from scipy.sparse.csgraph import floyd_warshall
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, m, l = map(int, input().split())
    inf = 10 ** 20
    ar = [[0] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        ar[a - 1][b - 1] = c
    x = floyd_warshall(ar, directed=False)
    br = [[0] * n for _ in range(n)]
    for i in range(n):
        for j in range(i + 1, n):
            if x[i, j] <= l:
                br[i][j] = 1
    y = floyd_warshall(br, directed=False)
    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        print(int(y[s - 1, t - 1]) - 1 if y[s - 1, t - 1] < inf else -1) def resolve():
    from scipy.sparse.csgraph import floyd_warshall
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, m, l = map(int, input().split())
    inf = 10 ** 20
    ar = [[0] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        ar[a - 1][b - 1] = c
    x = floyd_warshall(ar, directed=False)
    br = [[0] * n for _ in range(n)]
    for i in range(n):
        for j in range(i + 1, n):
            if x[i, j] <= l:
                br[i][j] = 1
    y = floyd_warshall(br, directed=False)
    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        print(int(y[s - 1, t - 1]) - 1 if y[s - 1, t - 1] < inf else -1) resolve
def resolve():
    from scipy.sparse.csgraph import floyd_warshall
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, m, l = map(int, input().split())
    inf = 10 ** 20
    ar = [[0] * n for _ in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        ar[a - 1][b - 1] = c
    x = floyd_warshall(ar, directed=False)
    br = [[0] * n for _ in range(n)]
    for i in range(n):
        for j in range(i + 1, n):
            if x[i, j] <= l:
                br[i][j] = 1
    y = floyd_warshall(br, directed=False)
    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        print(int(y[s - 1, t - 1]) - 1 if y[s - 1, t - 1] < inf else -1)


if __name__ == "__main__":
    resolve()
