//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=s;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;
	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++){
		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);
	luu1[++l1]=aa[1];
	for(i=2;i<=a;i++){
		if(aa[i].pr!=aa[i-1].pr){
			luu1[++l1]=aa[i];
		}
		if(aa[i].pr==aa[i-1].pr){
			luu1[l1].v+=aa[i].v;
		}
	}
	luu2[++l2]=bb[1];
	for(i=2;i<=b;i++){
		if(bb[i].pr!=bb[i-1].pr){
			luu2[++l2]=bb[i];
		}
		if(bb[i].pr==bb[i-1].pr){
			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 0;
	if(td1.pr>td2.pr) return 1;
	return 1;
}
bool ss2(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;
		}
	}

	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 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=s;i>=1&&dem<s;i--){
		if(luu2[i].v>0){
			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;

	return 1;
}
bool ss2(xxx td1,xxx td2){
	if(td1.pr>td2.pr) return 0;
	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;
		}
	}

	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;
	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;
		}
	}

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


//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;
	if(td1.pr<td2.pr) return 0;

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

	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;
		}
	}

	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;
}


#include <bits/stdc++.h>
#define maxn 1234
using namespace std;

struct data
{
    char f;
    int s, t;
};
int n, sm, sls= 0, slb = 0, sls1 = 1, slb1 = 1;
bool cmp(data a1, data a2)
{
    if(a1.f == a2.f && a2.f == 'S')
        return a1.s > a2.s;
    if(a1.f == a2.f && a2.f == 'B')
        return a1.s > a2.s;
    else return a1.f < a2.f;
}
data s[maxn], b[maxn],s1[maxn], b1[maxn];
int main()
{
    cin >> n >> sm;
    for(int i = 1; i <= n; i++)
    {
        char f;
        cin >> f;
        if(f == 'S')
        {
            sls++;
            s[sls].f = 'S';
            cin >> s[sls].s;
            cin >> s[sls].t;
        }
        if(f == 'B')
        {
            slb++;
            b[slb].f = 'B';
            cin >> b[slb].s;
            cin >> b[slb].t;
        }
    }
    sort(s+1,s +sls +1, cmp);
    sort(b+1, b+slb+1, cmp);
    s1[1] = s[1];
    b1[1] = b[1];
    for(int i = 2; i <= sls; i++)
        if(s[i].s == s[i-1].s)
            s1[sls1].t += s[i].t;
        else
        {
            sls1 ++;
            s1[sls1] = s[i];
        }
    for(int i = 2; i <= slb; i++)
        if(b[i].s == b[i-1].s)
            b1[slb1].t += b[i].t;
        else
        {
            slb1 ++;
            b1[slb1] = b[i];
        }
    for(int i = 1; i <= min(sm, sls1); i++)
        cout << s1[i].f  <<  " " << s1[i].s << "  "<< s1[i].t<< endl;
    for(int i = 1; i <= min(sm, slb1); i++)
        cout << b1[i].f << " " << b1[i].s << "  "<< b1[i].t << endl;
    return 0;
}


#include <bits/stdc++.h>
#define FOR(i, j, k) for (int i = j; i <= k; i ++)
#define FORD(i, j, k) for (int i = j; i >= k; i --)
#define mn 201000
#define maxC 1000000007
#define mp make_pair
#define F first
#define S second
#define p2 pair<int, int>
#define pb push_back

using namespace std;
int n, s, b = 0, se = 0;
struct querry
{
    int d, p, q;
};
querry q[mn], buy[mn], sel[mn];

bool comp(querry a, querry b)
{
    if (a.d != b.d)
        return (a.d == 0);
    return a.p < b.p;
}

void setup ()
{
    cin >> n >> s;
    char c;
    FOR (i, 1, n)
    {
        cin >> c >> q[i].p >> q[i].q;
        q[i].d = (c == 'S');
    }
    sort(q + 1, q + n + 1, comp);
}

void xuly ()
{
    FOR (i, 1, n)
    if (q[i].d == 0)
    {
        if (q[i].p == q[i - 1].p)
            buy[b].q += q[i].q;
        else
            buy[++ b] = q[i];
    }
    else
    {
        if (q[i - 1].d && q[i].p == q[i - 1].p)
            sel[se].q += q[i].q;
        else
            sel[++ se] = q[i];
    }
    FORD (i, min(s, se), 1)
    cout << "S " << sel[i].p << " " << sel[i].q << "\n";
    FORD (i, b, b - min(s, b) + 1)
    cout << "B " << buy[i].p << " " << buy[i].q << "\n";
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    setup ();
    xuly ();
    return 0;
}


#include <bits/stdc++.h>
#define maxn 1234
using namespace std;

struct data
{
    char f;
    int s, t;
};
int n, sm, sls= 0, slb = 0, sls1 = 1, slb1 = 1;
bool cmp(data a1, data a2)
{
    if(a1.f == a2.f && a2.f == 'S')
        return a1.s >= a2.s;
    if(a1.f == a2.f && a2.f == 'B')
        return a1.s > a2.s;
    else return a1.f <= a2.f;
}
data s[maxn], b[maxn],s1[maxn], b1[maxn];
int main()
{
    //freopen("inp.txt", "r", stdin);
    cin >> n >> sm;
    for(int i = 1; i <= n; i++)
    {
        char f;
        cin >> f;
        if(f == 'S')
        {
            sls++;
            s[sls].f = 'S';
            cin >> s[sls].s;
            cin >> s[sls].t;
        }
        if(f == 'B')
        {
            slb++;
            b[slb].f = 'B';
            cin >> b[slb].s;
            cin >> b[slb].t;
        }
    }
    sort(s+1,s +sls +1, cmp);
    sort(b+1, b+slb+1, cmp);
    s1[1] = s[1];
    b1[1] = b[1];
    for(int i = 2; i <= sls; i++)
        if(s[i].s == s[i-1].s)
            s1[sls1].t += s[i].t;
        else
        {
            sls1 ++;
            s1[sls1] = s[i];
        }
    for(int i = 2; i <= slb; i++)
        if(b[i].s == b[i-1].s)
            b1[slb1].t += b[i].t;
        else
        {
            slb1 ++;
            b1[slb1] = b[i];
        }
    for(int i = 1; i <= sm; i++)
        cout << s1[i].f  <<  " " << s1[i].s << " "<< s1[i].t<< endl;
    for(int i = 1; i <= sm; i++)
        cout << b1[i].f << " " << b1[i].s << " "<< b1[i].t << endl;
    return 0;
}


#include <bits/stdc++.h>
#define maxn 1234
using namespace std;

struct data
{
    char f;
    int s, t;
};
int n, sm, sls= 0, slb = 0, sls1 = 1, slb1 = 1;
bool cmp(data a1, data a2)
{
    if(a1.f == a2.f && a2.f == 'S')
        return a1.s >= a2.s;
    if(a1.f == a2.f && a2.f == 'B')
        return a1.s > a2.s;
    else return a1.f <= a2.f;
}
data s[maxn], b[maxn],s1[maxn], b1[maxn];
int main()
{
    cin >> n >> sm;
    for(int i = 1; i <= n; i++)
    {
        char f;
        cin >> f;
        if(f == 'S')
        {
            sls++;
            s[sls].f = 'S';
            cin >> s[sls].s;
            cin >> s[sls].t;
        }
        if(f == 'B')
        {
            slb++;
            b[slb].f = 'B';
            cin >> b[slb].s;
            cin >> b[slb].t;
        }
    }
    sort(s+1,s +sls +1, cmp);
    sort(b+1, b+slb+1, cmp);
    s1[1] = s[1];
    b1[1] = b[1];
    for(int i = 2; i <= sls; i++)
        if(s[i].s == s[i-1].s)
            s1[sls1].t += s[i].t;
        else
        {
            sls1 ++;
            s1[sls1] = s[i];
        }
    for(int i = 2; i <= slb; i++)
        if(b[i].s == b[i-1].s)
            b1[slb1].t += b[i].t;
        else
        {
            slb1 ++;
            b1[slb1] = b[i];
        }
    for(int i = 1; i <= min(sm, sls1) ; i++)
        cout << s1[i].f  <<  " " << s1[i].s << " "<< s1[i].t<< endl;
    for(int i = slb1; i >= slb1 - min(sm, slb1)+1; i--)
        cout << b1[i].f << " " << b1[i].s << " "<< b1[i].t << endl;
    return 0;
}


#include <bits/stdc++.h>
#define maxn 1234
using namespace std;

struct data
{
    char f;
    int s, t;
};
int n, sm, sls= 0, slb = 0, sls1 = 1, slb1 = 1;
bool cmp(data a1, data a2)
{
    if(a1.f == a2.f && a2.f == 'S')
        return a1.s >= a2.s;
    if(a1.f == a2.f && a2.f == 'B')
        return a1.s > a2.s;
    else return a1.f <= a2.f;
}
data s[maxn], b[maxn],s1[maxn], b1[maxn];
int main()
{
    cin >> n >> sm;
    for(int i = 1; i <= n; i++)
    {
        char f;
        cin >> f;
        if(f == 'S')
        {
            sls++;
            s[sls].f = 'S';
            cin >> s[sls].s;
            cin >> s[sls].t;
        }
        if(f == 'B')
        {
            slb++;
            b[slb].f = 'B';
            cin >> b[slb].s;
            cin >> b[slb].t;
        }
    }
    sort(s+1,s +sls +1, cmp);
    sort(b+1, b+slb+1, cmp);
    s1[1] = s[1];
    b1[1] = b[1];
    for(int i = 2; i <= sls; i++)
        if(s[i].s == s[i-1].s)
            s1[sls1].t += s[i].t;
        else
        {
            sls1 ++;
            s1[sls1] = s[i];
        }
    for(int i = 2; i <= slb; i++)
        if(b[i].s == b[i-1].s)
            b1[slb1].t += b[i].t;
        else
        {
            slb1 ++;
            b1[slb1] = b[i];
        }
    for(int i = 1; i <= min(sm, sls1) ; i++)
        cout << s1[i].f  <<  " " << s1[i].s << " "<< s1[i].t<< endl;
    for(int i = 1; i <= slb1 - min(sm, slb1)+1; i++)
        cout << b1[i].f << " " << b1[i].s << " "<< b1[i].t << endl;
    return 0;
}


#include <bits/stdc++.h>
#define maxn 1234
using namespace std;

