#pragma warning (disable : 4786)

#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <math.h>
#include <sstream>
#include <map>
#include <set>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;

#define REP(i,n) for (int i = 0; i < (n); i++)
#define IREP(i,n) for (i = 0; i < (n); i++)

map<string, set<int> > list;
map<string, set<int> >::iterator MI;
set<int>::iterator SI;

int main()
{
   ifstream ifs("document.in");
   ofstream ofs("document.out");
   vector<string> a;
   vector<int> begin,end,len;

   string s;
   getline(ifs,s);
   int i,j,n;
   sscanf(s.c_str(), "%d", &n);
   int curp = -1;
   begin.push_back(0);
   end.push_back(0);
   len.push_back(0);
   int cur = 0;
   while (!ifs.eof())
   {
      getline(ifs,s);
      a.push_back(s);
   }
   a.push_back("");

   bool preading = false;
   for (i = 0; i < a.size(); ++i)
   {
      s = a[i];
      if (s == "" && preading)
      {
         end[curp] = cur-1;
         len[curp] = end[curp]-begin[curp]+1;
         begin.push_back(0);
         end.push_back(0);
         len.push_back(0);
         preading = false;
      }
      if (s != "" && !preading)
      {  
         ++curp;
         begin[curp] = cur;               
         preading = true;
      }
      ++cur;
   }
 
   int curpage = 0;
   int curpar = 0;
   int curs = 0;
   preading = true;
   for (i = 0; i < a.size(); ++i)
   {
      if (a[i] == "" && preading)
      {
         preading = false;
      }
      else if (!preading)
      {
         ++curpar;
         preading = true;
      }
      if (a[i] != "")
      {
         if (curs == n-1 && i == begin[curpar] && len[curpar] > 1)
         {
            curpage++;
            curs = 0;
         }
         else if (curs == n-1 && i == end[curpar]-1 && len[curpar] > 3)
         {
            curpage++;
            curs = 0;
         }
         else if ((curs == n-2 && i == end[curpar]-2 && len[curpar] == 3) ||
                  (curs == n-1 && i == end[curpar]-1 && len[curpar] == 2))
         {
            curpage++;
            curs = 0;
         }
         int len = a[i].size();
         for (int j = 0; j < len; ++j)
            if (!isalpha(a[i][j]))
               a[i][j] = ' ';
         stringstream str(a[i]);
         string st;
      
         while (str >> st)
         {
            len = st.size();
            for (j =0; j < len; ++j)
            {
               st[j] = toupper(st[j]);                     
            }
            list[st].insert(curpage);
         }
      }
      else if (curs == 0) --curs;
      ++curs;
      if (curs == n)
      {
         ++curpage;
         curs = 0;
      }
   }
   for (MI = list.begin(); MI != list.end(); ++MI)
   {
      ofs << MI->first << " ";
      list[MI->first].insert(1000000);
      int last=0,first;
      bool outwas = false;
      for (SI = MI->second.begin(), i = 0; SI != MI->second.end(); ++SI, ++i)
      {
         if (i > 0)
         {
            if (*SI > last+1)
            {
               if (outwas) ofs << ",";
               if (last == first)
                  ofs << first+1;
               else if (last-first == 1)
               {
                  ofs << first+1 << "," << last+1;
               }
               else 
                  ofs << first+1 << "-" << last+1;
               outwas = true;
               first = *SI;
            }
         }
         else
         {
            first = *SI;
         }
         last = *SI;
      }
      ofs << endl;   
   }
   return 0;
}