#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <map>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int n,m;

char          lines[20001][201];
map<string,int> id;
int             nid;
int cnt;
vector<int> list[20001];
int         no[20001];
char s[201];
char *ss;
string r;
bool emp(int i)
{
    if(i<0||i>=m) return true;
    if(lines[i][0]==0) return true;
    return false;
}

bool read()
{
    while(*ss&&!isalpha(*ss)) ss++;
    if(!*ss) return false;
    r="";
    while(isalpha(*ss))
    {
       r+=toupper(*ss);
       ss++;
    }
    return true;
}
int main()
{
  freopen("document.in","r",stdin);
  freopen("document.out","w",stdout);
  int i,j,k;
  gets(s);
  sscanf(s,"%d",&n);
  m=0;
  while(gets(s))
  {
  	strcpy(lines[m++],s);
  }
  lines[m][0]=0;
  i=0; k=0; j=1;
  while(true)
  {
     k++;
     no[i]=j;
     if(k<n)
     {
        i++;
     }
     else
     {
        k=0;
        j++;
        if(emp(i+1)) 
        {
           i++; while(i<m&&emp(i)) i++;
        }
		else
		if(emp(i-1))
		{
			no[i]=j;
			k++;
			i++;
		}	 
        else
        {
           if(emp(i+2))
           {
               no[i]=j; k++;
               if(emp(i-2))
               { no[i-1]=j; k++;}
           }
           i++;
        }
     }
     if(i==m) break;
  }
  for(i=0;i<m;i++)
  if(!emp(i))
  {
     ss=lines[i];
     while(read())
     {
         if(id.count(r)==0)
         {
             k=nid;
             id[r]=nid++;
         }
         else k=id[r];
         if(list[k].size()==0||list[k][list[k].size()-1]!=no[i]) list[k].push_back(no[i]);
     }
  }
  map<string,int>::iterator cur;
  cur=id.begin();
  while(cur!=id.end())
  {
      printf("%s ",cur->first.c_str());
      k=cur->second;
      bool first=true;
      int beg=list[k][0];
      int fin=list[k][0];
      i=1;
      while(i<list[k].size())
      {
          if(list[k][i]==fin+1)
          {
              fin++;
          }
          else
          {
              if(!first) printf(",");
              else first=false;
              if(beg==fin) printf("%d",beg);
			  else
			  if(beg+1==fin) printf("%d,%d",beg,fin);
              else printf("%d-%d",beg,fin);
              beg=list[k][i];
              fin=list[k][i];
          }
          i++;
      }
      if(!first) printf(",");
      else first=false;
      if(beg==fin) printf("%d",beg);
	  else
	  if(beg+1==fin) printf("%d,%d",beg,fin);
      else printf("%d-%d",beg,fin);
      printf("\n");
	  cur++;
  }
  return 0;
}