struct data
{
    char f;
    int s, t;
};
int n, sm, sls= 0, slb = 0, sls1 = 1, slb1 = 1;
bool cmp(data a1, data a2)
{
    if(a1.f == a2.f && a2.f == 'S')
        return a1.s > a2.s;
    if(a1.f == a2.f && a2.f == 'B')
        return a1.s > a2.s;
    else return a1.f < a2.f;
}
data s[maxn], b[maxn],s1[maxn], b1[maxn];
int main()
{
    //freopen("inp.txt", "r", stdin);
    cin >> n >> sm;
    for(int i = 1; i <= n; i++)
    {
        char f;
        cin >> f;
        if(f == 'S')
        {
            sls++;
            s[sls].f = 'S';
            cin >> s[sls].s;
            cin >> s[sls].t;
        }
        if(f == 'B')
        {
            slb++;
            b[slb].f = 'B';
            cin >> b[slb].s;
            cin >> b[slb].t;
        }
    }
    sort(s+1,s +sls +1, cmp);
    sort(b+1, b+slb+1, cmp);
    s1[1] = s[1];
    b1[1] = b[1];
    for(int i = 2; i <= sls; i++)
        if(s[i].s == s[i-1].s)
            s1[sls1].t += s[i].t;
        else
        {
            sls1 ++;
            s1[sls1] = s[i];
        }
    for(int i = 2; i <= slb; i++)
        if(b[i].s == b[i-1].s)
            b1[slb1].t += b[i].t;
        else
        {
            slb1 ++;
            b1[slb1] = b[i];
        }
    for(int i = 1; i <= sm; i++)
        cout << s1[i].f  <<  " " << s1[i].s << " "<< s1[i].t<< endl;
    for(int i = 1; i <= sm; i++)
        cout << b1[i].f << " " << b1[i].s << " "<< b1[i].t << endl;
    return 0;
}


#include <bits/stdc++.h>
#define maxn 1234
using namespace std;

struct data
{
    char f;
    int s, t;
};
int n, sm, sls= 0, slb = 0, sls1 = 1, slb1 = 1;
bool cmp(data a1, data a2)
{
    if(a1.f == a2.f && a2.f == 'S')
        return a1.s > a2.s;
    if(a1.f == a2.f && a2.f == 'B')
        return a1.s > a2.s;
    else return a1.f < a2.f;
}
data s[maxn], b[maxn],s1[maxn], b1[maxn];
int main()
{
    //freopen("inp.txt", "r", stdin);
    cin >> n >> sm;
    for(int i = 1; i <= n; i++)
    {
        char f;
        cin >> f;
        if(f == 'S')
        {
            sls++;
            s[sls].f = 'S';
            cin >> s[sls].s;
            cin >> s[sls].t;
        }
        if(f == 'B')
        {
            slb++;
            b[slb].f = 'B';
            cin >> b[slb].s;
            cin >> b[slb].t;
        }
    }
    sort(s+1,s +sls +1, cmp);
    sort(b+1, b+slb+1, cmp);
    s1[1] = s[1];
    b1[1] = b[1];
    for(int i = 2; i <= sls; i++)
        if(s[i].s == s[i-1].s)
            s1[sls1].t += s[i].t;
        else
        {
            sls1 ++;
            s1[sls1] = s[i];
        }
    for(int i = 2; i <= slb; i++)
        if(b[i].s == b[i-1].s)
            b1[sls1].t += b[i].t;
        else
        {
            slb1 ++;
            b1[slb1] = b[i];
        }
    for(int i = 1; i <= min(sm, sls1); i++)
        cout << s1[i].f  <<  " " << s1[i].s << "  "<< s1[i].t<< endl;
    for(int i = 1; i <= min(sm, slb1); i++)
        cout << b1[i].f << " " << b1[i].s << "  "<< b1[i].t << endl;
    return 0;
}


#include <bits/stdc++.h>
#define maxn 1234
using namespace std;

struct data
{
    char f;
    int s, t;
};
int n, sm, sls= 0, slb = 0, sls1 = 1, slb1 = 1;
bool cmp(data a1, data a2)
{
    if(a1.f == a2.f && a2.f == 'S')
        return a1.s > a2.s;
    if(a1.f == a2.f && a2.f == 'B')
        return a1.s > a2.s;
    else return a1.f < a2.f;
}
data s[maxn], b[maxn],s1[maxn], b1[maxn];
int main()
{
    cin >> n >> sm;
    for(int i = 1; i <= n; i++)
    {
        char f;
        cin >> f;
        if(f == 'S')
        {
            sls++;
            s[sls].f = 'S';
            cin >> s[sls].s;
            cin >> s[sls].t;
        }
        if(f == 'B')
        {
            slb++;
            b[slb].f = 'B';
            cin >> b[slb].s;
            cin >> b[slb].t;
        }
    }
    sort(s+1,s +sls +1, cmp);
    sort(b+1, b+slb+1, cmp);
    s1[1] = s[1];
    b1[1] = b[1];
    for(int i = 2; i <= sls; i++)
        if(s[i].s == s[i-1].s)
            s1[sls1].t += s[i].t;
        else
        {
            sls1 ++;
            s1[sls1] = s[i];
        }
    for(int i = 2; i <= slb; i++)
        if(b[i].s == b[i-1].s)
            b1[sls1].t += b[i].t;
        else
        {
            slb1 ++;
            b1[slb1] = b[i];
        }
    for(int i = 1; i <= sm; i++)
        cout << s1[i].f  <<  " " << s1[i].s << "  "<< s1[i].t<< endl;
    for(int i = 1; i <= sm; i++)
        cout << b1[i].f << " " << b1[i].s << "  "<< b1[i].t << endl;
    return 0;
}


using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>
int main()
{
	char ch;
	int i,j,k,l,n,a,b,m,x,ans=0,cnt=0,s,cnt1=0,cnt2=0;
	cin>>n>>s;
	vector < pair<int,int> > arr;
	vector < pair<int,int> > brr;
	vector< pair<int,int> > q;
	for(i=0;i<n;i++)
	{
		cin>>ch>>a>>b;
		if(ch=='B')
		{
			arr.push_back(make_pair(a,b));
			cnt1++;
		}
		else
		{
			cnt2++;
			brr.push_back(make_pair(a,b));
		}

	}

	sort(arr.rbegin(),arr.rend());
	sort(brr.begin(),brr.end());

	cnt=brr[0].second;ans=0;
	for(i=1;i<=cnt2-1;i++)
	{
		if(brr[i-1].first==brr[i].first)
		{
			cnt+=brr[i].second;
			if(i==cnt2-1 && ans<s)
			{
				q.push_back(make_pair(brr[i-1].first,cnt));

			}
		}
		else
		{
			ans++;
			q.push_back(make_pair(brr[i-1].first,cnt));
			//cout<<'S'<<" "<<brr[i-1].first<<" "<<cnt<<endl;
			cnt=brr[i].second;
			if(i==cnt2-1 && ans<s)
			{
				q.push_back(make_pair(brr[i-1].first,cnt));

			}

		}
		if(ans==s)
			break;
	}
	vector<pair<int ,int> >::iterator it;
	reverse(q.begin(),q.end());
	for(it=q.begin();it!=q.end();it++)
	cout<<'S'<<" "<<it->first<<" "<<it->second<<endl;

	cnt=arr[0].second;ans=0;
	for(i=1;i<cnt1;i++)
	{
		if(arr[i-1].first==arr[i].first)
		{
			cnt+=arr[i].second;
			if(i==cnt1-1 && ans<s)
			{
				cout<<'B'<<" "<<arr[i].first<<" "<<cnt<<endl;

			}
		}
		else
		{
			ans++;
			cout<<'B'<<" "<<arr[i-1].first<<" "<<cnt<<endl;
			cnt=arr[i].second;
			if(i==cnt1-1 && ans<s)
			{
				cout<<'B'<<" "<<arr[i].first<<" "<<cnt<<endl;

			}

		}
		if(ans==s)
		{

			break;
		}
	}

	return 0;
}


using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>
int main()
{
	char ch;
	int i,j,k,l,n,a,b,m,x,ans=0,cnt=0,s,cnt1=0,cnt2=0;
	cin>>n>>s;
	vector < pair<int,int> > arr;
	vector < pair<int,int> > brr;
	for(i=0;i<n;i++)
	{
		cin>>ch>>a>>b;
		if(ch=='B')
		{
			arr.push_back(make_pair(a,b));
			cnt1++;
		}
		else
		{
			cnt2++;
			brr.push_back(make_pair(a,b));
		}

	}

	sort(arr.rbegin(),arr.rend());
	sort(brr.rbegin(),brr.rend());

	cnt=brr[0].second;ans=0;
	for(i=1;i<cnt2;i++)
	{
		if(brr[i-1].first==brr[i].first)
		cnt+=brr[i].second;
		else
		{
			ans++;
			cout<<'S'<<" "<<brr[i-1].first<<" "<<cnt<<endl;
			cnt=brr[i].second;
			if(i==cnt2-1 && ans<s)
			{
				cout<<'S'<<" "<<brr[i].first<<" "<<cnt<<endl;

			}

		}
		if(ans==s)
			break;
	}

	cnt=arr[0].second;ans=0;
	for(i=1;i<cnt1;i++)
	{
		if(arr[i-1].first==arr[i].first)
		cnt+=arr[i].second;
		else
		{
			ans++;
			cout<<'B'<<" "<<arr[i-1].first<<" "<<cnt<<endl;
			cnt=arr[i].second;
			if(i==cnt1-1 && ans<s)
			{
				cout<<'B'<<" "<<arr[i].first<<" "<<cnt<<endl;

			}

		}
		if(ans==s)
		{

			break;
		}
	}

	return 0;
}


using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>
int main()
{
	char ch;
	int i,j,k,l,n,a,b,m,x,ans=0,cnt=0,s,cnt1=0,cnt2=0;
	cin>>n>>s;
	vector < pair<int,int> > arr;
	vector < pair<int,int> > brr;
	vector< pair<int,int> > q;
	for(i=0;i<n;i++)
	{
		cin>>ch>>a>>b;
		if(ch=='B')
		{
			arr.push_back(make_pair(a,b));
			cnt1++;
		}
		else
		{
			cnt2++;
			brr.push_back(make_pair(a,b));
		}

	}

	sort(arr.rbegin(),arr.rend());
	sort(brr.begin(),brr.end());

	cnt=brr[0].second;ans=0;
	for(i=1;i<cnt2;i++)
	{
		if(ans==s)
			break;
		if(brr[i-1].first==brr[i].first)
		{
			cnt+=brr[i].second;
			if(i==cnt2-1 && ans<s)
			{
				q.push_back(make_pair(brr[i].first,cnt));

			}
		}
		else
		{
			ans++;
			q.push_back(make_pair(brr[i-1].first,cnt));
			//cout<<'S'<<" "<<brr[i-1].first<<" "<<cnt<<endl;
			cnt=brr[i].second;
			if(i==cnt2-1 && ans<s)
			{
				q.push_back(make_pair(brr[i-1].first,cnt));

			}

		}

	}
	vector<pair<int ,int> >::iterator it;
	reverse(q.begin(),q.end());
	for(it=q.begin();it!=q.end();it++)
	cout<<'S'<<" "<<it->first<<" "<<it->second<<endl;

	cnt=arr[0].second;ans=0;
	for(i=1;i<cnt1;i++)
	{
		if(ans==s)
		{

			break;
		}
		if(arr[i-1].first==arr[i].first)
		{
			cnt+=arr[i].second;
			if(i==cnt1-1 && ans<s)
			{
				cout<<'B'<<" "<<arr[i].first<<" "<<cnt<<endl;

			}
		}
		else
		{
			ans++;
			cout<<'B'<<" "<<arr[i-1].first<<" "<<cnt<<endl;
			cnt=arr[i].second;
			if(i==cnt1-1 && ans<s)
			{
				cout<<'B'<<" "<<arr[i].first<<" "<<cnt<<endl;

			}

		}

	}

	return 0;
}


