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

const int n_max = 20000;

struct {
	char str[1000];
	int page, par;
} text[20000];

char s[1000];

struct word {
	char s[210];
	int page;
} a[n_max];


int compare(const void *pa, const void *pb) {
	int k;

	if(!(k = strcmp(((struct word*)pa)->s, ((struct word*)pb)->s)))
		return ((struct word*)pa)->page - ((struct word*)pb)->page;
	return k;
}

int main() {
	int i, j, k;
	int n, p, l, c;

	fclose(stdin);	fopen("document.in", "r");
	fclose(stdout);	fopen("document.out", "w");
	
	gets(s);
	sscanf(s, "%d", &n);
	
	i = 0;
	p = 0;
	while (gets(text[i].str) != NULL) {
		if (text[i].str[0] == '\0') p++;
		text[i].par = p;
		i++;
	}

	l = i;
	c = 0;
	p = 0;

	for (i = 0; i < l; i++) {
		if ((c == 0) && (text[i].str[0] == '\0')) continue;
		else
		if ((c == n - 1) && (text[i].str[0] != '\0') &&
			(text[i - 1].str[0] == '\0') && (text[i + 1].str[0] != '\0')) {
				p++;
				c = 0;
		}
		else
		if ((c == n - 1) && (text[i].str[0] != '\0') &&
			(text[i + 1].str[0] != '\0') && (text[i + 2].str[0] == '\0') &&
			(text[i - 1].str[0] != '\0') && (text[i - 2].str[0] != '\0')) {
			p++;
			c = 0;
		}
		else
		if ((c == n - 1) && (text[i].str[0] != '\0') &&
			(text[i + 1].str[0] != '\0') && (text[i + 2].str[0] == '\0') &&
			((text[i - 1].str[0] == '\0') || (text[i - 2].str[0] == '\0'))) {
			p++;
			c = 0;
			if (text[i - 1].str[0] != '\0') {
				text[i - 1].page = p;
				c++;
			}
		}
		text[i].page = p;
		c++;
		if (c == n) {
			c = 0;
			p++;
		}
	}

	k = 0;
	for (i = 0; i < l; i++) {
		strupr(text[i].str);
		p = 0;
		for(j = 0; j <= strlen(text[i].str); j++)
			if(isalpha(text[i].str[j]))
				a[k].s[p++] = text[i].str[j];
			else {
				a[k].page = text[i].page;
				a[k].s[p] = 0;
				if(p > 0) k++;
				p = 0;
			}
	}
	
	qsort(a, k, sizeof(a[0]), compare);

	/*i = 0;
	ile (i < k) {
		printf("%s %d", a[i].s, a[i].page);
		a = b = -1;
		i++;
		while ((i < k) && !strcmp(a[i].s, a[i - 1].s)) {
			if (a[i].page == a[i - 1].page + 1) b = i;
			else {
				if (b != -1) {
					printf("-%d", i + 1);
				}
				else {
					printf(",%d", i + 1);
				}
				b = -1;
			}
			i++;
		}
	}*/

	i = 0;
	while(i < k) {
		j = i++;
		while((a[i].page == (a[i - 1].page + 1) && i < k) && !strcmp(a[i].s, a[i - 1].s)) {
			i++;
			if(i >= k)
				break;
		}
		
		if(i - j == 1)
			printf("%s %d", a[j].s, a[j].page + 1);
		else if(i - j == 2)
			printf("%s %d, %d", a[j].s, a[j].page + 1, a[i].page + 1);
		else
			printf("%s %d - %d\n", a[i].s, a[i].page + 1, a[j].page + 1);
		puts("");
	}
	
	return 0;
}
