{$DEFINE DEBUG}

{$IFDEF DEBUG}
{$A+,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N+,O-,P+,Q+,R+,S+,T-,U-,V+,W-,X+,Y+,Z1}
{$ELSE}
{$A+,B-,C+,D-,E-,F-,G+,H+,I+,J-,K-,L+,M-,N+,O+,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y+,Z1}
{$ENDIF}

{$MINSTACKSIZE $00004000}
{$MAXSTACKSIZE $02000000}
{$APPTYPE CONSOLE}

uses
  Math, SysUtils;

type
  int = longint;
  float = extended;
  tstr = string[222];

const
  maxn = 111;
  maxst = 22000;
  maxw = maxst;
  cin_file = 'document.in';
  cout_file = 'document.out';
  eps : float = 1e-9;
  fmax : float = 1e40;
  besk = $3F3F3F3F;
  alph = ['A'..'Z', 'a'..'z'];

type
  trec = record
    pg, ln: int;
  end;

var
  n, cnt : int;
  a: array[1..maxst] of tstr;
  wh: array[1..maxst] of trec;
  w: array[0..maxw] of tstr;
  g: array[0..maxw] of int;

procedure swap(var a, b : int);
var
  t : int;
begin
  t := a;
  a := b;
  b := t;
end;

procedure swaps(var a, b : tstr);
var
  t : tstr;
begin
  t := a;
  a := b;
  b := t;
end;

procedure hs(n : int);
var
  i,j,p,q,y : int;
  x: tstr;

  function less(const a: tstr; b: int; const x: tstr; y: int): boolean;
  begin
    less := (a < x) or ((a = x) and (b < y));
  end;

  procedure sisp;
  begin
    i:=p; j:=i shl 1; x:=w[i]; y := g[i];
    if (j < q) and less(w[j], g[j], w[j+1], g[j+1]) then inc(j);
    while (j<=q) and less(x, y, w[j], g[j]) do
    begin
      w[i]:=w[j]; g[i] := g[j]; i:=j; j:=i shl 1;
      if (j < q) and less(w[j], g[j], w[j+1], g[j+1]) then inc(j);
    end;
    w[i]:=x; g[i] := y;
  end;

begin
  q:=n;
  for p:=n downto 1 do sisp;

  p:=1;
  while q > 1 do
  begin
    swaps(w[p], w[q]);
    swap(g[p], g[q]);
    dec(q);
    sisp;
  end;
end;

function getw(const s: tstr; var p: int): tstr;
var
  i: int;
begin
  result := '';
  i := p;
  while (i <= length(s)) and not (s[i] in alph) do inc(i);
  while (i <= length(s)) and (s[i] in alph) do
  begin
    result := result + UpCase(s[i]);
    inc(i);
  end;
  p := i;
end;

procedure solve;
var
  q, i, k, ps, j, oldi, c, mn, cur : int;
  zpt: boolean;
begin
  readln(n);
  cnt := 0;
  while not eof do
  begin
    inc(cnt);
    readln(a[cnt]);
  end;
  fillchar(wh, sizeof(wh), 0);
  i := 1;
  k := 1;
  ps := 1;
  while (i <= cnt) do
  begin
    j := i;
    while (j <= cnt) and (a[j] <> '') do
      inc(j);
    dec(j);
    {i..j}
    c := j - i + 1;
    if (c = 1) then
    begin
      wh[i].pg := k;
      wh[i].ln := ps;
      inc(ps, 2);
      if (ps > n) then
      begin
        ps := 1;
        inc(k);
      end;
    end else
    if (c = 2) then
    begin
      if (ps = n) then
      begin
        ps := 1;
        inc(k);
      end;
      wh[i].pg := k;
      wh[i].ln := ps;
      inc(ps);
      wh[i + 1].pg := k;
      wh[i + 1].ln := ps;
      inc(ps);
      inc(ps);
      if (ps > n) then
      begin
        ps := 1;
        inc(k);
      end;
    end else
    if (c = 3) then
    begin
      if (ps >= n - 1) then
      begin
        ps := 1;
        inc(k);
      end;
      wh[i].pg := k;
      wh[i].ln := ps;
      inc(ps);
      wh[i + 1].pg := k;
      wh[i + 1].ln := ps;
      inc(ps);
      wh[i + 2].pg := k;
      wh[i + 2].ln := ps;
      inc(ps);
      inc(ps);
      if (ps > n) then
      begin
        ps := 1;
        inc(k);
      end;
    end else
    begin
      {c lines}
      if (ps = n) then
      begin
        inc(k);
        ps := 1;
      end;
      for q := 1 to c - 2 do
      begin
        wh[i + q - 1].pg := k;
        wh[i + q - 1].ln := ps;
        inc(ps);
        if (ps > n) then
        begin
          ps := 1;
          inc(k);
        end;
      end;
      {2 last lines; ps, ps+1}
      if (ps = n) then
      begin
        inc(k);
        ps := 1;
      end;
      for q := c - 1 to c do
      begin
        wh[i + q - 1].pg := k;
        wh[i + q - 1].ln := ps;
        inc(ps);
        if (ps > n) then
        begin
          ps := 1;
          inc(k);
        end;
      end;
    end;
    i := j + 2;
  end;

  c := 0;

  for i := 1 to cnt do
  begin
    j := 1;
    while (j <= length(a[i])) do
    begin
      inc(c);
      w[c] := getw(a[i], j);
      if w[c] = '' then
        dec(c)
      else
        g[c] := wh[i].pg;
    end;
  end;
  hs(c);
  
  i := 1;
  while (i <= c) do
  begin
    write(w[i], ' ');
    j := i;
    while (j <= c) and (w[j] = w[i]) do
      inc(j);
    dec(j);
    {i..j}
    zpt := false;
    
    while (i <= j) do
    begin
      mn := g[i];
      cur := i + 1;
      while (cur <= j) and ((g[cur] = g[cur - 1]) or (g[cur] = g[cur - 1] + 1)) do
        inc(cur);
      dec(cur);
      {i..cur}
      if (g[cur] - g[i] + 1 < 3) then
      begin
        oldi := i;
        while (i <= cur) do
        begin
          if (i = oldi) or (g[i] <> g[i - 1]) then
          begin
            if (zpt) then
              write(',');
            zpt := true;
            write(g[i]);
          end;
          inc(i);
        end;
      end else
      begin
        if (zpt) then
          write(',');
        zpt := true;
        write(mn, '-', g[cur]);
      end;
      i := cur + 1;
    end;

    i := j + 1;
    writeln;
  end;
end;

begin
  assign(input, cin_file);
  reset(input);
  assign(output,  cout_file);
  rewrite(output);

  solve;

  close(input);
  close(output);
end.