using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>
int main()
{
	int i,j,ans=0,k,l,m,n,s,cntb=0,cnts=0,cnt=0,a,b,flag;
	char ch;
	cin>>n>>s;
	vector< pair<int ,int> > brr;
	vector< pair<int ,int> > srr;
	vector< pair<int ,int> > tmp;
	for(i=0;i<n;i++)
	{
		cin>>ch>>a>>b;
		if(ch=='S')
		{
			brr.push_back(make_pair(a,b));
			cntb++;
		}
		else
		{
			srr.push_back(make_pair(a,b));
			cnts++;
		}
	}
	sort(brr.begin(),brr.end());
flag=0;
	for(i=0;i<cntb-1;i++)
	{

		if(brr[i].first==brr[i+1].first)
		{
			cnt+=brr[i].second;
			flag=1;
		}
		else
		{
			//cout<<'B'<<" "<<arr[i].first<<cnt<<endl;
			if(flag!=0)
			tmp.push_back(make_pair(brr[i].first,cnt));
		else
			tmp.push_back(make_pair(brr[i].first,brr[i].second));
			flag=0;
			ans++;
			cnt=0;
		}
		if(ans>=s)
		{
			break;
		}

	}
	if(ans<s && cntb>0)
	{
		//cout<<'B'<<" "<<brr[cntb-1].first<<cnt<<endl;
		if(flag==1)
		tmp.push_back(make_pair(brr[cntb-1].first,cnt+brr[cntb-1].second));
	else
		tmp.push_back(make_pair(brr[cntb-1].first,brr[cntb-1].second));

	}
	reverse(tmp.begin(),tmp.end());
	vector<pair<int ,int> > ::iterator it;
	for(it=tmp.begin();it!=tmp.end();it++)
	{
		cout<<'S'<<" "<<it->first<<" "<<it->second<<endl;
	}

	cnt=0;flag=0;ans=0;
	sort(srr.rbegin(),srr.rend());
	for(i=0;i<cnts-1;i++)
	{

		if(srr[i].first==srr[i+1].first)
		{
			cnt+=srr[i].second;
			flag=1;
		}
		else
		{
			if(flag!=0)
			cout<<'B'<<" "<<srr[i].first<<" "<< cnt<<endl;
			else
			cout<<'B'<<" "<<srr[i].first<<" "<< srr[i].second<<endl;
			//tmp.push_back(make_pair(arr[i].first,cnt));
			flag=0;
			ans++;
			cnt=0;
		}
		if(ans>=s)
		{
			break;
		}

	}
	if(ans<s && cnts>0)
	{
		if(flag==1)
		cout<<'B'<<" "<<srr[cnts-1].first<<" "<<cnt+srr[cnts-1].second<<endl;
		else
		cout<<'B'<<" "<<srr[cnts-1].first<<" "<<srr[cnts-1].second<<endl;

	}

	return 0;
}


using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>
int main()
{
	int i,j,ans=0,k,l,m,n,s,cntb=0,cnts=0,cnt=0,a,b,flag;
	char ch;
	cin>>n>>s;
	vector< pair<int ,int> > brr;
	vector< pair<int ,int> > srr;
	vector< pair<int ,int> > tmp;
	for(i=0;i<n;i++)
	{
		cin>>ch>>a>>b;
		if(ch=='S')
		{
			brr.push_back(make_pair(a,b));
			cntb++;
		}
		else
		{
			srr.push_back(make_pair(a,b));
			cnts++;
		}
	}
	sort(brr.begin(),brr.end());
flag=0;
	for(i=0;i<cntb-1;i++)
	{

		if(brr[i].first==brr[i+1].first)
		{
			cnt+=brr[i].second;
			flag=1;
		}
		else
		{
			//cout<<'B'<<" "<<arr[i].first<<cnt<<endl;
			if(flag!=0)
			tmp.push_back(make_pair(brr[i].first,cnt));
		else
			tmp.push_back(make_pair(brr[i].first,brr[i].second));
			flag=0;
			ans++;
			cnt=0;
		}
		if(ans>=s)
		{
			break;
		}

	}
	if(flag==1 && ans<s && cntb>0)
	{
		//cout<<'B'<<" "<<brr[cntb-1].first<<cnt<<endl;
		if(flag==1)
		tmp.push_back(make_pair(brr[cntb-1].first,cnt+brr[cntb-1].second));
	else
		tmp.push_back(make_pair(brr[cntb-1].first,brr[cntb-1].second));

	}
	reverse(tmp.begin(),tmp.end());
	vector<pair<int ,int> > ::iterator it;
	for(it=tmp.begin();it!=tmp.end();it++)
	{
		cout<<'S'<<" "<<it->first<<" "<<it->second<<endl;
	}

	cnt=0;flag=0;ans=0;
	sort(srr.rbegin(),srr.rend());
	for(i=0;i<cnts-1;i++)
	{

		if(srr[i].first==srr[i+1].first)
		{
			cnt+=srr[i].second;
			flag=1;
		}
		else
		{
			if(flag!=0)
			cout<<'B'<<" "<<srr[i].first<<" "<< cnt<<endl;
			else
			cout<<'B'<<" "<<srr[i].first<<" "<< srr[i].second<<endl;
			//tmp.push_back(make_pair(arr[i].first,cnt));
			flag=0;
			ans++;
			cnt=0;
		}
		if(ans>=s)
		{
			break;
		}

	}
	if(ans<s && cnts>0)
	{
		if(flag==1)
		cout<<'B'<<" "<<srr[cnts-1].first<<cnt+srr[cnts-1].second<<endl;
		else
		cout<<'B'<<" "<<srr[cnts-1].first<<srr[cnts-1].second<<endl;

	}

	return 0;
}


using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>
int main()
{
	char ch;
	int i,j,k,l,n,a,b,m,x,ans=0,cnt=0,s,cnt1=0,cnt2=0;
	cin>>n>>s;
	vector < pair<int,int> > arr;
	vector < pair<int,int> > brr;
	vector< pair<int,int> > q;
	for(i=0;i<n;i++)
	{
		cin>>ch>>a>>b;
		if(ch=='B')
		{
			arr.push_back(make_pair(a,b));
			cnt1++;
		}
		else
		{
			cnt2++;
			brr.push_back(make_pair(a,b));
		}

	}

	sort(arr.rbegin(),arr.rend());
	sort(brr.begin(),brr.end());

	cnt=brr[0].second;ans=0;
	for(i=1;i<=cnt2-1;i++)
	{
		if(brr[i-1].first==brr[i].first)
		{
			cnt+=brr[i].second;
			if(i==cnt2-1 && ans<s)
			{
				q.push_back(make_pair(brr[i-1].first,cnt));

			}
		}
		else
		{
			ans++;
			q.push_back(make_pair(brr[i-1].first,cnt));
			//cout<<'S'<<" "<<brr[i-1].first<<" "<<cnt<<endl;
			cnt=brr[i].second;
			if(i==cnt2-1 && ans<s)
			{
				q.push_back(make_pair(brr[i-1].first,cnt));

			}

		}
		if(ans==s)
			break;
	}
	vector<pair<int ,int> >::iterator it;
	reverse(q.begin(),q.end());
	for(it=q.begin();it!=q.end();it++)
	cout<<'S'<<" "<<it->first<<" "<<it->second<<endl;

	cnt=arr[0].second;ans=0;
	for(i=1;i<cnt1;i++)
	{
		if(arr[i-1].first==arr[i].first)
		cnt+=arr[i].second;
		else
		{
			ans++;
			cout<<'B'<<" "<<arr[i-1].first<<" "<<cnt<<endl;
			cnt=arr[i].second;
			if(i==cnt1-1 && ans<s)
			{
				cout<<'B'<<" "<<arr[i].first<<" "<<cnt<<endl;

			}

		}
		if(ans==s)
		{

			break;
		}
	}

	return 0;
}


using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>
int main()
{
	int i,j,ans=0,k,l,m,n,s,cntb=0,cnts=0,cnt=0,a,b,flag;
	char ch;
	cin>>n>>s;
	vector< pair<int ,int> > brr;
	vector< pair<int ,int> > srr;
	vector< pair<int ,int> > tmp;
	for(i=0;i<n;i++)
	{
		cin>>ch>>a>>b;
		if(ch=='S')
		{
			brr.push_back(make_pair(a,b));
			cntb++;
		}
		else
		{
			srr.push_back(make_pair(a,b));
			cnts++;
		}
	}
	sort(brr.begin(),brr.end());
flag=0;
	for(i=0;i<cntb-1;i++)
	{

		if(brr[i].first==brr[i+1].first)
		{
			cnt+=brr[i].second;
			flag=1;
		}
		else
		{
			//cout<<'B'<<" "<<arr[i].first<<cnt<<endl;
			if(flag!=0)
			tmp.push_back(make_pair(brr[i].first,cnt));
		else
			tmp.push_back(make_pair(brr[i].first,brr[i].second));
			flag=0;
			ans++;
			cnt=0;
		}
		if(ans>=s)
		{
			break;
		}

	}
	if(ans<s && cntb>0)
	{
		//cout<<'B'<<" "<<brr[cntb-1].first<<cnt<<endl;
		if(flag==1)
		tmp.push_back(make_pair(brr[cntb-1].first,cnt+brr[cntb-1].second));
	else
		tmp.push_back(make_pair(brr[cntb-1].first,brr[cntb-1].second));

	}
	reverse(tmp.begin(),tmp.end());
	vector<pair<int ,int> > ::iterator it;
	for(it=tmp.begin();it!=tmp.end();it++)
	{
		cout<<'S'<<" "<<it->first<<" "<<it->second<<endl;
	}

	cnt=0;flag=0;ans=0;
	sort(srr.rbegin(),srr.rend());
	for(i=0;i<cnts-1;i++)
	{

		if(srr[i].first==srr[i+1].first)
		{
			cnt+=srr[i].second;
			flag=1;
		}
		else
		{
			if(flag!=0)
			cout<<'B'<<" "<<srr[i].first<<" "<< cnt<<endl;
			else
			cout<<'B'<<" "<<srr[i].first<<" "<< srr[i].second<<endl;
			//tmp.push_back(make_pair(arr[i].first,cnt));
			flag=0;
			ans++;
			cnt=0;
		}
		if(ans>=s)
		{
			break;
		}

	}
	if(ans<s && cnts>0)
	{
		if(flag==1)
		cout<<'B'<<" "<<srr[cnts-1].first<<cnt+srr[cnts-1].second<<endl;
		else
		cout<<'B'<<" "<<srr[cnts-1].first<<srr[cnts-1].second<<endl;

	}

	return 0;
}


