#include <bitset>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <math.h>
#include <algorithm>
#include <time.h>
#include <map>
#include <set>
#include <iostream>
#include <assert.h>
#include <string>

using namespace std;

#define forn(i,n) for(int i=0;i<(int)n;i++)
#define forab(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define mp make_pair
#define pb push_back
#define dprintf if(DEBUG)printf
#define dputs if(DEBUG)puts
#define dassert if(DEBUG)assert
#define fi first
#define se second
#define j0 jqwerty
#define j1 jasdf
#define i0 iqwerty
#define i1 iasdf
#define sq(x) (x)*(x)

typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int DEBUG=1;
vector<pii > v;
vector<int> ans,p;
ll lans,lp;
int n;
vector<int> X;
ll len(int i,int j)
{
  return abs(v[i].fi-v[j].fi)+abs(v[j].se-v[i].se);
}

inline ll calc()
{
  ll ans=0;
  forn(i,n-1)
    ans+=len(p[i],p[i+1]);
  return ans;
}
void opt()
{
  if(n<100)return;
  int ii=abs(rand())%(n-1);
  int i=p[ii];
  int j;
  int neo=0;
  forn(jj,n)if(i!=jj){
    j=p[jj];
    if(len(i,neo)>len(j,i))
      neo=jj;
  }
  swap(p[i+1],p[neo]);
}
int cmp1(int i,int j)
{
  if(abs(v[i].fi-v[i].se)==abs(v[j].se-v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi-v[i].se)<abs(v[j].se-v[j].fi);
}
int cmp2(int i,int j)
{
  return mp(v[i].se,v[i].fi)<mp(v[j].se,v[j].fi);
}
int cmp3(int i,int j)
{
  if(abs(v[i].fi+v[i].se)==abs(v[j].se+v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi+v[i].se)<abs(v[j].se+v[j].fi);
}
inline pii rev(const pii& a)
{
  if((lower_bound(X.begin(),X.end(),a.fi)-X.begin())%2==1)
    return mp(a.fi,-a.se);
  return a;
}
inline bool cmp4(const int& i,const int& j)
{
  return rev(v[i])<rev(v[j]);
}
int main()
{
  ios::sync_with_stdio(0);
  scanf("%d",&n);
  v.resize(n);
  forn(i,n)
    p.pb(i),scanf("%d%d",&v[i].fi,&v[i].se),X.pb(v[i].fi);

  sort(X.begin(),X.end());
  X.resize(unique(X.begin(),X.end())-X.begin());

  sort(p.begin(),p.end());
  ans=p;
  lans=calc();
  //lp=calc();
  // if(lp<lans){
  lans=lp,ans=p;
    //}
  /*
  sort(p.begin(),p.end(),cmp1);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp2);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp3);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
    }*/
  sort(p.begin(),p.end(),cmp4);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  /*
  forn(i,17957)opt();
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  while(lans>2500000000){
  // forn(i,20)
    // swap(p[abs(rand())%n],p[abs(rand())%n]);
    // random_shuffle(p.begin(),p.end());
    forn(i,17957)opt();

    lp=calc();
    if(lp<lans){
    lans=lp,ans=p;
      }
    }
  */
  //cout<<lans<<endl;
 //assert(lans<=250000000);
 forn(i,n)
   printf("%d ",ans[i]+1);
 puts("");
 //     cerr<<lans<<endl;
     return 0;
}


#include <bitset>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <math.h>
#include <algorithm>
#include <time.h>
#include <map>
#include <set>
#include <iostream>
#include <assert.h>
#include <string>

using namespace std;

#define forn(i,n) for(int i=0;i<(int)n;i++)
#define forab(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define mp make_pair
#define pb push_back
#define dprintf if(DEBUG)printf
#define dputs if(DEBUG)puts
#define dassert if(DEBUG)assert
#define fi first
#define se second
#define j0 jqwerty
#define j1 jasdf
#define i0 iqwerty
#define i1 iasdf
#define sq(x) (x)*(x)

typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int DEBUG=1;
vector<pii > v;
vector<int> ans,p;
ll lans,lp;
int n;
vector<int> X;
inline ll len(const int& i,const int& j)
{
  return abs(v[i].fi-v[j].fi)+abs(v[j].se-v[i].se);
}

inline ll calc()
{
  ll ans=0;
  forn(i,n-1)
    ans+=len(p[i],p[i+1]);
  return ans;
}

void opt()
{
  if(n<100)return;
  int ii=abs(rand())%(n-1);
  int i=p[ii];
  int j;
  int neo=0;
  forn(jj,n)if(i!=jj){
    j=p[jj];
    if(len(i,neo)>len(j,i))
      neo=jj;
  }
  swap(p[i+1],p[neo]);
}
int cmp1(int i,int j)
{
  if(abs(v[i].fi-v[i].se)==abs(v[j].se-v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi-v[i].se)<abs(v[j].se-v[j].fi);
}
int cmp2(int i,int j)
{
  return mp(v[i].se,v[i].fi)<mp(v[j].se,v[j].fi);
}
int cmp3(int i,int j)
{
  if(abs(v[i].fi+v[i].se)==abs(v[j].se+v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi+v[i].se)<abs(v[j].se+v[j].fi);
}
int XX[1000005];
inline pii rev(const pii& a)
{
  //if((lower_bound(X.begin(),X.end(),a.fi)-X.begin())%2==1)
  if((a.fi/1000)%2==1)
    return mp(a.fi/1000,-a.se);
  return a;
}
inline bool cmp4(const int& i,const int& j)
{
  return rev(v[i])<rev(v[j]);
}
int main()
{
  // ios::sync_with_stdio(0);
  scanf("%d",&n);
  v.resize(n);
  forn(i,n)
    p.pb(i),scanf("%d%d",&v[i].fi,&v[i].se),X.pb(v[i].fi);

  sort(X.begin(),X.end());
  X.resize(unique(X.begin(),X.end())-X.begin());

  {

    forn(i,X.size())
      XX[X[i]]=i/100;
  }
  //sort(p.begin(),p.end());
  ans=p;
  lans=calc();
  lp=calc();
  // if(lp<lans){
  //  lans=lp,ans=p;
    //}

  /*  sort(p.begin(),p.end(),cmp1);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp2);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp3);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
    }
  */
  sort(ans.begin(),ans.end(),cmp4);
  /* lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
    }*/
  /*
  forn(i,17957)opt();
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }

  */
  /*while(lans>2500000000){
  // forn(i,20)
    // swap(p[abs(rand())%n],p[abs(rand())%n]);
    // random_shuffle(p.begin(),p.end());
    // forn(i,17957)opt();

    lp=calc();
    if(lp<lans){
    lans=lp,ans=p;
      }
    }
  */
  //cout<<lans<<endl;
 //assert(lans<=250000000);
 forn(i,n)
   printf("%d ",ans[i]+1);
 puts("");
 //     cerr<<lans<<endl;
     return 0;
}


#include <bitset>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <math.h>
#include <algorithm>
#include <time.h>
#include <map>
#include <set>
#include <iostream>
#include <assert.h>
#include <string>

using namespace std;

#define forn(i,n) for(int i=0;i<(int)n;i++)
#define forab(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define mp make_pair
#define pb push_back
#define dprintf if(DEBUG)printf
#define dputs if(DEBUG)puts
#define dassert if(DEBUG)assert
#define fi first
#define se second
#define j0 jqwerty
#define j1 jasdf
#define i0 iqwerty
#define i1 iasdf
#define sq(x) (x)*(x)

typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int DEBUG=1;
vector<pii > v;
vector<int> ans,p;
ll lans,lp;
int n;
vector<int> X;
inline ll len(const int& i,const int& j)
{
  return abs(v[i].fi-v[j].fi)+abs(v[j].se-v[i].se);
}

inline ll calc()
{
  ll ans=0;
  forn(i,n-1)
    ans+=len(p[i],p[i+1]);
  return ans;
}

void opt()
{
  if(n<100)return;
  int ii=abs(rand())%(n-1);
  int i=p[ii];
  int j;
  int neo=0;
  forn(jj,n)if(i!=jj){
    j=p[jj];
    if(len(i,neo)>len(j,i))
      neo=jj;
  }
  swap(p[i+1],p[neo]);
}
int cmp1(int i,int j)
{
  if(abs(v[i].fi-v[i].se)==abs(v[j].se-v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi-v[i].se)<abs(v[j].se-v[j].fi);
}
int cmp2(int i,int j)
{
  return mp(v[i].se,v[i].fi)<mp(v[j].se,v[j].fi);
}
int cmp3(int i,int j)
{
  if(abs(v[i].fi+v[i].se)==abs(v[j].se+v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi+v[i].se)<abs(v[j].se+v[j].fi);
}
int XX[1000005];
inline pii rev(const pii& a)
{
  //if((lower_bound(X.begin(),X.end(),a.fi)-X.begin())%2==1)
  if((a.fi/1000)%2==1)
    return mp(a.fi,-a.se);
  return a;
}
inline bool cmp4(const int& i,const int& j)
{
  return rev(v[i])<rev(v[j]);
}
int main()
{
  // ios::sync_with_stdio(0);
  scanf("%d",&n);
  v.resize(n);
  forn(i,n)
    p.pb(i),scanf("%d%d",&v[i].fi,&v[i].se),X.pb(v[i].fi);

  sort(X.begin(),X.end());
  X.resize(unique(X.begin(),X.end())-X.begin());

  {

    forn(i,X.size())
      XX[X[i]]=i/100;
  }
  //sort(p.begin(),p.end());
  ans=p;
  lans=calc();
  lp=calc();
  // if(lp<lans){
  //  lans=lp,ans=p;
    //}

  /*  sort(p.begin(),p.end(),cmp1);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp2);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp3);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
    }
  */
  sort(ans.begin(),ans.end(),cmp4);
  /* lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
    }*/
  /*
  forn(i,17957)opt();
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }

  */
  /*while(lans>2500000000){
  // forn(i,20)
    // swap(p[abs(rand())%n],p[abs(rand())%n]);
    // random_shuffle(p.begin(),p.end());
    // forn(i,17957)opt();

    lp=calc();
    if(lp<lans){
    lans=lp,ans=p;
      }
    }
  */
  //cout<<lans<<endl;
 //assert(lans<=250000000);
 forn(i,n)
   printf("%d ",ans[i]+1);
 puts("");
 //     cerr<<lans<<endl;
     return 0;
}


#include <bitset>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <math.h>
#include <algorithm>
#include <time.h>
#include <map>
#include <set>
#include <iostream>
#include <assert.h>
#include <string>

using namespace std;

#define forn(i,n) for(int i=0;i<(int)n;i++)
#define forab(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define mp make_pair
#define pb push_back
#define dprintf if(DEBUG)printf
#define dputs if(DEBUG)puts
#define dassert if(DEBUG)assert
#define fi first
#define se second
#define j0 jqwerty
#define j1 jasdf
#define i0 iqwerty
#define i1 iasdf
#define sq(x) (x)*(x)

typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int DEBUG=1;
vector<pii > v;
vector<int> ans,p;
ll lans,lp;
int n;
vector<int> X;
ll len(int i,int j)
{
  return abs(v[i].fi-v[j].fi)+abs(v[j].se-v[i].se);
}

ll calc()
{
  ll ans=0;
  forn(i,n-1)
    ans+=len(p[i],p[i+1]);
  return ans;
}
void opt()
{
  if(n<100)return;
  int ii=abs(rand())%(n-1);
  int i=p[ii];
  int j;
  int neo=0;
  forn(jj,n)if(i!=jj){
    j=p[jj];
    if(len(i,neo)>len(j,i))
      neo=jj;
  }
  swap(p[i+1],p[neo]);
}
int cmp1(int i,int j)
{
  if(abs(v[i].fi-v[i].se)==abs(v[j].se-v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi-v[i].se)<abs(v[j].se-v[j].fi);
}
int cmp2(int i,int j)
{
  return mp(v[i].se,v[i].fi)<mp(v[j].se,v[j].fi);
}
int cmp3(int i,int j)
{
  if(abs(v[i].fi+v[i].se)==abs(v[j].se+v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi+v[i].se)<abs(v[j].se+v[j].fi);
}
pii rev(pii a)
{
  if((lower_bound(X.begin(),X.end(),a.fi)-X.begin())%2==1)
    return mp(a.fi,-a.se);
  return a;
}
bool cmp4(int i,int j)
{
  return rev(v[i])<rev(v[j]);
}
int main()
{
  ios::sync_with_stdio(0);
  scanf("%d",&n);
  v.resize(n);
  forn(i,n)
    p.pb(i),scanf("%d%d",&v[i].fi,&v[i].se),X.pb(v[i].fi);
  sort(X.begin(),X.end());
  X.resize(unique(X.begin(),X.end())-X.begin());
  ans=p;
  lans=calc();
  sort(p.begin(),p.end());
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp1);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp2);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp3);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp4);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  /*
  forn(i,17957)opt();
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  while(lans>2500000000){
  // forn(i,20)
    // swap(p[abs(rand())%n],p[abs(rand())%n]);
    // random_shuffle(p.begin(),p.end());
    forn(i,17957)opt();

    lp=calc();
    if(lp<lans){
    lans=lp,ans=p;
      }
    }
  */
  //cout<<lans<<endl;
 //assert(lans<=250000000);
 forn(i,n)
   printf("%d ",ans[i]+1);
 puts("");
     cerr<<lans<<endl;
     return 0;
}


#include <bitset>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <math.h>
#include <algorithm>
#include <time.h>
#include <map>
#include <set>
#include <iostream>
#include <assert.h>
#include <string>

using namespace std;

#define forn(i,n) for(int i=0;i<(int)n;i++)
#define forab(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define mp make_pair
#define pb push_back
#define dprintf if(DEBUG)printf
#define dputs if(DEBUG)puts
#define dassert if(DEBUG)assert
#define fi first
#define se second
#define j0 jqwerty
#define j1 jasdf
#define i0 iqwerty
#define i1 iasdf
#define sq(x) (x)*(x)

typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int DEBUG=1;
vector<pii > v;
vector<int> ans,p;
ll lans,lp;
int n;
vector<int> X;
inline ll len(const int& i,const int& j)
{
  return abs(v[i].fi-v[j].fi)+abs(v[j].se-v[i].se);
}

inline ll calc()
{
  ll ans=0;
  forn(i,n-1)
    ans+=len(p[i],p[i+1]);
  return ans;
}
/*
void opt()
{
  if(n<100)return;
  int ii=abs(rand())%(n-1);
  int i=p[ii];
  int j;
  int neo=0;
  forn(jj,n)if(i!=jj){
    j=p[jj];
    if(len(i,neo)>len(j,i))
      neo=jj;
  }
  swap(p[i+1],p[neo]);
}
int cmp1(int i,int j)
{
  if(abs(v[i].fi-v[i].se)==abs(v[j].se-v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi-v[i].se)<abs(v[j].se-v[j].fi);
}
int cmp2(int i,int j)
{
  return mp(v[i].se,v[i].fi)<mp(v[j].se,v[j].fi);
}
int cmp3(int i,int j)
{
  if(abs(v[i].fi+v[i].se)==abs(v[j].se+v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi+v[i].se)<abs(v[j].se+v[j].fi);
  }*/
int XX[1000005];
inline pii rev(const pii& a)
{
  //if((lower_bound(X.begin(),X.end(),a.fi)-X.begin())%2==1)
  if(XX[a.fi]==1)
    return mp(a.fi,-a.se);
  return a;
}
inline bool cmp4(const int& i,const int& j)
{
  return rev(v[i])<rev(v[j]);
}
int main()
{
  // ios::sync_with_stdio(0);
  scanf("%d",&n);
  v.resize(n);
  forn(i,n)
    p.pb(i),scanf("%d%d",&v[i].fi,&v[i].se),X.pb(v[i].fi);

  sort(X.begin(),X.end());
  X.resize(unique(X.begin(),X.end())-X.begin());
  {
    int p=0;
    forn(i,X.size())
      XX[X[i]]=p,p^=1;
  }
  //sort(p.begin(),p.end());
  ans=p;
  //lans=calc();
  //lp=calc();
  // if(lp<lans){
  //  lans=lp,ans=p;
    //}
  /*
  sort(p.begin(),p.end(),cmp1);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp2);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp3);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
    }*/
  sort(ans.begin(),ans.end(),cmp4);
  /*lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
    }*/
  /*
  forn(i,17957)opt();
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  while(lans>2500000000){
  // forn(i,20)
    // swap(p[abs(rand())%n],p[abs(rand())%n]);
    // random_shuffle(p.begin(),p.end());
    forn(i,17957)opt();

    lp=calc();
    if(lp<lans){
    lans=lp,ans=p;
      }
    }
  */
  //cout<<lans<<endl;
 //assert(lans<=250000000);
 forn(i,n)
   printf("%d ",ans[i]+1);
 puts("");
 //     cerr<<lans<<endl;
     return 0;
}


#include <bitset>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <math.h>
#include <algorithm>
#include <time.h>
#include <map>
#include <set>
#include <iostream>
#include <assert.h>
#include <string>

using namespace std;

#define forn(i,n) for(int i=0;i<(int)n;i++)
#define forab(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define mp make_pair
#define pb push_back
#define dprintf if(DEBUG)printf
#define dputs if(DEBUG)puts
#define dassert if(DEBUG)assert
#define fi first
#define se second
#define j0 jqwerty
#define j1 jasdf
#define i0 iqwerty
#define i1 iasdf
#define sq(x) (x)*(x)

typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int DEBUG=1;
vector<pair<int,int> > v;
vector<int> ans,p;
ll lans,lp;
int n;
ll len(int i,int j)
{
  return abs(v[i].fi-v[j].fi)+abs(v[j].se-v[i].se);
}
ll calc()
{
  ll ans=0;
  forn(i,n-1)
    ans+=len(p[i],p[i+1]);
  return ans;
}
void opt()
{
  if(n==1)return;
  int ii=abs(rand())%(n-1);
  int i=p[ii];
  int j;
  int neo=0;
  forn(jj,n)if(i!=jj){
    j=p[jj];
    if(len(i,neo)>len(j,i))
      neo=jj;
  }
  swap(p[i+1],p[neo]);
}
int cmp1(int i,int j)
{
  if(abs(v[i].fi-v[i].se)==abs(v[j].se-v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi-v[i].se)<abs(v[j].se-v[j].fi);
}
int cmp2(int i,int j)
{
  return mp(v[i].se,v[i].fi)<mp(v[j].se,v[j].fi);
}
int cmp3(int i,int j)
{
  if(abs(v[i].fi+v[i].se)==abs(v[j].se+v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi+v[i].se)<abs(v[j].se+v[j].fi);
}
int main()
{
  ios::sync_with_stdio(0);
  scanf("%d",&n);
  v.resize(n);
  forn(i,n)
    p.pb(i),scanf("%d%d",&v[i].fi,&v[i].se);
  ans=p;
  lans=calc();
  sort(p.begin(),p.end());
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp1);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp2);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp3);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  forn(i,179)opt();
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  while(lans>2500000000){
    random_shuffle(p.begin(),p.end());
    forn(i,179)opt();

    lp=calc();
    if(lp<lans){
      lans=lp,ans=p;
    }
  }
  //cout<<lans<<endl;
 //assert(lans<=250000000);
 forn(i,n)
   printf("%d ",ans[i]+1);
 puts("");
     cerr<<lans<<endl;
     return 0;
}


#include <bitset>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <math.h>
#include <algorithm>
#include <time.h>
#include <map>
#include <set>
#include <iostream>
#include <assert.h>
#include <string>

using namespace std;

#define forn(i,n) for(int i=0;i<(int)n;i++)
#define forab(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define mp make_pair
#define pb push_back
#define dprintf if(DEBUG)printf
#define dputs if(DEBUG)puts
#define dassert if(DEBUG)assert
#define fi first
#define se second
#define j0 jqwerty
#define j1 jasdf
#define i0 iqwerty
#define i1 iasdf
#define sq(x) (x)*(x)

typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int DEBUG=1;
vector<pii > v;
vector<int> ans,p;
ll lans,lp;
int n;
ll len(int i,int j)
{
  return abs(v[i].fi-v[j].fi)+abs(v[j].se-v[i].se);
}

ll calc()
{
  ll ans=0;
  forn(i,n-1)
    ans+=len(p[i],p[i+1]);
  return ans;
}
void opt()
{
  if(n<100)return;
  int ii=abs(rand())%(n-1);
  int i=p[ii];
  int j;
  int neo=0;
  forn(jj,n)if(i!=jj){
    j=p[jj];
    if(len(i,neo)>len(j,i))
      neo=jj;
  }
  swap(p[i+1],p[neo]);
}
int cmp1(int i,int j)
{
  if(abs(v[i].fi-v[i].se)==abs(v[j].se-v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi-v[i].se)<abs(v[j].se-v[j].fi);
}
int cmp2(int i,int j)
{
  return mp(v[i].se,v[i].fi)<mp(v[j].se,v[j].fi);
}
int cmp3(int i,int j)
{
  if(abs(v[i].fi+v[i].se)==abs(v[j].se+v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi+v[i].se)<abs(v[j].se+v[j].fi);
}
pii rev(pii a)
{
  if(a.fi%2==1)
    return mp(-a.fi,a.se);
  return a;
}
bool cmp4(int i,int j)
{
  return rev(v[i])<rev(v[j]);
}
int main()
{
  ios::sync_with_stdio(0);
  scanf("%d",&n);
  v.resize(n);
  forn(i,n)
    p.pb(i),scanf("%d%d",&v[i].fi,&v[i].se);
  ans=p;
  lans=calc();
  sort(p.begin(),p.end());
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp1);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp2);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp3);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp4);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  forn(i,17957)opt();
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  while(lans>2500000000){
    // forn(i,20)
    // swap(p[abs(rand())%n],p[abs(rand())%n]);
    // random_shuffle(p.begin(),p.end());
    forn(i,17957)opt();

    lp=calc();
    if(lp<lans){
      lans=lp,ans=p;
    }
  }
  //cout<<lans<<endl;
 //assert(lans<=250000000);
 forn(i,n)
   printf("%d ",ans[i]+1);
 puts("");
     cerr<<lans<<endl;
     return 0;
}


#include <bitset>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <math.h>
#include <algorithm>
#include <time.h>
#include <map>
#include <set>
#include <iostream>
#include <assert.h>
#include <string>

using namespace std;

#define forn(i,n) for(int i=0;i<(int)n;i++)
#define forab(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define mp make_pair
#define pb push_back
#define dprintf if(DEBUG)printf
#define dputs if(DEBUG)puts
#define dassert if(DEBUG)assert
#define fi first
#define se second
#define j0 jqwerty
#define j1 jasdf
#define i0 iqwerty
#define i1 iasdf
#define sq(x) (x)*(x)

typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int DEBUG=1;
vector<pair<int,int> > v;
vector<int> ans,p;
ll lans,lp;
int n;
ll len(int i,int j)
{
  return abs(v[i].fi-v[j].fi)+abs(v[j].se-v[i].se);
}
ll calc()
{
  ll ans=0;
  forn(i,n-1)
    ans+=len(p[i],p[i+1]);
  return ans;
}
void opt()
{
  if(n<100)return;
  int ii=abs(rand())%(n-1);
  int i=p[ii];
  int j;
  int neo=0;
  forn(jj,n)if(i!=jj){
    j=p[jj];
    if(len(i,neo)>len(j,i))
      neo=jj;
  }
  swap(p[i+1],p[neo]);
}
int cmp1(int i,int j)
{
  if(abs(v[i].fi-v[i].se)==abs(v[j].se-v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi-v[i].se)<abs(v[j].se-v[j].fi);
}
int cmp2(int i,int j)
{
  return mp(v[i].se,v[i].fi)<mp(v[j].se,v[j].fi);
}
int cmp3(int i,int j)
{
  if(abs(v[i].fi+v[i].se)==abs(v[j].se+v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi+v[i].se)<abs(v[j].se+v[j].fi);
}
int main()
{
  ios::sync_with_stdio(0);
  scanf("%d",&n);
  v.resize(n);
  forn(i,n)
    p.pb(i),scanf("%d%d",&v[i].fi,&v[i].se);
  ans=p;
  lans=calc();
  sort(p.begin(),p.end());
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp1);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp2);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp3);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  forn(i,17957)opt();
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  while(lans>2500000000){
    for(i,20)swap(p[abs(rand())%n],p[abs(rand())%n]);
    // random_shuffle(p.begin(),p.end());
    forn(i,17957)opt();

    lp=calc();
    if(lp<lans){
      lans=lp,ans=p;
    }
  }
  //cout<<lans<<endl;
 //assert(lans<=250000000);
 forn(i,n)
   printf("%d ",ans[i]+1);
 puts("");
     cerr<<lans<<endl;
     return 0;
}


