import sys

from scipy.sparse.csgraph import floyd_warshall as f

def main():

  n,m,l,*t=list(map(int,sys.stdin.buffer.read().split()))

  d=[[0]*(n+1)for _ in range(n+1)]

  for a,b,c in zip(*[iter(t[:m*3])]*3):d[a][b]=c

  d=f(f(d,0)<=l)

  d[d>n]=0

  print(('\n'.join(map(str,list(map(int,[d[i]-1for i in zip(*[iter(t[m*3+1:])]*2)]))))))

main()
import sys

from scipy.sparse.csgraph import floyd_warshall as f

def main():

  n,m,l,*t=list(map(int,sys.stdin.buffer.read().split()))

  d=[[0]*(n+1)for _ in range(n+1)]

  for a,b,c in zip(*[iter(t[:m*3])]*3):d[a][b]=c

  d=f(f(d,0)<=l)-1

  d[d>n]=-1

  print(('\n'.join(str(int(d[i]))for i in zip(*[iter(t[m*3+1:])]*2))))

main()
import sys
from scipy.sparse.csgraph import floyd_warshall as f


def main():
    n, m, l, *t = list(map(int, sys.stdin.buffer.read().split()))
    d = [[0] * (n + 1) for _ in range(n + 1)]
    for a, b, c in zip(*[iter(t[: m * 3])] * 3):
        d[a][b] = c
    d = f(f(d, 0) <= l)
    d[d > n] = 0
    print(
        (
            "\n".join(
                map(
                    str,
                    list(
                        map(int, [d[i] - 1 for i in zip(*[iter(t[m * 3 + 1 :])] * 2)])
                    ),
                )
            )
        )
    )


main()

import sys
from scipy.sparse.csgraph import floyd_warshall as f


def main():
    n, m, l, *t = list(map(int, sys.stdin.buffer.read().split()))
    d = [[0] * (n + 1) for _ in range(n + 1)]
    for a, b, c in zip(*[iter(t[: m * 3])] * 3):
        d[a][b] = c
    d = f(f(d, 0) <= l) - 1
    d[d > n] = -1
    print(("\n".join(str(int(d[i])) for i in zip(*[iter(t[m * 3 + 1 :])] * 2))))


main()

-    d = f(f(d, 0) <= l) -    d[d > n] = 0 -    print( -        ( -            "\n".join( -                map( -                    str, -                    list( -                        map(int, [d[i] - 1 for i in zip(*[iter(t[m * 3 + 1 :])] * 2)]) -                    ), -                ) -            ) -        ) -    ) +    d = f(f(d, 0) <= l) - 1 +    d[d > n] = -1 +    print(("\n".join(str(int(d[i])) for i in zip(*[iter(t[m * 3 + 1 :])] * 2))))