{$APPTYPE CONSOLE}

uses
  SysUtils;

type TPair = record w:string; p:integer; end;

var
  lines:array[1..20000] of string;
  pp,pf,pl,tf,tl:array[1..20000] of integer;

  L,j,i,st,p,t,n,curp,curl,ls:longint;
  ce,le:boolean;
  tw,w:array[1..20000] of TPair;
  tws,ws:longint;

procedure add( var wrd:string; p:integer );
begin
  inc(ws);
  w[ws].w := wrd;
  w[ws].p := p;
end;

procedure doo( var s:string; p:integer );
var i,j,len:integer;
    t:string;
begin
  i := 1;
  len := Length( s );
  while i <= len do
  begin
    while (i<= len) and not (s[i] in ['a'..'z', 'A'..'Z' ]) do inc(i);
    if i > len then exit;
    j := i;
    while (i <= len ) and (s[i] in ['a'..'z', 'A'..'Z' ]) do inc(i);
    t:= Uppercase( copy(s,j,i-j) );
    Add( t, p );
  end;
end;

function Equal( var a,b:TPair):boolean;
begin
  Equal := (a.w = b.w) and (a.p=b.p);
end;

function LessEqual(var a,b:TPair):boolean;
begin
  LessEqual := Equal(a,b) or (a.w < b.w) or (a.w=b.w) and (a.p<b.p);
end;

function GreatEqual(var a,b:TPair):boolean;
begin
  GreatEqual := Equal(a,b) or (a.w > b.w) or (a.w=b.w) and (a.p>b.p);
end;

function swp(var a,b:TPair):boolean;
var t:TPair;
begin
  t:=a;
  a:=b;
  b:=t;
end;

Function Part(i, j : LongInt) : LongInt;

Var
        x : TPair;
        k : LongInt;
        
Begin
        x:= w[i];
        Inc(j);
        Dec(i);
        k:= 0;
        While (k = 0) Do
        begin
                Repeat Inc(i); Until GreatEqual(w[i], x);
                Repeat DEc(j); Until LessEqual(w[j], x);
                If (i < j) Then
                        swp(w[i], w[j]) Else
                        k:= j;
        End;
        Part:= k;
End;

procedure sort( i,j:integer );

Var
        q: LongInt;
begin
        if (i < j) Then
        Begin
          q:= Part(i, j);
          Sort(i, q);
          Sort(q+1, j);
        End;
end;

procedure outp( i,j:integer);
begin
  if j-i > 1 then write( i,'-',j)
  else if i = j then write(i)
  else write(i,',',j);
end;

procedure outw( a,b:integer);
var st,k:integer;
begin
  write( w[a].w,' ' );
  k:=a;
  st:=a;
  while k <= b do
  begin
    while (k<=b) and ((k=st) or (w[k].p = w[k-1].p + 1)) do inc(k);
    if st > a then write(',');
    outp( w[st].p, w[k-1].p );
    st := k; 
  end;
  writeln;
end;

procedure postsort();
var i:integer;
begin
  tws := 0;
  for i:=1 to ws do
    if (i = 1) or (w[i].w <> w[i-1].w) or (w[i].p <> w[i-1].p) then
    begin
      inc(tws);
      tw[tws]:=w[i];
    end;
  w:=tw;
  ws:=tws;
end;

begin
  assign(input,'document.in');reset(input);
  assign(output,'document.out');rewrite(output);

  readln(n);

  ls := 0;
  t:=0;
  le := true;
  
  while not eof do
  begin
    inc(ls);
    readln(lines[ls]);
    ce := Length(lines[ls]) = 0;

    if le then
    begin
      inc(t);
      tf[t] := ls;
      tl[t] := 0;
    end;

    if not ce then inc( tl[t] );

    le := ce;
  end;

  p := 0;
  curp := 1;
  curl := 0;

  i := 1;
  while i <= t do
  begin
    if curl+tl[i] <= N then
    begin
      inc(p);
      pf[p] := tf[i];
      pl[p] := tl[i];
      pp[p] := curp;
      curl := curl + tl[i];
    end
    else if ( tl[i] <= 3 ) or ( N - curl <= 1 ) then
    begin
      inc(curp);
      curl := 0;
      continue;
    end
    else
    begin
      inc(p);
      pf[p] := tf[i];
      pl[p] := N - curL;
      inc(curp);
      curl := 0;

      if( tl[i] - pl[i] = 1 ) then dec( pl[p] );

      tf[i] := pf[p] + pl[p] - 1;
      tl[i] := tl[i] - pl[i];

      if tl[i] = 0 then inc(i);
      
      continue;
    end;
    inc(curl);
    if curl>=N then
    begin
      inc(curp);
      curl := 0;
    end;
    inc(i);
  end;

  for i:=1 to p do
    for j:= pf[i] to pf[i] + pl[i] - 1 do
      doo( lines[j], pp[i] );

  sort( 1, ws );
  postsort();

  i := 1;
  st := 1;
  while i <= ws do
  begin
    while (i <= ws) and (w[i].w = w[st].w ) do inc(i);
    outw( st, i-1 );
    st := i; 
  end;

end.

