{$APPTYPE CONSOLE}
{$O-,R+,Q+}
uses
  Sysutils, Math;
const
  inf = 1000000000;
  eps = 1e-9;
  base = 10;
type
  int = longint;
  bool = boolean;
  real = extended;
  tarr = record
    w: bool;
    last, first: int;
    str: string;
  end;

var
  i, j, k, n, m: int;
  cline, cpages: longint;
  next: array[1..30000, 'A'..'Z'] of int;
  last: array[1..30000] of tarr;
  free: longint;
  curpar, line: array[1..20000] of string;
  page: array[1..20000] of int;

procedure add(s: string; p: int);
var
  curr: int;
begin
  if s = '' then
    exit;
  curr := 1;
  for i := 1 to length(s) do
  begin
    if next[curr, s[i]] = 0 then
    begin
      next[curr, s[i]] := free;
      last[free].w := false;
      curr := free;
      inc(free);
    end else begin
      curr := next[curr, s[i]];
    end;
  end;
  if not last[curr].w then
  begin
    last[curr].w := true;
    last[curr].first := p;
    last[curr].last := p;
    last[curr].str := '';
  end else begin
    if last[curr].last = p then
      exit;
    if last[curr].last = p - 1 then
    begin
      last[curr].last := p;
      exit;
    end;
    if (last[curr].str <> '') then
    begin
      last[curr].str := last[curr].str + ',';
    end;
    if last[curr].first = last[curr].last then
    begin
      last[curr].str := last[curr].str + inttostr(last[curr].last);
    end else if last[curr].first + 1 = last[curr].last then
    begin
      last[curr].str := last[curr].str + inttostr(last[curr].first) + ',' + inttostr(last[curr].last);
    end else begin
      last[curr].str := last[curr].str + inttostr(last[curr].first) + '-' + inttostr(last[curr].last);
    end;
    last[curr].first := p;
    last[curr].last := p;
  end;
end;

procedure toout(pref: string; p: int);
var
  i: longint;
  ch: char;
  curr: int;
begin
  if last[p].w then
  begin
    curr := p;
    if (last[curr].str <> '') then
    begin
      last[curr].str := last[curr].str + ',';
    end;
    if last[curr].first = last[curr].last then
    begin
      last[curr].str := last[curr].str + inttostr(last[curr].last);
    end else if last[curr].first + 1 = last[curr].last then
    begin
      last[curr].str := last[curr].str + inttostr(last[curr].first) + ',' + inttostr(last[curr].last);
    end else begin
      last[curr].str := last[curr].str + inttostr(last[curr].first) + '-' + inttostr(last[curr].last);
    end;
    write(pref, ' ');
    writeln(last[p].str);
  end;
  for ch := 'A' to 'Z' do
  begin
    if next[p, ch] > 0 then
      toout(pref + ch, next[p, ch]);
  end;
end;

procedure solve;
var
  i: int;
  str: string;
begin
  free := 2;
  for i := 1 to cline do
  begin
    str := '';
    for j := 1 to length(line[i]) do
    begin
      if line[i][j] in ['A'..'Z'] then
      begin
        str := str + line[i][j];
      end else begin
        add(str, page[i]);
        str := '';
      end;
    end;
    add(str, page[i]);
  end;
  toout('', 1);
end;

var
  wst: string;
  pagelines, parlines: int;
  pnum: int;
  
begin
  assign(input, 'document.in');
  reset(input);
  assign(output, 'document.out');
  rewrite(output);
  readln(n);
  parlines := 0;
  pagelines := 0;
  pnum := 1;
  readln(wst);
  wst := uppercase(wst);
  while not eof do
  begin
    parlines := 0;
    while wst <> '' do
    begin
      inc(parlines);
      curpar[parlines] := wst;
      readln(wst);
      wst := uppercase(wst);
    end;

    i := 1;
    while i <= parlines do
    begin
      if (i = parlines) and (pagelines = n) then
      begin
        inc(pnum);
        inc(cline);
        readln(wst);
        pagelines := 1;
      end;
      if (pagelines = n - 1) and (parlines >= 1) and (i = 1) then
      begin
        pagelines := 1;
        inc(pnum);
      end;
      if (i = parlines - 1) and (pagelines = n) and (parlines > 3) then
      begin
        inc(pnum);
        pagelines := 1;
      end;
      if (i = parlines - 1) and (pagelines = n) and ((parlines = 3) or (parlines = 2)) then
      begin
        i := 1;
        inc(pnum);
        pagelines := 1;
      end;
      inc(cline);
      line[cline] := curpar[i];
      page[cline] := pnum;
      inc(pagelines);
      inc(i);
    end;
    inc(cline);
    readln(wst);
    inc(pagelines);
    if pagelines >= n then
    begin
      pagelines := 1;
      inc(pnum);
    end;
    wst := uppercase(wst);
  end;

    parlines := 0;
    while (wst <> '') do
    begin
      inc(parlines);
      curpar[parlines] := wst;
      readln(wst);
      wst := uppercase(wst);
      if eof then break;
    end;
    i := 1;
    while i <= parlines do
    begin
      if (i = parlines) and (pagelines = n) then
      begin
        inc(pnum);
        inc(cline);
        readln(wst);
        pagelines := 1;
      end;
      if (pagelines = n - 1) and (parlines >= 1) and (i = 1) then
      begin
        pagelines := 1;
        inc(pnum);
      end;
      if (i = parlines - 1) and (pagelines = n) and (parlines > 3) then
      begin
        inc(pnum);
        pagelines := 1;
      end;
      if (i = parlines - 1) and (pagelines = n) and ((parlines = 3) or (parlines = 2)) then
      begin
        i := 1;
        inc(pnum);
        pagelines := 1;
      end;
      inc(cline);
      line[cline] := curpar[i];
      page[cline] := pnum;
      inc(pagelines);
      inc(i);
    end;
    readln(wst);
    inc(pagelines);
    if pagelines >= n then
    begin
      pagelines := 1;
      inc(pnum);
    end;
    wst := uppercase(wst);

  solve;
  close(input);
  close(output);
end.
//Leafy tops of root vegetables strike back...