#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;
const long MAX = 210;
char s[MAX];
struct {
	char s[MAX];
	long p;
} mas[10100];

inline 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;
}

inline void Ext(char * s) {
	assert(T);
	strcpy(s, mas[T - 1].s);
	T--;
}

void Input(){
	cin >> N;
	cin.ignore();
	P = 1;
	I = 0; 
	T = 0;
	long pari = -1;
	char tmp[MAX];
	cin.getline(tmp, MAX);
	while (1) {
		if (!tmp[0] && cin.eof()) 
			break;
		strcpy(s, tmp);
		if (s[0])
			pari++;
		else
			pari = -1;
		cin.getline(tmp, MAX);
		if (!I && !s[0])
			continue;
		if (I == N - 1) {
			if (!pari && tmp[0]) {
				P++;
				I = 0;
			}
			if (cin.peek() == '\n') {
				if (pari > 1) {
					P++;
					I = 0;
				}
				else if (pari == 1) {
					char fir[MAX];
					Ext(fir);
					P++;
					I = 0;
					Add(fir, P);
					I = 1;
				}
			}
		}
		Add(s, P);
		I++;
		if (I == N) {
			I = 0;
			P++;
		}
	}
	if (I == 1 && pari > 0) 
		mas[T - 2].p++;
}

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;
}