#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

struct prr
{
  char *w;
  int p;
} h[20000];
int z;

void show(int st, int len)
{
  if(len > 2) printf("%d-%d", st, st+len-1);
  else if(len==2) printf("%d,%d", st, st+1);
  else printf("%d", st);
}

int operator<(const prr &a, const prr &b)
{
  int res = strcmp(a.w, b.w);
  if(res < 0) return 1; if(res > 0) return 0;
  return a.p < b.p;
}

int n, m, p, g;
char s[20000][201];
int para[20000];
int size[20000];
int page[20000];

int main(void)
{
char tmp[32];
int in, i, j, k;
int st, len;
  freopen("document.in",  "rt", stdin);
  freopen("document.out", "wt", stdout);
  gets(tmp); sscanf(tmp, "%d", &n);
  in = 0;
  while(gets(s[m]))
  {
    strupr(s[m]);
    if(!s[m][0])
    {
      in = 0;
      para[m] = -1;
    }
    else
    {
    if(!in) { ++p; in = 1; }
      para[m] = p-1;
      ++size[p-1];
    }
    ++m;
  }
  j = 0;
  for(i = 0; i < m; i++)
  {
    if(j < n) ++j;
    else
    {
      j = 0;
      ++g;
    }
    page[i] = g;

    if(j == n && para[i] != -1 && (i == m-1 || para[i+1] != para[i]))
    {
      ++i;
      continue;
    }
    if(j == n && para[i] != -1 && (!i || para[i-1] != para[i]) && size[para[i]] > 1)
    {
      ++page[i];
      ++g;
      j = 1;
      continue;
    }
    if(j == n && para[i] != -1 && i < m-1 && (i == m-2 || (para[i] != para[i+2] && para[i] == para[i+1])) && size[para[i]] > 3)
    {
      ++page[i];
      ++g;
      j = 1;
      continue;
    }
    if(j == n && para[i] != -1 && i < m-1 && (i == m-2 || (para[i] != para[i+2] && para[i] == para[i+1])) && (size[para[i]] == 3 || size[para[i]] == 2))
    {
      for(k = i; k && para[k] == para[i]; k--);
      j = 0;
      for(++k; k <= i; k++)
      {
        ++page[k];
        ++j;
      }
      ++g;
      continue;
    }
  }
  if(j != 0) ++g;

  for(i = 0; i < m; i++)
  {
    if(!s[i][0]) continue;
    in = 0;
    for(j = 0; s[i][j]; j++)
    {
      if(isalpha(s[i][j]))
      {
        if(!in)
        {
          tmp[0] = s[i][j];
          k = 1;
          in = 1;
        }
        else
        {
          tmp[k++] = s[i][j];
        }
      }
      else
      {
        if(in)
        {
          tmp[k] = 0;
          h[z].w = new char[k+1];
          strcpy(h[z].w, tmp);
          h[z].p = page[i];
          in = 0;
          ++z;
        }
      }
    }
    if(in)
    {
      tmp[k] = 0;
      h[z].w = new char[k+1];
      strcpy(h[z].w, tmp);
      h[z].p = page[i];
      in = 0;
      ++z;
    }
  }
  stable_sort(h, h+z);
  st = -1; len = 0;
  for(i = 0; i < z; i++)
  {
    if(!i || strcmp(h[i].w, h[i-1].w))
    {
      if(st != -1)
      {
        show(st, len);
        putchar('\n');
      }
      printf("%s ", h[i].w);
    st = h[i].p+1;
    len = 1;
    }
    else
    {
      if(h[i].p > h[i-1].p+1)
      {
        show(st, len);
        st = h[i].p+1; len = 1;
        putchar(',');
      }
      else if(h[i].p != h[i-1].p) ++len;
    }
  }
  if(st != -1)
  {
    show(st, len);
    putchar('\n');
  }
  return 0;
}