let parse_line l =
  String.split_on_char ' ' l
  |> List.map (fun e -> e |> String.trim |> int_of_string)

let frequency_appearance ss =
  let freqs = Hashtbl.create (List.length ss) in
  ss |> List.iter (fun s ->
      let count = try Hashtbl.find freqs s with Not_found -> 0 in
      Hashtbl.add freqs s (count + 1));
  List.map (Hashtbl.find freqs) ss

let apply_until_fix ss =
  let rec go ss n =
    let ss' = frequency_appearance ss in
    if ss = ss' then (ss, n) else go ss' (n + 1) in
  go ss 0

let () =
  ignore @@ read_line ();  (* Drop first line. *)
  let rec loop () =
    let l = read_line () |> String.trim in
    if l = "0" then ()
    else
      let ss, c = parse_line l |> apply_until_fix in
      print_int c; print_newline ();
      List.map string_of_int ss |> String.concat " " |> print_endline;
      loop () in
  loop ()