Buggy python submission (verdict: Time Limit Exceeded):
n,m,l = map(int,input().split())
road = [[] for _ in range(n)]
for _ in range(m):
  a,b,c = map(int,input().split())
  a -= 1
  b -= 1
  road[a].append([b,c])
  road[b].append([a,c])
cango = []
for i in range(n):
  tmp = [l+1 for _ in range(n)]
  tmp[i] = 0
  now = [[i,0]]
  while now:
    last = now
    now = []
    for x in last:
      for yz in road[x[0]]:
        y,z = yz
        if x[1] + z < tmp[y]:
          tmp[y] = x[1] + z
          now.append([y,tmp[y]])
  add = []
  for i in range(n):
    if 0 < tmp[i] <= l:
      add.append(i)
  cango.append(add)
q = int(input())
for _ in range(q):
  s,t = map(int,input().split())
  s -= 1
  t -= 1
  tmp = [-1 for _ in range(n)]
  tmp[s] = 0
  time = 0
  now = [s]
  while now:
    time += 1
    last = now
    now = []
    for x in last:
      for y in cango[x]:
        if tmp[y] == -1:
          tmp[y] = time - 1
          now.append(y)
    if tmp[t] != -1:
      print(tmp[t])
      break
  else:
    print(tmp[t])

Fixed submission:
n,m,l = map(int,input().split())
road = [[10**18 for _ in range(n)] for _ in range(n)]
for _ in range(m):
  a,b,c = map(int,input().split())
  a -= 1
  b -= 1
  road[a][b] = c
  road[b][a] = c
#print(road)
for k in range(n):
  for i in range(n):
    for j in range(n):
      road[i][j] = min(road[i][j],road[i][k]+road[k][j])
#print(road)
cango = [[1000 for _ in range(n)] for _ in range(n)]
for i in range(n):
  for j in range(n):
    if road[i][j] == 0:
      cango[i][j] = 0
    elif road[i][j] <= l:
      cango[i][j] = 1
#print(cango)
for k in range(n):
  for i in range(n):
    for j in range(n):
      cango[i][j] = min(cango[i][j],cango[i][k]+cango[k][j])
#print(cango)
q = int(input())
for _ in range(q):
  s,t = map(int,input().split())
  ans = cango[s-1][t-1]-1
  if ans > 500:
    print(-1)
  else:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m,l = map(int,input().split())
road = [[] for _ in range(n)]
for _ in range(m):
  a,b,c = map(int,input().split())
  a -= 1
  b -= 1
  road[a].append([b,c])
  road[b].append([a,c])
cango = []
for i in range(n):
  tmp = [l+1 for _ in range(n)]
  tmp[i] = 0
  now = [[i,0]]
  while now:
    last = now
    now = []
    for x in last:
      for yz in road[x[0]]:
        y,z = yz
        if x[1] + z < tmp[y]:
          tmp[y] = x[1] + z
          now.append([y,tmp[y]])
  add = []
  for i in range(n):
    if tmp[i] == 0:
      add.append(0)
    elif tmp[i] <= l:
      add.append(1)
    else:
      add.append(1000)
  cango.append(add)
#print(cango)
for k in range(n):
  for i in range(n):
    for j in range(n):
      if cango[i][k] + cango[k][j] < cango[i][j]:
        cango[i][j] = cango[i][k] + cango[k][j]
#print(cango)
q = int(input())
for _ in range(q):
  s,t = map(int,input().split())
  ans = cango[s-1][t-1]-1
  if ans > 500:
    print(-1)
  else:
    print(ans)

Fixed submission:
n,m,l = map(int,input().split())
road = [[10**18 for _ in range(n)] for _ in range(n)]
for _ in range(m):
  a,b,c = map(int,input().split())
  a -= 1
  b -= 1
  road[a][b] = c
  road[b][a] = c
#print(road)
for k in range(n):
  for i in range(n):
    for j in range(n):
      road[i][j] = min(road[i][j],road[i][k]+road[k][j])
#print(road)
cango = [[1000 for _ in range(n)] for _ in range(n)]
for i in range(n):
  for j in range(n):
    if road[i][j] == 0:
      cango[i][j] = 0
    elif road[i][j] <= l:
      cango[i][j] = 1
#print(cango)
for k in range(n):
  for i in range(n):
    for j in range(n):
      cango[i][j] = min(cango[i][j],cango[i][k]+cango[k][j])
#print(cango)
q = int(input())
for _ in range(q):
  s,t = map(int,input().split())
  ans = cango[s-1][t-1]-1
  if ans > 500:
    print(-1)
  else:
    print(ans)