#include<algorithm>
#include"vector"
#include<string>
#include<iostream>
#include<fstream>
using namespace std;

ifstream fi("document.in");
ofstream fo("document.out");
typedef vector<int> veci;typedef vector<double> vecd;typedef vector<string> vecs;

#define forn(i,n) if(true)for(int i=0;i<(n);i++)
#define feach(i,v) forn(i,((int)v.size()))
#define VA(v) v.begin(),v.end()

int par;
vecs lines;
vecs pra;
vecs txt;

typedef pair<string,int> tpair;

int mod(int x){while(x<0)x+=par;return x%par;}

int le(tpair&a,tpair&b){
	if(a.first<b.first) return 1;
	if(a.first>b.first) return 0;
	return(a.second<b.second);
}

int main(){
	fi>>par;
	char s[512];
	fi.getline(s,250);

	while(!fi.eof()){
		fi.getline(s,250);

		int empty=1;
		char *ps=s;
		while(*ps){
			if(*ps!=' ')
			{empty=0;break;}
		}
		if(empty) s[0]=0;

		lines.push_back(s);
	}
	if(lines.size()==0) return 0;

	if(lines[lines.size()-1]!=""){
		lines.push_back("");
	}

	vector<tpair> v;

	int p;
	for(int i=0;i<lines.size();i++){
		if(lines[i]==""){
			p=mod(txt.size());

			int blank=1;
			if(mod(p+pra.size()-1)==par-1){
				blank=0;
			}else if(p==par-1 && pra.size()>1){
				pra.insert(pra.begin(),par-p,"");
			}else if(mod(p+pra.size()-1)==0){
				if(pra.size()>3){
					pra.insert(pra.begin()+pra.size()-1,"");
				}else if(pra.size()>=2){
					pra.insert(pra.begin(),par-p,"");
				}
			}

			feach(i,pra){
				cout<<pra[i]<<endl;
				txt.push_back(pra[i]);
			}cout<<endl;

			if(blank) txt.push_back("");

			pra.clear();
		}else{
			pra.push_back(lines[i]);
		}
	}

	for(int page=0;;page++){
		if(page*par>=txt.size()) break;
		for(int i=page*par;i<page*par+par;i++){
			if(i>=txt.size()) break;

			char ch=0;
			string w;
			string lin=txt[i]+".";
			feach(j,lin){
				ch=lin[j];
				if(isalpha(ch)){
					if(ch>='a' && ch<='z') ch+='A'-'a';
					w+=ch;
				}else if(w!=""){
					pair<string,int> val;
					val.first=w;
					val.second=page+1;
					v.push_back(val);
					w="";
				}
			}
		}
	}

	partial_sort(v.begin(),v.end(),v.end(),le);
	if(v.size()==0) return 0;
	
	veci vn;

	{
	v.push_back(pair<string,int>("",0));
	string p="#";
	feach(i,v){
		tpair tp=v[i];

		vn.resize(unique(VA(vn))-vn.begin());

		if(v[i].first!=p && p!="#"){
			fo<<p<<" ";

			int ww=0;

			vn.push_back(-7);
			int st=-7,pr=-7;
			feach(j,vn){
				int w=vn[j];
				if(w==pr+1){
				}else{
					if(st!=-7){
						if(ww) fo<<",";
						ww=1;

						if(st<pr)
							if(st+1==pr)
								fo<<st<<","<<pr;
							else
								fo<<st<<"-"<<pr;
						else
							fo<<pr;
					}					
					st=w;
				}
				pr=w;
			}
			fo<<endl;
			vn.clear();
			vn.push_back(v[i].second);
		}else{
			vn.push_back(v[i].second);
		}
		p=v[i].first;
	}
	}

	return 0;
}