let stream_fold f init st = let result = ref init in Stream.iter (fun x -> result := f !result x) st; !result let stream_to_rev_list st = stream_fold (fun result elem -> elem :: result) [] st let () = Stream.from (fun i -> if i >= 3 then None else let c = (65 + i) |> char_of_int and h, w = read_line () |> fun l -> Scanf.sscanf l "%d %d" (fun h w -> (h, w)) in Some(c, h, w)) |> stream_to_rev_list |> List.sort ( fun (_, h1, w1) (_, h2, w2) -> let c = compare h2 h1 in if c = 0 then compare w1 w2 else c) |> List.iter (fun (c, _, _) -> print_endline (String.make 1 c))