#include	<stdio.h>

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

//#define	MDEBUG

using namespace std;

//#pragma warning(disable:C4786)

typedef vector<string> TStringVector;
typedef vector<TStringVector> TText;
typedef vector<int> TIntVector;
typedef set<int> TIntSet;
typedef vector<TIntVector> TPages;
typedef map<string, TIntSet> TIndex;

int n;
TText text;
TPages pages;
TIndex index;

char upcase(char ch)
{
	if ( (ch >= 'a') && (ch <= 'z') )
		return ch - 'a' + 'A';
	else
		return ch;
}

string Upcase(const string& s)
{
	string Result = s;
	for (int i = 0; i < Result.size(); ++i)
		Result[i] = upcase(Result[i]);
	return Result;
}

void Input(void)
{
	scanf("%d", &n);

	text.push_back(TStringVector());

	char line[30000];
	while (gets(line))
	{
		if ( strlen(line) == 0 )
		{
			if (text.back().size())
				text.push_back(TStringVector());
		}
		else
			text.back().push_back( Upcase(line) );
	}
}

bool IsLetter(char ch)
{
	return (ch >= 'A') && (ch <= 'Z');
}

struct TInt
{
	int l;
	int r;
};
typedef vector<TInt> TInts;

TInts getInts(const TIntSet& s)
{
	TInts result;
	for (TIntSet::const_iterator toNum = s.begin(); toNum != s.end(); ++toNum)
	{
		if ( result.empty() || (result.back().r + 1 != *toNum) )
		{
			TInt nw; 
			nw.l = *toNum;
			nw.r = *toNum;
			result.push_back(nw);
		}
		else
			++result.back().r;
	}
	return result;
}

int main(void)
{
	freopen("document.in", "r", stdin);
	//freopen("document.out", "w", stdout);

	Input();

	int page = 0;
	int line = 0;
	int i, j;
	for (i = 0; i < text.size(); ++i)
	{
		pages.push_back( TIntVector() );
		
		if ( text[i].size() <= 3 )
		{
			// 4
			//printf("line=%d size=%d\n", line, text[i].size());
			if ( line + text[i].size() > n )
			{				
				++page;
				line = 0;
			}
			
			for (j = 0; j < text[i].size(); ++j)
			{
#ifdef	MDEBUG					
				printf("%s %d %d\n", text[i][j].c_str(), page, line);
#endif

				pages.back().push_back(page);
				++line;
			}
		}
		else
		{
			for (j = 0; j < text[i].size(); ++j)
			{
				// 2
				if ( (line == n - 1) && (j == 0) )
				{
					++page;
					line = 0;
				}
				else
				{
					// 3
					if ( (line == n - 1) && (j == text[i].size() - 2) )
					{
						line = 0;
						++page;
					}
				}

				
				pages.back().push_back(page);
#ifdef	MDEBUG					
				printf("%s %d %d\n", text[i][j].c_str(), page, line);
#endif

				++line;
				if (line == n)
				{
					line = 0;
					++page;
				}
			}
		}		
		
		// 1
		if ( line == n - 1 )
		{
			line = 0;
			++page;
		}
		else
		{
			++line;
		}		
	}

	for (i = 0; i < text.size(); ++i)
		for (j = 0; j < text[i].size(); ++j)
		{
			string word;
			bool inword = false;
			for (int k = 0; k <= text[i][j].length(); ++k)
			{
				if (inword)
				{
					if ( IsLetter(text[i][j][k]) )
						word += text[i][j][k];
					else
					{
						index[word].insert( pages[i][j] );
						inword = false;
					}
				}
				else
				{
					inword = IsLetter(text[i][j][k]);
					if ( inword )
					{
						word = "";
						word += text[i][j][k];
					}
				}
			}
		}

	for (TIndex::iterator toWord = index.begin(); toWord != index.end(); ++toWord)
	{
		printf("%s ", toWord->first.c_str());
		
		/*
		for ( TIntSet::iterator toPage = toWord->second.begin(); toPage != toWord->second.end(); ++toPage )
			printf("%d, ", *toPage + 1);
		*/

		TInts ints = getInts(toWord->second);

		for (TInts::const_iterator toI = ints.begin(); toI != ints.end(); ++toI)
		{
			if (toI != ints.begin())
				printf(",");
			if (toI->l + 1 < toI->r)
				printf("%d-%d", toI->l + 1, toI->r + 1);
			else
			{
				for (i = toI->l; i <= toI->r; ++i)
				{
					if (i != toI->l)
						printf(",");
					printf("%d", i + 1);
				}
			}
		}

		printf("\n");
	}
		
	return 0;
}