Scanf.scanf "%s %d" (fun s k ->
    let module S = Set.Make (struct type t = string let compare = compare end) in

    let n = String.length s in

    let rec loop st en set =
        if st = n then set else
            if en = n then loop (st + 1) (st + 1) set else
                loop st (en + 1) (S.add (String.sub s st (en - st + 1)) set)
    in
    let set = loop 0 0 S.empty in

    let rec loop i set =
        let m = S.min_elt set in
        if i = k then m else loop (i + 1) (S.remove m set)
    in
    loop 1 set |> print_endline
)