N,M,L=gets.chomp.split(" ").map(&:to_i)
edge_list = []
M.times do
    a,b,c=gets.chomp.split(" ").map(&:to_i)
    edge_list.push([a-1,b-1,c])
    edge_list.push([b-1,a-1,c])
end
def bellman_ford(edge_list, s, max_count)
    d = Array.new(N, 9999)
    d[s] = 0
    c = 0
    loop do
      is_update = false
      edge_list.each do |(u, v, c)|
        if d[u] != 9999 && d[u] + c < d[v]
          d[v] = d[u] + c
          is_update = true
        end
      end
      return d if !is_update
      c += 1
      return nil if c == max_count
    end
end
  
Q = gets.to_i
Q.times do
    s,t=gets.chomp.split(" ").map(&:to_i)
    ret = bellman_ford(edge_list, s-1, N)
    puts ret[t-1] == 9999 ? -1 : ret[t-1]/L
end