{$A+,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N+,O+,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y+,Z1}
program d;

{$APPTYPE CONSOLE}

Var wc,nn,I,J,K,L,N,M,ln,pc:Longint;
    A,A1:Array[1..10000] Of String[210];
    B,B1:Array[1..10000] Of Longint;
    str: string;
    lin: array[1..10010] of string[210];
    par, pag: array[-2..10010] of longint;

Procedure Merge(p,q,r:Longint);
 Var I,J,K:Longint;
Begin
  I:=p;
  J:=q+1;
  For K:=p To r Do
   If (I>q) Or ((J<=r) And (A[J]<A[I])) Then
    Begin
      A1[K]:=A[J];
      B1[K]:=B[J];
      Inc(J);
    End
   Else
    Begin
      A1[K]:=A[I];
      B1[K]:=B[I];
      Inc(I);
    End;
  For K:=p To r Do
   Begin
     A[K]:=A1[K];
     B[K]:=B1[K];
   End;
End;

Procedure Sort(p,r:Longint);
 Var q:Longint;
Begin
  If p<r Then
   Begin
     q:=(p+r) SHR 1;
     Sort(p,q);
     Sort(q+1,r);
     Merge(p,q,r);
   End;
End;

begin
  Assign(input,'document.in');
  Reset(input);
  Assign(output,'document.out');
  ReWrite(output);
  readln(n);
  ln := 0; pc := 1;
  while not eof(input) do
  begin
    readln(str);
    if str<>'' then
    begin
      str := str+' ';
      inc(ln);
      lin[ln] := str;
      par[ln] := pc;
    end
    else
      inc(pc);
  end;
  par[ln+1] := -1;
  par[0] := -2;
  i := 1;
  nn := 0;
  pc := 1;
  while i<=ln do
  begin
    inc(nn);
    if nn>n then
    begin
      nn := 1;
      inc(pc);
    end;
    pag[i] := pc;
    if (nn=n) and (par[i]<>par[i-1]) and (par[i]=par[i+1]) then
    begin
      nn := 1;
      inc(pc);
      pag[i] := pc;
    end;
    if (nn=n) and (par[i]=par[i+1]) and (par[i]<>par[i+2]) and (par[i]=par[i-1]) and (par[i]=par[i-2]) then
    begin
      nn := 1;
      inc(pc);
      pag[i] := pc;
    end;
    if (nn=n) and (par[i]=par[i+1]) and (par[i]<>par[i+2]) and (par[i]<>par[i-2]) then
    begin
      j := i;
      while par[j-1]=par[i] do dec(j);
      inc(pc);
      nn := 0;
      for k := j to i do
      begin
        inc(nn);
        pag[k] := pc;
      end;
    end;

    if (par[i+1]<>par[i]) then
      inc(nn);
    inc(i);
  end;
  wc := 0;
  for i := 1 to ln do
  begin
    j := 1;
    while j<=length(lin[i]) do
    begin
      k := j;
      while upcase(lin[i][k]) in ['A'..'Z'] do inc(k);
      if k>j then
      begin
        inc(wc);
        a[wc] := copy(lin[i], j, k-j);
        for l := 1 to length(a[wc]) do a[wc][l] := upcase(a[wc][l]);
        b[wc] := pag[i];
      end;

      j := k+1;
    end;
  end;
  n := wc;

   Sort(1,N);
  a1 := a;
  b1 := b;
  if n>0 then
  begin
  j := 1;
  for i := 2 to n do
    if (a1[i]<>a[j]) or (b1[i]<>b[j]) then
    begin
      inc(j);
      a[j] := a1[i];
      b[j] := b1[i];
    end;
  n := j;
  end;
  i := 1;
  while i<=n do
  begin
    j := i;
    while a[j+1]=a[i] do inc(j);
    write(a[i], ' ');
    k := i;
    while k<=j do
    begin
      l := k;
      while (l<j) and (b[l+1]-b[l]=1) do inc(l);
      if (l>k+1) then
      begin
        write(b[k], '-', b[l]);
        k := l+1;
      end
      else
      begin
        write(b[k]);
        inc(k);
      end;
      if k<=j then write(',');
    end;
    i := j+1;
    writeln;
  end;
  Close(input);
  Close(output);
end.
