### BUGGY CODE ###

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(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)]
		fuel[start][0] = 0
		fuel[start][1] = L
		queue = [start]
		while len(queue)>0:
			nq = []						# next target node
			nq_append = nq.append
			for i in queue:				# target node
				oc, om = fuel[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<fuel[j][0]) or (nc==fuel[j][0] and nm>fuel[j][1]):
						fuel[j][0], fuel[j][1] = nc, nm
						nq_append(j)

			queue = nq					# update target node

		ans = fuel[end][0]
		if ans==10000000: ans = -1

		fuellist[start] = fuel

		print(ans)


	for s, t in st:
		start, end = s-1, t-1

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


if __name__ == '__main__':
	main()

### FIXED CODE ###

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(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()