#include <bitset>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <math.h>
#include <algorithm>
#include <time.h>
#include <map>
#include <set>
#include <iostream>
#include <assert.h>
#include <string>

using namespace std;

#define forn(i,n) for(int i=0;i<(int)n;i++)
#define forab(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define mp make_pair
#define pb push_back
#define dprintf if(DEBUG)printf
#define dputs if(DEBUG)puts
#define dassert if(DEBUG)assert
#define fi first
#define se second
#define j0 jqwerty
#define j1 jasdf
#define i0 iqwerty
#define i1 iasdf
#define sq(x) (x)*(x)

typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int DEBUG=1;
vector<pii > v;
vector<int> ans,p;
ll lans,lp;
int n;
vector<int> X;
inline ll len(const int& i,const int& j)
{
  return abs(v[i].fi-v[j].fi)+abs(v[j].se-v[i].se);
}

inline ll calc()
{
  ll ans=0;
  forn(i,n-1)
    ans+=len(p[i],p[i+1]);
  return ans;
}

void opt()
{
  if(n<100)return;
  int ii=abs(rand())%(n-1);
  int i=p[ii];
  int j;
  int neo=0;
  forn(jj,n)if(i!=jj){
    j=p[jj];
    if(len(i,neo)>len(j,i))
      neo=jj;
  }
  swap(p[i+1],p[neo]);
}
int cmp1(int i,int j)
{
  if(abs(v[i].fi-v[i].se)==abs(v[j].se-v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi-v[i].se)<abs(v[j].se-v[j].fi);
}
int cmp2(int i,int j)
{
  return mp(v[i].se,v[i].fi)<mp(v[j].se,v[j].fi);
}
int cmp3(int i,int j)
{
  if(abs(v[i].fi+v[i].se)==abs(v[j].se+v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi+v[i].se)<abs(v[j].se+v[j].fi);
}
int XX[1000005];
inline pii rev(const pii& a)
{
  //if((lower_bound(X.begin(),X.end(),a.fi)-X.begin())%2==1)
  if((a.fi/1000)%2==1)
    return mp(a.fi/1000,-a.se);
  return mp(a.fi/1000,a.se);
}
inline bool cmp4(const int& i,const int& j)
{
  return rev(v[i])<rev(v[j]);
}
int main()
{
  // ios::sync_with_stdio(0);
  scanf("%d",&n);
  v.resize(n);
  forn(i,n)
    p.pb(i),scanf("%d%d",&v[i].fi,&v[i].se),X.pb(v[i].fi);

  sort(X.begin(),X.end());
  X.resize(unique(X.begin(),X.end())-X.begin());

  {

    forn(i,X.size())
      XX[X[i]]=i/100;
  }
  //sort(p.begin(),p.end());
  ans=p;
  lans=calc();
  lp=calc();
  // if(lp<lans){
  //  lans=lp,ans=p;
    //}

  /*  sort(p.begin(),p.end(),cmp1);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp2);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp3);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
    }
  */
  sort(ans.begin(),ans.end(),cmp4);
  /* lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
    }*/
  /*
  forn(i,17957)opt();
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }

  */
  /*while(lans>2500000000){
  // forn(i,20)
    // swap(p[abs(rand())%n],p[abs(rand())%n]);
    // random_shuffle(p.begin(),p.end());
    // forn(i,17957)opt();

    lp=calc();
    if(lp<lans){
    lans=lp,ans=p;
      }
    }
  */
  //cout<<lans<<endl;
 //assert(lans<=250000000);
 forn(i,n)
   printf("%d ",ans[i]+1);
 puts("");
 //     cerr<<lans<<endl;
     return 0;
}


#include <bitset>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <math.h>
#include <algorithm>
#include <time.h>
#include <map>
#include <set>
#include <iostream>
#include <assert.h>
#include <string>

using namespace std;

#define forn(i,n) for(int i=0;i<(int)n;i++)
#define forab(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define mp make_pair
#define pb push_back
#define dprintf if(DEBUG)printf
#define dputs if(DEBUG)puts
#define dassert if(DEBUG)assert
#define fi first
#define se second
#define j0 jqwerty
#define j1 jasdf
#define i0 iqwerty
#define i1 iasdf
#define sq(x) (x)*(x)

typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int DEBUG=1;
vector<pii > v;
vector<int> ans,p;
ll lans,lp;
int n;
ll len(int i,int j)
{
  return abs(v[i].fi-v[j].fi)+abs(v[j].se-v[i].se);
}

ll calc()
{
  ll ans=0;
  forn(i,n-1)
    ans+=len(p[i],p[i+1]);
  return ans;
}
void opt()
{
  if(n<100)return;
  int ii=abs(rand())%(n-1);
  int i=p[ii];
  int j;
  int neo=0;
  forn(jj,n)if(i!=jj){
    j=p[jj];
    if(len(i,neo)>len(j,i))
      neo=jj;
  }
  swap(p[i+1],p[neo]);
}
int cmp1(int i,int j)
{
  if(abs(v[i].fi-v[i].se)==abs(v[j].se-v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi-v[i].se)<abs(v[j].se-v[j].fi);
}
int cmp2(int i,int j)
{
  return mp(v[i].se,v[i].fi)<mp(v[j].se,v[j].fi);
}
int cmp3(int i,int j)
{
  if(abs(v[i].fi+v[i].se)==abs(v[j].se+v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi+v[i].se)<abs(v[j].se+v[j].fi);
}
pii rev(pii a)
{
  if(a.fi%2==1)
    return mp(a.fi,-a.se);
  return a;
}
bool cmp4(int i,int j)
{
  return rev(v[i])<rev(v[j]);
}
int main()
{
  ios::sync_with_stdio(0);
  scanf("%d",&n);
  v.resize(n);
  forn(i,n)
    p.pb(i),scanf("%d%d",&v[i].fi,&v[i].se);
  ans=p;
  lans=calc();
  sort(p.begin(),p.end());
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp1);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp2);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp3);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp4);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  forn(i,17957)opt();
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  while(lans>2500000000){
    // forn(i,20)
    // swap(p[abs(rand())%n],p[abs(rand())%n]);
    // random_shuffle(p.begin(),p.end());
    forn(i,17957)opt();

    lp=calc();
    if(lp<lans){
      lans=lp,ans=p;
    }
  }
  //cout<<lans<<endl;
 //assert(lans<=250000000);
 forn(i,n)
   printf("%d ",ans[i]+1);
 puts("");
     cerr<<lans<<endl;
     return 0;
}


