open Batteries

let () =
  let n = Scanf.scanf "%d " (fun a -> a) in
  let s_l = Array.to_list @@ Array.init n (fun _ -> Scanf.scanf "%s " (fun a -> a)) in
  let c_l_l = List.map (fun s -> String.to_list s |> List.sort compare) s_l in

  let rec test c = function
    | [] -> false, []
    | hd :: tl -> 
      if hd = c then true, tl else
      if hd < c then test c tl else
        false, (hd::tl)
  in

  let rec aux res c_l c_l_l = match c_l with
      [] -> res
    | c :: tl -> 
      let tmp = List.map (test c) c_l_l in
      let tf, tls = List.fold_left (fun (tf1, tls) (tf2, tl) -> (tf1 && tf2), (tl :: tls)) (true,[]) tmp in
      if tf then aux (c::res) tl tls else
        aux res tl tls
  in

  let res = aux [] (List.hd c_l_l) (List.tl c_l_l) in
  Printf.printf "%s\n" @@ 
  String.of_list (List.rev res)