using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>
int main()
{
	char ch;
	int i,j,k,l,n,a,b,m,x,ans=0,cnt=0,s,cnt1=0,cnt2=0;
	cin>>n>>s;
	vector < pair<int,int> > arr;
	vector < pair<int,int> > brr;
	vector< pair<int,int> > q;
	for(i=0;i<n;i++)
	{
		cin>>ch>>a>>b;
		if(ch=='B')
		{
			arr.push_back(make_pair(a,b));
			cnt1++;
		}
		else
		{
			cnt2++;
			brr.push_back(make_pair(a,b));
		}

	}

	sort(arr.rbegin(),arr.rend());
	sort(brr.begin(),brr.end());

	cnt=brr[0].second;ans=0;
	if(cnt2==1)
	{
		if(s>=1)
			cout<<'S'<<" "<<brr[0].first<<" "<<cnt<<endl;

	}
	else{
	for(i=1;i<cnt2;i++)
	{

		if(ans==s)
			break;
		if(brr[i-1].first==brr[i].first)
		{
			cnt+=brr[i].second;
			if(i==cnt2-1 && ans<s)
			{
				q.push_back(make_pair(brr[i].first,cnt));

			}
		}
		else
		{
			ans++;
			q.push_back(make_pair(brr[i-1].first,cnt));
			//cout<<'S'<<" "<<brr[i-1].first<<" "<<cnt<<endl;
			cnt=brr[i].second;
			if(i==cnt2-1 && ans<s)
			{
				q.push_back(make_pair(brr[i-1].first,cnt));

			}

		}

	}
}
	vector<pair<int ,int> >::iterator it;
	reverse(q.begin(),q.end());
	for(it=q.begin();it!=q.end();it++)
	cout<<'S'<<" "<<it->first<<" "<<it->second<<endl;

	cnt=arr[0].second;ans=0;
	if(cnt1==1)
	{
		if(s>=1)
			cout<<'B'<<" "<<arr[0].first<<" "<<cnt<<endl;

	}
	else
	{
	for(i=1;i<cnt1;i++)
	{
		if(ans==s)
		{

			break;
		}
		if(arr[i-1].first==arr[i].first)
		{
			cnt+=arr[i].second;
			if(i==cnt1-1 && ans<s)
			{
				cout<<'B'<<" "<<arr[i].first<<" "<<cnt<<endl;

			}
		}
		else
		{
			ans++;
			cout<<'B'<<" "<<arr[i-1].first<<" "<<cnt<<endl;
			cnt=arr[i].second;
			if(i==cnt1-1 && ans<s)
			{
				cout<<'B'<<" "<<arr[i].first<<" "<<cnt<<endl;

			}

		}

	}}

	return 0;
}


using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>
int main()
{
	int i,j,ans=0,k,l,m,n,s,cntb=0,cnts=0,cnt=0,a,b,flag;
	char ch;
	cin>>n>>s;
	vector< pair<int ,int> > brr;
	vector< pair<int ,int> > srr;
	vector< pair<int ,int> > tmp;
	for(i=0;i<n;i++)
	{
		cin>>ch>>a>>b;
		if(ch=='S')
		{
			brr.push_back(make_pair(a,b));
			cntb++;
		}
		else
		{
			srr.push_back(make_pair(a,b));
			cnts++;
		}
	}
	sort(brr.begin(),brr.end());
flag=0;
	for(i=0;i<cntb-1;i++)
	{

		if(brr[i].first==brr[i+1].first)
		{
			cnt+=brr[i].second;
			flag=1;
		}
		else
		{
			//cout<<'B'<<" "<<arr[i].first<<cnt<<endl;
			if(flag!=0)
			tmp.push_back(make_pair(brr[i].first,cnt+brr[i].second));
		else
			tmp.push_back(make_pair(brr[i].first,brr[i].second));
			flag=0;
			ans++;
			cnt=0;
		}
		if(ans>=s)
		{
			break;
		}

	}
	if(ans<s && cntb>0)
	{
		//cout<<'B'<<" "<<brr[cntb-1].first<<cnt<<endl;
		if(flag==1)
		tmp.push_back(make_pair(brr[cntb-1].first,cnt+brr[cntb-1].second));
	else
		tmp.push_back(make_pair(brr[cntb-1].first,brr[cntb-1].second));

	}
	reverse(tmp.begin(),tmp.end());
	vector<pair<int ,int> > ::iterator it;
	for(it=tmp.begin();it!=tmp.end();it++)
	{
		cout<<'S'<<" "<<it->first<<" "<<it->second<<endl;
	}

	cnt=0;flag=0;ans=0;
	sort(srr.rbegin(),srr.rend());
	for(i=0;i<cnts-1;i++)
	{

		if(srr[i].first==srr[i+1].first)
		{
			cnt+=srr[i].second;
			flag=1;
		}
		else
		{
			if(flag!=0)
			cout<<'B'<<" "<<srr[i].first<<" "<< cnt<<endl;
			else
			cout<<'B'<<" "<<srr[i].first<<" "<< srr[i].second<<endl;
			//tmp.push_back(make_pair(arr[i].first,cnt));
			flag=0;
			ans++;
			cnt=0;
		}
		if(ans>=s)
		{
			break;
		}

	}
	if(ans<s && cnts>0)
	{
		if(flag==1)
		cout<<'B'<<" "<<srr[cnts-1].first<<" "<<cnt+srr[cnts-1].second<<endl;
		else
		cout<<'B'<<" "<<srr[cnts-1].first<<" "<<srr[cnts-1].second<<endl;

	}

	return 0;
}


using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>
int main()
{
	char ch;
	int i,j,k,l,n,a,b,m,x,ans=0,cnt=0,s,cnt1=0,cnt2=0;
	cin>>n>>s;
	vector < pair<int,int> > arr;
	vector < pair<int,int> > brr;
	vector< pair<int,int> > q;
	for(i=0;i<n;i++)
	{
		cin>>ch>>a>>b;
		if(ch=='B')
		{
			arr.push_back(make_pair(a,b));
			cnt1++;
		}
		else
		{
			cnt2++;
			brr.push_back(make_pair(a,b));
		}

	}

	sort(arr.rbegin(),arr.rend());
	sort(brr.begin(),brr.end());
	if(cnt2>0)
	cnt=brr[0].second;ans=0;
	if(cnt2==1)
	{
		if(s>=1)
			cout<<'S'<<" "<<brr[0].first<<" "<<cnt<<endl;

	}
	else{
	for(i=1;i<cnt2;i++)
	{

		if(ans==s)
			break;
		if(brr[i-1].first==brr[i].first)
		{
			cnt+=brr[i].second;
			if(i==cnt2-1 && ans<s)
			{
				q.push_back(make_pair(brr[i].first,cnt));

			}
		}
		else
		{
			ans++;
			q.push_back(make_pair(brr[i-1].first,cnt));
			//cout<<'S'<<" "<<brr[i-1].first<<" "<<cnt<<endl;
			cnt=brr[i].second;
			if(i==cnt2-1 && ans<s)
			{
				q.push_back(make_pair(brr[i-1].first,cnt));

			}

		}

	}
}
	vector<pair<int ,int> >::iterator it;
	reverse(q.begin(),q.end());
	for(it=q.begin();it!=q.end();it++)
	cout<<'S'<<" "<<it->first<<" "<<it->second<<endl;
	if(cnt1>0)
	cnt=arr[0].second;ans=0;
	if(cnt1==1)
	{
		if(s>=1)
			cout<<'B'<<" "<<arr[0].first<<" "<<cnt<<endl;

	}
	else
	{
	for(i=1;i<cnt1;i++)
	{
		if(ans==s)
		{

			break;
		}
		if(arr[i-1].first==arr[i].first)
		{
			cnt+=arr[i].second;
			if(i==cnt1-1 && ans<s)
			{
				cout<<'B'<<" "<<arr[i].first<<" "<<cnt<<endl;

			}
		}
		else
		{
			ans++;
			cout<<'B'<<" "<<arr[i-1].first<<" "<<cnt<<endl;
			cnt=arr[i].second;
			if(i==cnt1-1 && ans<s)
			{
				cout<<'B'<<" "<<arr[i].first<<" "<<cnt<<endl;

			}

		}

	}}

	return 0;
}


using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>
int main()
{
	int i,j,ans=0,k,l,m,n,s,cntb=0,cnts=0,cnt=0,a,b,flag;
	char ch;
	cin>>n>>s;
	vector< pair<int ,int> > brr;
	vector< pair<int ,int> > srr;
	vector< pair<int ,int> > tmp;
	for(i=0;i<n;i++)
	{
		cin>>ch>>a>>b;
		if(ch=='S')
		{
			brr.push_back(make_pair(a,b));
			cntb++;
		}
		else
		{
			srr.push_back(make_pair(a,b));
			cnts++;
		}
	}
	sort(brr.begin(),brr.end());
flag=0;
	for(i=0;i<cntb-1;i++)
	{

		if(brr[i].first==brr[i+1].first)
		{
			cnt+=brr[i].second;
			flag=1;
		}
		else
		{
			//cout<<'B'<<" "<<arr[i].first<<cnt<<endl;
			if(flag!=0)
			tmp.push_back(make_pair(brr[i].first,cnt+brr[i].second));
		else
			tmp.push_back(make_pair(brr[i].first,brr[i].second));
			flag=0;
			ans++;
			cnt=0;
		}
		if(ans>=s)
		{
			break;
		}

	}
	if(ans<s && cntb>0)
	{
		//cout<<'B'<<" "<<brr[cntb-1].first<<cnt<<endl;
		if(flag==1)
		tmp.push_back(make_pair(brr[cntb-1].first,cnt+brr[cntb-1].second));
	else
		tmp.push_back(make_pair(brr[cntb-1].first,brr[cntb-1].second));

	}
	reverse(tmp.begin(),tmp.end());
	vector<pair<int ,int> > ::iterator it;
	for(it=tmp.begin();it!=tmp.end();it++)
	{
		cout<<'S'<<" "<<it->first<<" "<<it->second<<endl;
	}

	cnt=0;flag=0;ans=0;
	sort(srr.rbegin(),srr.rend());
	for(i=0;i<cnts-1;i++)
	{

		if(srr[i].first==srr[i+1].first)
		{
			cnt+=srr[i].second;
			flag=1;
		}
		else
		{
			if(flag!=0)
			cout<<'B'<<" "<<srr[i].first<<" "<< cnt+srr[i].second<<endl;
			else
			cout<<'B'<<" "<<srr[i].first<<" "<< srr[i].second<<endl;
			//tmp.push_back(make_pair(arr[i].first,cnt));
			flag=0;
			ans++;
			cnt=0;
		}
		if(ans>=s)
		{
			break;
		}

	}
	if(ans<s && cnts>0)
	{
		if(flag==1)
		cout<<'B'<<" "<<srr[cnts-1].first<<" "<<cnt+srr[cnts-1].second<<endl;
		else
		cout<<'B'<<" "<<srr[cnts-1].first<<" "<<srr[cnts-1].second<<endl;

	}

	return 0;
}


