#pragma warning(disable: 4786)

#include <fstream.h>
#include <vector>
#include <list>
#include <string.h>
#include <string>

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

using namespace std;



void add(vector<string> &dest,vector<string> &source,int from,int to)
{
	int i;
	for(i=from;i<to;i++)
		dest.push_back(source[i]);
}



char temp[300],temp1[300];

struct para{
	vector<string> str;
	int size;
} t_para;

struct page{
	vector<string> str;
	int col;
} t_page;

struct index_word{
	string word;
	int page;
} t_word;

bool operator==(const index_word& i1,const index_word& i2)
{
	return (i1.page==i2.page)&&(i1.word==i2.word);
}

bool operator<(const index_word& i1,const index_word& i2)
{
	if(i1.word<i2.word)
		return true;
	if(i1.word==i2.word&&i1.page<i2.page)
		return true;
	return false;
}

list<index_word> index;

vector<para> text1;
vector<vector<string> > text2;

vector<int> numbers;

vector<para>::iterator it_text1;
vector<string>::iterator it_string;
list<index_word>::iterator it_word;

void print_numbers(void)
{
	int length=0,i,prev;
	for(i=0;i<numbers.size();)
	{
		if(length==0)
		{
			out<<numbers[i];
			prev=numbers[i];
			i++;
			length++;
		}
		else
		{
			if(numbers[i]==prev+1)
			{
				prev=numbers[i];
				length++;
				i++;
			}
			else
			{
				if(length==1)
					out<<',';
				if(length==2)
					out<<','<<prev<<',';
				else if(length>=3)
					out<<'-'<<prev<<',';
				length=0;
			}
		}

	}
	if(length>1)
	{
		if(length==2)
			out<<','<<prev;
		else if(length>=3)
			out<<'-'<<prev;
	}
	numbers.clear();
}

int i,j,k,l;
int n,num_of_pages;

int count,prev_page;

int main()
{
	in>>n;
	t_para.size=0;
	in.getline(temp,250);

	while(!in.eof())
	{
		in.getline(temp,250);
		if(temp[0]==0)
		{
			text1.push_back(t_para);
			t_para.size=0;
			t_para.str.clear();
		}
		else
		{
			k=strlen(temp);
			for(i=0;i<k;i++)
				temp[i]=toupper(temp[i]);
			t_para.str.push_back((string)temp);
			t_para.size++;
		}
	}

	if(t_para.size)
	{
		text1.push_back(t_para);
		t_para.size=0;
		t_para.str.clear();
	}

	t_page.col=0;
	t_page.str.clear();
	for(it_text1=text1.begin();it_text1!=text1.end();)
	{
		if(t_page.col>=n)
		{
			text2.push_back(t_page.str);
			t_page.col=0;
			t_page.str.clear();
		}

		else if(t_page.col+it_text1->size<=n)
		{
			t_page.col+=it_text1->size+1;
			add(t_page.str,it_text1->str,0,it_text1->size);
			it_text1++;
		}

		else if(t_page.col==n-1)
		{
			text2.push_back(t_page.str);
			t_page.col=0;
			t_page.str.clear();
		}

		else if(t_page.col+it_text1->size==n+1)
		{
			if(it_text1->size>3)
			{
				add(t_page.str,it_text1->str,0,it_text1->size-2);
				text2.push_back(t_page.str);
				t_page.col=2;
				t_page.str.clear();
				add(t_page.str,it_text1->str,it_text1->size-2,it_text1->size);
				it_text1++;
			}
			else 
			{
				text2.push_back(t_page.str);
				t_page.col=0;
				t_page.str.clear();
			}
		}

		else 
		while(it_text1->size+t_page.col>n+1)
		{
			add(t_page.str,it_text1->str,0,n-t_page.col);
			text2.push_back(t_page.str);			
			j=it_text1->str.size();
			k=n-t_page.col;
			for(i=k;i<j;i++)
			{
				it_text1->str[i-k]=it_text1->str[i];
			}
			it_text1->str.resize(j-k);
			it_text1->size-=k;
			t_page.col=0;
			t_page.str.clear();
		}
	}

	if(t_page.col)
		text2.push_back(t_page.str);
	
	num_of_pages=text2.size();

	for(i=0;i<num_of_pages;i++)
	{
		for(it_string=text2[i].begin();it_string!=text2[i].end();it_string++)
		{			
			k=0;
			strcpy(temp,it_string->c_str());
			cout<<temp<<endl;
			for(l=0;l<it_string->length();l++)
			{				
				if(temp[l]<'A'||temp[l]>'Z')
				{
					if(k!=l)
					{
						temp[l]=0;
						strcpy(temp1,&temp[k]);
						t_word.word=temp1;
						t_word.page=i+1;
						index.push_back(t_word);
					}
					k=l+1;
				}
			}
			if(k!=l)
			{
				
				strcpy(temp1,&temp[k]);
				t_word.word=temp1;
				t_word.page=i+1;
				index.push_back(t_word);
			}
			k=l+1;
		}
		cout<<"---"<<endl;
	}

	index.sort();
	index.unique();

	string prev="";
	numbers.clear();

	for(it_word=index.begin();it_word!=index.end();it_word++)
	{
		if(it_word->word!=prev)
		{	
			if(prev!="")
			{
				print_numbers();
				out<<'\n';
			}
			prev=it_word->word;
			out<<it_word->word.c_str()<<' ';
			numbers.push_back(it_word->page);
		}
		else
		{
			numbers.push_back(it_word->page);
		}
	}
	
	if(prev!="")
		print_numbers();

	return 0;
}