{.$define debug}

{$apptype console}

{$ifdef debug}
{$R+,S+,Q+,I+,O-}
{$else}
{$R+,S-,Q+,I-,O+}
{$endif}

{$assertions on}
{$apptype console}

program template;
uses
 SysUtils;
const
 module = 100003;
 kmax = 20000;
 {$ifdef debug}
 cinfile = 'input.txt';
 coutfile = 'output.txt';
 {$else}
 cinfile = 'document.in';
 coutfile = 'document.out';
 {$endif}
type
 int = longint;
 real = extended;
 tPtr = ^tlist;
 tList = record
  inf :int;
  next :tptr;
 end;
 tHashPtr = ^tHashList;
 tHashList = record
  word :string;
  pages :tPtr;  
  next :tHashPtr;
 end;
 tPara = record
  st,fn :int;
 end; 
var
 n :int;
 hash :array [0..module] of tHashPtr;
 strings :array [1..kmax] of string;
 pageNumber :array [1..kmax] of int;
 para :array [1..kmax] of int;
 pr :array [1..kmax] of tPara;
 words :array [1..kmax] of tHashPtr;
 m :int;
 curPage,curLine :int;
 parCount :int;
 k :int; 
 ans :int;

procedure init;
begin
 fillchar (n,integer(@ans) - integer(@n),0);
 ans := 0;
end;

procedure readdata;
var i,j :int;
begin
 assign (input,cinfile);
 reset (input);
 readln (n);
 while not eof do
  begin
   inc (k);
   readln (strings[k]);
  end;
 close (input);
end;

procedure writePages (pages :tptr);
var pageNumbers :array [1..kmax] of int;
    pagesCount :int;
    newCount :int;
    i :int;
    flag :boolean;
begin
 pagesCount := 0;
 while assigned(pages) do
  begin
   inc (pagesCount);
   pageNumbers[pagesCount] := pages^.inf;
   pages := pages^.next;
  end;
 flag := false;
 while (pagesCount > 0) do
  begin
   if flag then
    write (',');
   newCount := pagesCount-1;
   while (newCount >= 1) and
    (pageNumbers[newCount] = pageNumbers[newCount+1] + 1) do
      dec (newCount);
   if (pagesCount - newCount) >= 3 then
    write (pageNumbers[pagesCount],'-',pageNumbers[newCount+1])
   else
    begin
     write (pageNumbers[pagesCount]);
     for i := pagesCount-1 downto newCount + 1 do
      write (',',pageNumbers[i]);
    end;
   flag := true;
   pagesCount := newCount;
  end;
end;

procedure outdata;
var i,j :int;
begin
 assign (output,coutfile);
 rewrite (output);
 for i := 1 to m do
  begin
   write (words[i]^.word,' ');
   writePages (words[i]^.pages);
   writeln;
  end;
 close (output);
end;

function h (const s :string):int;
var t :int;
    i :int;
begin
 t := 1;
 result := 0;
 for i := 1 to length(s) do
  begin
   t := (t*13) mod module;
   result := (result + t*ord(s[i])) mod module;
  end;
end;

procedure addPage (var list :tptr; value :int);
var x :tptr;
begin
 new (x);
 x^.inf := value;
 x^.next := list;
 list := x;
end;

procedure addSingleWord (const word :string; pageNumber:int);
var hs :int;
    x :tHashPtr;
begin
 hs := h(word);
 x := hash[hs];
 while assigned(x) do
  begin
   if (x^.word = word) then
    begin
     if assigned(x^.pages) and (x^.pages^.inf = pageNumber) then
      exit;
     addPage (x^.pages,pageNumber);
     exit;
    end;
   x := x^.next; 
  end;
 new (x);
 x^.word := word;
 x^.pages := nil;
 addPage (x^.pages,pageNumber);
 x^.next := hash[hs];
 hash[hs] := x;  
end;

procedure addWords (const s :string; pageNumber :int);
var i :int;
    curWord :string;
begin
 curWord := '';
 for i := 1 to length(s) do
  begin
   if (upcase(s[i]) in ['A'..'Z']) then
    curWord := curWord + upcase(s[i])
   else
    begin
     if curWord <> '' then
      addSingleWord(curWord,pageNumber);
     curWord := '';
    end;
  end;
 if curWord <> '' then
  addSingleWord(curWord,pageNumber);
end;

procedure fillWords;
var x :tHashPtr;
    i :int;
begin
 for i := 0 to module-1 do
  begin
   x := hash[i];
   while assigned(x) do
    begin
     inc (m);
     words[m] := x;
     x := x^.next;
    end;
  end;
end;

procedure swapPtr (var a,b :tHashPtr);
var tmp :tHashPtr;
begin
 tmp := a;
 a := b;
 b := tmp;
end;

procedure sort (l,r :int);
var i,j :int;
    x :string;
begin
 i := l;
 j := r;
 x := words[l + random(r-l+1)]^.word;
 repeat
  while (words[i]^.word < x) do
   inc(i);
  while (words[j]^.word > x) do
   dec(j);
  if i <= j then
   begin
    swapPtr(words[i],words[j]);
    inc(i);
    dec(j);
   end;
 until i > j;
 if i < r then
  sort (i,r);
 if l < j then
  sort (l,j);
end;

procedure solve;
var i,j :int;
    newCurline :int;
    curPara :int;
begin
 parCount := 1;
 para[1] := 1;
 pr[1].st := 1;
 i := 2;
 while (i <= k) do
  begin
   if (strings[i] = '') then
    begin
     pr[parCount].fn := i-1;
     inc (parCount);
     pr[parCount].st := i+1;
     inc (i);
     continue;
    end;
   para[i] := parCount;
   inc(i);
  end;
 pr[parCount].fn := k;
 curPage := 1;
 curLine := 1;
 while curLine <= k do
  begin
   newCurline := curLine + n;
   if (strings[newCurLine] = '') then
    Inc(newCurline)
   else
   if (strings[newCurline-1] <> '') then
    begin
     curPara := para[newCurline-1];
     if (pr[curPara].fn > pr[curPara].st) and (strings[newCurline-2] = '') then
       Dec(newCurline)
     else
      begin
       curPara := para[newCurline];
       if (strings[newCurline+1] = '') then
        begin
         if pr[curPara].fn - pr[curPara].st + 1 > 3 then
          dec (newCurLine)
         else
          dec (newCurLine,pr[curPara].fn - pr[curPara].st);
        end;
      end;
    end;
   for i := curLine to newCurline-1 do
    pageNumber[i] := curPage;
   inc (curPage);
   curLine := newCurline;   
  end;
 for i := 1 to k do
  addWords(strings[i],pageNumber[i]);
 fillWords;
 if m <> 0 then
  sort (1,m);  
end;

begin
 init;
 readdata;
 solve;
 outdata;
end.
