import sys





def main():

	input = sys.stdin.readline

	N, M, L = list(map(int,input().split()))

	P = [list(map(int,input().split())) for i in range(M)]

	Q = int(eval(input()))

	st = [list(map(int,input().split())) for i in range(Q)]



	neighbor = [[] for i in range(N)]

	for s,e,d in P:

		if d<=L:

			neighbor[s-1].append((e-1,d))

			neighbor[e-1].append((s-1,d))

		# if d<=L:

		# 	neighbor[s-1][e-1] = neighbor[e-1][s-1] = d



	# print(neighbor)

	# start, end = 2,1



	fuellist = [0]*N

	def bfs(start, end):

		# fuel = [[10000000,L+1] for i in range(N)]

		fuelc, fuelm = [10000000]*N, [L+1]*N

		# fuel[start][0] = 0

		# fuel[start][1] = L

		fuelc[start], fuelm[start] = 0, L

		queue = [start]

		while len(queue)>0:

			nq = []						# next target node

			nq_append = nq.append

			for i in queue:				# target node

				oc, om = fuelc[i], fuelm[i]

				for j, d in neighbor[i]:

				# for j, d in enumerate(neighbor[i]):	# next node

					# if d==10**10: continue

					if om<d:

						nc = oc+1

						nm = L-d

					else:

						nc = oc

						nm = om-d



					# print(i,j,nc,nm)



					if nc<fuelc[j]:

						fuelc[j], fuelm[j] = nc, nm

						nq_append(j)

					elif nc==fuelc[j] and nm>fuelm[j]:

						fuelm[j] = nm

						nq_append(j)



			queue = nq					# update target node



		# ans = fuel[end][0]

		ans = fuelc[end]

		if ans==10000000: ans = -1



		fuellist[start] = fuelc



		print(ans)



	for s, t in st:

		start, end = s-1, t-1



		if fuellist[start]!=0:

			ans = fuellist[start][end]

			if ans==10000000: ans = -1

			print(ans)

		elif fuellist[end]!=0:

			ans = fuellist[end][start]

			if ans==10000000: ans = -1

			print(ans)

		else:

			bfs(start,end)





if __name__ == '__main__':

	main()
import sys



input = sys.stdin.readline

N, M, L = list(map(int,input().split()))

P = [list(map(int,input().split())) for i in range(M)]

Q = int(eval(input()))

st = [list(map(int,input().split())) for i in range(Q)]



INF = 100100100100100



dp = [[INF]*N for i in range(N)]

for s,e,d in P:

	if d<=L:

		dp[s-1][e-1] = d

		dp[e-1][s-1] = d

for i in range(N):

	dp[i][i] = 0



for k in range(N):

	for y in range(N):

		for x in range(N):

			dp[y][x] = min(dp[y][x], dp[y][k]+dp[k][x])



dp2 = [[INF]*N for i in range(N)]

for y in range(N):

	for x in range(N):

		if dp[y][x]<=L:

			dp2[y][x] = 1



for k in range(N):

	for y in range(N):

		for x in range(N):

			dp2[y][x] = min(dp2[y][x], dp2[y][k]+dp2[k][x])



for s, t in st:

	count = dp2[s-1][t-1]

	if count==INF:

		print((-1))

	else:

		print((count-1))
import sys


def main():
    input = sys.stdin.readline
    N, M, L = list(map(int, input().split()))
    P = [list(map(int, input().split())) for i in range(M)]
    Q = int(eval(input()))
    st = [list(map(int, input().split())) for i in range(Q)]
    neighbor = [[] for i in range(N)]
    for s, e, d in P:
        if d <= L:
            neighbor[s - 1].append((e - 1, d))
            neighbor[e - 1].append((s - 1, d))
        # if d<=L:
        # 	neighbor[s-1][e-1] = neighbor[e-1][s-1] = d
    # print(neighbor)
    # start, end = 2,1
    fuellist = [0] * N

    def bfs(start, end):
        # fuel = [[10000000,L+1] for i in range(N)]
        fuelc, fuelm = [10000000] * N, [L + 1] * N
        # fuel[start][0] = 0
        # fuel[start][1] = L
        fuelc[start], fuelm[start] = 0, L
        queue = [start]
        while len(queue) > 0:
            nq = []  # next target node
            nq_append = nq.append
            for i in queue:  # target node
                oc, om = fuelc[i], fuelm[i]
                for j, d in neighbor[i]:
                    # for j, d in enumerate(neighbor[i]):	# next node
                    # if d==10**10: continue
                    if om < d:
                        nc = oc + 1
                        nm = L - d
                    else:
                        nc = oc
                        nm = om - d
                    # print(i,j,nc,nm)
                    if nc < fuelc[j]:
                        fuelc[j], fuelm[j] = nc, nm
                        nq_append(j)
                    elif nc == fuelc[j] and nm > fuelm[j]:
                        fuelm[j] = nm
                        nq_append(j)
            queue = nq  # update target node
        # ans = fuel[end][0]
        ans = fuelc[end]
        if ans == 10000000:
            ans = -1
        fuellist[start] = fuelc
        print(ans)

    for s, t in st:
        start, end = s - 1, t - 1
        if fuellist[start] != 0:
            ans = fuellist[start][end]
            if ans == 10000000:
                ans = -1
            print(ans)
        elif fuellist[end] != 0:
            ans = fuellist[end][start]
            if ans == 10000000:
                ans = -1
            print(ans)
        else:
            bfs(start, end)


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.readline
N, M, L = list(map(int, input().split()))
P = [list(map(int, input().split())) for i in range(M)]
Q = int(eval(input()))
st = [list(map(int, input().split())) for i in range(Q)]
INF = 100100100100100
dp = [[INF] * N for i in range(N)]
for s, e, d in P:
    if d <= L:
        dp[s - 1][e - 1] = d
        dp[e - 1][s - 1] = d
