{$R+,Q-,O+}
{$MINSTACKSIZE $00404000}
{$APPTYPE CONSOLE}
type
  int = longint;

var
  n, m: int;
  s: array [1..21000] of string;
  par, page: array [1..21000] of int;
  pnum, psize: array [1..21000] of int;

procedure do_paging;
var
  i, u, cur, v, cpage: int;

begin
  u:=1;
  fillchar(psize, sizeof(psize), 0);
  v:=0;
  for i:=1 to m do
  begin
    if (s[i] = '') then
    begin
      inc(u);
      v:=0;
      continue;
    end;
    par[i]:=u;
    inc(v);
    pnum[i]:=v;
    inc(psize[u]);
  end;

  cur:=1;
  i:=1;
  cpage:=1;
  while (i <= m) do
  begin
    u:=par[i];
    page[i]:=cpage;
    if (s[i] = '') then
    begin
      inc(i, 1);
      inc(cur);
      if (cur = n + 1) then
      begin
        inc(cpage);
        cur:=1;
      end;
      continue;
    end;

    if (cur = n) and (pnum[i] = psize[u]) then
    begin
      inc(i, 2);
      cur:=1;
      inc(cpage);
      continue;
    end;

    if (cur = n) and (pnum[i] = 1) and (psize[u] > 1) then
    begin
      inc(cpage);
      page[i]:=cpage;
      cur:=2;
      inc(i);
      continue;
    end;

    if (cur = n) and (pnum[i] = psize[u] - 1) and (psize[u] > 3) then
    begin
      inc(cpage);
      page[i]:=cpage;
      cur:=1;
      inc(i);
      continue;
    end;

    if (cur = n) and (pnum[i] = psize[u] - 1) and ((psize[u] = 2) or (psize[u] = 3)) then
    begin
      inc(cpage);
      if (psize[u] = 3) then
        page[i - 1]:=cpage;

      page[i]:=cpage;
      cur:=1;
      inc(i);
      continue;
    end;

    page[i]:=cpage;
    inc(i);
    inc(cur);
    if (cur = n + 1) then
    begin
      inc(cpage);
      cur:=1;
    end;
  end;
end;

var
  go: array [1..21000, char] of int;
  finish: array [1..21000] of boolean;
  head, info, next: array [1..21000] of int;
  bsize, w: int;

function is_letter(ch: char): boolean;
begin
  result:=(('a' <= ch) and (ch <= 'z')) or (('A' <= ch) and (ch <= 'Z'));
end;

procedure add(cur, p: int);
begin
  inc(w);
  info[w]:=p;
  next[w]:=head[cur];
  head[cur]:=w;
end;

procedure process;
var
  i, j, cur: int;
  ch: char;

begin
  bsize:=1;
  w:=0;
  fillchar(finish, sizeof(finish), false);
  for i:=1 to m do
  begin
    cur:=1;
    for j:=1 to length(s[i]) do
    begin
      ch:=upcase(s[i][j]);
      if (is_letter(ch)) then
      begin
        if (go[cur][ch] = 0) then
        begin
          inc(bsize);
          go[cur][ch]:=bsize;
        end;
        cur:=go[cur][ch];
        continue;
      end;

      if (cur > 1) then
      begin
        finish[cur]:=true;
        add(cur, page[i]);
        cur:=1;
      end;
    end;
    if (cur > 1) then
    begin
      finish[cur]:=true;
      add(cur, page[i]);
      cur:=1;
    end;
  end;
end;

var
  mymark: array [0..5000] of int;
  mymarkcur: int;

procedure print_word_info(cur: int);
var
  i, u, prev, len: int;
  first: boolean;
  
begin
  u:=head[cur];
  inc(mymarkcur);
  while (u > 0) do
  begin
    mymark[info[u]]:=mymarkcur;
    u:=next[u];
  end;

  write(' ');
  prev:=-1;
  len:=0;
  first:=true;
  for i:=1 to 5000 do
    if (mymark[i] = mymarkcur) then
    begin
      if (i > 1) and (mymark[i - 1] = mymarkcur) then
      begin
        inc(len);
        continue;
      end;

      prev:=i;
      len:=1;
    end
    else
    begin
      if (prev = -1) then
        continue;
        
      if (not first) then
        write(',');

      first:=false;
      if (len > 2) then
        write(prev, '-', i - 1)
      else if (len = 2) then
        write(i - 2, ',', i - 1)
      else
        write(i - 1);

      prev:=-1;
    end;
end;

procedure print(cur: int; prefix: string);
var
  ch: char;
  
begin
  if (finish[cur]) then
  begin
    write(prefix);
    print_word_info(cur);
    writeln;
  end;
  
  for ch:=#32 to #255 do
    if (go[cur][ch] <> 0) then
      print(go[cur][ch], prefix + ch);
end;

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

  readln(n);
  m:=0;
  while not eof do
  begin
    inc(m);
    readln(s[m]);
  end;
  
  do_paging;
  process;
  mymarkcur:=1;
  fillchar(mymark, sizeof(mymark), 0);
  print(1, '');
end.

