program d;

{$APPTYPE CONSOLE}
{$R+,Q+,S+,H+}

uses
  Math,
  SysUtils;

Type
  Integer=LongInt;
  Real=Extended;

Const
  TaskID='document';
  InFile=TaskID+'.in';
  OutFile=TaskID+'.out';
  MaxM=20002;

Var
  Page:Integer;
  M:Integer;
  Para:Array[1..MaxM]Of String;
  NTr:Integer;
  Tr:Array[1..MaxM]Of Record
    Child:Array['A'..'Z']Of Integer;
    Where,LWhere:Integer;
  End;
  NWs:Integer;
  Ws:Array[1..MaxM]Of Record
    Item:Integer;
    Next:Integer;
  End;

Procedure Load;
Var
  I:Integer;
Begin
  ReSet(Input,InFile);
  ReadLn(Page);
End;

Procedure ReadPara;
Var
  S:String;
Begin
  M:=0;
  Repeat
    ReadLn(S);
    S:=UpperCase(S);
    If S='' Then Break;
    Inc(M);
    Para[M]:=S;
  Until EOF;
End;

Procedure PutIt(A:Integer; WAt:Integer);
Var
  I:Integer;
  W:String;
  Ch:Char;
Procedure Process(W:String);
Var
  Cur:Integer;
  At:Integer;
  Ch:Char;
Begin
  If W='' Then Exit;
  Cur:=1;
  For At:=1 To Length(W) Do Begin
    Ch:=W[At];
    If Tr[Cur].Child[Ch]=0 Then Begin
      Inc(NTr);
      Tr[Cur].Child[Ch]:=NTr;
      FillChar(Tr[NTr],SizeOf(Tr[NTr]),0);
    End;
    Cur:=Tr[Cur].Child[Ch];
  End;
  Inc(NWs);
  Ws[NWs].Item:=WAt;
  Ws[NWs].Next:=0;
  If Tr[Cur].Where=0 Then
    Tr[Cur].Where:=NWs
  Else
    Ws[Tr[Cur].LWhere].Next:=NWs;
  Tr[Cur].LWhere:=NWs;
End;
Begin
  W:='';
  For I:=1 To Length(Para[A]) Do Begin
    Ch:=Para[A][I];
    Case Ch Of
      'A'..'Z':W:=W+Ch;
      Else Begin
        Process(W);
        W:='';
      End;
    End;
  End;
  Process(W);
End;

Procedure Solve;
Var
  Cur,Rem,I,Start:Integer;
Begin
  ReWrite(Output,OutFile);
  Cur:=1;
  Rem:=Page;
  NTr:=1;
  FillChar(Tr[1],SizeOf(Tr[1]),0);
  NWs:=0;
  While Not SeekEOF Do Begin
    ReadPara;
    If (M>1) And (Rem=1) Then Begin
      Rem:=Page;
      Inc(Cur);
    End;
    For I:=1 To M Do Begin
      If Rem<=0 Then Begin
        Rem:=Page;
        Inc(Cur);
      End;
      If (Rem=1) And (I=M-1) Or (Rem=2) And (I=1) And (M=3) Then Begin
        Rem:=Page;
        Inc(Cur);
      End;
      PutIt(I,Cur);
      Dec(Rem);
    End;
    Dec(Rem);
    If Rem<=0 Then Begin
      Rem:=Page;
      Inc(Cur);
    End;
  End;
End;

Procedure WalkTrie(Prefix:String; At:Integer);
Var
  I,J,PJ,Start:Integer;
  First:Boolean;
  Ch:Char;
Procedure WriteSeg(A,B:Integer);
Begin
  If A>B Then Exit;
  If First Then First:=False Else Write(',');
  If A=B Then Write(A) Else If A+1=B Then Write(A,',',B) Else Write(A,'-',B);
End;
Begin
  If At=0 Then Exit;
  If Tr[At].Where>0 Then Begin
    Write(Prefix,' ');
    I:=Tr[At].Where;
    PJ:=-2;
    Start:=-1;
    First:=True;
    While I>0 Do Begin
      J:=Ws[I].Item;
      If J>PJ+1 Then Begin
        WriteSeg(Start,PJ);
        Start:=J;
      End;
      PJ:=J;
      I:=Ws[I].Next;
    End;
    WriteSeg(Start,PJ);
    WriteLn;
  End;
  For Ch:='A' To 'Z' Do If Tr[At].Child[Ch]>0 Then WalkTrie(Prefix+Ch,Tr[At].Child[Ch]);
End;

Procedure Save;
Begin
  WalkTrie('',1);
  Close(Input);
  Close(Output);
End;

begin
  Load;
  Solve;
  Save;
end.
