{$APPTYPE CONSOLE}
{$O-,Q+,R+}

uses Math, SysUtils;

type
  int = integer;
  bool = boolean;
  real = extended;
var
  l : array [1..10001] of string; // lines
  nl : integer; // # of lines
  np : integer; // # of paragraphs
  p : array [1..10001] of integer; // para[line]
  psz : array [1..10000] of integer; // lines in i-th para
  fl : array [1..10000] of integer ; // # of 1st line in i-th paragraph
  n : integer; // lines on a page
  page : array [1..10000] of integer; // page i-th line belongs to
  cp, pg, ln, line : integer;

  i : integer;

function getword(var s : string) : string;
var x : integer;
    ss : string;
begin
  s := s + ' ';
  x := 1;
  while s[x] in ['A'..'Z'] do inc(x);
  result := copy(s, 1, x - 1);
  delete(s, 1, x);
  s := trim(s);
end;

var ss : string;
    m : integer;
    sss : array [0..10000] of string;
    ppp : array [0..10000] of integer;
procedure add(word : string; pg : integer);
begin
  inc(m);
  sss[m] := word;
  ppp[m] := pg;
end;

function greater(i, j : integer) : boolean;
begin
  greater := (sss[i] > sss[j]) or ((sss[i] = sss[j]) and (ppp[i] > ppp[j]));
end;

procedure swap(i, j : integer);
var x : integer;
    s : string;
begin
  s := sss[i];
  sss[i] := sss[j];
  sss[j] := s;
  x := ppp[i];
  ppp[i] := ppp[j];
  ppp[j] := x;
end;

procedure Sort(l, r : integer);
var i, j, m : integer;
begin
  i := l;
  j := r;
  m := l + random(r - l + 1);
  sss[0] := sss[m];
  ppp[0] := ppp[m];
  while i <= j do
  begin
    while greater(0, i) do inc(i);
    while greater(j, 0) do dec(j);
    if i <= j then
    begin
      swap(i, j);
      inc(i);
      dec(j);
    end;
  end;
  if i < r then Sort(i, r);
  if l < j then Sort(l, j);
end;

var z : array [1..10000] of integer;
    zz, kk, r : integer;

procedure print(n : integer);
var i, j : integer;
begin
  write(z[1]);
  i := 2;
  while i <= n do
  begin
    j := i;
    while (j <= n) and (z[j] = z[j-1] + 1) do inc(j);
    if j > i then
    begin
      if j = i+1 then write(',', z[j-1]) else
      if j > i+1 then write('-', z[j-1]);
      if j <= n then write(',', z[j]);
      i := j;
    end else
    begin
      write(',', z[i]);
      inc(i);
    end;
  end;
end;
var j : integer;
begin
  randseed := 238723;
  reset(input, 'document.in');
  rewrite(output, 'document.out');
  readln(n);
  nl := 0;
  while not eof do
  begin
    inc(nl);
    readln(l[nl]);
    l[nl] := UpperCase(l[nl]);
    assert((l[nl] = '') or ((l[nl][1] <> ' ') and (l[nl][length(l[nl])] <> ' ')));
  end;
  np := 0;
  for i := 1 to nl do
    if l[i] = '' then p[i] := 0 else
  begin
    if (i = 1) or (p[i-1] = 0) then // new paragraph
    begin
      inc(np);
      p[i] := np;
      psz[np] := 1;
      fl[np] := i;
    end else
    begin
      p[i] := p[i-1];
      inc(psz[p[i]]);
    end;
  end;

  l[nl+1] := '';
  p[nl+1] := 0;

  // now pagination...
  // just filling page[i]

//  cp -- current paragraph
  pg := 1; // current page
  ln := 1; // current line of current page

  for cp := 1 to np do
  begin
    if (psz[cp] > 1) and (ln = n) then
    begin
      ln := 1;
      inc(pg);
    end;
    if (psz[cp] = 3) and (ln = n - 1) then
    begin
      ln := 1;
      inc(pg);
    end;

    line := fl[cp];
    while p[line] = cp do
    begin
      if (ln = n) and (p[line+1] = cp) and (p[line+2] <> cp) then
      begin
        ln := 1;
        inc(pg);
      end;
      page[line] := pg;
      inc(ln);
      if ln > n then
      begin
        ln := 1;
        inc(pg);
      end;
      inc(line);
    end;
    if ln <> 1 then inc(ln); // blank line after paragraph
    if ln > n then
    begin
      ln := 1;
      inc(pg);
    end;
  end;

  // pagination done!!!

  m := 0;

  for i := 1 to nl do if l[i] <> '' then
  begin
    assert((1 <= page[i]) and (page[i] <= pg));
    while l[i] <> '' do
    begin
      ss := getword(l[i]);
      if ss <> '' then
         add(ss, page[i]);
    end;
  end;

  Sort(1, m);


  i := 1;
  while i <= m do
  begin
    j := i;
    while (j < m) and (sss[j] = sss[j+1]) do inc(j);
    write(sss[i], ' ');
    zz := 0;
    for kk := i to j do
    begin
      inc(zz);
      z[zz] := ppp[kk];
    end;
    r := 1;
    for kk := 2 to zz do if z[kk] <> z[kk-1] then
    begin
      inc(r);
      z[r] := z[kk];
    end;
    print(r);
    writeln;

    i := j + 1;
  end;
end.