#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <iostream>

using namespace std;

ifstream fin("document.in");
ofstream fout("document.out");

int n;
struct word { string w; vector<int> v; } w[20000];
int nw;
map<string, word *> m;



void index(string s, int page)
{
	int i, len = 0;
	string w1;

	s += ' ';
	for (i = 0; i < s.size(); i++) {
		if (s[i] >= 'a' && s[i] <= 'z') s[i] += 'A' - 'a';
		if (s[i] >= 'A' && s[i] <= 'Z') len++; else {
			if (len > 0) {
				// ir vaards
				w1 = s.substr(i - len, len);
				if (!m[w1]) {
					// jauns
					w[nw].w = w1;
					m[w1] = &w[nw++];
				}
				if (!m[w1]->v.size() || m[w1]->v[m[w1]->v.size() - 1] != page) m[w1]->v.push_back(page);
			}
			len = 0;
		}
	}
}



void output()
{
	map<string, word *>::iterator p;
	int i, j;

	for (p = m.begin(); p != m.end(); p++) {
		word &w1 = *(*p).second;
		fout << w1.w << ' ';
		for (i = 0; i < w1.v.size(); i++) {
			if (i) fout << ',';
//			fout << w1.v[i];
			for (j = i + 1; j < w1.v.size() && w1.v[j] == w1.v[j - 1] + 1; j++) ;
			if (j > i + 2) {
				fout << w1.v[i] << '-' << w1.v[j - 1];
				i = j - 1;
			} else {
				fout << w1.v[i];
			}
		}
		fout << '\n';
	}
}



int main()
{
	string s;
	vector<string> v;
	int page, count, nv, i;

	fin >> n;
	getline(fin, s);
	while (getline(fin, s)) v.push_back(s);

//	for (i = 0; i < v.size(); i++) cout << v[i] << '\n';
//	return 0;

	nv = v.size();
	v.push_back(""); v.push_back(""); v.push_back(""); // eertaak, ka beigaas taadi

	page = 1;
	count = 0;
	for (i = 0; i < nv; i++) {
		if (count == 0 && v[i] == "") {
			// izlaizham
			continue;
		}
		if ((count == n - 2 && v[i - 1] == "" && v[i] != "" && v[i + 1] != "" && v[i + 2] != "" && v[i + 3] == "")
		 || (count == n - 1 && v[i - 1] == "" && v[i] != "" && v[i + 1] != "" && v[i + 2] == "")) {
			// nesalauzhamais mazais
			page++, count = 0;
		}
		if (count == n - 1 && v[i - 1] == "" && v[i] != "" && v[i + 1] != "") {
			// orphan
			page++, count = 0;
		}
		if (count == n - 1 && (v[i - 2] != "" && v[i - 1] != "" && v[i] != "" && v[i + 1] != "" && v[i + 2] == "")) { 
			// widow
			page++, count = 0;
		}
		index(v[i], page);
		count++;
		if (count == n) page++, count = 0;
	}

	output();

	return 0;
}