#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <memory.h>
#include <malloc.h>
#include <cassert>
#include <ctype.h>
//#include <vector>
#include <algorithm>

const int maxn = 10100;
const int maxl = 210;
char L [110][maxl];
bool nonempty [110];
int cl, curl, curp, currem;
int N;

struct Word {
	bool pp [102];
	char *w;
};

Word ww [maxn];
int cww;
int I [maxn];

bool less (int a, int b)
{
	return -1 == strcmp (ww [a].w, ww [b].w);
}

Word *lookup (const char *w)
{
	int i, k, l;
	for (i = 0; i < cww; i++)
		if (0 == strcmpi (w, ww [i].w))
			return &ww [i];
	i = cww++;
	memset (ww [i].pp, 0, sizeof (ww [i].pp));
	l = strlen (w);
	ww [i].w = new char [l+1];
	for (k = 0; k < l; k++)
		ww [i].w [k] = toupper (w [k]);
	ww [i].w[l] = 0;
	return &ww [i];
}

bool done ()
{
	return curl >= cl;
}

bool ee (int s)
{
	return !nonempty [curl + s];
}

bool ff (int s)
{
	return nonempty [curl + s];
}

bool rem (int n)
{
	return currem == n;
}

void eject ()
{
	curp++;
	currem = N;
}

void skip ()
{
	curl++;
}

void submit_word (const char *s)
{
	Word *w = lookup (s);
	w->pp [curp] = true;
}

void submit ()
{
	char *s = 0;
	char *pch = L [curl];
	bool done;
	do {
		done = (*pch == 0);
		if (s == 0) {
			if (isalpha (*pch))
				s = pch;
		} else {
			if (!isalpha (*pch))
			{
				*pch = 0;
				submit_word (s);
				s = 0;
			}
		}
		++pch;
	} while (!done);
	++curl;
	--currem;
	assert (currem >= 0);
}

void rprint (int v)
{
	static int last;
	static int consec;
	if (v == -1) { // init
		last = -2;
	} else if (v == -2) {
		if (consec == 2) {
			printf (",%d", last+1);
		} else if (consec > 2) {
			printf ("-%d", last+consec-1);
		}
	} else {
		if (last == -2) {
			printf ("%d", v);
			consec = 1;
			last = v;
		} else if (v == last+consec) {
			consec++;
		} else {
			if (consec == 2) {
				printf (",%d,%d", last+1,v);
			} else if (consec > 2) {
				printf ("-%d,%d", last+consec-1,v);
			} else {
				printf (",%d",v);
			}
			last = v;
			consec = 1;
		}
	}
}

int main ()
{
	int k;

	freopen ("document.in", "rt", stdin);
	freopen ("document.out", "wt", stdout);

	memset (ww, 0, sizeof (ww));
	memset (nonempty, 0, sizeof (nonempty));
	
	scanf ("%d ", &N);
	int i = 0;
	while (!feof (stdin))
	{
		L [i][0] = '\n';
		if (0 == fgets (L [i], maxl, stdin))
			break;
		nonempty [i] = ! (L [i][0] == '\n' || L [i][0] == '\r');
		++i;
	}
	cl = i;

	curp = -1;
	eject();
	while (!done ())
	{
		if (rem (0)) { // cond 0
			eject ();
		} else if (rem(2) && ee(0) && ff(1) && ff(2)) { // cond 2
			eject();
			skip();
		} else if (rem (2) && ee(0) && ff(1) && ff(2) && ee(3)) { //cond 4.2
			skip();
			eject();
		} else if (rem(3) && ff(0) && ff(1) && ff(2) && ff(3)) { //cond 3
			submit();
			submit();
			eject();
		} else if (rem (3) && ee(0) && ff(1) && ff(2) && ff(3) && ee(4)) { //cond 4.1
			skip();
			eject();
		} else {
			submit();
		}
	}

	for (i = 0; i < cww; i++)
		I [i] = i;
	std::sort (I, I + cww, less);

	for (i = 0; i < cww; i++)
	{
		printf ("%s ", ww [I [i]].w);
		rprint (-1);
		for (k = 0; k <= curp; k++)
			if (ww [I [i]].pp [k])
				rprint (k+1);
		rprint(-2);
		printf ("\n");
	}

	fclose (stdin);
	fclose (stdout);
	return 0;
}