N, M, L = gets.chomp.split(" ").map(&:to_i)
R = Hash.new{|h, k| h[k] = []}
M.times{|i|
  a, b, c = gets.chomp.split(" ").map(&:to_i)
  next if c > L
  R[a] << [b, c]
  R[b] << [a, c]
}

parent = Array.new(N + 1, 0)
(1..N).each{|i|
  next if parent[i] != 0
  parent[i] = i
  bef = [i]
  until bef.empty? do
    nxt = []
    bef.each{|b|
      R[b].each{|a, |
        next if parent[a] != 0
        parent[a] = i
        nxt << a
      }
    }
    bef = nxt.uniq
  end
}

def dfs(a, b, c)
  R[b].each{|t, d|
    next if t == a
    next if d > c
    next if R[a].any?{|r| r[0] == t}
    R[a] << [t, L - c + d]
    R[t] << [a, L - c + d]
    dfs(a, t, c - d)
  }
end
(1..N).each{|i|
  R[i].each{|t, d|
    dfs(i, t, L - d)
  }
}

def bfs(tgt, bef, cnt)
  nxt = []
  bef.each{|b|
    R[b].each{|r, |
      return cnt if r == tgt
      next if T.include?(r)
      nxt << r
      T << r
    }
  }
  bfs(tgt, nxt.uniq, cnt + 1)
end

T = []
Q = gets.to_i
Q.times{|i|
  f, t = gets.chomp.split(" ").map(&:to_i)
  if parent[f] != parent[t] then
    puts -1
  else
    T.clear
    T << f
    puts bfs(t, [f], 0)
  end
}