#include <bitset>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <math.h>
#include <algorithm>
#include <time.h>
#include <map>
#include <set>
#include <iostream>
#include <assert.h>
#include <string>

using namespace std;

#define forn(i,n) for(int i=0;i<(int)n;i++)
#define forab(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define mp make_pair
#define pb push_back
#define dprintf if(DEBUG)printf
#define dputs if(DEBUG)puts
#define dassert if(DEBUG)assert
#define fi first
#define se second
#define j0 jqwerty
#define j1 jasdf
#define i0 iqwerty
#define i1 iasdf
#define sq(x) (x)*(x)

typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int DEBUG=1;
vector<pair<int,int> > v;
vector<int> ans,p;
ll lans,lp;
int n;
ll len(int i,int j)
{
  return abs(v[i].fi-v[j].fi)+abs(v[j].se-v[i].se);
}
ll calc()
{
  ll ans=0;
  forn(i,n-1)
    ans+=len(p[i],p[i+1]);
  return ans;
}
void opt()
{
  int ii=abs(rand())%(n-1);
  int i=p[ii];
  int j;
  int neo=0;
  forn(jj,n)if(i!=jj){
    j=p[jj];
    if(len(i,neo)>len(j,i))
      neo=jj;
  }
  swap(p[i+1],p[neo]);
}
int cmp1(int i,int j)
{
  if(abs(v[i].fi-v[i].se)==abs(v[j].se-v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi-v[i].se)<abs(v[j].se-v[j].fi);
}
int cmp2(int i,int j)
{
  return mp(v[i].se,v[i].fi)<mp(v[j].se,v[j].fi);
}
int cmp3(int i,int j)
{
  if(abs(v[i].fi+v[i].se)==abs(v[j].se+v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi+v[i].se)<abs(v[j].se+v[j].fi);
}
int main()
{
  ios::sync_with_stdio(0);
  scanf("%d",&n);
  v.resize(n);
  forn(i,n)
    p.pb(i),scanf("%d%d",&v[i].fi,&v[i].se);
  ans=p;
  lans=calc();
  sort(p.begin(),p.end());
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp1);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp2);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp3);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  forn(i,50)opt();
 lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  while(lans>2500000000){
    random_shuffle(p.begin(),p.end());
    forn(i,50)opt();

    lp=calc();
    if(lp<lans){
      lans=lp,ans=p;
    }
  }
  //cout<<lans<<endl;
 //assert(lans<=250000000);
 forn(i,n)
   printf("%d ",ans[i]+1);
 puts("");
     cerr<<lans<<endl;
     return 0;
}


#include <bitset>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <math.h>
#include <algorithm>
#include <time.h>
#include <map>
#include <set>
#include <iostream>
#include <assert.h>
#include <string>

using namespace std;

#define forn(i,n) for(int i=0;i<(int)n;i++)
#define forab(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define mp make_pair
#define pb push_back
#define dprintf if(DEBUG)printf
#define dputs if(DEBUG)puts
#define dassert if(DEBUG)assert
#define fi first
#define se second
#define j0 jqwerty
#define j1 jasdf
#define i0 iqwerty
#define i1 iasdf
#define sq(x) (x)*(x)

typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int DEBUG=1;
vector<pii > v;
vector<int> ans,p;
ll lans,lp;
int n;
vector<int> X;
inline ll len(const int& i,const int& j)
{
  return abs(v[i].fi-v[j].fi)+abs(v[j].se-v[i].se);
}

inline ll calc()
{
  ll ans=0;
  forn(i,n-1)
    ans+=len(p[i],p[i+1]);
  return ans;
}

void opt()
{
  if(n<100)return;
  int ii=abs(rand())%(n-1);
  int i=p[ii];
  int j;
  int neo=0;
  forn(jj,n)if(i!=jj){
    j=p[jj];
    if(len(i,neo)>len(j,i))
      neo=jj;
  }
  swap(p[i+1],p[neo]);
}
int cmp1(int i,int j)
{
  if(abs(v[i].fi-v[i].se)==abs(v[j].se-v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi-v[i].se)<abs(v[j].se-v[j].fi);
}
int cmp2(int i,int j)
{
  return mp(v[i].se,v[i].fi)<mp(v[j].se,v[j].fi);
}
int cmp3(int i,int j)
{
  if(abs(v[i].fi+v[i].se)==abs(v[j].se+v[j].fi))
    return v[i]<v[j];
  return abs(v[i].fi+v[i].se)<abs(v[j].se+v[j].fi);
}
int XX[1000005];
inline pii rev(const pii& a)
{
  //if((lower_bound(X.begin(),X.end(),a.fi)-X.begin())%2==1)
  if(XX[a.fi]==1)
    return mp(a.fi,-a.se);
  return a;
}
inline bool cmp4(const int& i,const int& j)
{
  return rev(v[i])<rev(v[j]);
}
int main()
{
  // ios::sync_with_stdio(0);
  scanf("%d",&n);
  v.resize(n);
  forn(i,n)
    p.pb(i),scanf("%d%d",&v[i].fi,&v[i].se),X.pb(v[i].fi);

  sort(X.begin(),X.end());
  X.resize(unique(X.begin(),X.end())-X.begin());
  {
    int p=0;
    forn(i,X.size())
      XX[X[i]]=p,p^=1;
  }
  //sort(p.begin(),p.end());
  ans=p;
  lans=calc();
  lp=calc();
  // if(lp<lans){
  //  lans=lp,ans=p;
    //}

  sort(p.begin(),p.end(),cmp1);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp2);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  sort(p.begin(),p.end(),cmp3);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
    }
  sort(p.begin(),p.end(),cmp4);
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  /*
  forn(i,17957)opt();
  lp=calc();
  if(lp<lans){
    lans=lp,ans=p;
  }
  */
  while(lans>2500000000){
  // forn(i,20)
    // swap(p[abs(rand())%n],p[abs(rand())%n]);
    // random_shuffle(p.begin(),p.end());
    // forn(i,17957)opt();

    lp=calc();
    if(lp<lans){
    lans=lp,ans=p;
      }
    }

  //cout<<lans<<endl;
 //assert(lans<=250000000);
 forn(i,n)
   printf("%d ",ans[i]+1);
 puts("");
 //     cerr<<lans<<endl;
     return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define vi vector<int>
#define mp make_pair
#define all(v) v.begin(),v.end()
#define SET_ZERO(a) memset(a,0,sizeof(a))
#define SET_MAX(a) memset(a,MOD,sizeof(a))
#define pii  pair<int,int>
#define vpii vector<pair<int,int> >
#define ll long long
typedef priority_queue<pii,vector<pii>,greater<pii> > PQG;
typedef priority_queue<pii,vector<pii> > PQS;
const int MAX = 1e6 +9;
const long long int MOD = 1e9 + 7;
int dx[] = {-1,1,0,0};
int dy[] = {0,0,1,-1};

bool comp1(pair<int,int> a,pair<int,int> b)
{
    return a.first * a.second > b.first * b.second;
}
bool comp2(pair<int,int> a,pair<int,int> b)
{
    return a.first * a.second < b.first * b.second;
}

vector<pair<int,int> > buy;
vector<pair<int,int> > sell;

long long int hash1[100009];
long long int hash2[100009];

