{DEFINE DMI}
{DEFINE DO0}
{DEFINE DEBUG}
{$IFDEF DEBUG}
{$B-,I+,N+,O-,Q+,R+,S+}
{$ELSE}
{$B-,I-,N+,O+,Q-,R-,S-}
{$ENDIF}
{$APPTYPE CONSOLE}
Program OlympSol;
uses SysUtils;
const TaskName = 'document';
 MaxN = 20007;
 alpha: set of Char = ['a'..'z', 'A'..'Z'];
type List = ^ListElm;
 ListElm = record d: Integer; Next: List end;
 str01 = record page: List; Next: array ['A'..'Z'] of Integer end;
var i, j, k, l, m, n: Integer;
 s: array [1..MaxN] of String;
 q: array [1..MaxN] of Integer;
 elm: array [1..MaxN] of str01;
 line, page, num, cur, len, r, z: Integer;
 p: List;
 ans: String;
 ok: Boolean;
 c: Char;

function is_full (k: Integer): Boolean;
begin
 result := (k >= 1) AND (k <= m) AND (s[k] <> '');
end;

function is_empty (k: Integer): Boolean;
begin
 result := (k < 1) OR (k > m) OR (s[k] = '');
end;

procedure resetpage;
begin
 ok := False;
end;

procedure outpage (k: Integer);
begin
 if NOT ok then ok := True else write (',');
 write (k);
end;

procedure outpages (k, l: Integer);
begin
 if NOT ok then ok := True else write (',');
 write (k, '-', l);
end;

procedure recur (k: Integer);
var pb, pe: Integer; c: Char;
begin
 with elm[k] do begin
  if page <> nil then begin
   write (ans, ' ');
   p := page;
   resetpage;
   while p <> nil do begin
    pb := p^.d; pe := pb;
    repeat
     p := p^.Next;
     if (p = nil) OR (p^.d > pe + 1) then begin
      if pb = pe then outpage (pb) else
       if pb + 1 = pe then begin outpage (pb); outpage (pe); end else
        outpages (pb, pe);
      break;
     end else pe := p^.d;
    until False;
   end;
   writeln;
  end;
  for c := 'A' to 'Z' do if Next[c] > 0 then begin
   inc (z); SetLength (ans, z); ans[z] := c;
   recur (Next[c]);
   dec (z); SetLength (ans, z);
  end;
 end;
end;

BEGIN
 assign (input, TaskName + '.in'); reset (input);
 assign (output, TaskName + '.out'); rewrite (output);
{$IFDEF DMI} repeat {$ENDIF}
 readln (n);
 m := 0;
 while NOT eof do begin inc (m); readln (s[m]); end;
 page := 1; line := 0;
 for i := 1 to m do begin
  inc (line); if line > n then begin inc (page); line := 1; end;
  if is_empty (i) then begin
   if line = 1 then dec (line);
  end else begin
   if line = n then
    if is_empty (i - 1) AND is_full (i + 1) then begin
     inc (page); line := 1;
    end;
   if line = n then
    if is_full (i + 1) AND is_empty (i + 2) AND (is_full (i - 1) AND
     is_full (i - 2)) then begin
     inc (page); line := 1;
    end;
   if line = n then
    if is_full (i + 1) AND is_empty (i + 2) AND (is_empty (i - 1) OR
     is_empty (i - 2)) then begin
     inc (page); line := 1;
     if is_full (i - 1) then begin q[i - 1] := page; inc (line); end;
    end;
  end;
  q[i] := page;
{$IFDEF DO0} writeln (i, ' ', page, ' ', line); {$ENDIF}
 end;
 fillchar (elm, sizeof (elm), 0);
 for i := 1 to MaxN do elm[i].page := nil;
 num := 1;
 for i := m downto 1 do begin
  cur := 1;
  len := Length (s[i]);
  while cur <= len do begin
   repeat
    if s[i][cur] in alpha then break;
    inc (cur);
    if cur > len then break;
   until False;
   if cur > len then break;
   r := 1;
   repeat
    if NOT (s[i][cur] in alpha) then break;
    c := s[i][cur];
    if c in ['a'..'z'] then dec (c, 32); {Make ['A'..'Z'];}
    with elm[r] do begin
     if Next[c] = 0 then begin inc (num); Next[c] := num; end;
     r := Next[c];
    end;
    inc (cur);
    if cur > len then break;
   until False;
   if (elm[r].page = nil) OR (elm[r].page^.d <> q[i]) then begin
    new (p); p^.d := q[i]; p^.Next := elm[r].page; elm[r].page := p;
   end;
  end;
 end;
 z := 0;
 recur (1);
{$IFDEF DMI} until seekeof; {$ENDIF}
END.
