program d;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes;

var
  txt: TStringList;
  i, n, onpage, pagenr, code: integer;
  index: TStringList;
  s: string;


procedure saveres;
var f: text;
    lastword, word: string;
    pn: integer;
    pns: string;
    pages: array[0..10000] of integer;
    i, j: integer;
    a,b,c: integer;
begin
  assign (f, 'document.out');
  rewrite (f);

  lastword := '';
  pages[0] := 0;
  for i := 0 to index.Count-1 do
  begin

    word := index.Strings[i];
    pn := pos(' ', word);
    pns := copy(word,pn+1,length(word)-pn);
    word := copy(word,1,pn-1);
    val (pns, pn, j);


    if (i>0) and (lastword <> word) then
    begin
      for a := 1 to pages[0] do
        for b := a to pages[0] do
        begin
          if (pages[a] > pages[b]) then
          begin
            c := pages[a];
            pages[a] := pages[b];
            pages[b] := c;
          end;
        end;

      c := 0;
      pns := IntToStr(pages[1]);
      for j := 2 to pages[0] do
      begin
        if (j < pages[0]) and (pages[j-1]+2 = pages[j+1]) then
        begin
          c := j+1;
          //pns := pns+'-';
        end
        else
        begin
          if (c = j) then pns := pns+'-'+inttostr(pages[j])
          else pns := pns+','+inttostr(pages[j]);

        end
      end;
      writeln (f, lastword, ' ', pns);
      pages[0] := 0;
    end;

    inc(pages[0]);
    pages[pages[0]] := pn;
    lastword := word;
  end;



      for a := 1 to pages[0] do
        for b := a to pages[0] do
        begin
          if (pages[a] > pages[b]) then
          begin
            c := pages[a];
            pages[a] := pages[b];
            pages[b] := c;
          end;
        end;

      c := 0;
      pns := IntToStr(pages[1]);
      for j := 2 to pages[0] do
      begin
        if (j < pages[0]) and (pages[j-1]+2 = pages[j+1]) then
        begin
          c := j+1;
          //pns := pns+'-';
        end
        else
        begin
          if (c = j) then pns := pns+'-'+inttostr(pages[j])
          else pns := pns+','+inttostr(pages[j]);

        end
      end;
      writeln (f, lastword, ' ', pns);
      pages[0] := 0;




  close (f);
end;


procedure indexline(s: string; pn: integer);
  var word: string;
      p: integer;
begin
//  writeln (s, ' ', inttostr(pn));
  p := 1;
  while p <= length(s) do
  begin
    while not (s[p] in ['A'..'Z','a'..'z']) do
      inc(p);
    word := '';
    if (p <= length(s)) then
    while (s[p] in ['A'..'Z','a'..'z']) do
    begin
      word := word + s[p];
      inc(p);
    end;
    if length(word) > 0 then
      index.Add(uppercase(word) + ' ' + inttostr(pn));
  end;
end;


var firstline, lastline, nexttolast: boolean;
    j,parlines: integer;

begin
  txt := TStringList.Create;
  index := TStringList.Create;
  index.Sorted := true;
  index.Duplicates := dupIgnore;

  txt.LoadFromFile('document.in');
  val (txt.Strings[0], n, code);
  firstline := true;
  pagenr := 1;
  onpage := 0;
  parlines := 0;
  for i := 1 to txt.Count-1 do
  begin
    s := txt.strings[i];
    lastline := (i = txt.Count-1) or (txt.strings[i+1] = '');
    inc(onpage);

    if (firstLine) then
    begin
      j := 1;
      while (i+j < txt.Count) and (j<4) and (txt.strings[i+j] <> '') do
        inc (j);
      parlines := j;

      if ((parlines = 2) and (onpage=n)) or
         ((parlines = 3) and (onpage=n-1)) then
      begin
        onpage := 1;
        inc(pagenr);
      end;
    end;

    nexttolast := (i+2 < txt.Count) and (txt.strings[i+2] = '') and (txt.strings[i+1] <> '');

    if (s = '') and (onpage = 1) then dec(onpage) // 1 sal
    else if (firstline) and (not lastline) and (onpage = n) then    // 2 sal
    begin
      onpage := 1;
      inc(pagenr);
    end
    else if nexttolast and (onpage=n) and (parlines > 3) then
    begin
      onpage := 1;
      inc(pagenr);
    end;

    indexline (s, pagenr);
    firstline := s = '';

    if (onpage = n) then
    begin
      onpage := 0;
      inc(pagenr);
    end;
  end;
  txt.Free;

  //index.Sort;
  //index.SaveToFile('document.out');

  saveres;

  index.Free;

// readln;
end.
