#include <fstream.h>
#include <math.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include <map>
#include <set>
#include <string>

using std::map;
using std::set;
using std::string;

typedef set<long> lset;
typedef map<string, lset> storage;

long N, P, I, T, M;
const long MAX = 210;
char s[MAX];
struct {
	char s[MAX];
	long p;
} mas[10100];
char mass[10100][MAX];

void Add(char s[], long p) {
	strcpy(mas[T].s, s);
	for (long i = strlen(s) - 1; i >= 0; i--)
		if (islower(s[i]))
			mas[T].s[i] = toupper(mas[T].s[i]);
	mas[T++].p = p;
}

void Input(){
	cin >> N;
	cin.ignore();
	char s[MAX];
	M = 0;
	while (1) {
		s[0] = 0;
		cin.getline(s, MAX);
		if (cin.eof() && !s[0])
			break;
		strcpy(mass[M++], s);
	}
	mass[M][0] = 0;
	I = 0;
	P = 1;
	while (1) {
		change = 0;
		P = 1;
		I = 0;
		T = 0;
	for (long i = 0; i < M; i++) {
		if (I == 0 && !mass[i][0]) {
			if (I == N - 1) {
				I = 0;
				P++;
			}
			else
				I++;
			continue;
		}
		if (I == N - 1 && (!i || !mass[i - 1][0]) && mass[i + 1][0]) {
			change=1;
			I = 0;
			P++;
		}
		if (I == N - 1 && i != M-1 && mass[i+1][0] && !mass[i+2][0] && i >= 2 && mass[i - 2][0] && mass[i - 1][0]) {
			change=1;
			I = 0;
			P++;
		}
		if (I == N - 1 && i != M - 1 && mass[i + 1][0] && !mass[i + 2][0] && (i == 1 || !mass[i-2][0]) && mass[i-1][0]) {
			change=1;
			mas[T - 1].p++;
			P++;
			I = 1;
		}
		Add(mass[i], P);
		if (I == N - 1) {
			I = 0;
			P++;
		}
		else
			I++;
	}
	if (!change)
		break;
	}
}

storage S;

void Solve(){
	long i, j, prev, L;
	string ss;
	for (i = 0; i < T; i++){
		L = strlen(mas[i].s) + 1;
		ss = "";
		for (j = 0; j < L; j++)
			if (!isalpha(mas[i].s[j]) && j && isalpha(mas[i].s[j - 1])){
				S[ss].insert(mas[i].p);		
				ss = "";
			}
			else if (isalpha(mas[i].s[j]))
				ss += mas[i].s[j];
			
	}
}

void Output(){
	int first = 1;
	for(storage::iterator i = S.begin(); i != S.end(); i++){
		cout << (i->first).c_str();
		first = 1;
		long j, nj;
		for (j = 1; j <= P; j++)
			if ((i->second).count(j)){
				for (nj = j + 1; nj <= P; nj++)
					if (!(i->second).count(nj))
						break;
				if (nj == j + 1) {
					if (first)
						cout << " ";
					else
						cout << ",";
					cout << j;	
				}
				else if (nj == j + 2) {
					if (first)
						cout << " ";
					else
						cout << ",";
					cout << j << ',' << j+1;
				}
				else {
					if (first)
						cout << " ";
					else
						cout << ",";
					cout << j << '-' << nj-1;
				}
				first = 0;
				j = nj;
			}
		cout << "\n";
	}
}

int main(){
	ifstream fi("document.in");
	ofstream fo("document.out");
	cin = fi;
	cout = fo;
	Input();
	Solve();
	Output();
	return 0;
}