#include <stdio.h>
#include <queue>
#include <vector>

using namespace std;

FILE *f;

bool line[40000];
int page[40000];
int lines;
int n,i;

typedef priority_queue<int, vector<int> > heap;

struct word{
	char *s;
	heap *h;
};	


word a[10000];
int w;

char tmp[250];

void add(char *s, int l)
{
	strcpy(tmp, s);
	int len = strlen(tmp);
	int i;
	for(i=0; i<len; i++)
		tmp[i] = toupper(tmp[i]);

	for(i=0; i<w; i++)
		 if (strcmp(a[i].s,tmp)==0)
		 {
			a[i].h->push(-l);
			return ;
		 }

	a[w].s = (char*)malloc(len+1);
	strcpy(a[w].s, tmp);
	a[w].h = new heap();
	a[w].h->push(-l);
	w++;
}


void qsort(int l, int r)
{
	int i = l;
	int j = r;
	char *x = a[(i+j)/2].s;
	heap *tmph;
	char *tmps;
	do
	{
		while (strcmp(a[i].s,x)<0) i++;
		while (strcmp(x, a[j].s)<0) j--;
		if (i<=j)
		{
			tmps = a[i].s;
			tmph = a[i].h;
			a[i].s = a[j].s;
			a[i].h = a[j].h;
			a[j].s = tmps;
			a[j].h = tmph;
			i++;
			j--;
		}
	}while (i<=j);
	if (l<j) qsort(l,j);
	if (i<r) qsort(i,r);
}


void genpages()
{
	int p = 1;
	int l = 0;
	int i;
	for(i=0; i<lines; i++)
	{
		l++;
		if (l>n)
			{	p++; l = 0; }
		if (l==n && !line[i-1] && line[i] && line[i+1])
			{	p++; l = 0; }
		if (l==n && line[i] && line[i+1] && !line[i+2])
		{	p++; l = 0; }
		if ( (l+1 == n) && line[i] && line[i+1] && line[i+1])
			{	p++; l = 0; }
		page[i] = p;
	}
}

void printpages(int k)
{
	int prev = -4;
	int eq = 0;
	int cur;
	bool was= false;
	while (!a[k].h->empty())
	{
		cur = page[-a[k].h->top()];
		a[k].h->pop();
		if (cur == prev) continue;
		if (cur == prev + 1)
			eq++;
		else
		{
			if (eq ==0)
			{
				if (was) fprintf(f,",");	was = true;
				fprintf(f,"%d",cur);
			}
			if (eq==1)
			{
				fprintf(f,",%d,%d",prev,cur);
				eq = 0;
			}
			if (eq>=2)
			{
				fprintf(f,"-%d,%d",prev,cur);
				eq = 0;
			}
			prev = cur;
		}
	}
	if (eq==1) fprintf(f,",%d",cur);
	if (eq>=2) fprintf(f,"-%d",cur);
}


int main()
{
	f = fopen("d.in","rt");
	fscanf(f,"%d\n",&n);

	char s[300], d[300];

	lines = 0;
	w = 0;
	while (1)
	{
		int c;
		c = fgetc(f);
		if (c == EOF) break;
		ungetc(c,f);

		fgets(s, 250, f);
		if (strcmp(s,"\n") ==0)
		{
			line[lines] = false;
			lines++;
			continue;
		}
		else
			line[lines] = true;

		char *pp;

		strcpy(d,s);
		pp = strtok(s, " \n");
		add(pp, lines);
		for(;;)
		{
			pp = strtok(NULL, " \n");
			if (pp == NULL) break;
			if (strcmp(pp,"")==0) continue;

			add(pp, lines);
		}
		lines++;
	}
	fclose(f);
	line[lines] = false;


	qsort(0, w-1);
	genpages();

	f = fopen("d.out","wt");
	for(i=0; i<w; i++)
	{
		fprintf(f,"%s ",a[i].s);
		printpages(i);
		fprintf(f,"\n");
	}

	fclose(f);

	return 0;
}
