let () =
    let main () =
        let n, m, l = Scanf.sscanf (read_line ()) "%d %d %d" (fun n m l -> n, m, l) in
        let a = Array.make m 0 in
        let b = Array.make m 0 in
        let c = Array.make m 0 in
        let rd = Array.make (n + 1) [] in
        for i = 0 to m - 1 do
            let aa, bb, cc = Scanf.sscanf (read_line ()) "%d %d %d" (fun a b c -> a, b, c) in
            a.(i) <- aa; b.(i) <- bb; c.(i) <- cc;
            rd.(aa) <- (bb, cc) :: rd.(aa);
            rd.(bb) <- (aa, cc) :: rd.(bb)
        done;
        let ans = Array.make (n + 1) [||] in
        let calc s t =
            if ans.(s) = [||] then (
                let acount = Array.make (n + 1) 999 in
                let afuel  = Array.make (n + 1) 0   in
                let rec dfs next = function
                    | [] -> if next <> [] then dfs [] next
                    | (cur, count, fuel) :: tl ->
                        if acount.(cur) < count || acount.(cur) = count && afuel.(cur) > fuel then dfs next tl else (
                            acount.(cur) <- count;
                            afuel.(cur) <- fuel;
                            let next = List.fold_left (fun next (nx, len) ->
                                if len <= fuel then (nx, count, fuel - len) :: next else
                                    if len > l then next else (nx, count + 1, l - len) :: next) next rd.(cur)
                            in
                            dfs next tl
                        )
                in
                dfs [] [ s, 0, l ];
                ans.(s) <- acount
            );
            if ans.(s).(t) = 999 then -1 else ans.(s).(t)
        in
        let q = int_of_string (read_line ()) in
        for i = 1 to q do
            let s, t = Scanf.sscanf (read_line ()) "%d %d" (fun s t -> s, t) in
            Printf.printf "%d\n" (calc s t)
        done
    in
    main ()