{$O+,R-,Q-}
const
 P=9001;
type
 str=record
  s:string;
  yo:array[1..2000] of boolean;
 end;
var
 cnt,i2,jo,l,t,tq,n,i,k,j:longint;
 st:string;
 s:array[0..P] of string;
 num,par,parcol,page:array[0..P] of longint;
 yetpage,numc,parc,pagec,col:longint;
 h:array[0..P] of str;
 ind:array[0..P] of longint;
 fr:boolean;

function hash(const s:string; x,p:longint):longint;
var
 i:longint;
begin
 result:=0;
 for i:=1 to length(s) do begin
  result:=result*x+ord(s[i]);
  if result>=p then
   result:=result mod p; 
 end;
end;

function hashget(const s:string):longint;
var
 h1,h2:longint;
begin
 h1:=hash(s,3,p);
 if h[h1].s<>'' then
  h2:=hash(s,5,p-1)+1;
 while h[h1].s<>'' do begin
  if h[h1].s=s then begin
   result:=h1;
   exit;
  end;
  inc(h1,h2);
  if h1>=p then
   dec(h1,p); 
 end;
 h[h1].s:=s;
 result:=h1;
end;

procedure qsort(l,r:longint);
var
 i,j:longint;
 x:string;
begin
 i:=l;
 j:=r;
 x:=h[ind[random(r-l+1)+l]].s;
 repeat
  while h[ind[i]].s<x do inc(i);
  while h[ind[j]].s>x do dec(j);
  if i<=j then begin
   tq:=ind[i];
   ind[i]:=ind[j];
   ind[j]:=tq;
   inc(i);
   dec(j);
  end;
 until i>j;
 if i<r then qsort(i,r);
 if l<j then qsort(l,j);
end;

begin
 assign(input,'document.in'); reset(input);
 assign(output,'document.out'); rewrite(output);
 readln(n);
 col:=0;
 while not eof do begin
  inc(col);
  readln(s[col]);
  for k:=1 to length(s[col]) do
   s[col,k]:=upcase(s[col,k]);
 end;

 parc:=1;
 numc:=0;
 for i:=1 to col do begin
  if s[i]='' then begin
   parcol[parc]:=numc;
   inc(parc);
   numc:=0;
  end
  else begin
   inc(numc);
   num[i]:=numc;
   par[i]:=parc;
  end;
 end;
 parcol[parc]:=numc;

 pagec:=1;
 yetpage:=0;
 i:=0;
 while i<col do begin
  inc(i);
  inc(yetpage);
  page[i]:=pagec;
  if yetpage=n then begin
   inc(pagec);
   yetpage:=0;
   if (num[i]<>0) and (num[i]=parcol[par[i]]) then begin
    inc(i);
   end
   else if (num[i]=1) and (parcol[par[i]]>1) then begin
    page[i]:=-1;
    dec(i);
   end
   else if (num[i]=parcol[par[i]]-1) and (parcol[par[i]]>3) then begin
    page[i]:=-1;
    dec(i);
   end
   else if (num[i]=parcol[par[i]]-1) and ((parcol[par[i]]=2) or (parcol[par[i]]=3)) then begin
    t:=par[i];
    while par[i]=t do begin
     page[i]:=-1;
     dec(i);
    end;
   end
  end;
 end;


 for i:=1 to col do begin
  j:=1;
  l:=length(s[i]);
  while true do begin
   while (j<=l) and ((s[i,j]<'A') or (s[i,j]>'Z')) do
    inc(j);
   if j>l then break;
   jo:=j;
   cnt:=0;
   while (j<=l) and (s[i,j]>='A') and (s[i,j]<='Z') do begin
    inc(j);
    inc(cnt);
   end;
   st:=copy(s[i],jo,cnt);
   t:=hashget(st);
{   writeln(st);
   if st='JOIN' then begin
    writeln('nya');
   end;
   flush(output);}
   h[t].yo[page[i]]:=true;
  end;
 end;

 for i:=0 to p do
  ind[i]:=i;

 qsort(0,P);

 for i:=0 to P do begin
  i2:=ind[i];
  if h[i2].s<>'' then begin
   write(h[i2].s,' ');
   fr:=true;
   j:=0;
   while j<pagec do begin
    inc(j);
    if h[i2].yo[j] then begin
     if fr then fr:=false
     else write(',');
     write(j);
     jo:=j;
     while h[i2].yo[j+1] and h[i2].yo[j+2] do
      inc(j);
     if jo<>j then begin
      write('-',j+1);
      inc(j);
     end;
    end; // j<pagec
   end;
   writeln;
  end;
 end; // for
// writeln('nya');
end.