using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>
int main()
{
	char ch;
	int i,j,k,l,n,a,b,m,x,ans=0,cnt=0,s,cnt1=0,cnt2=0;
	cin>>n>>s;
	vector < pair<int,int> > arr;
	vector < pair<int,int> > brr;
	vector< pair<int,int> > q;
	for(i=0;i<n;i++)
	{
		cin>>ch>>a>>b;
		if(ch=='B')
		{
			arr.push_back(make_pair(a,b));
			cnt1++;
		}
		else
		{
			cnt2++;
			brr.push_back(make_pair(a,b));
		}

	}

	sort(arr.rbegin(),arr.rend());
	sort(brr.begin(),brr.end());

	cnt=brr[0].second;ans=0;
	for(i=1;i<cnt2;i++)
	{
		if(brr[i-1].first==brr[i].first)
		{
			cnt+=brr[i].second;
			if(i==cnt2-1 && ans<s)
			{
				q.push_back(make_pair(brr[i].first,cnt));

			}
		}
		else
		{
			ans++;
			q.push_back(make_pair(brr[i-1].first,cnt));
			//cout<<'S'<<" "<<brr[i-1].first<<" "<<cnt<<endl;
			cnt=brr[i].second;
			if(i==cnt2-1 && ans<s)
			{
				q.push_back(make_pair(brr[i-1].first,cnt));

			}

		}
		if(ans==s)
			break;
	}
	vector<pair<int ,int> >::iterator it;
	reverse(q.begin(),q.end());
	for(it=q.begin();it!=q.end();it++)
	cout<<'S'<<" "<<it->first<<" "<<it->second<<endl;

	cnt=arr[0].second;ans=0;
	for(i=1;i<cnt1;i++)
	{
		if(arr[i-1].first==arr[i].first)
		{
			cnt+=arr[i].second;
			if(i==cnt1-1 && ans<s)
			{
				cout<<'B'<<" "<<arr[i].first<<" "<<cnt<<endl;

			}
		}
		else
		{
			ans++;
			cout<<'B'<<" "<<arr[i-1].first<<" "<<cnt<<endl;
			cnt=arr[i].second;
			if(i==cnt1-1 && ans<s)
			{
				cout<<'B'<<" "<<arr[i].first<<" "<<cnt<<endl;

			}

		}
		if(ans==s)
		{

			break;
		}
	}

	return 0;
}


using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>
int main()
{
	char ch;
	int i,j,k,l,n,a,b,m,x,ans=0,cnt=0,s,cnt1=0,cnt2=0;
	cin>>n>>s;
	vector < pair<int,int> > arr;
	vector < pair<int,int> > brr;
	for(i=0;i<n;i++)
	{
		cin>>ch>>a>>b;
		if(ch=='B')
		{
			arr.push_back(make_pair(a,b));
			cnt1++;
		}
		else
		{
			cnt2++;
			brr.push_back(make_pair(a,b));
		}

	}

	sort(arr.rbegin(),arr.rend());
	sort(brr.begin(),brr.end());

	cnt=brr[0].second;ans=0;
	for(i=1;i<cnt2;i++)
	{
		if(brr[i-1].first==brr[i].first)
		cnt+=brr[i].second;
		else
		{
			ans++;
			cout<<'S'<<" "<<brr[i-1].first<<" "<<cnt<<endl;
			cnt=brr[i].second;
			if(i==cnt2-1 && ans<s)
			{
				cout<<'S'<<" "<<brr[i].first<<" "<<cnt<<endl;

			}

		}
		if(ans==s)
			break;
	}

	cnt=arr[0].second;ans=0;
	for(i=1;i<cnt1;i++)
	{
		if(arr[i-1].first==arr[i].first)
		cnt+=arr[i].second;
		else
		{
			ans++;
			cout<<'B'<<" "<<arr[i-1].first<<" "<<cnt<<endl;
			cnt=arr[i].second;
			if(i==cnt1-1 && ans<s)
			{
				cout<<'B'<<" "<<arr[i].first<<" "<<cnt<<endl;

			}

		}
		if(ans==s)
		{

			break;
		}
	}

	return 0;
}


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

int main()
{
int n,d;
cin>>n>>d;
char o;

map<int,int>s;
map<int,int>b;
map<int,int>::iterator its;
map<int,int>::iterator itb;
for(int i = 0 ; i < n;i++){
cin>>o;
int p,d;
cin>>p>>d;
if(o=='S'){

s[p]+=d;

}
if(o=='B'){
b[p]+=d;
}

}
its=s.end();
its--;
itb=b.end();
itb--;
for(int i =0;i<d;i++){
cout<< 'S' <<' ' << its->first<<' '<<its->second<<endl;

its--;

}

for(int i =0;i<d;i++){
cout<< 'B' <<' ' << itb->first<<' '<<itb->second<<endl;
itb--;
}

return 0;
}


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

int main()
{
int n,d;
cin>>n>>d;
char o;

map<int,int>s;
map<int,int>b;
map<int,int>::iterator its;
map<int,int>::iterator itb;
for(int i = 0 ; i < n;i++){
cin>>o;
int p,d;
cin>>p>>d;
if(o=='S'){

s[p]+=d;

}
if(o=='B'){
b[p]+=d;
}

}
if(!s.empty()){

its=s.end();
its--;
}
if(!b.empty())
{
itb=b.end();
itb--;
}
if(!s.empty())
for(int i =0;i<d&&i<=s.size();i++){

cout<< 'S' <<' ' << its->first<<' '<<its->second<<endl;
its--;

}
if(!b.empty())
for(int i =0;i<d&&i<=b.size();i++){
cout<< 'B' <<' ' << itb->first<<' '<<itb->second<<endl;
itb--;
}

return 0;
}


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

int main()
{
int n,d;
cin>>n>>d;
char o;

map<int,int>s;
map<int,int>b;
map<int,int>::iterator its;
map<int,int>::iterator itb;
for(int i = 0 ; i < n;i++){
cin>>o;
int p,d;
cin>>p>>d;
if(o=='S'){

s[p]+=d;

}
if(o=='B'){
b[p]+=d;
}

}
if(!s.empty()){

its=s.end();
its--;
}
if(!b.empty())
{
itb=b.end();
itb--;
}
if(!s.empty())
for(int i =0;i<d;i++){

cout<< 'S' <<' ' << its->first<<' '<<its->second<<endl;
its--;

}
if(!b.empty())
for(int i =0;i<d;i++){
cout<< 'B' <<' ' << itb->first<<' '<<itb->second<<endl;
itb--;
}

return 0;
}


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

int main()
{
int n,d;
cin>>n>>d;
char o;

map<int,int>s;
map<int,int>b;
map<int,int>::iterator its;
map<int,int>::iterator itb;
for(int i = 0 ; i < n;i++){
cin>>o;
int p,d;
cin>>p>>d;
if(o=='S'){

s[p]+=d;

}
if(o=='B'){
b[p]+=d;
}

}
its=s.end();
its--;
itb=b.end();
itb--;
for(int i =0;i<d;i++){

cout<< 'S' <<' ' << its->first<<' '<<its->second<<endl;
its--;

}

for(int i =0;i<d;i++){
cout<< 'B' <<' ' << itb->first<<' '<<itb->second<<endl;
itb--;
}

return 0;
}


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

int main()
{
int n,d;
cin>>n>>d;
char o;

map<int,int>s;
map<int,int>b;
map<int,int>::iterator its;
map<int,int>::iterator itb;
for(int i = 0 ; i < n;i++){
cin>>o;
int p,d;
cin>>p>>d;
if(o=='S'){

s[p]+=d;

}
if(o=='B'){
b[p]+=d;
}

}
if(!s.empty()){

its=s.begin();
if(d<s.size())
for(int i = 0 ; i < d-1;i++){
its++;
}
else{
its = s.end();
its--;
}
}
if(!b.empty())
{
itb=b.end();
itb--;
}
if(!s.empty())
for(int i =0;i<d&&i<s.size();i++){

cout<< 'S' <<' ' << its->first<<' '<<its->second<<endl;
its--;

}
if(!b.empty())
for(int i =0;i<d&&i<b.size();i++){
cout<< 'B' <<' ' << itb->first<<' '<<itb->second<<endl;
itb--;
}

return 0;
}


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

int main()
{
int n,d;
cin>>n>>d;
char o;

map<int,int>s;
map<int,int>b;
map<int,int>::iterator its;
map<int,int>::iterator itb;
for(int i = 0 ; i < n;i++){
cin>>o;
int p,d;
cin>>p>>d;
if(o=='S'){

s[p]+=d;

}
if(o=='B'){
b[p]+=d;
}

}
if(!s.empty()){

its=s.end();
its--;
}
if(!b.empty())
{
itb=b.end();
itb--;
}
if(!s.empty())
for(int i =0;i<s.size();i++){

cout<< 'S' <<' ' << its->first<<' '<<its->second<<endl;
its--;

}
if(!b.empty())
for(int i =0;i<b.size();i++){
cout<< 'B' <<' ' << itb->first<<' '<<itb->second<<endl;
itb--;
}

return 0;
}


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

int main()
{
int n,d;
cin>>n>>d;
char o;

map<int,int>s;
map<int,int>b;
map<int,int>::iterator its;
map<int,int>::iterator itb;
for(int i = 0 ; i < n;i++){
cin>>o;
int p,d;
cin>>p>>d;
if(o=='S'){

s[p]+=d;

}
if(o=='B'){
b[p]+=d;
}

}
if(!s.empty()){

its=s.end();
its--;
}
if(!b.empty())
{
itb=b.end();
itb--;
}
if(!s.empty())
for(int i =0;i<d&&i<s.size();i++){

cout<< 'S' <<' ' << its->first<<' '<<its->second<<endl;
its--;

}
if(!b.empty())
for(int i =0;i<d&&i<b.size();i++){
cout<< 'B' <<' ' << itb->first<<' '<<itb->second<<endl;
itb--;
}

return 0;
}


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

int main()
{
int n,d;
cin>>n>>d;
char o;

map<int,int>s;
map<int,int>b;
map<int,int>::iterator its;
map<int,int>::iterator itb;
for(int i = 0 ; i < n;i++){
cin>>o;
int p,d;
cin>>p>>d;
if(o=='S'){

s[p]+=d;

}
if(o=='B'){
b[p]+=d;
}

}
if(!s.empty()){

its=s.begin();
if(d<s.size())
for(int i = 0 ; i < d;i++){
its++;
}
else{
its = s.end();
its--;
}
}
if(!b.empty())
{
itb=b.end();
itb--;
}
if(!s.empty())
for(int i =0;i<d&&i<s.size();i++){

cout<< 'S' <<' ' << its->first<<' '<<its->second<<endl;
its--;

}
if(!b.empty())
for(int i =0;i<d&&i<b.size();i++){
cout<< 'B' <<' ' << itb->first<<' '<<itb->second<<endl;
itb--;
}

return 0;
}


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

