program taskd;
{$APPTYPE CONSOLE}
{ $I+,Q+,R+,O-}
{$I-,Q-,R-,O+}
uses
  SysUtils,Math;
type
  short=smallint;
  long=longint;
  real=extended;
  bool=boolean;
type
  plinelist=^tlinelist;
  tlinelist=record
    st: string;
    next: plinelist;
  end;
  pnumlist=^tnumlist;
  tnumlist=record
    x: long;
    next: pnumlist;
  end;
  pnode=^tnode;
  tnode=record
    pages: pnumlist;
    next: array['A'..'Z'] of pnode;
  end;

  procedure readpar(var cnt: long; var p: plinelist);
  var
    st: string;
    q1,q2: plinelist;
  begin
    if eof then st:=''
    else readln(st);
    cnt:=0;
    if st='' then begin
      p:=nil;
      exit;
    end;
    new(p);
    p^.st:=st;
    p^.next:=nil;
    q1:=p;
    cnt:=1;
    repeat
      if eof then st:=''
      else readln(st);
      if st='' then break;
      inc(cnt);
      new(q2);
      q2^.st:=st;
      q2^.next:=nil;
      q1^.next:=q2;
      q1:=q2;
    until false;
  end;

var
  pos: long;
  str: array[1..300] of char;

  procedure print(root: pnode);
  var
    i,c,last,pr: long;
    p,q1,q2: pnumlist;
    ch: char;
    first: bool;
    procedure outp;
    begin
      if first then first:=false else write(',');
      if c=1 then write(last)
      else if c=2 then write(last-1,',',last)
      else write(last-c+1,'-',last);
    end;
  begin
    if root^.pages<>nil then begin
      p:=nil;
      q1:=root^.pages;
      while q1<>nil do begin
        q2:=q1^.next;
        q1^.next:=p;
        p:=q1;
        q1:=q2;
      end;
      for i:=1 to pos do write(str[i]);
      write(' ');
      first:=true;
      c:=0; last:=0;
      pr:=0;
      while p<>nil do begin
        if p^.x<>pr then
          if c=0 then begin
            c:=1;
            last:=p^.x;
          end
          else if p^.x=last+1 then begin
            inc(c);
            last:=p^.x;
          end
          else begin
            outp;
            c:=1;
            last:=p^.x;
          end;
        pr:=p^.x;
        p:=p^.next;
      end;
      if c>0 then outp;
      writeln;
    end;
    inc(pos);
    for ch:='A' to 'Z' do
      if root^.next[ch]<>nil then begin
        str[pos]:=ch;
        print(root^.next[ch]);
      end;
    dec(pos);
  end;

var
  n,left,c,curpage: long;
  p: plinelist;
  root: pnode;

  procedure nextpage;
  begin
    inc(curpage);
    left:=n;
  end;

  procedure addword(st: string);
  var
    p,q: pnode;
    qq: pnumlist;
    i: long;
    ch: char;
  begin
    p:=root;
    for i:=1 to length(st) do begin
      ch:=st[i];
      if p^.next[ch]=nil then begin
        new(q);
        fillchar(q^,sizeof(q^),0);
        p^.next[ch]:=q;
      end;
      p:=p^.next[ch];
    end;
    new(qq);
    qq^.x:=curpage;
    qq^.next:=p^.pages;
    p^.pages:=qq;
  end;

  procedure addstr(st: string);
  var i,j: long;
  begin
    for i:=1 to length(st) do st[i]:=upcase(st[i]);
    i:=1;
    while i<=length(st) do
      if st[i] in ['A'..'Z'] then begin
        j:=i+1;
        while (j<=length(st)) and (st[j] in ['A'..'Z']) do inc(j);
        addword(copy(st,i,j-i));
        i:=j+1;
      end
      else inc(i);
  end;

  procedure addpar(cnt: long; p: plinelist);
  var
    i: long;
  begin
    if left=1 then nextpage;
    if (left=2) and (cnt>1) then nextpage;
    if (cnt=2) or (cnt=3) and (left=cnt) then nextpage;
    if left<n then dec(left);
    for i:=1 to cnt do begin
      if left=0 then nextpage;
      dec(left);
      if (left=1) and (i=cnt-1) then nextpage;
      addstr(p^.st);
      p:=p^.next;
    end;
  end;

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

  readln(n);
  new(root);
  fillchar(root^,sizeof(root^),0);
  curpage:=1;
  left:=n;
  repeat
    readpar(c,p);
    if p=nil then break;
    addpar(c,p);
  until false;
  pos:=0;
  print(root);

  close(input);
  close(output);
end.