int main()
{
memset(hash1,0,sizeof hash1);
memset(hash2,0,sizeof hash2);
int n,s;
scanf("%d%d",&n,&s);
char d;
int p,q;

for(int i = 0 ;i<n;i++)
{
    cin>>d>>p>>q;
    if(d == 'B')
    {
        buy.push_back(make_pair(p,q));
        hash1[p]+=q;
    }
    else
    {
        sell.push_back(make_pair(p,q));
        hash2[p]+=q;
    }
}

sort(all(buy),comp2);
sort(all(sell),comp1);
vector<pair<int,long long int> >ans1;
vector<pair<int,long long int> >ans2;

for(int i = 0 ; i<buy.size() ;i++)
{
    if(hash1[buy[i].first]!=-1)
        {
            ans1.push_back(make_pair(buy[i].first,hash1[buy[i].first]));
            hash1[buy[i].first]=-1;
        }
}

for(int i = 0 ; i<sell.size() ;i++)
{
    if(hash2[sell[i].first]!=-1)
        {
            ans2.push_back(make_pair(sell[i].first,hash2[sell[i].first]));
            hash2[sell[i].first]=-1;
        }
}

reverse(ans1.begin(),ans1.end());
int temp = s;
if(ans2.size() < s)
    s= ans2.size();
for(int i = 0 ;i<s;i++)
{
    printf("S %d %lld",ans2[i].first,ans2[i].second);
    printf("\n");
}
s = temp;
if(ans1.size() < s)
    s = ans1.size();
for(int i = 0 ;i<s;i++)
{
    printf("B %d %lld",ans1[i].first,ans1[i].second);
    printf("\n");
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define vi vector<int>
#define mp make_pair
#define all(v) v.begin(),v.end()
#define SET_ZERO(a) memset(a,0,sizeof(a))
#define SET_MAX(a) memset(a,MOD,sizeof(a))
#define pii  pair<int,int>
#define vpii vector<pair<int,int> >
#define ll long long
typedef priority_queue<pii,vector<pii>,greater<pii> > PQG;
typedef priority_queue<pii,vector<pii> > PQS;
const int MAX = 1e6 +9;
const long long int MOD = 1e9 + 7;
int dx[] = {-1,1,0,0};
int dy[] = {0,0,1,-1};

bool comp1(pair<int,int> a,pair<int,int> b)
{
    return a.first > b.first ;
}
bool comp2(pair<int,int> a,pair<int,int> b)
{
    return a.first < b.first ;
}

vector<pair<int,int> > buy;
vector<pair<int,int> > sell;

long long int hash1[100009];
long long int hash2[100009];

int main()
{
memset(hash1,0,sizeof hash1);
memset(hash2,0,sizeof hash2);
int n,s;
scanf("%d%d",&n,&s);
char d;
int p,q;

for(int i = 0 ;i<n;i++)
{
    cin>>d>>p>>q;
    if(d == 'B')
    {
        buy.push_back(make_pair(p,q));
        hash1[p]+=q;
    }
    else
    {
        sell.push_back(make_pair(p,q));
        hash2[p]+=q;
    }
}

sort(all(buy),comp1);
sort(all(sell),comp2);
vector<pair<int,long long int> >ans1;
vector<pair<int,long long int> >ans2;

for(int i = 0; i<buy.size() ;i++)
{
    if(hash1[buy[i].first]!=-1)
        {
            ans1.push_back(make_pair(buy[i].first,hash1[buy[i].first]));
            hash1[buy[i].first]=-1;
        }
}

for(int i = 0 ; i<sell.size() ;i++)
{
    if(hash2[sell[i].first]!=-1)
        {
            ans2.push_back(make_pair(sell[i].first,hash2[sell[i].first]));
            hash2[sell[i].first]=-1;
        }
}

reverse(ans1.begin(),ans1.end());
reverse(ans2.begin(),ans2.end());
int temp = s;
if(ans2.size() < s)
    s= ans2.size();
for(int i = 0 ;i<s;i++)
{
    printf("S %d %lld",ans2[i].first,ans2[i].second);
    printf("\n");
}
s = temp;
if(ans1.size() < s)
    s = ans1.size();
for(int i = 0 ;i<s;i++)
{
    printf("B %d %lld",ans1[i].first,ans1[i].second);
    printf("\n");
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define vi vector<int>
#define mp make_pair
#define all(v) v.begin(),v.end()
#define SET_ZERO(a) memset(a,0,sizeof(a))
#define SET_MAX(a) memset(a,MOD,sizeof(a))
#define pii  pair<int,int>
#define vpii vector<pair<int,int> >
#define ll long long
typedef priority_queue<pii,vector<pii>,greater<pii> > PQG;
typedef priority_queue<pii,vector<pii> > PQS;
const int MAX = 1e6 +9;
const long long int MOD = 1e9 + 7;
int dx[] = {-1,1,0,0};
int dy[] = {0,0,1,-1};

bool comp(pair<int,int> a,pair<int,int> b)
{
    if(a.first > b.first)
         return true;
    else return false;
}
vector<pair<int,int> > buy;
vector<pair<int,int> > sell;

int hash1[100009];
int hash2[100009];

int main()
{
memset(hash1,0,sizeof hash1);
memset(hash2,0,sizeof hash2);
int n,s;
scanf("%d%d",&n,&s);
char d;
int p,q;

for(int i = 0 ;i<n;i++)
{
    cin>>d>>p>>q;
    if(d == 'B')
    {
        buy.push_back(make_pair(p,q));
        hash1[p]+=q;
    }
    else
    {
        sell.push_back(make_pair(p,q));
        hash2[p]+=q;
    }
}

sort(all(buy),comp);
sort(all(sell),comp);

    if(sell.size() <s)
    {
        for(int j = 0 ;j<sell.size();j++)
        {
            printf("S %d %d",sell[j].first,hash2[sell[j].first]);
            printf("\n");
        }
    }
    else
    {
    for(int j = 0 ;j<s;j++)
    {
        printf("S %d %d",sell[j].first,hash2[sell[j].first]);
        printf("\n");
    }
    }

    if(buy.size() <s)
    {
        for(int j = 0 ; j<buy.size() ;j++)
        {
            printf("B %d %d",buy[j].first,hash1[buy[j].first]);
            printf("\n");
        }
    }
    else
    {
        for(int j = 0 ; j<s ;j++)
        {
            printf("B %d %d",buy[j].first,hash1[buy[j].first]);
            printf("\n");
        }
    }
return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <list>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <set>
#include <cmath>
#include <map>
#include <string.h>
using namespace std;

#define ALL(v)  v.begin(), v.end()
#define SZ(v)   ((int)(v.size()))
#define ALLR(v) v.rbegin(), v.rend()
#define pii pair<int,int>
#define pb push_back
#define Int long long
#define ull unsigned long long
#define mp make_pair
#define For(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)

inline void end(string s){cout << s; exit(0);}
inline void end(Int n){cout << n; exit(0);}

int mod(int a, int b) {
    return (((a % b) + b) % b);
}

const int N = 1e5 + 10;
int n,s;

struct state
{
    state(char dd, int pp, int qq){
        q = qq;
        p = pp;
        d = dd;
    }
    char d;
    int p, q;
};

bool fun(state  &s1, state &s2){
    if(s1.d != s2.d )
        return s1.d > s2.d;
    if(s1.p == s2.p && s1.p!=-1){
        s1.q += s2.q;
        s2.p = -1;
    }
    return s1.p < s2.p;
}

//An order book of depth s contains s best aggregated orders for each direction.
// A buy order is better if it has higher price and a sell order is better if it has lower price.
//If there are less then s aggregated orders for some direction then all of them will be in the final order book.

bool fun2(state s1, state s2){
    if(s1.d != s2.d )
        return s1.d > s2.d;

    if(s1.d == 'S'){
           return s1.p > s2.p;
    }
    else{
        return s1.p > s2.p;
    }
}

int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
    freopen("code.in","r", stdin);
#endif

    cin >> n >> s;
    vector<state> v;
    for (int i = 0; i < n; ++i)
    {
        char c; int p,q;
        cin >> c >> p >> q;
        v.push_back( state(c, p, q) );
    }
    sort(v.begin(), v.end(), fun);
    int deleted = 0;
    int lastindex = 0;

    vector<state> res;
    for(int i=0; i < v.size(); i++){
        if(v[i].p != -1){
            res.pb(v[i]);
        }

     }
     sort(res.begin(), res.end() , fun2);
     int s1,s2;
     s1 = s2 = s;

     for (int i = 0; i < res.size(); ++i)
     {
           if(res[i].d == 'S' && s1){
            cout << res[i].d << ' ' << res[i].p << ' ' << res[i].q << endl;
            s1--;
            }

        if(res[i].d == 'B' && s2){
            cout << res[i].d << ' ' << res[i].p << ' ' << res[i].q << endl;
            s2--;
        }

     }

  return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <list>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <set>
#include <cmath>
#include <map>
#include <string.h>
using namespace std;

#define ALL(v)  v.begin(), v.end()
#define SZ(v)   ((int)(v.size()))
#define ALLR(v) v.rbegin(), v.rend()
#define pii pair<int,int>
#define pb push_back
#define Int long long
#define ull unsigned long long
#define mp make_pair
#define For(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)

inline void end(string s){cout << s; exit(0);}
inline void end(Int n){cout << n; exit(0);}

int mod(int a, int b) {
    return (((a % b) + b) % b);
}

const int N = 1e5 + 10;
int n,s;

struct state
{
    state(char dd, int pp, int qq){
        q = qq;
        p = pp;
        d = dd;
    }
    char d;
    int p, q;
};

bool fun(state  &s1, state &s2){

    if(s1.p == s2.p && s1.p!=-1){
        s1.q += s2.q;
        s2.p = -1;
    }
    return s1.p < s2.p;
}

//An order book of depth s contains s best aggregated orders for each direction.
// A buy order is better if it has higher price and a sell order is better if it has lower price.
//If there are less then s aggregated orders for some direction then all of them will be in the final order book.

bool fun2(state s1, state s2){
    if(s1.d != s2.d )
        return s1.d > s2.d;

    if(s1.d == 'S'){
           return s1.p > s2.p;
    }
    else{
        return s1.p > s2.p;
    }
}

void print_v(vector<state> v){
    for(int i=0; i < v.size(); i++){
        cout << v[i].d << ' ' << v[i].p << ' ' << v[i].q << endl;
    }
}

bool mysell(state s1, state s2){
    return s1.p < s2.p;
}

bool mybuy(state s1, state s2){
    return s1.p > s2.p;
}

int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
    freopen("code.in","r", stdin);
#endif

    cin >> n >> s;
    vector<state> sells, buys;

    for (int i = 0; i < n; ++i)
    {
        char c; int p,q;
        cin >> c >> p >> q;
        if(c == 'S'){
            sells.pb( state(c, p, q) );
        }
        else
            buys.pb( state(c, p, q) );
    }
    vector<state> res1, res2;
    sort(sells.begin(), sells.end() , fun);
    for (int i = 0; i < sells.size(); ++i)
    {
        if(sells[i].p != -1){
            res1.pb(sells[i]);
        }
    }
    sort(buys.begin(), buys.end() , fun);
    for (int i = 0; i < buys.size(); ++i)
    {
        if(buys[i].p != -1){
            res2.pb(buys[i]);
        }
    }

    sort(res1.begin(), res1.end() , mysell);
    sort(res2.begin(), res2.end() , mybuy);
    int s1,s2;
    s1 = s2 = s;
    sells.clear();
    for (int i = 0; i < res1.size() && s1; ++i)
    {
        sells.pb(res1[i]);
        s1--;
    }
    reverse(sells.begin() , sells.end());
    print_v(sells);

    buys.clear();
    for (int i = 0; i < res2.size() && s2; ++i)
    {
        buys.pb(res2[i]);
        s2--;
    }

    print_v(buys);
  return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <list>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <set>
#include <cmath>
#include <map>
#include <string.h>
using namespace std;

#define ALL(v)  v.begin(), v.end()
#define SZ(v)   ((int)(v.size()))
#define ALLR(v) v.rbegin(), v.rend()
#define pii pair<int,int>
#define pb push_back
#define Int long long
#define ull unsigned long long
#define mp make_pair
#define For(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)

inline void end(string s){cout << s; exit(0);}
inline void end(Int n){cout << n; exit(0);}

int mod(int a, int b) {
    return (((a % b) + b) % b);
}

const int N = 1e5 + 10;
int n,s;
int m[2][N];

struct state
{
    state(char dd, int pp, int qq){
        q = qq;
        p = pp;
        d = dd;
    }
    char d;
    int p, q;
};

bool fun(state s1, state s2){
    if(s1.d != s2.d )
        return s1.d > s2.d;
    return s1.p < s2.p;
}

//An order book of depth s contains s best aggregated orders for each direction.
// A buy order is better if it has higher price and a sell order is better if it has lower price.
//If there are less then s aggregated orders for some direction then all of them will be in the final order book.

bool fun2(state s1, state s2){
    if(s1.d != s2.d )
        return s1.d > s2.d;
    if(s1.q == s2.q){
        if(s1.d == 'S'){
            return s1.p < s2.p;
        }
        else{
            return s1.p > s2.p;
        }
    }
    return s1.q > s2.q;
}

int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
    freopen("code.in","r", stdin);
#endif

    cin >> n >> s;
    vector<state> v;
    for (int i = 0; i < n; ++i)
    {
        char c; int p,q;
        cin >> c >> p >> q;
        v.push_back( state(c, p, q) );
    }
    sort(v.begin(), v.end(), fun);
    int deleted = 0;
    int lastindex = 0;
    v.push_back(state('Z',0,0));
    vector<state> res;
    for(int i=1; i < v.size(); i++){
        if( v[lastindex].d == v[i].d && v[lastindex].p == v[i].p){
            v[lastindex].q += v[i].q;
        }
        else {
            res.pb(v[lastindex]);
            lastindex = i;
        }

     }
     sort(res.begin(), res.end() , fun2);
     int s1,s2;
     s1 = s2 = s;

     for (int i = 0; i < res.size(); ++i)
     {
           if(res[i].d == 'S' && s1){
            cout << res[i].d << ' ' << res[i].p << ' ' << res[i].q << endl;
            s1--;
        }

        if(res[i].d == 'B' && s2){
            cout << res[i].d << ' ' << res[i].p << ' ' << res[i].q << endl;
            s2--;
        }

     }

  return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <list>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <set>
#include <cmath>
#include <map>
#include <string.h>
using namespace std;

#define ALL(v)  v.begin(), v.end()
#define SZ(v)   ((int)(v.size()))
#define ALLR(v) v.rbegin(), v.rend()
#define pii pair<int,int>
#define pb push_back
#define Int long long
#define ull unsigned long long
#define mp make_pair
#define For(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)

inline void end(string s){cout << s; exit(0);}
inline void end(Int n){cout << n; exit(0);}

int mod(int a, int b) {
    return (((a % b) + b) % b);
}

const int N = 1e5 + 10;
int n,s;

int order[2][N];
struct state
{
    char c;
    int p, q;
    state(char cc, int pp, int qq){
        c = cc;
        p = pp;
        q = qq;
    }
};
vector<state> v;

bool fun(pair<int,int> s1, pair<int,int> s2){
    return s1.first < s2.first;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
    freopen("code.in","r", stdin);
#endif
    cin >> n >> s;
    char c;
    int p, q;
    set<int> prices;
    for (int i = 0; i < n; ++i)
    {
        cin >> c >> p >> q;
        if( c== 'S'){
            order[0][p] += q;
        } else {
            order[1][p] += q;
        }
        prices.insert(p);
    }
    for(set<int>::iterator it = prices.begin(); it != prices.end() ; it++){
        if(order[0][*it])
            v.pb(state('S', *it, order[0][*it]));
        if(order[1][*it])
            v.pb(state('B', *it, order[1][*it]));
    }

    vector<pair<int,int> > sells, buys;
    for (int i = 0; i < v.size(); ++i)
    {
        state s = v[i];

        if(s.c == 'S'){

            sells.pb(mp(s.p,s.q));
        } else {
            buys.pb(mp(s.p,s.q));
        }
    }

    sort(sells.begin(), sells.end(), fun);
    sort(buys.rbegin(), buys.rend(), fun);
    int s1,s2;
    s1 = s2 = s;
    vector<pair<int,int> > res;
    for(int i=0; i < sells.size() && s1;i++){
        res.pb(mp(sells[i].first, sells[i].second));
        s1--;
    }
    reverse(res.begin(), res.end());
    for (int i = 0; i < res.size(); ++i)
    {
        cout << "S " << res[i].first << ' ' << res[i].second << endl;
    }

    for(int i=0; i < buys.size() && s2;i++){
        cout << "B " << buys[i].first << ' ' << buys[i].second << endl;
        s2--;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <list>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <set>
#include <cmath>
#include <map>
#include <string.h>
using namespace std;

#define ALL(v)  v.begin(), v.end()
#define SZ(v)   ((int)(v.size()))
#define ALLR(v) v.rbegin(), v.rend()
#define pii pair<int,int>
#define pb push_back
#define Int long long
#define ull unsigned long long
#define mp make_pair
#define For(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)

inline void end(string s){cout << s; exit(0);}
inline void end(Int n){cout << n; exit(0);}

int mod(int a, int b) {
    return (((a % b) + b) % b);
}

const int N = 1e5 + 10;
int n,s;
int m[2][N];

struct state
{
    state(char dd, int pp, int qq){
        q = qq;
        p = pp;
        d = dd;
    }
    char d;
    int p, q;
};

bool fun(state s1, state s2){
    if(s1.d != s2.d )
        return s1.d > s2.d;
    return s1.p < s2.p;
}

bool fun2(state s1, state s2){
    if(s1.d != s2.d )
        return s1.d > s2.d;
    return s1.q > s2.q;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
    freopen("code.in","r", stdin);
#endif

    cin >> n >> s;
    vector<state> v;
    for (int i = 0; i < n; ++i)
    {
        char c; int p,q;
        cin >> c >> p >> q;
        v.push_back( state(c, p, q) );
    }
    sort(v.begin(), v.end(), fun);
    int deleted = 0;
    for(int i=0; i < v.size() - 1; i++){
        state s = v[i];
        state next = v[i+1];
        if( v[i].d == v[i+1].d && v[i].p == v[i+1].p){
            v[i].q += v[i+1].q;
            v.erase(v.begin() + i -deleted + 1);
            deleted++;
        }

     }

     sort(v.begin(), v.end() , fun2);
     int s1,s2;
     s1 = s2 = s;

     for (int i = 0; i < v.size() && (s1||s2); ++i)
     {
        if(v[i].d == 'S' && s1){
            cout << v[i].d << ' ' << v[i].p << ' ' << v[i].q << endl;
            s1--;
        }

        if(v[i].d == 'B' && s2){
            cout << v[i].d << ' ' << v[i].p << ' ' << v[i].q << endl;
            s2--;
        }
     }
  return 0;
}


n, k = gets.split.map(&:to_i)

s = Hash.new(0)
b = Hash.new(0)

n.times do
  d, p, q = gets.split
  p = p.to_i
  q = q.to_i
  s[p] += q if d == 'S'
  b[p] += q if d == 'B'
end
s.keys.sort.first(k).reverse_each { |e| puts "S #{e} #{s[e]}" }
b.keys.sort.last(k).reverse_each { |e| puts "B #{e} #{b[e]}" }


#include <iostream>
#include <string>
#include <algorithm>
#include <list>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <set>
#include <cmath>
#include <map>
#include <string.h>
using namespace std;

#define ALL(v)  v.begin(), v.end()
#define SZ(v)   ((int)(v.size()))
#define ALLR(v) v.rbegin(), v.rend()
#define pii pair<int,int>
#define pb push_back
#define Int long long
#define ull unsigned long long
#define mp make_pair
#define For(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)

inline void end(string s){cout << s; exit(0);}
inline void end(Int n){cout << n; exit(0);}

int mod(int a, int b) {
    return (((a % b) + b) % b);
}

const int N = 1e5 + 10;
int n,s;
int m[2][N];

struct state
{
    state(char dd, int pp, int qq){
        q = qq;
        p = pp;
        d = dd;
    }
    char d;
    int p, q;
};

bool fun(state s1, state s2){
    if(s1.d != s2.d )
        return s1.d > s2.d;
    return s1.p < s2.p;
}

//An order book of depth s contains s best aggregated orders for each direction.
// A buy order is better if it has higher price and a sell order is better if it has lower price.
//If there are less then s aggregated orders for some direction then all of them will be in the final order book.

bool fun2(state s1, state s2){
    if(s1.d != s2.d )
        return s1.d > s2.d;
    if(s1.q == s2.q){
        if(s1.d == 'S'){
            return s1.p < s2.p;
        }
        else{
            return s1.p > s2.p;
        }
    }
    return s1.q > s2.q;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
    freopen("code.in","r", stdin);
#endif

    cin >> n >> s;
    vector<state> v;
    for (int i = 0; i < n; ++i)
    {
        char c; int p,q;
        cin >> c >> p >> q;
        v.push_back( state(c, p, q) );
    }
    sort(v.begin(), v.end(), fun);
    int deleted = 0;
    for(int i=0; i < v.size() - 1; i++){
        state s = v[i];
        state next = v[i+1];
        if( v[i].d == v[i+1].d && v[i].p == v[i+1].p){
            v[i].q += v[i+1].q;
            v.erase(v.begin() + i -deleted + 1);
            deleted++;
        }

     }

     sort(v.begin(), v.end() , fun2);
     int s1,s2;
     s1 = s2 = s;

     for (int i = 0; i < v.size() && (s1||s2); ++i)
     {
        if(v[i].d == 'S' && s1){
            cout << v[i].d << ' ' << v[i].p << ' ' << v[i].q << endl;
            s1--;
        }

        if(v[i].d == 'B' && s2){
            cout << v[i].d << ' ' << v[i].p << ' ' << v[i].q << endl;
            s2--;
        }
     }
  return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <list>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <set>
#include <cmath>
#include <map>
#include <string.h>
using namespace std;

#define ALL(v)  v.begin(), v.end()
#define SZ(v)   ((int)(v.size()))
#define ALLR(v) v.rbegin(), v.rend()
#define pii pair<int,int>
#define pb push_back
#define Int long long
#define ull unsigned long long
#define mp make_pair
#define For(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)

inline void end(string s){cout << s; exit(0);}
inline void end(Int n){cout << n; exit(0);}

int mod(int a, int b) {
    return (((a % b) + b) % b);
}

const int N = 1e5 + 10;
int n,s;

struct state
{
    state(char dd, int pp, int qq){
        q = qq;
        p = pp;
        d = dd;
    }
    char d;
    int p, q;
};

bool fun(state  &s1, state &s2){
    if(s1.d != s2.d )
        return s1.d > s2.d;
    if(s1.p == s2.p && s1.p!=-1){
        s1.q += s2.q;
        s2.p = -1;
    }
    return s1.p < s2.p;
}

//An order book of depth s contains s best aggregated orders for each direction.
// A buy order is better if it has higher price and a sell order is better if it has lower price.
//If there are less then s aggregated orders for some direction then all of them will be in the final order book.

bool fun2(state s1, state s2){
    if(s1.d != s2.d )
        return s1.d > s2.d;
    if(s1.q == s2.q){
        if(s1.d == 'S'){
            return s1.p < s2.p;
        }
        else{
            return s1.p > s2.p;
        }
    }
    return s1.q > s2.q;
}

int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
    freopen("code.in","r", stdin);
#endif

    cin >> n >> s;
    vector<state> v;
    for (int i = 0; i < n; ++i)
    {
        char c; int p,q;
        cin >> c >> p >> q;
        v.push_back( state(c, p, q) );
    }
    sort(v.begin(), v.end(), fun);
    int deleted = 0;
    int lastindex = 0;

    vector<state> res;
    for(int i=0; i < v.size(); i++){
        if(v[i].p != -1){
            res.pb(v[i]);
        }

     }
     sort(res.begin(), res.end() , fun2);
     int s1,s2;
     s1 = s2 = s;

     for (int i = 0; i < res.size(); ++i)
     {
           if(res[i].d == 'S' && s1){
            cout << res[i].d << ' ' << res[i].p << ' ' << res[i].q << endl;
            s1--;
            }

        if(res[i].d == 'B' && s2){
            cout << res[i].d << ' ' << res[i].p << ' ' << res[i].q << endl;
            s2--;
        }

     }

  return 0;
}


#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<utility>
using namespace std;

int main()
{
    int n,s;
    scanf("%d %d",&n,&s);
    int buyq[100005]={0},sellq[100005]={0};
    int d=s;
    char ch[1];
    long long int i,j,k;
    for(i=0;i<n;i++)
    {
        scanf("%s",ch);
        scanf("%I64d %I64d",&j,&k);
        if(ch[0]=='B')
        {

                buyq[j]+=k;

        }
        else if(ch[0]=='S')
        {

            sellq[j]+=k;
           // printf("%d\n",sellq[j]);
        }
    //  printf("%d\n",i);
    }

    vector <pair<long long int,long long int> > b[n+1],se[n+1];

    for(i=0,j=0,k=0;i<=100000;i++)
    {

        if(buyq[i]!=0)
        {

            b[j].push_back(make_pair((i,buyq[i]));
            j++;
        }
        if(sellq[i]!=0)
        {

            se[k].push_back(make_pair((i,sellq[i]));
            k++;
        }
    }

    sort(b,b+j);
    sort(se,se+k);
    s=d;

/*  printf("hua yahan tak %d %d %d\n",j,k,s);
    for(i=0;i<j;i++)
    printf("%d %d\n",b[i][0].second,(b[i][0].first/b[i][0].second));*/

    if(s>=k)
    {
        for(i=k-1;i>=0;i--)
        {
            long long int temp=se[i][0].second;
            long long int a=se[i][0].first;
            if(a!=0)
            printf("S %I64d %I64d\n",a,temp);
            else
             printf("S %I64d %I64d\n",a,sellq[0]);
        }
    }
    else
    {
        for(i=s-1;i>=0;i--)
        {
            if(se[i][0].first!=0)
            printf("S %I64d %I64d\n",se[i][0].first,se[i][0].second);
            else
            {
                 printf("S %I64d %I64d\n",se[i][0].first,sellq[0]);
            }
        }
    }
    //  printf("hua yahan tak %d %d %d\n",j,k,s);
    if(s>=j)
    {
            for(i=j-1;i>=0;i--)
        {
            if(b[i][0].first!=0)
            printf("B %I64d %I64d\n",b[i][0].first,b[i][0].second);
            else
                printf("B %I64d %I64d\n",b[i][0].first,buyq[0]);
        }
    }
    else
    {
            for(i=j-1,k=1;k<=s;i--,k++)
        {
            if(b[i][0].first!=0)
            printf("B %I64d %I64d\n",b[i][0].firstb[i][0].first,b[i][0].second);
            else
                printf("B %I64d %I64d\n",b[i][0].first,buyq[0]);
        }
    }

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<utility>
using namespace std;

int main()
{
    int n,s;
    scanf("%d %d",&n,&s);
    int buyq[100005]={0},sellq[100005]={0};
    int d=s;
    char ch[1];
    int i,j,k;
    for(i=0;i<n;i++)
    {
        scanf("%s",ch);
        scanf("%d %d",&j,&k);
        if(ch[0]=='B')
        {

                buyq[j]+=k;

        }
        else if(ch[0]=='S')
        {

            sellq[j]+=k;
        }
    //  printf("%d\n",i);
    }

    vector <pair<int,int> > b[n+1],se[n+1];

    for(i=0,j=0,k=0;i<=100000;i++)
    {
        if(buyq[i]!=0)
        {
            b[j].push_back(make_pair((buyq[i]*i),i));
            j++;
        }
        if(sellq[i]!=0)
        {
            se[k].push_back(make_pair((sellq[i]*i),i));
            k++;
        }
    }

    sort(b,b+j);
    sort(se,se+k);
    s=d;
/*  printf("hua yahan tak %d %d %d\n",j,k,s);
    for(i=0;i<j;i++)
    printf("%d %d\n",b[i][0].second,(b[i][0].first/b[i][0].second));*/

    if(s>=k)
    {
        for(i=k-1;i>=0;i--)
        {
            int temp=se[i][0].second;
            int a=se[i][0].first;
            a=a/temp;
            printf("S %d %d\n",temp,a);
        }
    }
    else
    {
        for(i=s-1;i>=0;i--)
        {
            printf("S %d %d\n",se[i][0].second,(se[i][0].first/se[i][0].second));
        }
    }
    //  printf("hua yahan tak %d %d %d\n",j,k,s);
    if(s>=j)
    {
            for(i=j-1;i>=0;i--)
        {
            printf("B %d %d\n",b[i][0].second,(b[i][0].first/b[i][0].second));
        }
    }
    else
    {
            for(i=j-1,k=1;k<=s;i--,k++)
        {
            printf("B %d %d\n",b[i][0].second,(b[i][0].first/b[i][0].second));
        }
    }

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<utility>
using namespace std;

int main()
{
    int n,s;
    scanf("%d %d",&n,&s);
    int buyq[100005]={0},sellq[100005]={0};
    int d=s;
    char ch[1];
    int i,j,k;
    for(i=0;i<n;i++)
    {
        scanf("%s",ch);
        scanf("%d %d",&j,&k);
        if(ch[0]=='B')
        {

                buyq[j]+=k;

        }
        else if(ch[0]=='S')
        {

            sellq[j]+=k;
        }
    //  printf("%d\n",i);
    }

    vector <pair<int,int> > b[n+1],se[n+1];

    for(i=0,j=0,k=0;i<=100000;i++)
    {
        if(buyq[i]!=0)
        {
            b[j].push_back(make_pair(i,buyq[i]));
            j++;
        }
        if(sellq[i]!=0)
        {
            se[k].push_back(make_pair(i,sellq[i]));
            k++;
        }
    }

    sort(b,b+j);
    sort(se,se+k);
    s=d;
/*  printf("hua yahan tak %d %d %d\n",j,k,s);
    for(i=0;i<j;i++)
    printf("%d %d\n",b[i][0].second,(b[i][0].first/b[i][0].second));*/

    if(s>=k)
    {
        for(i=k-1;i>=0;i--)
        {
            int temp=se[i][0].second;
            int a=se[i][0].first;

            printf("S %d %d\n",a,temp);
        }
    }
    else
    {
        for(i=s-1;i>=0;i--)
        {
            printf("S %d %d\n",(se[i][0].first),se[i][0].second);
        }
    }
    //  printf("hua yahan tak %d %d %d\n",j,k,s);
    if(s>=j)
    {
            for(i=j-1;i>=0;i--)
        {
            printf("B %d %d\n",(b[i][0].first),b[i][0].second);
        }
    }
    else
    {
            for(i=j-1,k=1;k<=s;i--,k++)
        {
            printf("B %d %d\n",(b[i][0].first),b[i][0].second);
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,size;
	scanf("%d %d",&n,&size);
	map <int, int> buy,sell;
	int maxbuy=1000000000,minsell=0;
	vector <int> b,s;
	int bf[100005]={0};
	int sf[100005]={0};
	for(int i=0;i<n;i++)
	{
		char c;
		int p,q;
		cin>>c;
		cin>>p>>q;
		if(c=='S')
		{
			sell[p]+=q;
			if(p<minsell)
			minsell=p;
			if(!sf[p])
			s.push_back(p);
			sf[p]=1;

		}
		else
		{
			buy[p]+=q;
			if(p>maxbuy)
			maxbuy=p;
			if(!bf[p])
			b.push_back(p);
			bf[p]=1;

		}
	}
	sort(s.begin(),s.end());
	sort(b.begin(),b.end(),greater<int>());
	int p=s.size();
	if(p>=size)
	p=size-1;
	else
	p--;
	for(int i=p;i>=0; i--)
	{
		printf("S %d %d\n",s[i],sell[s[i]]);

	}
	for(int i=0;i<b.size() && i<size;i++)
	{
		printf("B %d %d\n",b[i],buy[b[i]]);
	}
	return 0;
}
/*
6 5
B 10 3
S 50 2
S 40 1
S 50 6
B 20 4
B 25 10
*/


#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<utility>
using namespace std;

int main()
{
    int n,s;
    scanf("%d %d",&n,&s);
    int buyq[100005]={0},sellq[100005]={0};
    int d=s;
    char ch[1];
    long long int i,j,k;
    for(i=0;i<n;i++)
    {
        scanf("%s",ch);
        scanf("%I64d %I64d",&j,&k);
        if(ch[0]=='B')
        {

                buyq[j]+=k;

        }
        else if(ch[0]=='S')
        {

            sellq[j]+=k;
           // printf("%d\n",sellq[j]);
        }
    //  printf("%d\n",i);
    }

    vector <pair<long long int,long long int> > b[n+1],se[n+1];

    for(i=0,j=0,k=0;i<=100000;i++)
    {

        if(buyq[i]!=0)
        {
            if(i==0)
            b[j].push_back(make_pair(buyq[i]*i,i));
            else
            b[j].push_back(make_pair((buyq[i]*i),i));
            j++;
        }
        if(sellq[i]!=0)
        {
            if(i==0)
            se[k].push_back(make_pair(sellq[i]*i,i));
            else
            se[k].push_back(make_pair((sellq[i]*i),i));
             // printf("S %I64d %I64d\n",se[i][0].second,(se[i][0].first/se[i][0].second));
            k++;
        }
    }

    sort(b,b+j);
    sort(se,se+k);
    s=d;

/*  printf("hua yahan tak %d %d %d\n",j,k,s);
    for(i=0;i<j;i++)
    printf("%d %d\n",b[i][0].second,(b[i][0].first/b[i][0].second));*/

    if(s>=k)
    {
        for(i=k-1;i>=0;i--)
        {
            long long int temp=se[i][0].second;
            long long int a=se[i][0].first;
            if(temp!=0)
            printf("S %I64d %I64d\n",temp,(a/temp));
            else
             printf("S %I64d %I64d\n",temp,sellq[0]);
        }
    }
    else
    {
        for(i=s-1;i>=0;i--)
        {
            if(se[i][0].second!=0)
            printf("S %I64d %I64d\n",se[i][0].second,(se[i][0].first/se[i][0].second));
            else
            {
                 printf("S %I64d %I64d\n",se[i][0].second,sellq[0]);
            }
        }
    }
    //  printf("hua yahan tak %d %d %d\n",j,k,s);
    if(s>=j)
    {
            for(i=j-1;i>=0;i--)
        {
            if(b[i][0].second!=0)
            printf("B %I64d %I64d\n",b[i][0].second,(b[i][0].first/b[i][0].second));
            else
                printf("B %I64d %I64d\n",b[i][0].second,buyq[0]);
        }
    }
    else
    {
            for(i=j-1,k=1;k<=s;i--,k++)
        {
            if(b[i][0].second!=0)
            printf("B %I64d %I64d\n",b[i][0].second,(b[i][0].first/b[i][0].second));
            else
                printf("B %I64d %I64d\n",b[i][0].second,buyq[0]);
        }
    }

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<utility>
using namespace std;

int main()
{
    int n,s;
    scanf("%d %d",&n,&s);
    int buyq[100005]={0},sellq[100005]={0};
    int d=s;
    char ch[1];
    long long int i,j,k;
    for(i=0;i<n;i++)
    {
        scanf("%s",ch);
        scanf("%I64d %I64d",&j,&k);
        if(ch[0]=='B')
        {

                buyq[j]+=k;

        }
        else if(ch[0]=='S')
        {

            sellq[j]+=k;
           // printf("%d\n",sellq[j]);
        }
    //  printf("%d\n",i);
    }

    vector <pair<long long int,long long int> > b[n+1],se[n+1];

    for(i=0,j=0,k=0;i<=100000;i++)
    {

        if(buyq[i]!=0)
        {

            b[j].push_back(make_pair(i,buyq[i]));
            j++;
        }
        if(sellq[i]!=0)
        {

            se[k].push_back(make_pair(i,sellq[i]));
            k++;
        }
    }

    sort(b,b+j);
    sort(se,se+k);
    s=d;

/*  printf("hua yahan tak %d %d %d\n",j,k,s);
    for(i=0;i<j;i++)
    printf("%d %d\n",b[i][0].second,(b[i][0].first/b[i][0].second));*/

    if(s>=k)
    {
        for(i=k-1;i>=0;i--)
        {
            long long int temp=se[i][0].second;
            long long int a=se[i][0].first;
            if(a!=0)
            printf("S %I64d %I64d\n",a,temp);
            else
             printf("S %I64d %I64d\n",a,sellq[0]);
        }
    }
    else
    {
        for(i=s-1;i>=0;i--)
        {
            if(se[i][0].first!=0)
            printf("S %I64d %I64d\n",se[i][0].first,se[i][0].second);
            else
            {
                 printf("S %I64d %I64d\n",se[i][0].first,sellq[0]);
            }
        }
    }
    //  printf("hua yahan tak %d %d %d\n",j,k,s);
    if(s>=j)
    {
            for(i=j-1;i>=0;i--)
        {
            if(b[i][0].first!=0)
            printf("B %I64d %I64d\n",b[i][0].first,b[i][0].second);
            else
                printf("B %I64d %I64d\n",b[i][0].first,buyq[0]);
        }
    }
    else
    {
            for(i=j-1,k=1;k<=s;i--,k++)
        {
            if(b[i][0].first!=0)
            printf("B %I64d %I64d\n",b[i][0].first,b[i][0].second);
            else
                printf("B %I64d %I64d\n",b[i][0].first,buyq[0]);
        }
    }

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<utility>
using namespace std;

int main()
{
    int n,s;
    scanf("%d %d",&n,&s);
    int buyq[100005]={0},sellq[100005]={0};
    int d=s;
    char ch[1];
    long long int i,j,k;
    for(i=0;i<n;i++)
    {
        scanf("%s",ch);
        scanf("%I64d %I64d",&j,&k);
        if(ch[0]=='B')
        {

                buyq[j]+=k;

        }
        else if(ch[0]=='S')
        {

            sellq[j]+=k;
        }
    //  printf("%d\n",i);
    }

    vector <pair<long long int,long long int> > b[n+1],se[n+1];

    for(i=0,j=0,k=0;i<=100000;i++)
    {
        if(buyq[i]!=0)
        {
            b[j].push_back(make_pair((buyq[i]*i),i));
            j++;
        }
        if(sellq[i]!=0)
        {
            se[k].push_back(make_pair((sellq[i]*i),i));
            k++;
        }
    }

    sort(b,b+j);
    sort(se,se+k);
    s=d;
/*  printf("hua yahan tak %d %d %d\n",j,k,s);
    for(i=0;i<j;i++)
    printf("%d %d\n",b[i][0].second,(b[i][0].first/b[i][0].second));*/

    if(s>=k)
    {
        for(i=k-1;i>=0;i--)
        {
            long long int temp=se[i][0].second;
            long long int a=se[i][0].first;
            a=a/temp;
            printf("S %I64d %I64d\n",temp,a);
        }
    }
    else
    {
        for(i=s-1;i>=0;i--)
        {
            printf("S %I64d %I64d\n",se[i][0].second,(se[i][0].first/se[i][0].second));
        }
    }
    //  printf("hua yahan tak %d %d %d\n",j,k,s);
    if(s>=j)
    {
            for(i=j-1;i>=0;i--)
        {
            printf("B %I64d %I64d\n",b[i][0].second,(b[i][0].first/b[i][0].second));
        }
    }
    else
    {
            for(i=j-1,k=1;k<=s;i--,k++)
        {
            printf("B %I64d %I64d\n",b[i][0].second,(b[i][0].first/b[i][0].second));
        }
    }

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
using namespace std;

int main()
{
    int n,s;
    scanf("%d %d",&n,&s);

    int d=s;
    char ch[1];
    long long int i,j,k;

    map <int, long long int> buyq,sellq;
    for(i=0;i<n;i++)
    {
        scanf("%s",ch);
        scanf("%I64d %I64d",&j,&k);
        if(ch[0]=='B')
        {
            	if(buyq.find(j)!=buyq.end())
                buyq[j]+=k;
                else
                buyq[j]=k;

        }
        else if(ch[0]=='S')
        {

            if(sellq.find(j)!=sellq.end())
            sellq[j]+=k;
            else
            sellq[j]=k;

        }

    }

    int m=d;
    std::map <int, long long int>::iterator a;
	for(a=sellq.begin();a!=sellq.end();a++,m--)
	{
		if(m>0)
		continue;
		else
		sellq.erase(a);
	}

	 std::map <int, long long int>::reverse_iterator b;

	for(b=sellq.rbegin();b!=sellq.rend();b++)
	{
		printf("S %d %I64d\n",b->first,b->second);
	}
		m=d;
	for(b=buyq.rbegin();b!=buyq.rend();b--,m--)
	{
		if(m>0)
		printf("B %d %I64d\n",b->first,b->second);
		else
		{
			break;
		}
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct info
{
    long sap,sav,bp,bv;
} sa[1005],bb[1005];
bool cmp(info a,info b)
{
    if(a.sap==b.sap) return a.sav>b.sav;
    return a.sap>b.sap;
}
bool cmp1(info a,info b)
{
    if(a.bp==b.bp) return a.bv>b.bv;
    return a.bp>b.bp;
}
int main()
{
    long n,m,i,j,p[1006],q[1006],ss[1006],b[1006],kk,h;
    char ch[100][2],s[1005][2],ff;
    while(cin>>n>>m)
    {
        long dd=0,y=0,aa=0,vol;
        getchar();
        for(i=0; i<n; i++)  cin>>ch[i]>>p[i]>>q[i];
        for(i=0; i<n; i++)
        {
            vol=q[i];
            ff=ch[i][0];
            h=p[i];
            for(j=i+1; j<n; j++)
            {
                if(ch[i][0]==ch[j][0]&&p[i]==p[j]&&(ch[j][0]=='B'||ch[j][0]=='S'))
                {
                    ff=ch[j][0];
                    ch[j][0]='r';
                    h=p[j];
                    vol+=q[j];
                }
            }
            if(ff=='B'||ff=='S')
            {
                s[dd++][0]=ff;
                ss[aa++]=h;
                b[y++]=vol;
            }
        }
        long jj=0,z=0,ee=0,o=0;
        for(i=0; i<y; i++)
        {
            if(s[i][0]=='S')
            {
                sa[jj++].sap=ss[i];
                sa[o++].sav=b[i];
            }
            if(s[i][0]=='B')
            {
                bb[ee++].bp=ss[i];
                bb[z++].bv=b[i];
            }
        }
        sort(sa,sa+jj,cmp);
        sort(bb,bb+ee,cmp1);
        for(i=0; i<jj; i++)
        {
            if(i<m)
                cout<<"S "<<sa[i].sap<<" "<<sa[i].sav<<endl;
            else break;
        }
        for(i=0; i<ee; i++)
        {
            if(i<m)
                cout<<"B "<<bb[i].bp<<" "<<bb[i].bv<<endl;
            else break;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct info
{
    long sap,sav,bp,bv;
} sa[1005],bb[1005];
bool cmp(info a,info b)
{
    if(a.sap==b.sap) return a.sav<b.sav;
    return a.sap<b.sap;
}
bool cmp1(info a,info b)
{
    if(a.bp==b.bp) return a.bv>b.bv;
    return a.bp>b.bp;
}
int main()
{
    long n,m,i,j,p[1006],q[1006],ss[1006],b[1006],kk,h;
    char ch[100][2],s[1005][2],ff;
    while(cin>>n>>m)
    {
        long dd=0,y=0,aa=0,vol;
        getchar();
        for(i=0; i<n; i++)  cin>>ch[i]>>p[i]>>q[i];
        for(i=0; i<n; i++)
        {
            vol=q[i];
            ff=ch[i][0];
            h=p[i];
            for(j=i+1; j<n; j++)
            {
                if(ch[i][0]==ch[j][0]&&p[i]==p[j]&&(ch[j][0]=='B'||ch[j][0]=='S'))
                {
                    ff=ch[j][0];
                    ch[j][0]='r';
                    h=p[j];
                    vol+=q[j];
                }
            }
            if(ff=='B'||ff=='S')
            {
                s[dd++][0]=ff;
                ss[aa++]=h;
                b[y++]=vol;
            }
        }
        long jj=0,ee=0;
        for(i=0; i<y; i++)
        {
            if(s[i][0]=='S')
            {
                sa[jj].sap=ss[i];
                sa[jj].sav=b[i];
                jj++;
            }
            if(s[i][0]=='B')
            {
                bb[ee].bp=ss[i];
                bb[ee].bv=b[i];
                ee++;
            }
        }
        int cnt=0;
        sort(sa,sa+jj,cmp);
        sort(bb,bb+ee,cmp1);
        /*if(jj<m||ee<m)
        {
            for(i=0; i<n; i++) cout<<ch[i][0]<<" "<<sa[i].sap<<" "<<sa[i].sav<<endl;
        }
        else*/
        {
            for(i=m-1; i>=0; i--) cout<<"S "<<sa[i].sap<<" "<<sa[i].sav<<endl;
            for(i=0; i<ee; i++)
            {
                if(i<m)  cout<<"B "<<bb[i].bp<<" "<<bb[i].bv<<endl;
                else break;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct info
{
    long sap,sav,bp,bv;
} sa[1005],bb[1005];
bool cmp(info a,info b)
{
    if(a.sap==b.sap) return a.sav>b.sav;
    return a.sap>b.sap;
}
bool cmp1(info a,info b)
{
    if(a.bp==b.bp) return a.bv>b.bv;
    return a.bp>b.bp;
}
int main()
{
    long n,m,i,j,p[1006],q[1006],ss[1006],b[1006],kk,h;
    char ch[100][2],s[1005][2],ff;
    while(cin>>n>>m)
    {
        long dd=0,y=0,aa=0,vol;
        getchar();
        for(i=0; i<n; i++)  cin>>ch[i]>>p[i]>>q[i];
        for(i=0; i<n; i++)
        {
            vol=q[i];
            ff=ch[i][0];
            h=p[i];
            for(j=i+1; j<n; j++)
            {
                if(ch[i][0]==ch[j][0]&&p[i]==p[j]&&(ch[j][0]=='B'||ch[j][0]=='S'))
                {
                    ff=ch[j][0];
                    ch[j][0]='r';
                    h=p[j];
                    vol+=q[j];
                }
            }
            if(ff=='B'||ff=='S')
            {
                s[dd++][0]=ff;
                ss[aa++]=h;
                b[y++]=vol;
            }
        }
        long jj=0,ee=0;
        for(i=0; i<y; i++)
        {
            if(s[i][0]=='S')
            {
                sa[jj].sap=ss[i];
                sa[jj].sav=b[i];
                jj++;
            }
            if(s[i][0]=='B')
            {
                bb[ee].bp=ss[i];
                bb[ee].bv=b[i];
                ee++;
            }
        }
        sort(sa,sa+jj,cmp);
        sort(bb,bb+ee,cmp1);
        if(jj+ee<m)
        {
            for(i=0;i<jj;i++) cout<<"S "<<sa[i].sap<<" "<<sa[i].sav<<endl;
            for(i=0;i<ee;i++) cout<<"B "<<bb[i].bp<<" "<<bb[i].bv<<endl;
        }
        for(i=0; i<jj; i++)
        {
            if(i<m) cout<<"S "<<sa[i].sap<<" "<<sa[i].sav<<endl;
            else break;
        }
        for(i=0; i<ee; i++)
        {
            if(i<m)  cout<<"B "<<bb[i].bp<<" "<<bb[i].bv<<endl;
            else break;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct info
{
    long sap,sav,bp,bv;
} sa[1005],bb[1005];
bool cmp(info a,info b)
{
    if(a.sap==b.sap) return a.sav>b.sav;
    return a.sap>b.sap;
}
bool cmp1(info a,info b)
{
    if(a.bp==b.bp) return a.bv>b.bv;
    return a.bp>b.bp;
}
int main()
{
    long n,m,i,j,p[1006],q[1006],ss[1006],b[1006],kk,h;
    char ch[100][2],s[1005][2],ff;
    while(cin>>n>>m)
    {
        long dd=0,y=0,aa=0,vol;
        getchar();
        for(i=0; i<n; i++)  cin>>ch[i]>>p[i]>>q[i];
        for(i=0; i<n; i++)
        {
            vol=q[i];
            ff=ch[i][0];
            h=p[i];
            for(j=i+1; j<n; j++)
            {
                if(ch[i][0]==ch[j][0]&&p[i]==p[j]&&(ch[j][0]=='B'||ch[j][0]=='S'))
                {
                    ff=ch[j][0];
                    ch[j][0]='r';
                    h=p[j];
                    vol+=q[j];
                }
            }
            if(ff=='B'||ff=='S')
            {
                s[dd++][0]=ff;
                ss[aa++]=h;
                b[y++]=vol;
            }
        }
        long jj=0,ee=0;
        for(i=0; i<y; i++)
        {
            if(s[i][0]=='S')
            {
                sa[jj].sap=ss[i];
                sa[jj].sav=b[i];
                jj++;
            }
            if(s[i][0]=='B')
            {
                bb[ee].bp=ss[i];
                bb[ee].bv=b[i];
                ee++;
            }
        }
        sort(sa,sa+jj,cmp);
        sort(bb,bb+ee,cmp1);
        if(jj+ee<2*m)
        {
            for(i=0; i<jj; i++) cout<<"S "<<sa[i].sap<<" "<<sa[i].sav<<endl;
            for(i=0; i<ee; i++) cout<<"B "<<bb[i].bp<<" "<<bb[i].bv<<endl;
        }
        else
        {
            for(i=0; i<jj; i++)
            {
                if(i<m) cout<<"S "<<sa[i].sap<<" "<<sa[i].sav<<endl;
                else break;
            }
            for(i=0; i<ee; i++)
            {
                if(i<m)  cout<<"B "<<bb[i].bp<<" "<<bb[i].bv<<endl;
                else break;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct info
{
    long sap,sav,bp,bv;
} sa[1005],bb[1005];
bool cmp(info a,info b)
{
    if(a.sap==b.sap) return a.sav>b.sav;
    return a.sap<b.sap;
}
bool cmp1(info a,info b)
{
    if(a.bp==b.bp) return a.bv>b.bv;
    return a.bp>b.bp;
}
int main()
{
    long n,m,i,j,p[1006],q[1006],ss[1006],b[1006],kk,h;
    char ch[100][2],s[1005][2],ff;
    while(cin>>n>>m)
    {
        long dd=0,y=0,aa=0,vol;
        getchar();
        for(i=0; i<n; i++)  cin>>ch[i]>>p[i]>>q[i];
        for(i=0; i<n; i++)
        {
            vol=q[i];
            ff=ch[i][0];
            h=p[i];
            for(j=i+1; j<n; j++)
            {
                if(ch[i][0]==ch[j][0]&&p[i]==p[j]&&(ch[j][0]=='B'||ch[j][0]=='S'))
                {
                    ff=ch[j][0];
                    ch[j][0]='r';
                    h=p[j];
                    vol+=q[j];
                }
            }
            if(ff=='B'||ff=='S')
            {
                s[dd++][0]=ff;
                ss[aa++]=h;
                b[y++]=vol;
            }
        }
        long jj=0,ee=0;
        for(i=0; i<y; i++)
        {
            if(s[i][0]=='S')
            {
                sa[jj].sap=ss[i];
                sa[jj].sav=b[i];
                jj++;
            }
            if(s[i][0]=='B')
            {
                bb[ee].bp=ss[i];
                bb[ee].bv=b[i];
                ee++;
            }
        }
        sort(sa,sa+jj,cmp);
        sort(bb,bb+ee,cmp1);
        for(i=0; i<jj; i++)
        {
            if(i<m)
                cout<<"S "<<sa[i].sap<<" "<<sa[i].sav<<endl;
            else break;
        }
        for(i=0; i<ee; i++)
        {
            if(i<m)
                cout<<"B "<<bb[i].bp<<" "<<bb[i].bv<<endl;
            else break;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct info
{
    long sap,sav,bp,bv;
} sa[1005],bb[1005];
bool cmp(info a,info b)
{
    if(a.sap==b.sap) return a.sav<b.sav;
    return a.sap>b.sap;
}
bool cmp1(info a,info b)
{
    if(a.bp==b.bp) return a.bv>b.bv;
    return a.bp>b.bp;
}
int main()
{
    long n,m,i,j,p[1006],q[1006],ss[1006],b[1006],kk,h;
    char ch[100][2],s[1005][2],ff;
    while(cin>>n>>m)
    {
        long dd=0,y=0,aa=0,vol;
        getchar();
        for(i=0; i<n; i++)  cin>>ch[i]>>p[i]>>q[i];
        for(i=0; i<n; i++)
        {
            vol=q[i];
            ff=ch[i][0];
            h=p[i];
            for(j=i+1; j<n; j++)
            {
                if(ch[i][0]==ch[j][0]&&p[i]==p[j]&&(ch[j][0]=='B'||ch[j][0]=='S'))
                {
                    ff=ch[j][0];
                    ch[j][0]='r';
                    h=p[j];
                    vol+=q[j];
                }
            }
            if(ff=='B'||ff=='S')
            {
                s[dd++][0]=ff;
                ss[aa++]=h;
                b[y++]=vol;
            }
        }
        long jj=0,ee=0;
        for(i=0; i<y; i++)
        {
            if(s[i][0]=='S')
            {
                sa[jj].sap=ss[i];
                sa[jj].sav=b[i];
                jj++;
            }
            if(s[i][0]=='B')
            {
                bb[ee].bp=ss[i];
                bb[ee].bv=b[i];
                ee++;
            }
        }
        sort(sa,sa+jj,cmp);
        sort(bb,bb+ee,cmp1);
        if(jj<m||ee<m)
        {
            for(i=0; i<n; i++) cout<<ch[i][0]<<" "<<sa[i].sap<<" "<<sa[i].sav<<endl;
        }
        else
        {
            for(i=0; i<jj; i++)
            {
                if(i<m) cout<<"S "<<sa[i].sap<<" "<<sa[i].sav<<endl;
                else break;
            }
            for(i=0; i<ee; i++)
            {
                if(i<m)  cout<<"B "<<bb[i].bp<<" "<<bb[i].bv<<endl;
                else break;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct info
{
    long sap,sav,bp,bv;
} sa[1005],bb[1005];
bool cmp(info a,info b)
{
    if(a.sap==b.sap) return a.sav>b.sav;
    return a.sap>b.sap;
}
bool cmp1(info a,info b)
{
    if(a.bp==b.bp) return a.bv>b.bv;
    return a.bp>b.bp;
}
int main()
{
    long n,m,i,j,p[1006],q[1006],ss[1006],b[1006],kk,h;
    char ch[100][2],s[1005][2],ff;
    while(cin>>n>>m)
    {
        long dd=0,y=0,aa=0,vol;
        getchar();
        for(i=0; i<n; i++)  cin>>ch[i]>>p[i]>>q[i];
        for(i=0; i<n; i++)
        {
            vol=q[i];
            ff=ch[i][0];
            h=p[i];
            for(j=i+1; j<n; j++)
            {
                if(ch[i][0]==ch[j][0]&&p[i]==p[j]&&(ch[j][0]=='B'||ch[j][0]=='S'))
                {
                    ff=ch[j][0];
                    ch[j][0]='r';
                    h=p[j];
                    vol+=q[j];
                }
            }
            if(ff=='B'||ff=='S')
            {
                s[dd++][0]=ff;
                ss[aa++]=h;
                b[y++]=vol;
            }
        }
        long jj=0,ee=0;
        for(i=0; i<y; i++)
        {
            if(s[i][0]=='S')
            {
                sa[jj].sap=ss[i];
                sa[jj].sav=b[i];
                jj++;
            }
            if(s[i][0]=='B')
            {
                bb[ee].bp=ss[i];
                bb[ee].bv=b[i];
                ee++;
            }
        }
        sort(sa,sa+jj,cmp);
        sort(bb,bb+ee,cmp1);
        if(jj<m||ee<m)
        {
            for(i=0; i<jj; i++) cout<<"S "<<sa[i].sap<<" "<<sa[i].sav<<endl;
            for(i=0; i<ee; i++) cout<<"B "<<bb[i].bp<<" "<<bb[i].bv<<endl;
        }
        else
        {
            for(i=0; i<jj; i++)
            {
                if(i<m) cout<<"S "<<sa[i].sap<<" "<<sa[i].sav<<endl;
                else break;
            }
            for(i=0; i<ee; i++)
            {
                if(i<m)  cout<<"B "<<bb[i].bp<<" "<<bb[i].bv<<endl;
                else break;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct info
{
    long sap,sav,bp,bv;
} sa[1005],bb[1005];
bool cmp(info a,info b)
{
    if(a.sap==b.sap) return a.sav>b.sav;
    return a.sap>b.sap;
}
bool cmp1(info a,info b)
{
    if(a.bp==b.bp) return a.bv>b.bv;
    return a.bp>b.bp;
}
int main()
{
    long n,m,i,j,p[1006],q[1006],ss[1006],b[1006],kk,h;
    char ch[100][2],s[1005][2],ff;
    while(cin>>n>>m)
    {
        long dd=0,y=0,aa=0,vol;
        getchar();
        for(i=0; i<n; i++)  cin>>ch[i]>>p[i]>>q[i];
        for(i=0; i<n; i++)
        {
            vol=q[i];
            ff=ch[i][0];
            h=p[i];
            for(j=i+1; j<n; j++)
            {
                if(ch[i][0]==ch[j][0]&&p[i]==p[j]&&(ch[j][0]=='B'||ch[j][0]=='S'))
                {
                    ff=ch[j][0];
                    ch[j][0]='r';
                    h=p[j];
                    vol+=q[j];
                }
            }
            if(ff=='B'||ff=='S')
            {
                s[dd++][0]=ff;
                ss[aa++]=h;
                b[y++]=vol;
            }
        }
        long jj=0,ee=0;
        for(i=0; i<y; i++)
        {
            if(s[i][0]=='S')
            {
                sa[jj].sap=ss[i];
                sa[jj].sav=b[i];
                jj++;
            }
            if(s[i][0]=='B')
            {
                bb[ee].bp=ss[i];
                bb[ee].bv=b[i];
                ee++;
            }
        }
        sort(sa,sa+jj,cmp);
        sort(bb,bb+ee,cmp1);
        for(i=0; i<jj; i++)
        {
            if(i<m) cout<<"S "<<sa[i].sap<<" "<<sa[i].sav<<endl;
            else break;
        }
        for(i=0; i<ee; i++)
        {
            if(i<m)  cout<<"B "<<bb[i].bp<<" "<<bb[i].bv<<endl;
            else break;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct info
{
    long sap,sav,bp,bv;
} sa[1005],bb[1005];
bool cmp(info a,info b)
{
    if(a.sap==b.sap) return a.sav<b.sav;
    return a.sap<b.sap;
}
bool cmp1(info a,info b)
{
    if(a.bp==b.bp) return a.bv>b.bv;
    return a.bp>b.bp;
}
int main()
{
    long n,m,i,j,p[1006],q[1006],ss[1006],b[1006],kk,h;
    char ch[100][2],s[1005][2],ff;
    while(cin>>n>>m)
    {
        long dd=0,y=0,aa=0,vol;
        getchar();
        for(i=0; i<n; i++)  cin>>ch[i]>>p[i]>>q[i];
        for(i=0; i<n; i++)
        {
            vol=q[i];
            ff=ch[i][0];
            h=p[i];
            for(j=i+1; j<n; j++)
            {
                if(ch[i][0]==ch[j][0]&&p[i]==p[j]&&(ch[j][0]=='B'||ch[j][0]=='S'))
                {
                    ff=ch[j][0];
                    ch[j][0]='r';
                    h=p[j];
                    vol+=q[j];
                }
            }
            if(ff=='B'||ff=='S')
            {
                s[dd++][0]=ff;
                ss[aa++]=h;
                b[y++]=vol;
            }
        }
        long jj=0,ee=0;
        for(i=0; i<y; i++)
        {
            if(s[i][0]=='S')
            {
                sa[jj].sap=ss[i];
                sa[jj].sav=b[i];
                jj++;
            }
            if(s[i][0]=='B')
            {
                bb[ee].bp=ss[i];
                bb[ee].bv=b[i];
                ee++;
            }
        }
        int cnt=0;
        sort(sa,sa+jj,cmp);
        sort(bb,bb+ee,cmp1);
        /*if(jj<m||ee<m)
        {
            for(i=0; i<n; i++) cout<<ch[i][0]<<" "<<sa[i].sap<<" "<<sa[i].sav<<endl;
        }
        else*/
        {
            for(i=m-1; i>=0; i--)
            {
                if(sa[i].sap>0) cout<<"S "<<sa[i].sap<<" "<<sa[i].sav<<endl;
            }
            for(i=0; i<ee; i++)
            {
                if(i<m)  cout<<"B "<<bb[i].bp<<" "<<bb[i].bv<<endl;
                else break;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct info
{
    long sap,sav,bp,bv;
} sa[1005],bb[1005];
bool cmp(info a,info b)
{
    if(a.sap==b.sap) return a.sav>b.sav;
    return a.sap<b.sap;
}
bool cmp1(info a,info b)
{
    if(a.bp==b.bp) return a.bv>b.bv;
    return a.bp>b.bp;
}
int main()
{
    long n,m,i,j,p[1006],q[1006],ss[1006],b[1006],kk,h;
    char ch[1005][2],s[1005][2],ff;
    while(cin>>n>>m)
    {
        long dd=0,y=0,aa=0,vol;
        getchar();
        for(i=0; i<n; i++)  cin>>ch[i]>>p[i]>>q[i];
        for(i=0; i<n; i++)
        {
            vol=q[i];
            ff=ch[i][0];
            h=p[i];
            for(j=i+1; j<n; j++)
            {
                if(ch[i][0]==ch[j][0]&&p[i]==p[j]&&(ch[j][0]=='B'||ch[j][0]=='S'))
                {
                    ff=ch[j][0];
                    ch[j][0]='r';
                    h=p[j];
                    vol+=q[j];
                }
            }
            if(ff=='B'||ff=='S')
            {
                s[dd++][0]=ff;
                ss[aa++]=h;
                b[y++]=vol;
            }
        }
        long jj=0,ee=0;
        for(i=0; i<y; i++)
        {
            if(s[i][0]=='S')
            {
                sa[jj].sap=ss[i];
                sa[jj].sav=b[i];
                jj++;
            }
            if(s[i][0]=='B')
            {
                bb[ee].bp=ss[i];
                bb[ee].bv=b[i];
                ee++;
            }
        }
        int cnt=0;
        sort(sa,sa+jj,cmp);
        sort(bb,bb+ee,cmp1);
        /*if(jj<m||ee<m)
        {
            for(i=0; i<n; i++) cout<<ch[i][0]<<" "<<sa[i].sap<<" "<<sa[i].sav<<endl;
        }
        else*/
        {
            for(i=m-1; i>=0; i--)
            {
                if(sa[i].sap>0) cout<<"S "<<sa[i].sap<<" "<<sa[i].sav<<endl;
            }
            for(i=0; i<ee; i++)
            {
                if(i<m)  cout<<"B "<<bb[i].bp<<" "<<bb[i].bv<<endl;
                else break;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct info
{
    long sap,sav,bp,bv;
} sa[1005],bb[1005];
bool cmp(info a,info b)
{
    if(a.sap==b.sap) return a.sav<b.sav;
    return a.sap>b.sap;
}
bool cmp1(info a,info b)
{
    if(a.bp==b.bp) return a.bv>b.bv;
    return a.bp>b.bp;
}
int main()
{
    long n,m,i,j,p[1006],q[1006],ss[1006],b[1006],kk,h;
    char ch[100][2],s[1005][2],ff;
    while(cin>>n>>m)
    {
        long dd=0,y=0,aa=0,vol;
        getchar();
        for(i=0; i<n; i++)  cin>>ch[i]>>p[i]>>q[i];
        for(i=0; i<n; i++)
        {
            vol=q[i];
            ff=ch[i][0];
            h=p[i];
            for(j=i+1; j<n; j++)
            {
                if(ch[i][0]==ch[j][0]&&p[i]==p[j]&&(ch[j][0]=='B'||ch[j][0]=='S'))
                {
                    ff=ch[j][0];
                    ch[j][0]='r';
                    h=p[j];
                    vol+=q[j];
                }
            }
            if(ff=='B'||ff=='S')
            {
                s[dd++][0]=ff;
                ss[aa++]=h;
                b[y++]=vol;
            }
        }
        long jj=0,ee=0;
        for(i=0; i<y; i++)
        {
            if(s[i][0]=='S')
            {
                sa[jj].sap=ss[i];
                sa[jj].sav=b[i];
                jj++;
            }
            if(s[i][0]=='B')
            {
                bb[ee].bp=ss[i];
                bb[ee].bv=b[i];
                ee++;
            }
        }
        sort(sa,sa+jj,cmp);
        sort(bb,bb+ee,cmp1);
        if(jj<m||ee<m)
        {
            for(i=0; i<jj; i++) cout<<"S "<<sa[i].sap<<" "<<sa[i].sav<<endl;
            for(i=0; i<ee; i++) cout<<"B "<<bb[i].bp<<" "<<bb[i].bv<<endl;
        }
        else
        {
            for(i=0; i<jj; i++)
            {
                if(i<m) cout<<"S "<<sa[i].sap<<" "<<sa[i].sav<<endl;
                else break;
            }
            for(i=0; i<ee; i++)
            {
                if(i<m)  cout<<"B "<<bb[i].bp<<" "<<bb[i].bv<<endl;
                else break;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct info
{
    long sap,sav,bp,bv;
} sa[1005],bb[1005];
bool cmp(info a,info b)
{
    if(a.sap==b.sap) return a.sav>b.sav;
    return a.sap<b.sap;
}
bool cmp1(info a,info b)
{
    if(a.bp==b.bp) return a.bv>b.bv;
    return a.bp>b.bp;
}
int main()
{
    long n,m,i,j,p[1006],q[1006],ss[1006],b[1006],kk,h;
    char ch[100][2],s[1005][2],ff;
    while(cin>>n>>m)
    {
        long dd=0,y=0,aa=0,vol;
        getchar();
        for(i=0; i<n; i++)  cin>>ch[i]>>p[i]>>q[i];
        for(i=0; i<n; i++)
        {
            vol=q[i];
            ff=ch[i][0];
            h=p[i];
            for(j=i+1; j<n; j++)
            {
                if(ch[i][0]==ch[j][0]&&p[i]==p[j]&&(ch[j][0]=='B'||ch[j][0]=='S'))
                {
                    ff=ch[j][0];
                    ch[j][0]='r';
                    h=p[j];
                    vol+=q[j];
                }
            }
            if(ff=='B'||ff=='S')
            {
                s[dd++][0]=ff;
                ss[aa++]=h;
                b[y++]=vol;
            }
        }
        long jj=0,ee=0;
        for(i=0; i<y; i++)
        {
            if(s[i][0]=='S')
            {
                sa[jj].sap=ss[i];
                sa[jj].sav=b[i];
                jj++;
            }
            if(s[i][0]=='B')
            {
                bb[ee].bp=ss[i];
                bb[ee].bv=b[i];
                ee++;
            }
        }
        int cnt=0;
        sort(sa,sa+jj,cmp);
        sort(bb,bb+ee,cmp1);
        /*if(jj<m||ee<m)
        {
            for(i=0; i<n; i++) cout<<ch[i][0]<<" "<<sa[i].sap<<" "<<sa[i].sav<<endl;
        }
        else*/
        {
            for(i=m-1; i>=0; i--)
            {
                if(sa[i].sap>0) cout<<"S "<<sa[i].sap<<" "<<sa[i].sav<<endl;
            }
            for(i=0; i<ee; i++)
            {
                if(i<m)  cout<<"B "<<bb[i].bp<<" "<<bb[i].bv<<endl;
                else break;
            }
        }
    }
    return 0;
}


//SShifts-cf142 div1
//author: TriDorje

#include <iostream>
#include <fstream>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <algorithm>

#define fi "b.inp"
#define fo "b.out"

using namespace std;

int n,s;

struct xxx{
	int pr;
	int v;
};xxx aa[10005],bb[10005],luu1[10005],luu2[10005];

bool ss1(xxx td1,xxx td2){
	if(td1.pr<td2.pr) return 0;
	if(td1.pr>td2.pr) return 1;
	return 1;
}

void solve(){

	int i,x,y,a,b,l1,l2,dem;
	char ch[5];
	a=b=l1=l2=0;

	for(i=1;i<=n;i++){
		aa[i].pr=aa[i].v=bb[i].pr=bb[i].v=0;
		cin>>ch>>x>>y;
		if(ch[0]=='B'){
			aa[++a].pr=x;
			aa[a].v=y;
		}else{
			bb[++b].pr=x;
			bb[b].v=y;
		}
	}
	sort(aa+1,aa+a+1,ss1);
	sort(bb+1,bb+b+1,ss1);

	if(a>0){
		luu1[++l1]=aa[1];
	}

	for(i=2;i<=a;i++){
		if(aa[i].pr!=aa[i-1].pr){
			luu1[++l1].pr=aa[i].pr;
			luu1[l1].v=aa[i].v;
		}
		else{
			luu1[l1].v+=aa[i].v;
		}
	}

	if(b>0){
		luu2[++l2]=bb[1];
	}

	for(i=2;i<=b;i++){
		if(bb[i].pr!=bb[i-1].pr){
			luu2[++l2].pr=bb[i].pr;
			luu2[l2].v=bb[i].v;
		}
		else{
			luu2[l2].v+=bb[i].v;
		}
	}
	dem=0;
	for(i=1;i<=l2&&dem<s;i++){
		dem++;
		printf("S %d %d\n",luu2[i].pr,luu2[i].v);
	}
	dem=0;
	for(i=1;i<=l1&&dem<s;i++){
		dem++;
		printf("B %d %d\n",luu1[i].pr,luu1[i].v);
	}
}

int main(){

	//freopen(fi,"r",stdin);
	//freopen(fo,"w",stdout);

	while(cin>>n>>s){
		solve();
	}

	return 0;
}


//SShifts-cf142 div1
//author: TriDorje

#include <iostream>
#include <fstream>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <algorithm>

#define fi "b.inp"
#define fo "b.out"

using namespace std;

int n,s;

struct xxx{
	int pr;
	int v;
};xxx aa[10005],bb[10005],luu1[10005],luu2[10005];

bool ss1(xxx td1,xxx td2){

	if(td1.pr>td2.pr) return 1;

	return 0;
}
bool ss2(xxx t1,xxx t2){

	if(t1.pr<t2.pr) return 1;

	return 0;
}
void solve(){

	int i,x,y,a,b,l1,l2,dem,j;
	char ch[5];
	a=b=l1=l2=0;

	for(i=1;i<=n;i++){
		aa[i].pr=aa[i].v=bb[i].pr=bb[i].v=0;
		cin>>ch>>x>>y;
		if(ch[0]=='B'){
			aa[++a].pr=x;
			aa[a].v=y;
		}else{
			bb[++b].pr=x;
			bb[b].v=y;
		}
	}

	sort(aa+1,aa+a+1,ss1);
	sort(bb+1,bb+b+1,ss2);

	if(a>0){
		luu1[++l1]=aa[1];
	}

	for(i=2;i<=a;i++){
		if(aa[i].pr!=aa[i-1].pr){
			luu1[++l1].pr=aa[i].pr;
			luu1[l1].v=aa[i].v;
		}
		else{
			luu1[l1].v+=aa[i].v;
		}
	}

	if(b>0){
		luu2[++l2]=bb[1];
	}

	for(i=2;i<=b;i++){
		if(bb[i].pr!=bb[i-1].pr){
			luu2[++l2].pr=bb[i].pr;
			luu2[l2].v=bb[i].v;
		}
		else{
			luu2[l2].v+=bb[i].v;
		}
	}
	dem=0;
	for(i=l2;i>=1&&dem<s;i--){
		dem++;
		printf("S %d %d\n",luu2[i].pr,luu2[i].v);
	}
	dem=0;
	for(i=1;i<=l1&&dem<s;i++){
		dem++;
		printf("B %d %d\n",luu1[i].pr,luu1[i].v);
	}
}

int main(){

	//freopen(fi,"r",stdin);
	//freopen(fo,"w",stdout);

	//for(int i=1;i<=1000;i++){
	//	printf("S 100000 10000\n");
	//}

	while(cin>>n>>s){
		solve();
	}

	return 0;
}


//SShifts-cf142 div1
//author: TriDorje

#include <iostream>
#include <fstream>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <vector>
#include <algorithm>

#define fi "b.inp"
#define fo "b.out"

using namespace std;

int n,s;

struct xxx{
	int pr;
	int v;
};xxx aa[10005],bb[10005],luu1[10005],luu2[10005];

bool ss1(xxx td1,xxx td2){
	if(td1.pr<td2.pr) return 0;
	if(td1.pr>td2.pr) return 1;
	return 1;
}

void solve(){

	int i,x,y,a,b,l1,l2,dem,j;
	char ch[5];
	a=b=l1=l2=0;

	for(i=1;i<=n;i++){
		aa[i].pr=aa[i].v=bb[i].pr=bb[i].v=0;
		cin>>ch>>x>>y;
		if(ch[0]=='B'){
			aa[++a].pr=x;
			aa[a].v=y;
		}else{
			bb[++b].pr=x;
			bb[b].v=y;
		}
	}

	for(i=1;i<=a;i++){
		for(j=i+1;j<=a;j++){
			if(aa[i].pr<aa[j].pr){
				swap(aa[i],aa[j]);
			}
		}
	}

	for(i=1;i<=b;i++){
		for(j=i+1;j<=b;j++){
			if(bb[i].pr<bb[j].pr){
				swap(bb[i],bb[j]);
			}
		}
	}

//	sort(aa+1,aa+a+1,ss1);
//	sort(bb+1,bb+b+1,ss1);

	if(a>0){
		luu1[++l1]=aa[1];
	}

	for(i=2;i<=a;i++){
		if(aa[i].pr!=aa[i-1].pr){
			luu1[++l1].pr=aa[i].pr;
			luu1[l1].v=aa[i].v;
		}
		else{
			luu1[l1].v+=aa[i].v;
		}
	}

	if(b>0){
		luu2[++l2]=bb[1];
	}

	for(i=2;i<=b;i++){
		if(bb[i].pr!=bb[i-1].pr){
			luu2[++l2].pr=bb[i].pr;
			luu2[l2].v=bb[i].v;
		}
		else{
			luu2[l2].v+=bb[i].v;
		}
	}
	dem=0;
	for(i=1;i<=l2&&dem<s;i++){
		dem++;
		printf("S %d %d\n",luu2[i].pr,luu2[i].v);
	}
	dem=0;
	for(i=1;i<=l1&&dem<s;i++){
		dem++;
		printf("B %d %d\n",luu1[i].pr,luu1[i].v);
	}
}

int main(){

	//freopen(fi,"r",stdin);
	//freopen(fo,"w",stdout);

	while(cin>>n>>s){
		solve();
	}

	return 0;
}