int main()
{
int n,d;
cin>>n>>d;
char o;

map<int,int>s;
map<int,int>b;
map<int,int>::iterator its;
map<int,int>::iterator itb;
for(int i = 0 ; i < n;i++){
cin>>o;
int p,d;
cin>>p>>d;
if(o=='S'){

s[p]+=d;

}
if(o=='B'){
b[p]+=d;
}

}
its=s.end();
its--;
itb=b.end();
itb--;
for(int i =0;i<d&&d>=s.size();i++){

cout<< 'S' <<' ' << its->first<<' '<<its->second<<endl;
its--;

}

for(int i =0;i<d&&d>=b.size();i++){
cout<< 'B' <<' ' << itb->first<<' '<<itb->second<<endl;
itb--;
}

return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int s,n;
int i,j,k;
int num;
struct stock{
	char d;
	int p,q;
	static int sort_by;
	bool operator<(const stock& lhs) const
	{
		if(sort_by == 1) return p > lhs.p;
		if(sort_by == 2) return q > lhs.q;
		if(sort_by == 3) return d > lhs.d;
	}
};
stock all[1003];
int stock::sort_by = 3;
int main(){
	cin>>n>>s;
	for(i=0; i<n; i++){
		cin>>all[i].d>>all[i].p>>all[i].q;
	}
	sort(all, all+n);
	for(i =0; i<n;i ++){
		if(all[i].d == 'S' && all[i+1].d == 'B')
			num = i;
	}
	stock::sort_by = 1;
//	cout<<endl<<num<<endl;
	sort(all, all+num+1);
	sort(all+num, all+n);
	for(i=0; i<num; i++){
		if(all[i].p == all[i+1].p){
			all[i].q += all[i+1].q;
			all[i+1].d = 'A';
		}
	}
	for(j=num; j<n; j++){
		if(all[j].p == all[j+1].p){
			all[j].q += all[j+1].q;
			all[j+1].d = 'A';
		}
	}
/*	cout<<endl;
	for(i=0; i<n; i++){
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
	}*/
//		cout<<endl;
	stock::sort_by =3;
	sort(all, all+n);
	i =0;
	while(all[i].d!='A'){
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
		i++;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int s,n;
int i,j,k;
int num;
struct stock{
	char d;
	int p,q;
	static int sort_by;
	bool operator<(const stock& lhs) const
	{
		if(sort_by == 1) return p > lhs.p;
		if(sort_by == 2) return q > lhs.q;
		if(sort_by == 3) return d > lhs.d;
	}
};
stock all[1003];
int stock::sort_by = 1;
int main(){
	cin>>n>>s;
	for(i=0; i<n; i++){
		cin>>all[i].d>>all[i].p>>all[i].q;
	}
	sort(all, all+n);
/*		cout<<endl;
	for(i=0; i<n; i++){
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
	}
		cout<<endl;*/
	num = n;
	for(i =0; i<n;i ++){
		if(all[i].d == 'S' && all[i+1].d == 'B')
			num = i;
	}
	stock::sort_by = 1;
//	cout<<endl<<num<<endl;
	int sum =all[0].q;
	for(i=1; i<num; i++){
		if(all[i].p == all[i-1].p){
			sum += all[i].q;
			all[i-1].d = 'A';
		}
		if(all[i].p != all[i-1].p){
			all[i-1].q = sum;
			sum = all[i].q;
		}
		if(i == (num - 1)){
			all[i].q = sum;
		}
	}
	for(j=num+1; j<n; j++){
		if(all[j].p == all[j-1].p){
			all[j].q += all[j-1].q;
			all[j].d = 'A';
		}
	}
/*	cout<<endl;
	for(i=0; i<n; i++){
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
	}
		cout<<endl;*/
	stock::sort_by =3;
	sort(all, all+n);
	for(j = 0; j< 2*s; j++){
		if(all[j].d!='A' && all[j].p != 0){
			cout<<all[j].d<<' '<<all[j].p<<' '<<all[j].q<<endl;
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int s,n;
int i,j,k;
int num;
struct stock{
	char d;
	int p,q;
	static int sort_by;
	bool operator<(const stock& lhs) const
	{
		if(sort_by == 1) return p > lhs.p;
		if(sort_by == 2) return p < lhs.p;
		if(sort_by == 3) return d > lhs.d;
	}
};
stock all[1003];
int stock::sort_by = 1;
int main(){
	cin>>n>>s;
	for(i=0; i<n; i++){
		cin>>all[i].d>>all[i].p>>all[i].q;
	}
	sort(all, all+n);
/*		cout<<endl;
	for(i=0; i<n; i++){
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
	}
		cout<<endl;*/
	num = n;
	for(i = 0; i < n; i++){
		if(all[i].d == 'S' && all[i+1].d == 'B')
			num = i;
	}
	stock::sort_by = 1;
//	cout<<endl<<num<<endl;
	int sum1 =all[0].q;
	for(i=1; i<num; i++){
		if(all[i].p == all[i-1].p){
			sum1 += all[i].q;
			all[i-1].d = 'A';
		}
		else{
			all[i-1].q = sum1;
			sum1 = all[i].q;
		}
		if(i == (num - 1)){
			all[i].q = sum1;
		}
	}
	int sum2 = all[num].q;
	for(j=num+1; j<n; j++){
		if(all[j].p == all[j-1].p){
			sum2 += all[j].q;
			all[j-1].d = 'A';
		}
		else{
			all[j-1].q = sum2;
			sum2 = all[j].q;
		}
		if(j == (n-1)){
			all[j].q = sum2;
		}
	}
//	cout<<endl;
	stock::sort_by =1;
	sort(all, all+n);
	stock::sort_by =3;
	sort(all, all+n);
/*	for(i=0; i<n; i++){
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
	}
		cout<<endl;
/*
	stock::sort_by=1;
	sort(all, all+num);
	stock::sort_by=2;
	sort(all+num, all+n);

	for(i = num-1; i>=s; i--){
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
	}
	cout<<endl;
	for (i = num; i < num+s; ++i)
	{
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
	}*/
	int count = 0;
	for(j = 0; j< n; j++){
		if(all[j].d!='A' && all[j].p != 0){
			count++;
//			cout<<all[j].d<<' '<<all[j].p<<' '<<all[j].q<<endl;
		}
	}
	if( count < s*2){
		for(j = 0; j< n; j++){
		if(all[j].d!='A' && all[j].p != 0){
			cout<<all[j].d<<' '<<all[j].p<<' '<<all[j].q<<endl;
		}
	}
	}
	else{
		for(j = 0; j< 2*s; j++){
		if(all[j].d!='A' && all[j].p != 0){
			cout<<all[j].d<<' '<<all[j].p<<' '<<all[j].q<<endl;
		}
	}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int s,n;
int i,j,k;
int num;
struct stock{
	char d;
	int p,q;
	static int sort_by;
	bool operator<(const stock& lhs) const
	{
		if(sort_by == 1) return p > lhs.p;
		if(sort_by == 2) return q > lhs.q;
		if(sort_by == 3) return d > lhs.d;
	}
};
stock all[1003];
int stock::sort_by = 1;
int main(){
	cin>>n>>s;
	for(i=0; i<n; i++){
		cin>>all[i].d>>all[i].p>>all[i].q;
	}
	sort(all, all+n);
/*		cout<<endl;
	for(i=0; i<n; i++){
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
	}
		cout<<endl;*/
	num = n;
	for(i =0; i<n;i ++){
		if(all[i].d == 'S' && all[i+1].d == 'B')
			num = i;
	}
	stock::sort_by = 1;
//	cout<<endl<<num<<endl;
	int sum1 =all[0].q;
	for(i=1; i<num; i++){
		if(all[i].p == all[i-1].p){
			sum1 += all[i].q;
			all[i-1].d = 'A';
		}
		if(all[i].p != all[i-1].p){
			all[i-1].q = sum1;
			sum1 = all[i].q;
		}
		if(i == (num - 1)){
			all[i].q = sum1;
		}
	}
	int sum2 = all[num].q;
	for(j=num+1; j<n; j++){
		if(all[j].p == all[j-1].p){
			sum2 += all[j].q;
			all[j-1].d = 'A';
		}
		else{
			all[j-1].q = sum2;
			sum2 = all[j].q;
		}
		if(j == (n-1)){
			all[j].q = sum2;
		}
	}
/*	cout<<endl;
	for(i=0; i<n; i++){
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
	}
		cout<<endl;*/
	stock::sort_by =3;
	sort(all, all+n);
	for(j = 0; j< 2*s; j++){
		if(all[j].d!='A' && all[j].p != 0){
			cout<<all[j].d<<' '<<all[j].p<<' '<<all[j].q<<endl;
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int ss,n;
int i,j,k;
int num;
int p,q;
std::vector<pair <int, int> > v;
std::vector<pair <int, int> > s;
std::vector<pair <int, int> > ans;
std::vector<pair <int, int> > as;
int main(){
	char d;
	cin>>n>>ss;
	for(i=0; i<n; i++){
		cin>>d>>p>>q;
		if(d == 'B'){
			v.push_back(make_pair(p,q));
		}
		if(d == 'S'){
			s.push_back(make_pair(p,q));
		}
	}
	sort(v.begin(), v.end(), greater<pair<int,int> >());
	sort(s.begin(), s.end(), greater<pair<int,int> >());
	ans.push_back(make_pair(s[0].first, s[0].second));
	for(j = 0; j < s.size()-1; j++){
//		cout<<s[j].first<<' '<<s[j].second<<endl;
		if(s[j].first == s[j+1].first){
			ans[j].second += s[j+1].second;
		}
		if(s[j].first != s[j+1].first){
			ans.push_back(make_pair(s[j+1].first,s[j+1].second));
		}
	}
//	cout<<endl<<"new s"<<endl;
	int k = ans.size();
	for(j = 0; j < min(k,ss); j++){
		cout<<'S'<<' '<<ans[j].first<<' '<<ans[j].second<<endl;
	}
	as.push_back(make_pair(v[0].first, v[0].second));
	for(i = 0; i < v.size()-1; i++){
//		cout<<v[i].first<<' '<<v[i].second<<endl;
		if(v[i].first == v[i+1].first){
			as[i].second += v[i+1].second;
		}
		if(v[i].first != v[i+1].first){
			as.push_back(make_pair(v[i+1].first,v[i+1].second));
		}
	}
	int kk = as.size();
	for(j = 0; j < min(kk,ss); j++){
		cout<<'B'<<' '<<as[j].first<<' '<<as[j].second<<endl;
	}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int ss,n;
int i,j,k;
int num;
int p,q;
std::vector<pair <int, int> > v;
std::vector<pair <int, int> > s;
std::vector<pair <int, int> > ans;
std::vector<pair <int, int> > as;
int main(){
	char d;
	cin>>n>>ss;
	for(i=0; i<n; i++){
		cin>>d>>p>>q;
		if(d == 'B'){
			v.push_back(make_pair(p,q));
		}
		if(d == 'S'){
			s.push_back(make_pair(p,q));
		}
	}
	sort(v.begin(), v.end(), greater<pair<int,int> >());
	sort(s.begin(), s.end(), greater<pair<int,int> >());
	if(s.size())
		ans.push_back(make_pair(s[0].first, s[0].second));
	int ssize = s.size()-1;
	int o =0;
	for(j = 0; j < ssize; j++){
//		cout<<s[j].first<<' '<<s[j].second<<endl;
		if(s[j].first == s[j+1].first){
			ans[o].second += s[j+1].second;
		}
		if(s[j].first != s[j+1].first){
			ans.push_back(make_pair(s[j+1].first,s[j+1].second));
			o++;
		}
	}
//	cout<<endl<<"new s"<<endl;
	int k = ans.size();
	if(ss > k && k!=0){
		for(j = 0; j <k; j++)
			cout<<'S'<<' '<<ans[j].first<<' '<<ans[j].second<<endl;
		return 0;
	}
	for(j = max(k,ss)-min(k,ss); j < max(k,ss); j++){
		cout<<'S'<<' '<<ans[j].first<<' '<<ans[j].second<<endl;
	}
	if(v.size())
		as.push_back(make_pair(v[0].first, v[0].second));
	int vs = v.size()-1;
	int oo = 0;
	for(i = 0; i < vs; i++){
//		cout<<v[i].first<<' '<<v[i].second<<endl;
		if(v[i].first == v[i+1].first){
			as[oo].second += v[i+1].second;
		}
		if(v[i].first != v[i+1].first){
			as.push_back(make_pair(v[i+1].first,v[i+1].second));
			oo++;
		}
	}
	int kk = as.size();
	if(ss > kk && kk !=0){
		for(j = 0; j <kk; j++)
			cout<<'B'<<' '<<as[j].first<<' '<<as[j].second<<endl;
		return 0;
	}
	for(j = 0; j < min(kk,ss); j++){
		cout<<'B'<<' '<<as[j].first<<' '<<as[j].second<<endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int s,n;
int i,j,k;
int num;
struct stock{
	char d;
	int p,q;
	static int sort_by;
	bool operator<(const stock& lhs) const
	{
		if(sort_by == 1) return p > lhs.p;
		if(sort_by == 2) return p < lhs.p;
		if(sort_by == 3) return d > lhs.d;
	}
};
stock all[1003];
int stock::sort_by = 1;
int main(){
	cin>>n>>s;
	for(i=0; i<n; i++){
		cin>>all[i].d>>all[i].p>>all[i].q;
	}
	sort(all, all+n);
/*		cout<<endl;
	for(i=0; i<n; i++){
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
	}
		cout<<endl;*/
	num = n;
	for(i = 0; i < n; i++){
		if(all[i].d == 'S' && all[i+1].d == 'B')
			num = i;
	}
	stock::sort_by = 1;
//	cout<<endl<<num<<endl;
	int sum1 =all[0].q;
	for(i=1; i<num; i++){
		if(all[i].p == all[i-1].p){
			sum1 += all[i].q;
			all[i-1].d = 'A';
		}
		else{
			all[i-1].q = sum1;
			sum1 = all[i].q;
		}
		if(i == (num - 1)){
			all[i].q = sum1;
		}
	}
	int sum2 = all[num].q;
	for(j=num+1; j<n; j++){
		if(all[j].p == all[j-1].p){
			sum2 += all[j].q;
			all[j-1].d = 'A';
		}
		else{
			all[j-1].q = sum2;
			sum2 = all[j].q;
		}
		if(j == (n-1)){
			all[j].q = sum2;
		}
	}
//	cout<<endl;
	stock::sort_by =1;
	sort(all, all+n);
/*	for(i=0; i<n; i++){
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
	}
		cout<<endl;
/*
	stock::sort_by=1;
	sort(all, all+num);
	stock::sort_by=2;
	sort(all+num, all+n);

	for(i = num-1; i>=s; i--){
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
	}
	cout<<endl;
	for (i = num; i < num+s; ++i)
	{
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
	}*/
	int count = 0;
	for(j = 0; j< n; j++){
		if(all[j].d!='A' && all[j].p != 0){
			count++;
//			cout<<all[j].d<<' '<<all[j].p<<' '<<all[j].q<<endl;
		}
	}
	if( count < s*2){
		for(j = 0; j< n; j++){
		if(all[j].d!='A' && all[j].p != 0){
			cout<<all[j].d<<' '<<all[j].p<<' '<<all[j].q<<endl;
		}
	}
	}
	else{
		for(j = 0; j< 2*s; j++){
		if(all[j].d!='A' && all[j].p != 0){
			cout<<all[j].d<<' '<<all[j].p<<' '<<all[j].q<<endl;
		}
	}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int ss,n;
int i,j,k;
int num;
int p,q;
std::vector<pair <int, int> > v;
std::vector<pair <int, int> > s;
std::vector<pair <int, int> > ans;
std::vector<pair <int, int> > as;
int main(){
	char d;
	cin>>n>>ss;
	for(i=0; i<n; i++){
		cin>>d>>p>>q;
		if(d == 'B'){
			v.push_back(make_pair(p,q));
		}
		if(d == 'S'){
			s.push_back(make_pair(p,q));
		}
	}
	sort(v.begin(), v.end(), greater<pair<int,int> >());
	sort(s.begin(), s.end(), greater<pair<int,int> >());
	if(s.size())
		ans.push_back(make_pair(s[0].first, s[0].second));
	int ssize = s.size()-1;
	int o =0;
	for(j = 0; j < ssize; j++){
//		cout<<s[j].first<<' '<<s[j].second<<endl;
		if(s[j].first == s[j+1].first){
			ans[o].second += s[j+1].second;
		}
		if(s[j].first != s[j+1].first){
			ans.push_back(make_pair(s[j+1].first,s[j+1].second));
			o++;
		}
	}
//	cout<<endl<<"new s"<<endl;
	int k = ans.size();
	if(ss > k){
		for(j = 0; j <k; j++)
			cout<<'S'<<' '<<ans[j].first<<' '<<ans[j].second<<endl;
		return 0;
	}
	for(j = max(k,ss)-min(k,ss); j < max(k,ss); j++){
		cout<<'S'<<' '<<ans[j].first<<' '<<ans[j].second<<endl;
	}
	if(v.size())
		as.push_back(make_pair(v[0].first, v[0].second));
	int vs = v.size()-1;
	int oo = 0;
	for(i = 0; i < vs; i++){
//		cout<<v[i].first<<' '<<v[i].second<<endl;
		if(v[i].first == v[i+1].first){
			as[oo].second += v[i+1].second;
		}
		if(v[i].first != v[i+1].first){
			as.push_back(make_pair(v[i+1].first,v[i+1].second));
			oo++;
		}
	}
	int kk = as.size();
	if(ss > kk){
		for(j = 0; j <k; j++)
			cout<<'B'<<' '<<as[j].first<<' '<<as[j].second<<endl;
		return 0;
	}
	for(j = 0; j < min(kk,ss); j++){
		cout<<'B'<<' '<<as[j].first<<' '<<as[j].second<<endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int s,n;
int i,j,k;
int num;
struct stock{
	char d;
	int p,q;
	static int sort_by;
	bool operator<(const stock& lhs) const
	{
		if(sort_by == 1) return p > lhs.p;
		if(sort_by == 2) return q > lhs.q;
		if(sort_by == 3) return d > lhs.d;
	}
};
stock all[1003];
int stock::sort_by = 3;
int main(){
	cin>>n>>s;
	for(i=0; i<n; i++){
		cin>>all[i].d>>all[i].p>>all[i].q;
	}
	sort(all, all+n);
	for(i =0; i<n;i ++){
		if(all[i].d == 'S' && all[i+1].d == 'B')
			num = i;
	}
	stock::sort_by = 1;
//	cout<<endl<<num<<endl;
	sort(all, all+num+1);
	sort(all+num, all+n);
	for(i=0; i<num; i++){
		if(all[i].p == all[i+1].p){
			all[i].q += all[i+1].q;
			all[i+1].d = 'A';
		}
	}
	for(j=num; j<n; j++){
		if(all[j].p == all[j+1].p){
			all[j].q += all[j+1].q;
			all[j+1].d = 'A';
		}
	}
/*	cout<<endl;
	for(i=0; i<n; i++){
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
	}*/
	stock::sort_by =3;
	sort(all, all+n);
	for(i=0; i<2*s; i++){
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int ss,n;
int i,j,k;
int num;
int p,q;
std::vector<pair <int, int> > v;
std::vector<pair <int, int> > s;
std::vector<pair <int, int> > ans;
std::vector<pair <int, int> > as;
int main(){
	char d;
	cin>>n>>ss;
	for(i=0; i<n; i++){
		cin>>d>>p>>q;
		if(d == 'B'){
			v.push_back(make_pair(p,q));
		}
		if(d == 'S'){
			s.push_back(make_pair(p,q));
		}
	}
	sort(v.begin(), v.end(), greater<pair<int,int> >());
//	sort(s.begin(), s.end(), greater<pair<int,int> >());
	if(s.size())
		ans.push_back(make_pair(s[0].first, s[0].second));
	int ssize = s.size()-1;
	for(j = 0; j < ssize; j++){
//		cout<<s[j].first<<' '<<s[j].second<<endl;
		if(s[j].first == s[j+1].first){
			ans[j].second += s[j+1].second;
		}
		if(s[j].first != s[j+1].first){
			ans.push_back(make_pair(s[j+1].first,s[j+1].second));
		}
	}
//	cout<<endl<<"new s"<<endl;
	int k = ans.size();
	for(j = 0; j < min(k,ss); j++){
		cout<<'S'<<' '<<ans[j].first<<' '<<ans[j].second<<endl;
	}
	if(v.size())
		as.push_back(make_pair(v[0].first, v[0].second));
	int vs = v.size()-1;
	for(i = 0; i < vs; i++){
//		cout<<v[i].first<<' '<<v[i].second<<endl;
		if(v[i].first == v[i+1].first){
			as[i].second += v[i+1].second;
		}
		if(v[i].first != v[i+1].first){
			as.push_back(make_pair(v[i+1].first,v[i+1].second));
		}
	}
	int kk = as.size();
	for(j = 0; j < min(kk,ss); j++){
		cout<<'B'<<' '<<as[j].first<<' '<<as[j].second<<endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int s,n;
int i,j,k;
int num;
struct stock{
	char d;
	int p,q;
	static int sort_by;
	bool operator<(const stock& lhs) const
	{
		if(sort_by == 1) return p > lhs.p;
		if(sort_by == 2) return q > lhs.q;
		if(sort_by == 3) return d > lhs.d;
	}
};
stock all[1003];
int stock::sort_by = 3;
int main(){
	cin>>n>>s;
	for(i=0; i<n; i++){
		cin>>all[i].d>>all[i].p>>all[i].q;
	}
	sort(all, all+n);
	for(i =0; i<n;i ++){
		if(all[i].d == 'S' && all[i+1].d == 'B')
			num = i;
	}
	stock::sort_by = 1;
//	cout<<endl<<num<<endl;
	sort(all, all+num+1);
	sort(all+num, all+n);
	for(i=0; i<num; i++){
		if(all[i].p == all[i+1].p){
			all[i].q += all[i+1].q;
			all[i+1].d = 'A';
		}
	}
	for(j=num; j<n; j++){
		if(all[j].p == all[j+1].p){
			all[j].q += all[j+1].q;
			all[j+1].d = 'A';
		}
	}
/*	cout<<endl;
	for(i=0; i<n; i++){
		cout<<all[i].d<<' '<<all[i].p<<' '<<all[i].q<<endl;
	}*/
//		cout<<endl;
	stock::sort_by =3;
	sort(all, all+n);
	for(j = 0; j< 2*s; j++){
	if(all[j].d!='A'){
		cout<<all[j].d<<' '<<all[j].p<<' '<<all[j].q<<endl;
	}
}
	return 0;
}


import java.io.*;
import java.util.*;

public class Main {
    public static long bfs(ArrayList<ArrayList<Integer>> a, int root, boolean[] visited) {
        long count=0;
        Queue<Integer> q = new LinkedList<>();
        visited[root] = true;
        q.add(root);
        while (!q.isEmpty()) {
            int u = q.poll();
            count++;
            for (int i = 0; i < a.get(u).size(); i++) {
                if (!visited[a.get(u).get(i)]) {
                    visited[a.get(u).get(i)] = true;
                    q.add(a.get(u).get(i));
                }
            }
        }
        return count;
    }
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        String[] s = br.readLine().split(" ");
        int n = Integer.parseInt(s[0]),k=Integer.parseInt(s[1]);
        long ans = (long)Math.pow(n,k);
        ArrayList<ArrayList<Integer>> a = new ArrayList<>();
        for(int i=0;i<n;i++) a.add(new ArrayList<>());
        for(int i=0;i<n-1;i++){
            s = br.readLine().split(" ");
            int src = Integer.parseInt(s[0])-1,dest = Integer.parseInt(s[1])-1,edge = Integer.parseInt(s[2]);
            if(edge==0) {
                a.get(src).add(dest);
                a.get(dest).add(src);
            }
        }
        boolean[] visited = new boolean[n];
        for(int i=0;i<n;i++) {
            if (!visited[i]) {
                ans -= ((long) Math.pow(bfs(a, i, visited),k));
            }
        }
        pw.println(ans);
        pw.flush();
        pw.close();
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Jenish
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        ScanReader in = new ScanReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        CEdgyTrees solver = new CEdgyTrees();
        solver.solve(1, in, out);
        out.close();
    }

    static class CEdgyTrees {
        ArrayList<Integer>[] arrayList;
        boolean[] visited;
        long c = 0;
        long mod = (long) (1e9 + 7);

        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            int n = in.scanInt();
            int k = in.scanInt();
            arrayList = new ArrayList[n + 1];
            for (int i = 0; i <= n; i++) arrayList[i] = new ArrayList<>();
            for (int i = 0; i < n - 1; i++) {
                int a = in.scanInt();
                int b = in.scanInt();
                int c = in.scanInt();
                if (c == 0) {
                    arrayList[a].add(b);
                    arrayList[b].add(a);
                }
            }

            visited = new boolean[n + 1];
            long sub = 0;
            for (int i = 1; i <= n; i++) {
                if (!visited[i]) {
                    c = 0;
                    DFS(i);
                    sub = (sub + CodeX.power(c, k, mod)) % mod;
                }
            }
            long ans = CodeX.power(n, k, mod);
            ans = ans - sub;
            while (ans < 0) ans += mod;
            out.println(ans);

        }

        void DFS(int s) {
            c++;
            visited[s] = true;
            for (int j : arrayList[s]) {
                if (!visited[j]) {
                    DFS(j);
                }
            }
        }

    }

    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int INDEX;
        private BufferedInputStream in;
        private int TOTAL;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (INDEX >= TOTAL) {
                INDEX = 0;
                try {
                    TOTAL = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (TOTAL <= 0) return -1;
            }
            return buf[INDEX++];
        }

        public int scanInt() {
            int I = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    I *= 10;
                    I += n - '0';
                    n = scan();
                }
            }
            return neg * I;
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

    }

    static class CodeX {
        public static long power(long x, long y, long p) {
            long res = 1;
            x = x % p;
            while (y > 0) {
                if ((y & 1) != 0)
                    res = (res * x) % p;
                y = y >> 1;
                x = (x * x) % p;
            }
            return res;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author El-Bishoy
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        D2C1139 solver = new D2C1139();
        solver.solve(1, in, out);
        out.close();
    }

    static class D2C1139 {
        int[][] from;
        int MOD = 1000_000_000 + 7;
        int cnt;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), k = in.nextInt();

            from = new int[n + 1][n + 1];
            int nn = n;

            for (int i = 0; i <= n; i++) {
                Arrays.fill(from[i], -1);
            }

            GraphUtils.DiGraph diGraph = new GraphUtils.DiGraph(n);
            while (nn-- > 1) {
                int u = in.nextInt() - 1, v = in.nextInt() - 1, color = in.nextInt();
                from[u][v] = from[v][u] = color;
                diGraph.addEdge(u, v);
            }

            long sub = 0;
            for (int i = 0; i < n; i++) {
                if (!diGraph.visited[i]) {
                    cnt = 0;
                    dfs(diGraph, i, -1);
                    sub += (Math.pow(cnt, k)) % MOD;
                }
            }

            sub = sub % MOD;
            long all = ((long) Math.pow(n, k) % MOD - sub % MOD) % MOD;
            out.println(all);
        }

        void dfs(GraphUtils.DiGraph G, int v, int parent) {

            if (G.visited[v]) return;
            if (parent != -1)
                if (from[v][parent] == 1 || from[parent][v] == 1)
                    return;

            G.visited[v] = true;
            cnt++;
            for (int w : G.adj(v)) {
                dfs(G, w, v);
            }
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class GraphUtils {
        public static class DiGraph {
            ArrayList<Integer>[] adj;
            public boolean[] visited;

            public DiGraph(int n) {
                adj = new ArrayList[n];
                for (int i = 0; i < n; i++) {
                    adj[i] = new ArrayList<>();
                }

                visited = new boolean[n];

            }

            public void addEdge(int v, int w) {
                adj[v].add(w);
                adj[w].add(v);
            }

            public ArrayList<Integer> adj(int v) {
                return adj[v];
            }

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(long i) {
            writer.println(i);
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author El-Bishoy
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        D2C1139 solver = new D2C1139();
        solver.solve(1, in, out);
        out.close();
    }

    static class D2C1139 {
        int[][] from;
        int mod = (int) 1e9 + 7;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), k = in.nextInt();

            from = new int[n + 1][n + 1];
            int nn = n;

            for (int i = 0; i <= n; i++) {
                Arrays.fill(from[i], -1);
            }

            GraphUtils.DiGraph diGraph = new GraphUtils.DiGraph(n);
            while (nn-- > 1) {
                int u = in.nextInt() - 1, v = in.nextInt() - 1, color = in.nextInt();
                from[u][v] = from[v][u] = color;
                if (color != 1)
                    diGraph.addEdge(u, v);
            }

            int all = MathUtils.Mathematics.pow(n, k, mod);
            for (int i = 0; i < n; i++) {
                int cnt = dfs(diGraph, i, -1);
                all = MathUtils.Mathematics.mod(all - MathUtils.Mathematics.pow(cnt, k, mod), mod);

            }

            out.println(all);
        }

        int dfs(GraphUtils.DiGraph G, int v, int parent) {

            if (G.visited[v]) return 0;
//        if (parent != -1)
//            if (from[v][parent] == 1 || from[parent][v] == 1)
//                return 0;

            G.visited[v] = true;
            int count = 1;
            for (int w : G.adj(v)) {
                count += dfs(G, w, v);
            }

            return count;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class MathUtils {
        public static class Mathematics {
            public static int pow(int x, int n, int mod) {
                int bit = 31 - Integer.numberOfLeadingZeros(n);
                long product = 1;
                for (; bit >= 0; bit--) {
                    product = product * product % mod;
                    if (((1 << bit) & n) != 0) {
                        product = product * x % mod;
                    }
                }
                return (int) product;
            }

            public static int mod(int x, int mod) {
                x %= mod;
                if (x < 0) {
                    x += mod;
                }
                return x;
            }

        }

    }

    static class GraphUtils {
        public static class DiGraph {
            ArrayList<Integer>[] adj;
            public boolean[] visited;

            public DiGraph(int n) {
                adj = new ArrayList[n];
                for (int i = 0; i < n; i++) {
                    adj[i] = new ArrayList<>();
                }

                visited = new boolean[n];

            }

            public void addEdge(int v, int w) {
                adj[v].add(w);
                adj[w].add(v);
            }

            public ArrayList<Integer> adj(int v) {
                return adj[v];
            }

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author El-Bishoy
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        D2C1139 solver = new D2C1139();
        solver.solve(1, in, out);
        out.close();
    }

    static class D2C1139 {
        int mod = (int) 1e9 + 7;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), k = in.nextInt();

            int nn = n;

            GraphUtils.DiGraph diGraph = new GraphUtils.DiGraph(n);
            while (nn-- > 1) {
                int u = in.nextInt() - 1, v = in.nextInt() - 1, color = in.nextInt();
                if (color != 1)
                    diGraph.addEdge(u, v);
            }

            int all = MathUtils.Mathematics.pow(n, k, mod);
            for (int i = 0; i < n; i++) {
                int cnt = dfs(diGraph, i, -1);
                all = MathUtils.Mathematics.mod(all - MathUtils.Mathematics.pow(cnt, k, mod), mod);

            }

            out.println(all);
        }

        int dfs(GraphUtils.DiGraph G, int v, int parent) {

            if (G.visited[v]) return 0;
//        if (parent != -1)
//            if (from[v][parent] == 1 || from[parent][v] == 1)
//                return 0;

            G.visited[v] = true;
            int count = 1;
            for (int w : G.adj(v)) {
                count += dfs(G, w, v);
            }

            return count;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class MathUtils {
        public static class Mathematics {
            public static int pow(int x, int n, int mod) {
                int bit = 31 - Integer.numberOfLeadingZeros(n);
                long product = 1;
                for (; bit >= 0; bit--) {
                    product = product * product % mod;
                    if (((1 << bit) & n) != 0) {
                        product = product * x % mod;
                    }
                }
                return (int) product;
            }

            public static int mod(int x, int mod) {
                x %= mod;
                if (x < 0) {
                    x += mod;
                }
                return x;
            }

        }

    }

    static class GraphUtils {
        public static class DiGraph {
            ArrayList<Integer>[] adj;
            public boolean[] visited;

            public DiGraph(int n) {
                adj = new ArrayList[n];
                for (int i = 0; i < n; i++) {
                    adj[i] = new ArrayList<>();
                }

                visited = new boolean[n];

            }

            public void addEdge(int v, int w) {
                adj[v].add(w);
                adj[w].add(v);
            }

            public ArrayList<Integer> adj(int v) {
                return adj[v];
            }

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author El-Bishoy
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        D2C1139 solver = new D2C1139();
        solver.solve(1, in, out);
        out.close();
    }

    static class D2C1139 {
        int[][] from;
        int cnt;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), k = in.nextInt();

            from = new int[n + 1][n + 1];
            int nn = n;

            for (int i = 0; i <= n; i++) {
                Arrays.fill(from[i], -1);
            }

            GraphUtils.DiGraph diGraph = new GraphUtils.DiGraph(n);
            while (nn-- > 1) {
                int u = in.nextInt() - 1, v = in.nextInt() - 1, color = in.nextInt();
                from[u][v] = from[v][u] = color;
                diGraph.addEdge(u, v);
            }

            int sub = 0;
            for (int i = 0; i < n; i++) {
                if (!diGraph.visited[i]) {
                    cnt = 0;
                    dfs(diGraph, i, -1);
                    sub += Math.pow(cnt, k);
                }
            }

            out.println((int) Math.pow(n, k) - sub);
        }

        void dfs(GraphUtils.DiGraph G, int v, int parent) {

            if (G.visited[v]) return;
            if (parent != -1)
                if (from[v][parent] == 1 || from[parent][v] == 1)
                    return;

            G.visited[v] = true;
            cnt++;
            for (int w : G.adj(v)) {
                dfs(G, w, v);
            }
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class GraphUtils {
        public static class DiGraph {
            ArrayList<Integer>[] adj;
            public boolean[] visited;

            public DiGraph(int n) {
                adj = new ArrayList[n];
                for (int i = 0; i < n; i++) {
                    adj[i] = new ArrayList<>();
                }

                visited = new boolean[n];

            }

            public void addEdge(int v, int w) {
                adj[v].add(w);
                adj[w].add(v);
            }

            public ArrayList<Integer> adj(int v) {
                return adj[v];
            }

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }
}