for i in range(N):
    dp[i][i] = 0
for k in range(N):
    for y in range(N):
        for x in range(N):
            dp[y][x] = min(dp[y][x], dp[y][k] + dp[k][x])
dp2 = [[INF] * N for i in range(N)]
for y in range(N):
    for x in range(N):
        if dp[y][x] <= L:
            dp2[y][x] = 1
for k in range(N):
    for y in range(N):
        for x in range(N):
            dp2[y][x] = min(dp2[y][x], dp2[y][k] + dp2[k][x])
for s, t in st:
    count = dp2[s - 1][t - 1]
    if count == INF:
        print((-1))
    else:
        print((count - 1))

- -def main(): -    input = sys.stdin.readline -    N, M, L = list(map(int, input().split())) -    P = [list(map(int, input().split())) for i in range(M)] -    Q = int(eval(input())) -    st = [list(map(int, input().split())) for i in range(Q)] -    neighbor = [[] for i in range(N)] -    for s, e, d in P: -        if d <= L: -            neighbor[s - 1].append((e - 1, d)) -            neighbor[e - 1].append((s - 1, d)) -        # if d<=L: -        # 	neighbor[s-1][e-1] = neighbor[e-1][s-1] = d -    # print(neighbor) -    # start, end = 2,1 -    fuellist = [0] * N - -    def bfs(start, end): -        # fuel = [[10000000,L+1] for i in range(N)] -        fuelc, fuelm = [10000000] * N, [L + 1] * N -        # fuel[start][0] = 0 -        # fuel[start][1] = L -        fuelc[start], fuelm[start] = 0, L -        queue = [start] -        while len(queue) > 0: -            nq = []  # next target node -            nq_append = nq.append -            for i in queue:  # target node -                oc, om = fuelc[i], fuelm[i] -                for j, d in neighbor[i]: -                    # for j, d in enumerate(neighbor[i]):	# next node -                    # if d==10**10: continue -                    if om < d: -                        nc = oc + 1 -                        nm = L - d -                    else: -                        nc = oc -                        nm = om - d -                    # print(i,j,nc,nm) -                    if nc < fuelc[j]: -                        fuelc[j], fuelm[j] = nc, nm -                        nq_append(j) -                    elif nc == fuelc[j] and nm > fuelm[j]: -                        fuelm[j] = nm -                        nq_append(j) -            queue = nq  # update target node -        # ans = fuel[end][0] -        ans = fuelc[end] -        if ans == 10000000: -            ans = -1 -        fuellist[start] = fuelc -        print(ans) - -    for s, t in st: -        start, end = s - 1, t - 1 -        if fuellist[start] != 0: -            ans = fuellist[start][end] -            if ans == 10000000: -                ans = -1 -            print(ans) -        elif fuellist[end] != 0: -            ans = fuellist[end][start] -            if ans == 10000000: -                ans = -1 -            print(ans) -        else: -            bfs(start, end) - - -if __name__ == "__main__": -    main() +input = sys.stdin.readline +N, M, L = list(map(int, input().split())) +P = [list(map(int, input().split())) for i in range(M)] +Q = int(eval(input())) +st = [list(map(int, input().split())) for i in range(Q)] +INF = 100100100100100 +dp = [[INF] * N for i in range(N)] +for s, e, d in P: +    if d <= L: +        dp[s - 1][e - 1] = d +        dp[e - 1][s - 1] = d +for i in range(N): +    dp[i][i] = 0 +for k in range(N): +    for y in range(N): +        for x in range(N): +            dp[y][x] = min(dp[y][x], dp[y][k] + dp[k][x]) +dp2 = [[INF] * N for i in range(N)] +for y in range(N): +    for x in range(N): +        if dp[y][x] <= L: +            dp2[y][x] = 1 +for k in range(N): +    for y in range(N): +        for x in range(N): +            dp2[y][x] = min(dp2[y][x], dp2[y][k] + dp2[k][x]) +for s, t in st: +    count = dp2[s - 1][t - 1] +    if count == INF: +        print((-1)) +    else: +        print((count - 1))