{$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}

const fname = 'document';
      ifile = fname + '.in';
      ofile = fname + '.out';
      MAXM = 20000;
      MAXN = 210;
      MAXS = 40000;
      MAXBOR = 500000;
      MAXNE = 1000000;

type TT = record
         mas: array['A'..'Z']of longint;
         be: longint;
         end;

var outs: string;
    numo: longint;
    o: array[1..MAXS,1..2]of longint;

var numb,numne,n,m: longint;
    s: array[1..MAXM]of string;
    f: array[1..MAXM]of boolean;
    mm: array[1..MAXS,1..MAXN]of longint;
    ne: array[1..MAXNE,1..2]of longint;
    bor: array[1..MAXBOR]of TT;


procedure load();
var k: longint;
begin
  readln(n);
  while not eof do begin
    inc(m);
    readln(s[m]);
    for k:=1 to length(s[m]) do
      if s[m][k] in ['a'..'z'] then
        s[m][k]:=chr(ord(s[m][k])-ord('a')+ord('A'));
    if s[m]='' then
     f[m]:=true;
  end;
end;

procedure add(x,page: longint);
var str: string;
    len,b,k: longint;
    o: boolean;

                procedure f(b,e: longint);
                var k,l: longint;
                begin
                  l:=1;
                  for k:=b to e do begin
                    if bor[l].mas[str[k]]=0 then begin
                      inc(numb);
                      bor[l].mas[str[k]]:=numb;
                    end;
                    l:=bor[l].mas[str[k]];
                  end;
                  if (bor[l].be=0)or(ne[bor[l].be,1]<>page) then begin
                    inc(numne);
                    ne[numne,1]:=page;
                    ne[numne,2]:=bor[l].be;
                    bor[l].be:=numne;
                  end;
                end;

begin
  str:=s[x]+'!';
  len:=length(str);
  o:=false;
  for k:=1 to len do
    if str[k] in ['A'..'Z'] then begin
      if not o then begin
        b:=k;
        o:=true;
      end;
    end
    else if o then begin
      f(b,k-1);
      o:=false;
    end;
end;


procedure solve();
var k,l,num,p,len: longint;
begin
  numb:=1;
  numne:=0;
  num:=1; p:=0; len:=0;
  k:=1;
  while k<=m do begin
    inc(p);
    if f[k] then begin
      if p=1 then {1}
        dec(p);
      if (p=2)and(len>3) then begin {3}
        mm[num-1,n]:=0;
        mm[num,1]:=0;
        dec(k,2);
        p:=0;
        dec(len,2);
        continue;
      end;
      if (p=2)and(len=3) then begin {4.1}
        mm[num-1,n-1]:=0;
        mm[num-1,n]:=0;
        mm[num,1]:=0;
        dec(k,4);
        len:=0;
        p:=0;
        continue;
      end;
      len:=0;
      inc(k);
    end
    else begin
      inc(len);
      if (p=1)and(len=2) then begin {2}
        mm[num-1,n]:=0;
        dec(k);
        p:=0;
        len:=0;
        continue;
      end;
      mm[num,p]:=k;
      inc(k);
    end;
    if p=n then begin
      inc(num);
      p:=0;
    end;
  end;
{  for k:=1 to num do begin
    for l:=1 to n do
      write(mm[k,l],' ');
    writeln;
  end;{}
  for k:=1 to num do
    for l:=1 to n do
      if mm[k,l]>0 then
        add(mm[k,l],k);
end;

procedure rec(x: longint);
var f: boolean;
    k: longint;
    c:char;
begin
  if bor[x].be>0 then begin
    write(outs,' ');
    numo:=0;
    k:=bor[x].be;
    while k>0 do begin
      if (numo>0)and(o[numo,2]=ne[k,1]+1) then
        dec(o[numo,2])
      else begin
        inc(numo);
        o[numo,1]:=ne[k,1];
        o[numo,2]:=ne[k,1];
      end;
      k:=ne[k,2];
    end;
    f:=false;
    for k:=numo downto 1 do begin
      if f then
        write(',')
      else
        f:=true;
      if o[k,1]=o[k,2] then
        write(o[k,1])
      else if o[k,1]-1=o[k,2] then
        write(o[k,2],',',o[k,1])
      else
        write(o[k,2],'-',o[k,1]);
    end;
    writeln;    
  end;

  for c:='A' to 'Z' do
    if bor[x].mas[c]>0 then begin
      outs:=outs+c;
      rec(bor[x].mas[c]);
      outs:=copy(outs,1,length(outs)-1);      
    end;  
end;                         

procedure save();
begin
  outs:='';
  rec(1);
end;

begin
  assign(input,ifile);
  reset(input);
  assign(output,ofile);
  rewrite(output);
  load;
  solve;
  save;
end.
 