program d;{$APPTYPE CONSOLE}{$Q+,R+,I+,O-,A+,B-}

const let:set of char=['A'..'Z'];

type tword=record txt:string; p:longint; end;
var
i0,k,page,line,words,i,n:longint;
a:array[0..11000] of tword;
s:array[0..20000]of string;
label l0;

procedure readline(var s:string);
var c:char;
begin
s:='';
if eof or eoln then exit;
repeat
	read(c);
	s:=s+c;
	until eoln;
readln;
end;

procedure sort(p,r:longint);
var i,j:longint; t:tword; label l0;
begin
while p<r do begin
	//i:=random(r-p+1)+p;
	//t:=a[i];a[i]:=a[p];a[p]:=t;
	i:=p-1; j:=r+1;
	l0:
	repeat inc(i) until (a[i].txt>a[p].txt) or (a[i].txt=a[p].txt) and (a[i].p>=a[p].p);
	repeat dec(j) until (a[j].txt<a[p].txt) or (a[j].txt=a[p].txt) and (a[j].p<=a[p].p);
	if i<j then begin
		t:=a[i];a[i]:=a[j];a[j]:=t;
		goto l0;
		end;
	sort(p,j);
	p:=j+1;
	end;
end;

procedure procline(var s:string; page:longint);
var x,x0:longint;
begin
x:=1;
while x<=length(s) do begin
	while (x<=length(s)) and not (s[x] in let) do inc(x);
	x0:=x;
	while (x<=length(s)) and (s[x] in let) do inc(x);
	inc(words);
	a[words].txt:=copy(s,x0,x-x0);
	a[words].p:=page;
	end;
end;

procedure procwordset(p,r:longint);
var zap,i,i0:longint;
begin
zap:=0;
i:=p;
while i<=r do begin
	i0:=i;
	repeat
		inc(i);
		until (i>r) or (a[i].p>a[i-1].p+1);
	if (a[i-1].p=a[i0].p) then begin if zap=1 then write(','); write(a[i-1].p); end
	else if (a[i-1].p=a[i0].p+1) then begin if zap=1 then write(','); write(a[i0].p,',',a[i-1].p); end
	else begin if zap=1 then write(','); write(a[i0].p,'-',a[i-1].p); end;
	zap:=1;
	end;
end;

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

readln(n);
words:=0;
page:=1; line:=1;
while not seekeof do begin

	k:=0;
	while true do begin
		if eof then break;
		inc(k);
		readline(s[k]);
		if s[k]='' then begin dec(k);break; end;
		for i:=1 to length(s[k]) do s[k][i]:=upcase(s[k][i]);
		end;

	if (line=n) and (k>1) then begin inc(page); line:=1; end;

	if (line+k=n+2) and ((k=3) or (k=2)) then begin inc(page); line:=1; end;

	for i:=1 to k do begin
	  if (line=n) and (i=k-1) and (k>3) then begin inc(page); line:=1; end;
		procline(s[i],page);
		inc(line);
		if line>n then begin
			inc(page);
			line:=1;
			end;
		end;

	if line<>1 then begin
		inc(line);
		if line>n then inc(page);
		end;

	end;

sort(1,words);

i:=1; while i<=words do begin
	write(a[i].txt,' ');
	i0:=i;
	repeat
		inc(i)
		until (i>words) or (a[i].txt<>a[i0].txt);
	procwordset(i0,i-1);
	writeln;
	end;

close(output);
end.
