program d;

{$APPTYPE CONSOLE}

var
  Par: array[1..20000] of String;
  N, NP: Integer;

procedure ReadParagraph;
var
  S:String;
begin
  NP:=0;
  while (not SeekEof) do
    begin
      ReadLn(S);
      Inc(NP);
      Par[NP]:=S;
      if (S = '') then Exit;
    end;
  Inc(NP);
  Par[NP]:='';
end;

var
  W: array[1..10000] of record
    S:String;
    P:Integer;
  end;
  NW: Integer;
  Ind: array[1..10000] of Integer;

procedure AddWord(Wrd: String; Page: Integer);
var
  I:Integer;
begin
  for I:=1 to Length(Wrd) do
    Wrd[I]:=UpCase(Wrd[I]);

  Inc(NW);
  W[NW].S:=Wrd;
  W[NW].P:=Page;
  Ind[NW]:=NW;
end;

procedure ProcessLine(S:String; P: Integer);
var
  I, J:Integer;
  IsWord: Boolean;
begin
  S:=S+' ';
  I:=1;
  IsWord:=True;
  for J:=2 to Length(S) do
    if not (S[J] in ['a'..'z', 'A'..'Z']) and Isword then
      begin
        AddWord(Copy(S, I, J-I), P);
        IsWord:=False;
      end
    else if (S[J] in ['a'..'z', 'A'..'Z']) and (not IsWord) then
      begin
        IsWord:=True;
        I:=J;
      end;
end;

procedure Swap(var A, B: Integer);
var
  T: Integer;
begin
  T:=A;
  A:=B;
  B:=T;
end;

procedure Heapify(I, N:Integer);
var
  L: Integer;
begin
  repeat
    L:=I;
    if (I*2 <= N) and (W[Ind[I*2]].S > W[Ind[L]].S) then L:=I*2;
    if (I*2+1<= N) and (W[Ind[I*2+1]].S > W[Ind[L]].S) then L:=I*2+1;
    if (I = L) then Break;
    Swap(Ind[I], Ind[L]);
    I:=L;
  until false;
end;

procedure Sort;
var
  I:Integer;
begin
  for I:=NW div 2 downto 1 do
    Heapify(I, NW);

  for I:=NW-1 downto 1 do
    begin
      Swap(Ind[1], Ind[I+1]);
      Heapify(1, I);
    end;
end;

procedure Output;
var
  I, J, K:Integer;

  First: Boolean;
begin
  Sort;

  I:=1;
  while (I <= NW) do
    begin
      J:=I;

      First:=True;
      while (J <= NW) and (W[j].S = W[I].S) do
        begin
          if (not First) then Write(',');
          First:=False;
          K:=J+1;
          while (K <= NW) and (W[K].S = W[I].S) and (W[K].P = W[K-1].P+1) do Inc(K);
          if (K > J+2) then
            Write(W[J].P, '-', W[K-1].P)
          else
            begin
              Write(W[J].P);
              if (K > J+1) then
                Write(',', W[J+1].P);
            end;
          J:=K;
        end;
      I:=J;
    end;
end;

procedure Run;
var
  CurP, CurL: Integer;
  I:Integer;
begin
  ReadLn(N);

  NP:=0;
  CurP:=1;
  CurL:=1;

  while not SeekEof do
   begin

    ReadParagraph;

    if (CurL = N) or (NP = 4) and (N-CurL+1 < 3) then
      begin
        Inc(CurP);
        CurL:=1;
      end;

    for I:=1 to NP-1 do
      begin
        if (I = NP-1) and (CurL = N) then
          begin
            Inc(CurP);
            CurL:=1;
          end;

        ProcessLine(Par[I], CurP);
        Inc(CurL);
        if (CurL > N) then
          begin
            Inc(CurP);
            CurL:=1;
          end;
      end;

    if (CurL <> 1) then
      begin
        Inc(CurL);
        if (CurL > N) then
          begin
            Inc(CurP);
            CurL:=1;
          end;
      end;
    end;
end;

begin
  Assign(Input, 'document.in');
  Reset(Input);
  Assign(System.Output, 'document.out');
  ReWrite(System.Output);

  Run;
  Output;
end.
