def ee143
    n,m,l = gets.chomp.split(/\s/).map{|x| x.to_i}

    abcm = Array.new(m+1)
    for i in 1..m
        a,b,c = gets.chomp.split(/\s/).map{|x| x.to_i}
        a,b = [a,b].sort
        abcm[a] = [b, c]
    end

    q = gets.chomp.to_i
    r = []
    for i in 1..q
        s,t = gets.chomp.split(/\s/).map{|x| x.to_i}.sort

        w = abcm.length == 1 ? -1 : 0
        o = l
        for j in 1..m
            if abcm[s] == nil
                w = -1
                break
            end

            if o-abcm[s][1] < 0
                o = l
                w += 1
            end
            o -= abcm[s][1]
            if abcm[s][0] == t
                break
            end
            s = abcm[s][0]
        end
        r.push(w)
    end

    r.each do |q|
        puts q
    end
end

ee143