#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <math.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
	int n, s, bs = 0;

	cin >> n >> s;

	char* d = new char[n];

	std::vector<std::pair<int,int>> pq;

    //std::sort(baryProc.begin(),baryProc.end());
	int p;
	int q;

	for (int i = 0; i < n; ++i)
	{
		cin >> d[i] >> p >> q;
		pq.push_back(std::make_pair(p, q));

		if (d[i] == 'B')
		{
			++bs;
		}
	}

	std::sort(pq.begin(), pq.end());

	int s1 = s;
	for (int i = n - 1; i >= bs; --i)
	{
		int curr_p = pq[i].first;
		int curr_q = pq[i].second;
		for (int j = i - 1; j >= bs; --j)
		{
			if (pq[j].first == curr_p)
			{
				curr_q += pq[j].second;
			}
			else
			{
				++j;
				i = j;
				break;
			}
		}

		cout << "S " << curr_p << " " << curr_q;

		--s1;
		if (s1 == 0)
		{
			if (bs == 0)
				break;
			else
			{
				cout << endl;
				break;
			}
		}
		else {
			if (i == 0)
			{
				break;
			}
			cout << endl;
		}

	}

	int s2 = s;
	for (int i = bs - 1; i >= 0; --i)
	{
		int curr_p = pq[i].first;
		int curr_q = pq[i].second;
		for (int j = i - 1; j >= 0; --j)
		{
			if (pq[j].first == curr_p)
			{
				curr_q += pq[j].second;
			}
			else
			{
				++j;
				i = j;
				break;
			}
		}

		cout << "B " << curr_p << " " << curr_q;

		--s2;
		if (s2 == 0 || i == 0)
		{
			break;
		}
		cout << endl;
	}

    return 0;
}


#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <math.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
        int n, s, bs = 0;

        cin >> n >> s;

		char d;

        std::vector<std::pair<int,int>> pq;

        int p, q;

        for (int i = 0; i < n; ++i)
        {
                cin >> d >> p >> q;
                pq.push_back(std::make_pair(p, q));

                if (d == 'B')
                {
                        ++bs;
                }
        }

        std::sort(pq.begin(), pq.end());

        int s1 = s;
		vector<pair<int, int>> aggregated;
		for (int i = n - 1; i >= bs; --i)
		{
			int curr_p = pq[i].first;
			int curr_q = pq[i].second;
			for (int j = i - 1; j >= bs; --j)
			{
				if (pq[j].first == curr_p)
				{
					curr_q += pq[j].second;
				}
				else
				{
					i = j + 1;
					break;
				}
				if (j == bs)
				{
					i = bs;
				}
			}
			aggregated.push_back(make_pair(curr_p, curr_q));
		}

		int x;
		if (s > aggregated.size())
		{
			x = 0;
		}
		else
		{
			x = aggregated.size() - s;
		}

        for (int i = x; i < aggregated.size(); ++i)
        {
                cout << "S " << aggregated[i].first << " " << aggregated[i].second;

				if (i == aggregated.size() - 1 && bs == 0)
				{
					break;
				}
				cout << endl;
        }

        int s2 = s;
        for (int i = bs - 1; i >= 0; --i)
        {
                int curr_p = pq[i].first;
                int curr_q = pq[i].second;
                for (int j = i - 1; j >= 0; --j)
                {
                        if (pq[j].first == curr_p)
                        {
                                curr_q += pq[j].second;
                        }
                        else
                        {
                                i = j + 1;
                                break;
                        }
                        if (j == 0)
                        {
                                i = 0;
                        }
                }

                cout << "B " << curr_p << " " << curr_q;

                --s2;
                if (s2 == 0 || i == 0)
                {
                        break;
                }
                cout << endl;
        }

    return 0;
}


#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <math.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
        int n, s, bs = 0;

        cin >> n >> s;

		char d;

        std::vector<std::pair<int,int>> pq;

        int p, q;

        for (int i = 0; i < n; ++i)
        {
                cin >> d >> p >> q;
                pq.push_back(std::make_pair(p, q));

                if (d == 'B')
                {
                        ++bs;
                }
        }

        std::stable_sort(pq.begin(), pq.end());

        int s1 = s;
        for (int i = n - 1; i >= bs; --i)
        {
                int curr_p = pq[i].first;
                int curr_q = pq[i].second;
                for (int j = i - 1; j >= bs; --j)
                {
                        if (pq[j].first == curr_p)
                        {
                                curr_q += pq[j].second;
                        }
                        else
                        {
                                i = j + 1;
                                break;
                        }
                        if (j == bs)
                        {
                                i = bs;
                        }
                }

                cout << "S " << curr_p << " " << curr_q;

                --s1;
                if (s1 == 0)
                {
                        if (bs == 0)
                                break;
                        else
                        {
                                cout << endl;
                                break;
                        }
                }
                else
				{
                        if (i == bs)
                        {
                                break;
                        }
                        cout << endl;
                }

        }

        int s2 = s;
        for (int i = bs - 1; i >= 0; --i)
        {
                int curr_p = pq[i].first;
                int curr_q = pq[i].second;
                for (int j = i - 1; j >= 0; --j)
                {
                        if (pq[j].first == curr_p)
                        {
                                curr_q += pq[j].second;
                        }
                        else
                        {
                                i = j + 1;
                                break;
                        }
                        if (j == 0)
                        {
                                i = 0;
                        }
                }

                cout << "B " << curr_p << " " << curr_q;

                --s2;
                if (s2 == 0 || i == 0)
                {
                        break;
                }
                cout << endl;
        }

    return 0;
}


#include<iostream>
using namespace std;
#include<map>
map<long long int ,int>mp1;
map<long long int ,int>mp2;
#include<queue>
priority_queue<long long int>qu;
priority_queue<long long int>qu2;
    int main(){

long long int n,s,k,m;
char ch;
cin>>n>>s;
for(int i=0;i<n;i++){
cin>>ch>>k>>m;
if(ch=='B'){
    if(mp1[k]==0)
        qu.push(k);
mp1[k]+=m;
}
else{
if(mp2[k]==0)
        qu2.push(k);
mp2[k]+=m;

}
}
int counter=0;
long long int sum=0;
while(counter<s&&!qu2.empty()){
    cout<<"S"<<" "<<qu2.top()<<" "<<mp2[qu2.top()]<<endl;
    sum+=qu2.top()*mp2[qu2.top()];
    qu2.pop();
counter++;
}
 counter=0;
while(counter<s&&!qu.empty()){
    if(qu.top()*mp1[qu.top()]>sum){
    qu.pop();
    continue;
    }
    cout<<"B"<<" "<<qu.top()<<" "<<mp1[qu.top()]<<endl;
    sum-=qu.top()*mp1[qu.top()];
counter++;
}
return 0;
}


#include<iostream>
using namespace std;
#include<map>
map<long long int ,int>mp1;
map<long long int ,int>mp2;
#include<queue>
priority_queue<long long int>qu;
priority_queue<long long int>qu2;
    int main(){

long long int n,s,k,m;
char ch;
cin>>n>>s;
for(int i=0;i<n;i++){
cin>>ch>>k>>m;
if(ch=='B'){
    if(mp1[k]==0)
        qu.push(k);
mp1[k]+=m;
}
else{
if(mp2[k]==0)
        qu2.push(k);
mp2[k]+=m;

}
}
int counter=0;
long long int sum=0;
while(counter<s&&!qu2.empty()){
    if(qu2.size()<=s){
    cout<<"S"<<" "<<qu2.top()<<" "<<mp2[qu2.top()]<<endl;
    qu2.pop();
    counter++;}
    else
        qu2.pop();
}
 counter=0;
while(counter<s&&!qu.empty()){

    cout<<"B"<<" "<<qu.top()<<" "<<mp1[qu.top()]<<endl;
    qu.pop();
counter++;
}
return 0;
}


#include<iostream>
using namespace std;
#include<map>
map<long long int ,int>mp1;
map<long long int ,int>mp2;
#include<queue>
priority_queue<long long int>qu;
priority_queue<long long int>qu2;
    int main(){

long long int n,s,k,m;
char ch;
cin>>n>>s;
for(int i=0;i<n;i++){
cin>>ch>>k>>m;
if(ch=='B'){
    if(mp1[k]==0)
        qu.push(k);
mp1[k]+=m;
}
else{
if(mp2[k]==0)
        qu2.push(k);
mp2[k]+=m;

}
}
int counter=0;

while(counter<s&&!qu2.empty()){
    cout<<"S"<<" "<<qu2.top()<<" "<<mp2[qu2.top()]<<endl;
    qu2.pop();
counter++;
}
 counter=0;
while(counter<s&&!qu.empty()){
    cout<<"B"<<" "<<qu.top()<<" "<<mp1[qu.top()]<<endl;
counter++;
qu.pop();
}
return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
struct node
{
	int p,q;
};
node buy[1005];
node sell[1005];
int cmp(node  &a,node &b)
{
	return a.p<b.p;
}
int main()
{
	char op;
	int n,s,i,q,p;
	int ok1=0;
	int ok2=0;
	scanf("%d%d",&n,&s);getchar();
	for (i=1;i<=n;i++)
	{
		scanf("%c %d %d",&op,&p,&q);
		getchar();
		if (op=='B')
		{
			buy[++ok1].p=p;
			buy[ok1].q=q;
		}
		else
		{
			sell[++ok2].p=p;
			sell[ok2].q=q;
		}
	}

	sort(buy+1,buy+1+ok1,cmp);
	sort(sell+1,sell+1+ok2,cmp);
	int ok=0;
	buy[0].p=-1;
	for (i=1;i<=ok1;i++)
	{
		if (buy[i].p==buy[i-1].p)
			buy[ok].q+=buy[i].q;
		else
			buy[++ok]=buy[i];
	}
	ok1=ok;

	ok=0;
	sell[0].p=-1;
	for (i=1;i<=ok2;i++)
	{
		if (sell[i].p==sell[i-1].p)
			sell[ok].q+=sell[i].q;
		else
			sell[++ok]=sell[i];
	}
	ok2=ok;
	for (i=__min(ok2,s);i>=1 ;i--)
		printf("S %d %d\n",sell[i].p,sell[i].q);

	for (i=ok1;i>=1&&s--;i--)
		printf("B %d %d\n",buy[i].p,buy[i].q);

	return 0;

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
struct node
{
	int p,q;
};
node buy[1005];
node sell[1005];
int cmp(node  &a,node &b)
{
	return a.p<b.p;
}
int main()
{
	char op;
	int n,s,i,q,p;
	int ok1=0;
	int ok2=0;
	scanf("%d%d",&n,&s);getchar();
	for (i=1;i<=n;i++)
	{
		scanf("%c %d %d",&op,&p,&q);
		getchar();
		if (op=='B')
		{
			buy[++ok1].p=p;
			buy[ok1].q=q;
		}
		else
		{
			sell[++ok2].p=p;
			sell[ok2].q=q;
		}
	}

	sort(buy+1,buy+1+ok1,cmp);
	sort(sell+1,sell+1+ok2,cmp);
	int ok=0;
		buy[++ok]=buy[1];
	for (i=2;i<=ok1;i++)
	{
		if (buy[i].p==buy[i-1].p)
			buy[ok].q+=buy[i].q;
		else
			buy[++ok]=buy[i];
	}
	ok1=ok;

	ok=0;
		sell[++ok]=sell[1];
	for (i=2;i<=ok2;i++)
	{
		if (sell[i].p==sell[i-1].p)
			sell[ok].q+=sell[i].q;
		else
			sell[++ok]=sell[i];
	}
	ok2=ok;
	for (i=__min(ok2,s);i>=1 ;i--)
		printf("S %d %d\n",sell[i].p,sell[i].q);

	for (i=ok1;i>=1&&s--;i--)
		printf("B %d %d\n",buy[i].p,buy[i].q);

	return 0;

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
struct node
{
	int p,q;
};
node buy[1005];
node sell[1005];
int cmp(node  &a,node &b)
{
	return a.p<b.p;
}
int main()
{
	char op;
	int n,s,i,q,p;
	int ok1=0;
	int ok2=0;
	scanf("%d%d",&n,&s);getchar();
	for (i=1;i<=n;i++)
	{
		scanf("%c %d %d",&op,&p,&q);
		getchar();
		if (op=='B')
		{
			buy[++ok1].p=p;
			buy[ok1].q=q;
		}
		else
		{
			sell[++ok2].p=p;
			sell[ok2].q=q;
		}
	}

	sort(buy+1,buy+1+ok1,cmp);
	sort(sell+1,sell+1+ok2,cmp);
	int ok=0;
		buy[++ok]=buy[1];
	for (i=2;i<=ok1;i++)
	{
		if (buy[i].p==buy[i-1].p)
			buy[ok].q+=buy[i].q;
		else
			buy[++ok]=buy[i];
	}
	ok1=ok;

	ok=0;
		sell[++ok]=sell[1];
	for (i=2;i<=ok2;i++)
	{
		if (sell[i].p==sell[i-1].p)
			sell[ok].q+=sell[i].q;
		else
			sell[++ok]=sell[i];
	}
	ok2=ok;
	int tmp=s;
	for (i=ok2;i>=1&&tmp--;i--)
		printf("S %d %d\n",sell[i].p,sell[i].q);
	tmp=s;
	for (i=ok1;i>=1&&tmp--;i--)
		printf("S %d %d\n",buy[i].p,buy[i].q);

	return 0;

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
struct node
{
	int p,q;
};
node buy[1005];
node sell[1005];
int cmp(node  &a,node &b)
{
	return a.p<b.p;
}
int main()
{
	char op;
	int n,s,i,q,p;
	int ok1=0;
	int ok2=0;
	scanf("%d%d",&n,&s);getchar();
	for (i=1;i<=n;i++)
	{
		scanf("%c %d %d",&op,&p,&q);
		getchar();
		if (op=='B')
		{
			buy[++ok1].p=p;
			buy[ok1].q=q;
		}
		else
		{
			sell[++ok2].p=p;
			sell[ok2].q=q;
		}
	}

	sort(buy+1,buy+1+ok1,cmp);
	sort(sell+1,sell+1+ok2,cmp);
	int ok=0;
		buy[++ok]=buy[1];
	for (i=2;i<=ok1;i++)
	{
		if (buy[i].p==buy[i-1].p)
			buy[ok].q+=buy[i].q;
		else
			buy[++ok]=buy[i];
	}
	ok1=ok;

	ok=0;
		sell[++ok]=sell[1];
	for (i=2;i<=ok2;i++)
	{
		if (sell[i].p==sell[i-1].p)
			sell[ok].q+=sell[i].q;
		else
			sell[++ok]=sell[i];
	}
	ok2=ok;
	int tmp=s;
	for (i=ok2;i>=1&&tmp--;i--)
		printf("S %d %d\n",sell[i].p,sell[i].q);
	tmp=s;
	for (i=ok1;i>=1&&tmp--;i--)
		printf("B %d %d\n",buy[i].p,buy[i].q);

	return 0;

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
struct node
{
	int p,q;
};
node buy[1005];
node sell[1005];
int cmp(node  &a,node &b)
{
	return a.p<b.p;
}
int main()
{
	char op;
	int n,s,i,q,p;
	int ok1=0;
	int ok2=0;
	scanf("%d%d",&n,&s);getchar();
	for (i=1;i<=n;i++)
	{
		scanf("%c %d %d",&op,&p,&q);
		getchar();
		if (op=='B')
		{
			buy[++ok1].p=p;
			buy[ok1].q=q;
		}
		else
		{
			sell[++ok2].p=p;
			sell[ok2].q=q;
		}
	}

	sort(buy+1,buy+1+ok1,cmp);
	sort(sell+1,sell+1+ok2,cmp);
	int ok=0;
	for (i=1;i<=ok1;i++)
	{
		if (buy[i].p==buy[i-1].p)
			buy[ok].q+=buy[i].q;
		else
			buy[++ok]=buy[i];
	}
	ok1=ok;

	ok=0;
	for (i=1;i<=ok2;i++)
	{
		if (sell[i].p==sell[i-1].p)
			sell[ok].q+=sell[i].q;
		else
			sell[++ok]=sell[i];
	}
	ok2=ok;
	for (i=__min(ok2,s);i>=1 ;i--)
		printf("S %d %d\n",sell[i].p,sell[i].q);

	for (i=ok1;i>=1&&s--;i--)
		printf("B %d %d\n",buy[i].p,buy[i].q);

	return 0;

}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct order
{
	int pos;
	char d;
	int p;
	int q;
	bool is;
};

int main()
{
	int n, s;
	cin >> n >> s;
	vector <int> S(100001);
	vector <int> B(100001);
	for (int i = 0; i < n; ++i)
	{
		char d;
		int p, q;
		cin >> d >> p >> q;
		if (d == 'S')
			S[p] += q;
		else
			B[p] += q;
	}

	int count = 0;
	int i = 0;
	for (i = 0; i < 100001; ++i)
	{
		if (S[i] != 0)
			count++;
		if (count == s)
			break;
	}
	for (; i >= 0; --i)
	{
		if (S[i] != 0)
			cout << 'S' << ' ' << i << ' ' << S[i] << endl;
	}
	count = 0;
	for (i = 100000; i >= 0; --i)
	{

		if (B[i] != 0)
		{
			cout << 'B' << ' ' << i << ' ' << B[i] << endl;
			count++;
		}
		if (count == s)
			break;
	}
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct order
{
	int pos;
	char d;
	int p;
	int q;
	bool is;
};

int main()
{
	int n, s;
	cin >> n >> s;
	vector <int> S(100001);
	vector <int> B(100001);
	for (int i = 0; i < n; ++i)
	{
		char d;
		int p, q;
		cin >> d >> p >> q;
		if (d == 'S')
			S[p] += q;
		else
			B[p] += q;
	}

	int count = 0;
	for (int i = 100000; i >= 0; --i)
	{

		if (S[i] != 0)
		{
			cout << 'S' << ' ' << i << ' ' << S[i] << endl;
			count++;
		}
		if (count == s)
			break;
	}
	count = 0;
	for (int i = 100000; i >= 0; --i)
	{

		if (B[i] != 0)
		{
			cout << 'B' << ' ' << i << ' ' << B[i] << endl;
			count++;
		}
		if (count == s)
			break;
	}
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct order
{
	int pos;
	char d;
	int p;
	int q;
	bool is;
};

bool great(order& or1, order& or2)
{
	if (or1.d == 'S' && or2.d == 'S')
		return or1.q < or2.q;
	if (or1.d == 'B' && or2.d == 'B')
		return or1.q > or2.q;
	if (or1.d == 'B' && or2.d == 'S')
		return false;
	return true;
}

bool posit(order& or1, order& or2)
{
	return or1.q > or2.q;
}

int main()
{
	int n, s;
	cin >> n >> s;
	vector <order> v(n);
	for (int i = 0; i < n; ++i)
	{
		cin >> v[i].d >> v[i].p >> v[i].q;
		v[i].is = false;
		v[i].pos = i;
	}

	vector <order> ans;

	for (int i = 0; i < n; ++i)
	{
		if (!v[i].is)
		{
			v[i].is = true;
			ans.push_back(v[i]);
			for (int j = i; j < n; ++j)
			{
				if (v[j].d == ans[ans.size() - 1].d && v[j].p == ans[ans.size() - 1].p && v[j].is == false)
				{
					v[j].is = true;
					ans[ans.size() - 1].q += v[i].q;
				}
			}
		}
	}
	vector <order> B, S, newS, newB;
	for (int i = 0; i < ans.size(); i++)
	{
		if (ans[i].d == 'S')
			S.push_back(ans[i]);
		if (ans[i].d == 'B')
			B.push_back(ans[i]);
	}
	sort(B.begin(), B.end(), great);
	sort(S.begin(), S.end(), great);

	for (int i = 0; i < S.size() && i < s; ++i)
		newS.push_back(S[i]);
	for (int i = 0; i < B.size() && i < s; ++i)
		newB.push_back(B[i]);

	sort(newS.begin(), newS.end(), posit);
	sort(newB.begin(), newB.end(), posit);

	for (int i = 0; i < newS.size(); ++i)
		cout << newS[i].d << ' ' << newS[i].p << ' ' << newS[i].q << endl;
	for (int i = 0; i < newB.size(); ++i)
		cout << newB[i].d << ' ' << newB[i].p << ' ' << newB[i].q << endl;

}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct order
{
	int pos;
	char d;
	int p;
	int q;
	bool is;
};

bool great(order& or1, order& or2)
{
	if (or1.d == 'S' && or2.d == 'S')
		return or1.q < or2.q;
	if (or1.d == 'B' && or2.d == 'B')
		return or1.q > or2.q;
	if (or1.d == 'B' && or2.d == 'S')
		return false;
	return true;
}

bool posit(order& or1, order& or2)
{
	return or1.q > or2.q;
}

int main()
{
	int n, s;
	cin >> n >> s;
	vector <order> v(n);
	for (int i = 0; i < n; ++i)
	{
		cin >> v[i].d >> v[i].p >> v[i].q;
		v[i].is = false;
		v[i].pos = i;
	}

	vector <order> ans;

	for (int i = 0; i < n; ++i)
	{
		if (!v[i].is)
		{
			v[i].is = true;
			ans.push_back(v[i]);
			for (int j = i + 1; j < n; ++j)
			{
				if (v[j].d == ans[ans.size() - 1].d && v[j].p == ans[ans.size() - 1].p && v[j].is == false)
				{
					v[j].is = true;
					ans[ans.size() - 1].q += v[j].q;
				}
			}
		}
	}
	vector <order> B, S, newS, newB;
	for (int i = 0; i < ans.size(); i++)
	{
		if (ans[i].d == 'S')
			S.push_back(ans[i]);
		if (ans[i].d == 'B')
			B.push_back(ans[i]);
	}
	sort(B.begin(), B.end(), great);
	sort(S.begin(), S.end(), great);

	for (int i = 0; i < S.size() && i < s; ++i)
		newS.push_back(S[i]);
	for (int i = 0; i < B.size() && i < s; ++i)
		newB.push_back(B[i]);

	sort(newS.begin(), newS.end(), posit);
	sort(newB.begin(), newB.end(), posit);

	for (int i = 0; i < newS.size(); ++i)
		cout << newS[i].d << ' ' << newS[i].p << ' ' << newS[i].q << endl;
	for (int i = 0; i < newB.size(); ++i)
		cout << newB[i].d << ' ' << newB[i].p << ' ' << newB[i].q << endl;

}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct order
{
	char d;
	int p;
	int q;
	bool is;
};

bool great(order& or1, order& or2)
{
	if (or1.d == 'S' && or2.d == 'S')
		return or1.q < or2.q;
	if (or1.d == 'B' && or2.d == 'B')
		return or1.q > or2.q;
	if (or1.d == 'B' && or2.d == 'S')
		return false;
	return true;
}

int main()
{
	int n, s;
	cin >> n >> s;
	vector <order> v(n);
	for (int i = 0; i < n; ++i)
	{
		cin >> v[i].d >> v[i].p >> v[i].q;
		v[i].is = false;
	}

	vector <order> ans;

	for (int i = 0; i < n; ++i)
	{
		if (!v[i].is)
		{
			v[i].is = true;
			ans.push_back(v[i]);
			for (int j = i; j < n; ++j)
			{
				if (v[j].d == ans[ans.size() - 1].d && v[j].p == ans[ans.size() - 1].p && v[j].is == false)
				{
					v[j].is = true;
					ans[ans.size() - 1].q += v[i].q;
				}
			}
		}
	}
	vector <order> B, S;
	for (int i = 0; i < ans.size(); i++)
	{
		if (ans[i].d == 'S')
			S.push_back(ans[i]);
		if (ans[i].d == 'B')
			B.push_back(ans[i]);
	}
	sort(B.begin(), B.end(), great);
	sort(S.begin(), S.end(), great);
	for (int i = 0; i < S.size() && i < s; ++i)
		cout << S[i].d << ' ' << S[i].p << ' ' << S[i].q << endl;
	for (int i = 0; i < B.size() && i < s; ++i)
		cout << B[i].d << ' ' << B[i].p << ' ' << B[i].q << endl;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
#include <memory.h>

#include <time.h>
#pragma comment(linker, "/STACK:100000000")
using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long
#define sz(x) (int)(x).size()

int buy[100100];
int sell[100100];

int main() {
    //freopen("input.txt","rt",stdin);
    //freopen("intersection.out","wt",stdout);

	int n, s;
	scanf("%d %d", &n, &s);
	for(int i = 0; i < n; i++) {
		string s;
		cin >> s;
		int p, q;
		scanf("%d %d", &p, &q);
		if(s == "B") buy[p] += q;
		else sell[p] += q;
	}
	int s1 = s;
	for(int i = 100000; i >= 0; i--) {
		if(sell[i] != 0 && s1) {
			s1--;
			printf("S %d %d\n", i, sell[i]);
		}
	}
	int s2 = s;
	for(int i = 0; i <= 100000; i++) {
		if(buy[i] != 0 && s2) {
			s2--;
			printf("B %d %d\n", i, buy[i]);
		}
	}

    return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
#include <memory.h>

#include <time.h>
#pragma comment(linker, "/STACK:100000000")
using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long
#define sz(x) (int)(x).size()

int buy[100100];
int sell[100100];

int main() {
    //freopen("input.txt","rt",stdin);
    //freopen("intersection.out","wt",stdout);

	int n, s;
	scanf("%d %d", &n, &s);
	for(int i = 0; i < n; i++) {
		string s;
		cin >> s;
		int p, q;
		scanf("%d %d", &p, &q);
		if(s == "B") buy[p] += q;
		else sell[p] += q;
	}
	int s1 = s;
	for(int i = 100000; i >= 0; i--) {
		if(sell[i] != 0 && s1) {
			s1--;
			printf("S %d %d\n", i, sell[i]);
		}
	}
	int s2 = s;
	for(int i = 100000; i >= 0; i--) {
		if(buy[i] != 0 && s2) {
			s2--;
			printf("B %d %d\n", i, buy[i]);
		}
	}

    return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
#include <memory.h>

#include <time.h>
#pragma comment(linker, "/STACK:100000000")
using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long
#define sz(x) (int)(x).size()

int buy[100100];
int sell[100100];

int main() {
    //freopen("input.txt","rt",stdin);
    //freopen("intersection.out","wt",stdout);

	int n, s;
	scanf("%d %d", &n, &s);
	int cnt = 0;
	for(int i = 0; i < n; i++) {
		string s;
		cin >> s;
		int p, q;
		scanf("%d %d", &p, &q);
		if(s == "S" && sell[p] == 0) cnt++;
		if(s == "B") buy[p] += q;
		else sell[p] += q;
	}
	for(int i = 100000; i >= 0; i--) {
		if(sell[i] != 0) {
			if(cnt <= s) printf("S %d %d\n", i, sell[i]);
			cnt--;
		}
	}
	int s2 = s;
	for(int i = 100000; i >= 0; i--) {
		if(buy[i] != 0 && s2) {
			s2--;
			printf("B %d %d\n", i, buy[i]);
		}
	}

    return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
#include <memory.h>

#include <time.h>
#pragma comment(linker, "/STACK:100000000")
using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long
#define sz(x) (int)(x).size()

int buy[100100];
int sell[100100];

int main() {
    //freopen("input.txt","rt",stdin);
    //freopen("intersection.out","wt",stdout);

	int n, s;
	scanf("%d %d", &n, &s);
	for(int i = 0; i < n; i++) {
		string s;
		cin >> s;
		int p, q;
		scanf("%d %d", &p, &q);
		if(s == "B") buy[p] += q;
		else sell[p] += q;
	}
	int s1 = s;
	for(int i = 100000; i >= 0; i--) {
		if(buy[i] != 0 && s1) {
			s1--;
			printf("B %d %d\n", i, buy[i]);
		}
	}
	int s2 = s;
	for(int i = 0; i <= 100000; i++) {
		if(sell[i] != 0 && s2) {
			s2--;
			printf("S %d %d\n", i, sell[i]);
		}
	}

    return 0;
}


#include <map>
#include <iostream>
using namespace std;

int main()
{
	int								n;
	int								s;
	map<int, int>					sellAggrOrders;
	map<int, int>					BuyAggrOrders;
	char							chDirection;
	int								iPrice;
	int								iVolume;
	map<int, int>::reverse_iterator	it;

	cin >> n >> s;

	for (int i = 0; i < n; ++i)
	{
		cin >> chDirection >> iPrice >> iVolume;

		if (chDirection == 'S')
			sellAggrOrders[iPrice] += iVolume;
		else
			BuyAggrOrders[iPrice] += iVolume;
	}

	for (int i = 0; i < s; ++i)
	{
		cout << 'S' << " " << sellAggrOrders.rbegin()->first <<  " " << sellAggrOrders.rbegin()->second << endl;
		it = sellAggrOrders.rbegin();
		sellAggrOrders.erase( --(it.base()));
	}

	for (int i = 0; i < s; ++i)
	{
		cout << 'B' << " " << BuyAggrOrders.rbegin()->first <<  " " << BuyAggrOrders.rbegin()->second << endl;
		it = BuyAggrOrders.rbegin();
		BuyAggrOrders.erase( --(it.base()));
	}

	return 0;
}


#include <map>
#include <iostream>
#include <stack>
#include <utility>
using namespace std;

int main()
{
	int								n;
	int								s;
	map<int, int>					sellAggrOrders;
	map<int, int>					BuyAggrOrders;
	char							chDirection;
	int								iPrice;
	int								iVolume;
	map<int, int>::reverse_iterator	it;
	stack<pair<int, int> >			sellStackOrders;

	cin >> n >> s;

	for (int i = 0; i < n; ++i)
	{
		cin >> chDirection >> iPrice >> iVolume;

		if (chDirection == 'S')
			sellAggrOrders[iPrice] += iVolume;
		else
			BuyAggrOrders[iPrice] += iVolume;
	}

	for (int i = 0; ((i < s) && sellAggrOrders.empty() == false); ++i)
	{
		sellStackOrders.push(pair<int, int>(sellAggrOrders.begin()->first, sellAggrOrders.begin()->second));
		sellAggrOrders.erase(sellAggrOrders.begin());
	}

	while (sellStackOrders.empty() == false)
	{
		cout << 'S' << " " << sellStackOrders.top().first <<  " " << sellStackOrders.top().second << endl;
		sellStackOrders.pop();
	}

	for (int i = 0; ((i < s) && BuyAggrOrders.empty() == false); ++i)
	{
		cout << 'B' << " " << BuyAggrOrders.rbegin()->first <<  " " << BuyAggrOrders.rbegin()->second << endl;
		it = BuyAggrOrders.rbegin();
		BuyAggrOrders.erase( --(it.base()));
	}

	return 0;
}


#include <map>
#include <iostream>
using namespace std;

int main()
{
	int								n;
	int								s;
	map<int, int>					sellAggrOrders;
	map<int, int>					BuyAggrOrders;
	char							chDirection;
	int								iPrice;
	int								iVolume;
	map<int, int>::reverse_iterator	it;

	cin >> n >> s;

	for (int i = 0; i < n; ++i)
	{
		cin >> chDirection >> iPrice >> iVolume;

		if (chDirection == 'S')
			sellAggrOrders[iPrice] += iVolume;
		else
			BuyAggrOrders[iPrice] += iVolume;
	}

	for (int i = 0; ((i < s) && sellAggrOrders.empty() == false); ++i)
	{
		cout << 'S' << " " << sellAggrOrders.rbegin()->first <<  " " << sellAggrOrders.rbegin()->second << endl;
		it = sellAggrOrders.rbegin();
		sellAggrOrders.erase( --(it.base()));
	}

	for (int i = 0; ((i < s) && BuyAggrOrders.empty() == false); ++i)
	{
		cout << 'B' << " " << BuyAggrOrders.rbegin()->first <<  " " << BuyAggrOrders.rbegin()->second << endl;
		it = BuyAggrOrders.rbegin();
		BuyAggrOrders.erase( --(it.base()));
	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<queue>
#include<map>

using namespace std;

int main()
{
    int n, s;
    map<int, int> mapii;
    cin >> n >> s;
    s *= 2;
    for (int i = 0; i < n; i++)
    {
        char d;int p, q;
        cin >> d >> p >> q;
        mapii[(2 * (d != 66) - 1) * 100000 + p] += q;
    }
    for (map<int, int>::reverse_iterator it = mapii.rbegin(); it != mapii.rend()&&s--; it++)
    {
        char ch2[] = { 'B', 'S' };
        cout << ch2[(it->first)>0] << " " << it->first + (2*(it->first < 0)-1) * 100000 << " " << it->second<<endl;

    }
    //map<int, int>::reverse_iterator it=mapii.
    return 0;

}


#include<iostream>
#include<algorithm>
#include<queue>
#include<map>

using namespace std;

int main()
{

    int n, s;
    map<int, int> mapii[2];
    cin >> n >> s;
    //s *= 2;
    int c1 = 0, c2 = 0;
    for (int i = 0; i < n; i++)
    {
        char d; int p, q;
        cin >> d >> p >> q;
        mapii[(int)(d == 'B')][p] += q;
        c1 = mapii[1].size();
        c2 = mapii[0].size();
        if (c1>s)
        {
            mapii[1].erase(mapii[1].begin());
        }
        if (c2 > s)
        {
            mapii[0].erase(--mapii[0].end());
        }
        //if (c1 + c2>s)
        //{
        //  if (c1 > c2)
        //  {
        //      mapii[1].erase(mapii[1].begin());
        //      --c1;

        //  }
        //  else
        //  {
        //      mapii[0].erase(--mapii[0].end());
        //      --c2;
        //  }
        //}
    }
    for (map<int, int>::reverse_iterator it = mapii[0].rbegin(); it != mapii[0].rend(); it++)
    {
        cout << "S " << it->first << " " << it->second << endl;
    }
    //s /= 2;
    for (map<int, int>::reverse_iterator it = mapii[1].rbegin(); it != mapii[1].rend(); it++)
    {
        cout << "B " << it->first << " " << it->second << endl;
    }
    //int t = mapii[1].size();
    //int n, s;
    //map<int, int> mapii;
    //cin >> n >> s;
    //s *= 2;
    //for (int i = 0; i < n; i++)
    //{
    //  char d;int p, q;
    //  cin >> d >> p >> q;
    //  mapii[(2 * (d != 66) - 1) * 100000 + p] += q;
    //}
    //for (map<int, int>::reverse_iterator it = mapii.rbegin(); it != mapii.rend()&&s--; it++)
    //{
    //  char ch2[] = { 'B', 'S' };
    //  cout << ch2[(it->first)>0] << " " << it->first + (2*(it->first < 0)-1) * 100000 << " " << it->second<<endl;

    //}
    //map<int, int>::reverse_iterator it=mapii.
    return 0;

}


#include<iostream>
#include<algorithm>
#include<queue>
#include<map>

using namespace std;

int main()
{

    int n, s;
    map<int, int> mapii[2];
    cin >> n >> s;
    s *= 2;
    int c1 = 0, c2 = 0;
    for (int i = 0; i < n; i++)
    {
        char d; int p, q;
        cin >> d >> p >> q;
        //c1 += (int)(d == 'B')&mapii[(int)(d == 'B')][p];
        //c2 += (int)(d == 'S');
        mapii[(int)(d == 'B')][p] += q;
        c1 = mapii[1].size();
        c2 = mapii[0].size();
        if (c1 + c2>s)
        {
            if (c1 > c2)
            {
                mapii[1].erase(mapii[1].begin());
                --c1;

            }
            else
            {
                mapii[0].erase(--mapii[0].end());
                --c2;
            }
        }
    }
    for (map<int, int>::reverse_iterator it = mapii[0].rbegin(); it != mapii[0].rend(); it++)
    {
        cout << "S " << it->first << " " << it->second << endl;
    }
    s /= 2;
    for (map<int, int>::reverse_iterator it = mapii[1].rbegin(); it != mapii[1].rend()&&s; it++,s--)
    {
        cout << "B " << it->first << " " << it->second << endl;
    }
    //int t = mapii[1].size();
    //int n, s;
    //map<int, int> mapii;
    //cin >> n >> s;
    //s *= 2;
    //for (int i = 0; i < n; i++)
    //{
    //  char d;int p, q;
    //  cin >> d >> p >> q;
    //  mapii[(2 * (d != 66) - 1) * 100000 + p] += q;
    //}
    //for (map<int, int>::reverse_iterator it = mapii.rbegin(); it != mapii.rend()&&s--; it++)
    //{
    //  char ch2[] = { 'B', 'S' };
    //  cout << ch2[(it->first)>0] << " " << it->first + (2*(it->first < 0)-1) * 100000 << " " << it->second<<endl;

    //}
    //map<int, int>::reverse_iterator it=mapii.
    return 0;

}


#include<iostream>
#include<algorithm>
#include<queue>
#include<map>

using namespace std;

int main()
{

    int n, s;
    map<int, int> mapii[2];
    cin >> n >> s;
    s *= 2;
    int c1 = 0, c2 = 0;
    for (int i = 0; i < n; i++)
    {
        char d; int p, q;
        cin >> d >> p >> q;
        //c1 += (int)(d == 'B')&mapii[(int)(d == 'B')][p];
        //c2 += (int)(d == 'S');
        mapii[(int)(d == 'B')][p] += q;
        c1 = mapii[1].size();
        c2 = mapii[0].size();
        if (c1 + c2>s)
        {
            if (c1 > c2)
            {
                mapii[1].erase(mapii[1].begin());
                --c1;

            }
            else
            {
                mapii[0].erase(--mapii[0].end());
                --c2;
            }
        }
    }
    for (map<int, int>::reverse_iterator it = mapii[0].rbegin(); it != mapii[0].rend(); it++)
    {
        cout << "S " << it->first << " " << it->second << endl;
    }
    for (map<int, int>::reverse_iterator it = mapii[1].rbegin(); it != mapii[1].rend(); it++)
    {
        cout << "B " << it->first << " " << it->second << endl;
    }
    //int t = mapii[1].size();
    //int n, s;
    //map<int, int> mapii;
    //cin >> n >> s;
    //s *= 2;
    //for (int i = 0; i < n; i++)
    //{
    //  char d;int p, q;
    //  cin >> d >> p >> q;
    //  mapii[(2 * (d != 66) - 1) * 100000 + p] += q;
    //}
    //for (map<int, int>::reverse_iterator it = mapii.rbegin(); it != mapii.rend()&&s--; it++)
    //{
    //  char ch2[] = { 'B', 'S' };
    //  cout << ch2[(it->first)>0] << " " << it->first + (2*(it->first < 0)-1) * 100000 << " " << it->second<<endl;

    //}
    //map<int, int>::reverse_iterator it=mapii.
    return 0;

}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define MXN 1010
int n,s;
struct order{
    char d;
    int p,q;
    bool operator < (const order& rgt)const{
        if(d!=rgt.d)    return d=='S';
        if(d=='S')  return p<rgt.p;
        else    return p>rgt.p;
    }
    bool operator == (const order& rgt)const{
        return d==rgt.d&&p==rgt.p;
    }
}od[MXN];
int st[MXN];
int main(){
    while(scanf("%d%d",&n,&s)!=EOF){
        for(int i=0;i<n;++i)
            cin>>od[i].d>>od[i].p>>od[i].q;
        sort(od,od+n);
        for(int i=0;i<n-1;++i){
            if(od[i]==od[i+1]){
                if(od[i].q==-1){
                    for(int j=i-1;j>=0;--j) if(od[j]==od[i]&&od[j].q!=-1){
                        od[j].q+=od[i+1].q;
                        od[i+1].q=-1;
                        break;
                    }
                }
                else{
                    od[i].q=od[i].q+od[i+1].q;
                    od[i+1].q=-1;
                }
            }
        }
        int cur=0;
        int cnt=0;
        for(int i=0;i<n;++i)    if(od[i].d=='S'){
            if(cnt>=s)  break;
            if(od[i].q!=-1){
                st[cur++]=i;
                ++cnt;
            }
        }
        for(int i=cur-1;i>=0;--i)   printf("%c %d %d\n",od[st[i]].d,od[st[i]].p,od[st[i]].q);
        cnt=0;
        for(int i=0;i<n;++i)    if(od[i].d=='B'){
            if(cnt>=s)  break;
            if(od[i].q!=-1){
                printf("%c %d %d\n",od[i].d,od[i].p,od[i].q);
                ++cnt;
            }
        }
    }
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define MXN 1010
int n,s;
struct order{
    char d;
    int p,q;
    bool operator < (const order& rgt)const{
        if(d!=rgt.d)    return d=='S';
        if(d=='S')  return p<rgt.p;
        else    return p>rgt.p;
    }
    bool operator == (const order& rgt)const{
        return d==rgt.d&&p==rgt.p;
    }
}od[MXN];
int main(){
    while(scanf("%d%d",&n,&s)!=EOF){
        for(int i=0;i<n;++i)
            cin>>od[i].d>>od[i].p>>od[i].q;
        sort(od,od+n);
        for(int i=0;i<n-1;++i){
            if(od[i]==od[i+1]){
                if(od[i].q==-1){
                    for(int j=i-1;j>=0;--j) if(od[j]==od[i]&&od[j].q!=-1){
                        od[j].q+=od[i+1].q;
                        od[i+1].q=-1;
                        break;
                    }
                }
                od[i].q=od[i].q+od[i+1].q;
                od[i+1].q=-1;
                ++i;
            }
        }
        int cnt=0,i;
        for(i=n-1;i>=0;--i) if(od[i].d=='S'){
            if(cnt==s)  break;
            if(od[i].q!=-1){
                printf("%c %d %d\n",od[i].d,od[i].p,od[i].q);
                ++cnt;
            }
        }
        cnt=0;
        for(int i=0;i<n;++i)    if(od[i].d=='B'){
            if(cnt==s)  break;
            if(od[i].q!=-1){
                printf("%c %d %d\n",od[i].d,od[i].p,od[i].q);
                ++cnt;
            }
        }
        puts("");
    }
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define MXN 1010
int n,s;
struct order{
    char d;
    int p,q;
    bool operator < (const order& rgt)const{
        if(d!=rgt.d)    return d=='S';
        if(d=='S')  return p<rgt.p;
        else    return p>rgt.p;
    }
    bool operator == (const order& rgt)const{
        return d==rgt.d&&p==rgt.p;
    }
}od[MXN];
int main(){
    while(scanf("%d%d",&n,&s)!=EOF){
        for(int i=0;i<n;++i)
            cin>>od[i].d>>od[i].p>>od[i].q;
        sort(od,od+n);
        for(int i=0;i<n-1;++i){
            if(od[i]==od[i+1]){
                if(od[i].q==-1){
                    for(int j=i-1;j>=0;--j) if(od[j]==od[i]&&od[j].q!=-1){
                        od[j].q+=od[i+1].q;
                        od[i+1].q=-1;
                        break;
                    }
                }
                od[i].q=od[i].q+od[i+1].q;
                od[i+1].q=-1;
            }
        }
        int cnt=0,i;
        for(i=n-1;i>=0;--i) if(od[i].d=='S'){
            if(cnt==s)  break;
            if(od[i].q!=-1){
                printf("%c %d %d\n",od[i].d,od[i].p,od[i].q);
                ++cnt;
            }
        }
        cnt=0;
        for(int i=0;i<n;++i)    if(od[i].d=='B'){
            if(cnt==s)  break;
            if(od[i].q!=-1){
                printf("%c %d %d\n",od[i].d,od[i].p,od[i].q);
                ++cnt;
            }
        }
    }
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define MXN 1010
int n,s;
struct order{
    char d;
    int p,q;
    bool operator < (const order& rgt)const{
        if(d!=rgt.d)    return d=='S';
        if(d=='S')  return p<rgt.p;
        else    return p>rgt.p;
    }
    bool operator == (const order& rgt)const{
        return d==rgt.d&&p==rgt.p;
    }
}od[MXN];
int main(){
    while(scanf("%d%d",&n,&s)!=EOF){
        for(int i=0;i<n;++i)
            cin>>od[i].d>>od[i].p>>od[i].q;
        sort(od,od+n);
        for(int i=0;i<n-1;++i){
            if(od[i]==od[i+1]){
                od[i].q=od[i].q+od[i+1].q;
                od[i+1].q=-1;
                ++i;
            }
        }
        int cnt=0,i;
        for(i=n-1;i>=0;--i) if(od[i].d=='S'){
            if(cnt==s)  break;
            if(od[i].q!=-1){
                printf("%c %d %d\n",od[i].d,od[i].p,od[i].q);
                ++cnt;
            }
        }
        cnt=0;
        for(int i=0;i<n;++i)    if(od[i].d=='B'){
            if(cnt==s)  break;
            if(od[i].q!=-1){
                printf("%c %d %d\n",od[i].d,od[i].p,od[i].q);
                ++cnt;
            }
        }
        puts("");
    }
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define MXN 1010
int n,s;
struct order{
    char d;
    int p,q;
    bool operator < (const order& rgt)const{
        if(d!=rgt.d)    return d=='S';
        if(d=='S')  return p<rgt.p;
        else    return p>rgt.p;
    }
    bool operator == (const order& rgt)const{
        return d==rgt.d&&p==rgt.p;
    }
}od[MXN];
int main(){
    while(scanf("%d%d",&n,&s)!=EOF){
        for(int i=0;i<n;++i)
            cin>>od[i].d>>od[i].p>>od[i].q;
        sort(od,od+n);
        for(int i=0;i<n-1;++i){
            if(od[i]==od[i+1]){
                if(od[i].q==-1){
                    for(int j=i-1;j>=0;--j) if(od[j]==od[i]&&od[j].q!=-1){
                        od[j].q+=od[i+1].q;
                        od[i+1].q=-1;
                        break;
                    }
                }
                else{
                    od[i].q=od[i].q+od[i+1].q;
                    od[i+1].q=-1;
                }
            }
        }
        int cnt=0,i;
        for(i=n-1;i>=0;--i) if(od[i].d=='S'){
            if(cnt==s)  break;
            if(od[i].q!=-1){
                printf("%c %d %d\n",od[i].d,od[i].p,od[i].q);
                ++cnt;
            }
        }
        cnt=0;
        for(int i=0;i<n;++i)    if(od[i].d=='B'){
            if(cnt==s)  break;
            if(od[i].q!=-1){
                printf("%c %d %d\n",od[i].d,od[i].p,od[i].q);
                ++cnt;
            }
        }
    }
}


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<stdlib.h>
#include <queue>
#include<string>
#include<cstring>
#include<sstream>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<fstream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define PB push_back
#define PI acos(-1.0)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define LL long long

#define MOD 1000000007
#define MX 100010
#define pii pair<int,int>
int GCD(int b, int a)
{
	if(a == 0)
		return b;
	return GCD(a, b%a);
}

int isPalindrome(string str){
	int i,n=str.size(),j,mid=str.size()/2;
	for(i=0,j=n-1;i<mid;i++,j--)
		if(str[i] != str[j])
			return 0;
	if(i==mid)
		return 1;
	return 0;
}

int chcek_Larger(string s1,string s2){
	int i;
	for(i=0;i<s2.length();i++){
		if(s1[i]-'0' > s2[i]-'0')
			return 1;
		else if(s1[i]-'0' < s2[i]-'0')
			return 2;
	}
	return 1;//palindrome
}
string addOne(string s){
	int n = s.size(),x =s[n-1]-'0',i,y,flag =0;

	if(x<9){
		x++;
		s[n-1] = x+'0';
	}
	else{
		y = s[0]-'0';
		if(y==9){
			flag=1;
			s[0] = '0';
		}
		else
			s[0] = y+1+'0';
		for(i=1;i<n-1;i++)
			s[i] = '0';
		x=0;
		s[n-1] = x+'0';
	}
	if(flag)
		s= "1"+s;
	return s;
}
pair<int,int>P[15];
int binary_search( int first, int last,int key)
{
	while (last >= first)
	{
		// calculate the midpoint for roughly equal partition
		int imid = (first + last)/2;
		if(P[imid].first <= key && P[imid].second>= key)
			return imid;

		else if (P[imid].second < key)
			first = imid + 1;
		else
			last = imid - 1;
	}

	return -1;// key was not found
}

int a[100005],b[100005];
int main()
{
	int i,n,m,x,first=0,second=0,sum,j,k,max,y;
	string T,S;
	int candidate,city;

	int s,price,volume;
	char ch;
	while(cin>>n>>s){
		map<int,int>mapS;
		map<int,int>mapB;
		//map<int,int>::iterator itS;
		map<int,int>::reverse_iterator rit;

		for(i=0;i<n;i++){
			cin>>ch;
			cin>>price;
			cin>>volume;
			if(ch == 'B'){
				mapB[price]= mapB[price]+volume;
			}
			else if(ch == 'S'){
				mapS[price]= mapS[price]+volume;
			}
		}

		//for(i=1,itS= mapS.begin();itS!=mapS.end() && i<=s;itS++,i++)
		for(rit=mapS.rbegin(),i=1; rit!=mapS.rend() && i<=s; ++rit,i++){
			cout<<"S "<<rit->first<<" "<<rit->second<<"\n";
		}
		for(rit=mapB.rbegin(),i=1; rit!=mapB.rend() && i<=s; ++rit,i++)
			cout<<"B " << rit->first << " " << rit->second << '\n';
		}

	return 0;
}


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<stdlib.h>
#include <queue>
#include<string>
#include<cstring>
#include<sstream>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<fstream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
int GCD(int a,int b){   while(b)b^=a^=b^=a%=b;  return a;   }

#define ALL_BITS ((1 << 31) - 1)
#define NEG_BITS(mask) (mask ^= ALL_BITS)
#define TEST_BIT(mask, i) (mask & (1 << i))
#define ON_BIT(mask, i) (mask |= (1 << i))
#define OFF_BIT(mask, i) (mask &= NEG_BITS(1 << i))

// UP, RIGHT, DOWN, LEFT, UPPER-RIGHT, LOWER-RIGHT, LOWER-LEFT, UPPER-LEFT
int dx[8] = {-1, 0, 1, 0, -1, 1,  1, -1};
int dy[8] = { 0, 1, 0,-1,  1, 1, -1, -1};

#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

struct node{
	int count;
	int first_index;
	int last_index;
	node(){
	count = 0;last_index =0;
	first_index=0;
	}
};
//node A[1000005];
int main()
{
	int i,n,m,x,first=0,second=0,sum,num,min;
	int s,price,volume;
	char ch;
	while(cin>>n>>s){
		map<int,int>mapS;
		map<int,int>mapB;
		map<int,int>::iterator it;
		map<int,int>::reverse_iterator rit;

		map<int,int>result;
		for(i=0;i<n;i++){
			cin>>ch;
			cin>>price;
			cin>>volume;
			if(ch == 'B'){
				mapB[price]= mapB[price]+volume;
			}
			else if(ch == 'S'){
				mapS[price]= mapS[price]+volume;
			}
		}

		for(it=mapS.begin(),i=1; it!=mapS.end() && i<=s; ++it,i++){
			result[it->first] = it->second;
		}
		for(rit=result.rbegin(),i=1; rit!=result.rend() && i<=s; ++rit,i++){

			cout<<"S "<<rit->first<<" "<<rit->second<<"\n";
		}
		for(rit=mapB.rbegin(),i=1; rit!=mapB.rend() && i<=s; ++rit,i++)
			cout<<"B " << rit->first << " " << rit->second << '\n';
		}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<stdlib.h>
#include <queue>
#include<string>
#include<cstring>
#include<sstream>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<fstream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define PB push_back
#define PI acos(-1.0)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define LL long long

#define MOD 1000000007
#define MX 100010
#define pii pair<int,int>
int GCD(int b, int a)
{
	if(a == 0)
		return b;
	return GCD(a, b%a);
}

int isPalindrome(string str){
	int i,n=str.size(),j,mid=str.size()/2;
	for(i=0,j=n-1;i<mid;i++,j--)
		if(str[i] != str[j])
			return 0;
	if(i==mid)
		return 1;
	return 0;
}

int chcek_Larger(string s1,string s2){
	int i;
	for(i=0;i<s2.length();i++){
		if(s1[i]-'0' > s2[i]-'0')
			return 1;
		else if(s1[i]-'0' < s2[i]-'0')
			return 2;
	}
	return 1;//palindrome
}
string addOne(string s){
	int n = s.size(),x =s[n-1]-'0',i,y,flag =0;

	if(x<9){
		x++;
		s[n-1] = x+'0';
	}
	else{
		y = s[0]-'0';
		if(y==9){
			flag=1;
			s[0] = '0';
		}
		else
			s[0] = y+1+'0';
		for(i=1;i<n-1;i++)
			s[i] = '0';
		x=0;
		s[n-1] = x+'0';
	}
	if(flag)
		s= "1"+s;
	return s;
}
pair<int,int>P[15];
int binary_search( int first, int last,int key)
{
	while (last >= first)
	{
		// calculate the midpoint for roughly equal partition
		int imid = (first + last)/2;
		if(P[imid].first <= key && P[imid].second>= key)
			return imid;

		else if (P[imid].second < key)
			first = imid + 1;
		else
			last = imid - 1;
	}

	return -1;// key was not found
}

//int a[100005],b[100005];
int main()
{
	int i,n,m,x,first=0,second=0,sum,j,k,max,y;
	string T,S;
	int candidate,city;

	int s,price,volume;
	char ch;

	while(cin>>n>>s){
		map<int,int>mapS;
		map<int,int>mapB;
		//map<int,int>::iterator itS;
		map<int,int>::reverse_iterator rit,rit2;

		for(i=0;i<n;i++){
			cin>>ch;
			cin>>price;
			cin>>volume;
			if(ch == 'B'){
				mapB[price]= mapB[price]+volume;
			}
			else if(ch == 'S'){
				mapS[price]= mapS[price]+volume;
			}
		}

		//for(i=1,itS= mapS.begin();itS!=mapS.end() && i<=s;itS++,i++)
		for(rit=mapS.rbegin(),i=1; rit!=mapS.rend() && i<=s; ++rit,++i){
			cout<<"S "<<rit->first<<" "<<rit->second<<"\n";
		}
		/*int remain = 0;
		if(i<=s){
		remain = s-i+1;
		}*/

		for(rit2=mapB.rbegin(),i=1; rit2!=mapB.rend() && i<=s; ++rit2,++i){
			cout<<"B " << rit2->first << " " << rit2->second << "\n";
		}
	}

	return 0;
}


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<stdlib.h>
#include <queue>
#include<string>
#include<cstring>
#include<sstream>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<fstream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define PB push_back
#define PI acos(-1.0)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define LL long long

#define MOD 1000000007
#define MX 100010
#define pii pair<int,int>
int GCD(int b, int a)
{
	if(a == 0)
		return b;
	return GCD(a, b%a);
}

int isPalindrome(string str){
	int i,n=str.size(),j,mid=str.size()/2;
	for(i=0,j=n-1;i<mid;i++,j--)
		if(str[i] != str[j])
			return 0;
	if(i==mid)
		return 1;
	return 0;
}

int chcek_Larger(string s1,string s2){
	int i;
	for(i=0;i<s2.length();i++){
		if(s1[i]-'0' > s2[i]-'0')
			return 1;
		else if(s1[i]-'0' < s2[i]-'0')
			return 2;
	}
	return 1;//palindrome
}
string addOne(string s){
	int n = s.size(),x =s[n-1]-'0',i,y,flag =0;

	if(x<9){
		x++;
		s[n-1] = x+'0';
	}
	else{
		y = s[0]-'0';
		if(y==9){
			flag=1;
			s[0] = '0';
		}
		else
			s[0] = y+1+'0';
		for(i=1;i<n-1;i++)
			s[i] = '0';
		x=0;
		s[n-1] = x+'0';
	}
	if(flag)
		s= "1"+s;
	return s;
}
pair<int,int>P[15];
int binary_search( int first, int last,int key)
{
	while (last >= first)
	{
		// calculate the midpoint for roughly equal partition
		int imid = (first + last)/2;
		if(P[imid].first <= key && P[imid].second>= key)
			return imid;

		else if (P[imid].second < key)
			first = imid + 1;
		else
			last = imid - 1;
	}

	return -1;// key was not found
}

int a[100005],b[100005];
int main()
{
	int i,n,m,x,first=0,second=0,sum,j,k,max,y;
	string T,S;
	int candidate,city;

	int s,price,volume;
	char ch;
	while(cin>>n>>s){
		map<int,int>mapS;
		map<int,int>mapB;
		map<int,int>::iterator itS;
		map<int,int>::reverse_iterator rit;

		for(i=0;i<n;i++){
			cin>>ch;
			cin>>price;
			cin>>volume;
			if(ch == 'B'){
				mapB[price]= mapB[price]+volume;
			}
			else if(ch == 'S'){
				mapS[price]= mapS[price]+volume;
			}
		}

		for(i=1,itS= mapS.begin();itS!=mapS.end() && i<=s;itS++,i++){
			cout<<"S "<<itS->first<<" "<<itS->second<<"\n";
		}
		for(rit=mapB.rbegin(),i=1; rit!=mapB.rend() && i<=s; ++rit,i++)
			cout<<"B " << rit->first << " " << rit->second << '\n';
		}

	return 0;
}


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<stdlib.h>
#include <queue>
#include<string>
#include<cstring>
#include<sstream>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<fstream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define PB push_back
#define PI acos(-1.0)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define LL long long

#define MOD 1000000007
#define MX 100010
#define pii pair<int,int>
int GCD(int b, int a)
{
	if(a == 0)
		return b;
	return GCD(a, b%a);
}

int isPalindrome(string str){
	int i,n=str.size(),j,mid=str.size()/2;
	for(i=0,j=n-1;i<mid;i++,j--)
		if(str[i] != str[j])
			return 0;
	if(i==mid)
		return 1;
	return 0;
}

int chcek_Larger(string s1,string s2){
	int i;
	for(i=0;i<s2.length();i++){
		if(s1[i]-'0' > s2[i]-'0')
			return 1;
		else if(s1[i]-'0' < s2[i]-'0')
			return 2;
	}
	return 1;//palindrome
}
string addOne(string s){
	int n = s.size(),x =s[n-1]-'0',i,y,flag =0;

	if(x<9){
		x++;
		s[n-1] = x+'0';
	}
	else{
		y = s[0]-'0';
		if(y==9){
			flag=1;
			s[0] = '0';
		}
		else
			s[0] = y+1+'0';
		for(i=1;i<n-1;i++)
			s[i] = '0';
		x=0;
		s[n-1] = x+'0';
	}
	if(flag)
		s= "1"+s;
	return s;
}
pair<int,int>P[15];
int binary_search( int first, int last,int key)
{
	while (last >= first)
	{
		// calculate the midpoint for roughly equal partition
		int imid = (first + last)/2;
		if(P[imid].first <= key && P[imid].second>= key)
			return imid;

		else if (P[imid].second < key)
			first = imid + 1;
		else
			last = imid - 1;
	}

	return -1;// key was not found
}

int a[100005],b[100005];
int main()
{
	int i,n,m,x,first=0,second=0,sum,j,k,max,y;
	string T,S;
	int candidate,city;

	int s,price,volume;
	char ch;

	while(cin>>n>>s){
		map<int,int>mapS;
		map<int,int>mapB;
		//map<int,int>::iterator itS;
		map<int,int>::reverse_iterator rit;

		for(i=0;i<n;i++){
			cin>>ch;
			cin>>price;
			cin>>volume;
			if(ch == 'B'){
				mapB[price]= mapB[price]+volume;
			}
			else if(ch == 'S'){
				mapS[price]= mapS[price]+volume;
			}
		}

		//for(i=1,itS= mapS.begin();itS!=mapS.end() && i<=s;itS++,i++)
		for(rit=mapS.rbegin(),i=1; rit!=mapS.rend() && i<=s; ++rit,i++){
			cout<<"S "<<rit->first<<" "<<rit->second<<"\n";
		}
		int remain = 0;
		if(i<=s){
		remain = s-i+1;
		}

		for(rit=mapB.rbegin(),i=1; rit!=mapB.rend() && i<=s+remain; ++rit,i++)
			cout<<"B " << rit->first << " " << rit->second << "\n";
		}

	return 0;
}


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<stdlib.h>
#include <queue>
#include<string>
#include<cstring>
#include<sstream>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<fstream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define PB push_back
#define PI acos(-1.0)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define LL long long

#define MOD 1000000007
#define MX 100010
#define pii pair<int,int>
int GCD(int b, int a)
{
	if(a == 0)
		return b;
	return GCD(a, b%a);
}

int isPalindrome(string str){
	int i,n=str.size(),j,mid=str.size()/2;
	for(i=0,j=n-1;i<mid;i++,j--)
		if(str[i] != str[j])
			return 0;
	if(i==mid)
		return 1;
	return 0;
}

int chcek_Larger(string s1,string s2){
	int i;
	for(i=0;i<s2.length();i++){
		if(s1[i]-'0' > s2[i]-'0')
			return 1;
		else if(s1[i]-'0' < s2[i]-'0')
			return 2;
	}
	return 1;//palindrome
}
string addOne(string s){
	int n = s.size(),x =s[n-1]-'0',i,y,flag =0;

	if(x<9){
		x++;
		s[n-1] = x+'0';
	}
	else{
		y = s[0]-'0';
		if(y==9){
			flag=1;
			s[0] = '0';
		}
		else
			s[0] = y+1+'0';
		for(i=1;i<n-1;i++)
			s[i] = '0';
		x=0;
		s[n-1] = x+'0';
	}
	if(flag)
		s= "1"+s;
	return s;
}
pair<int,int>P[15];
int binary_search( int first, int last,int key)
{
	while (last >= first)
	{
		// calculate the midpoint for roughly equal partition
		int imid = (first + last)/2;
		if(P[imid].first <= key && P[imid].second>= key)
			return imid;

		else if (P[imid].second < key)
			first = imid + 1;
		else
			last = imid - 1;
	}

	return -1;// key was not found
}

//int a[100005],b[100005];
int main()
{
	int i,n,m,x,first=0,second=0,sum,j,k,max,y;
	string T,S;
	int candidate,city;

	int s,price,volume;
	char ch;

	while(cin>>n>>s){
		map<int,int>mapS;
		map<int,int>mapB;
		//map<int,int>::iterator itS;
		map<int,int>::reverse_iterator rit,rit2;

		for(i=0;i<n;i++){
			cin>>ch;
			cin>>price;
			cin>>volume;
			if(ch == 'B'){
				if(mapB[price])
				mapB[price]= mapB[price]+volume;
				else
					mapB[price] = volume;
			}
			else if(ch == 'S'){
				if(mapS[price])
					mapS[price]= mapS[price]+volume;
				else
					mapS[price] = volume;

			}
		}

		//for(i=1,itS= mapS.begin();itS!=mapS.end() && i<=s;itS++,i++)
		for(rit=mapS.rbegin(),i=1; rit!=mapS.rend() && i<=s; ++rit,++i){
			cout<<"S "<<rit->first<<" "<<rit->second<<"\n";
		}
		/*int remain = 0;
		if(i<=s){
		remain = s-i+1;
		}*/

		for(rit2=mapB.rbegin(),i=1; rit2!=mapB.rend() && i<=s; ++rit2,++i){
			cout<<"B " << rit2->first << " " << rit2->second << "\n";
		}
	}

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <tuple>

using namespace std;

int n, dep;
pair<int, int> b[100005], s[100005];
vector<int> ans;

int main()
{
    cin >> n >> dep;
    for (int i = 1; i <= 100000; ++i) b[i].second = s[i].second = i;
    for (int i = 1; i <= n; ++i)
    {
        string d;
        int p, q;
        cin >> d >> p >> q;
        if (d[0] == 'B') b[p].first += q * p;
        else if (d[0] == 'S') s[p].first += q * p;
    }
    sort(b + 1, b + 100001, greater< pair<int, int> >());
    sort(s + 1, s + 100001);
    for (int i = 1, j = 1; i <= 100000 && j <= dep; ++i)
        if (s[i].first)
        {
            ans.push_back(i);
            ++j;
        }
    for (auto it = ans.rbegin(); it != ans.rend(); ++it) cout << "S " << s[*it].second << ' ' << s[*it].first / s[*it].second << '\n';
    for (int i = 1, j = 1; i <= 100000 && j <= dep; ++i)
        if (b[i].first)
        {
            cout << "B " << b[i].second << ' ' << b[i].first / b[i].second << '\n';
            ++j;
        }
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <tuple>

using namespace std;

int n, dep, b[100005], s[100005];
vector<int> ans;

int main()
{
    cin >> n >> dep;
    for (int i = 1; i <= n; ++i)
    {
        string d;
        int p, q;
        cin >> d >> p >> q;
        if (d[0] == 'B') b[p] += q;
        else if (d[0] == 'S') s[p] += q;
    }
    for (int i = 1, j = 1; i <= 100000 && j <= dep; ++i)
        if (s[i])
        {
            ans.push_back(i);
            ++j;
        }
    for (auto it = ans.rbegin(); it != ans.rend(); ++it) cout << "S " << *it << ' ' << s[*it] << '\n';
    for (int i = 100000, j = 1; i >= 1 && j <= dep; --i)
        if (b[i])
        {
            cout << "B " << i << ' ' << b[i] << '\n';
            ++j;
        }
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <tuple>

using namespace std;

int n, dep, b[100005], s[100005];
vector<int> ans;

int main()
{
    cin >> n >> dep;
    for (int i = 1; i <= n; ++i)
    {
        string d;
        int p, q;
        cin >> d >> p >> q;
        if (d[0] == 'B') b[p] += q;
        else if (d[0] == 'S') s[p] += q;
    }
    for (int i = 0, j = 1; i <= 100000 && j <= dep; ++i)
        if (s[i])
        {
            ans.push_back(i);
            ++j;
        }
    for (auto it = ans.rbegin(); it != ans.rend(); ++it) cout << "S " << *it << ' ' << s[*it] << '\n';
    for (int i = 100000, j = 1; i >= 0 && j <= dep; --i)
        if (b[i])
        {
            cout << "B " << i << ' ' << b[i] << '\n';
            ++j;
        }
}


#include <cstdio>
#include <algorithm>
#include<math.h>
#include<string.h>
#include<memory.h>
#include<iostream>
#include<vector>
using namespace std;
int n,s,bb[100005],ss[100005],bb1[100005],ss1[100005];
int main(){
//freopen("in.txt","r",stdin);
cin>>n>>s;
int i,mas=0,mab=0;
char st[2];
for(i=1;i<=n;i++){
    scanf("%s",st);
    if(st[0]=='S'){
        int s1,s2;
        scanf("%d%d",&s1,&s2);
      mas=max(mas,s1);
       ss[s1]=s1;
       ss1[s1]+=s2;
        }
    else{
        int b1,b2;
        scanf("%d%d",&b1,&b2);
       mab=max(mab,b1);
        bb[b1]=b1;
        bb1[b1]+=b2;
        }
}
sort(ss,ss+1+mas);
sort(bb,bb+1+mab);
for(i=s;i>=1;i--){
   int data=ss[mas];
    if(ss1[data]==0)
        break;
    printf("S %d %d\n",data,ss1[data]);
mas--;
if(data==0)
    break;
}
int num=mab,num1=0;
for(i=s;i>=1;i--){
    int datb=bb[num];
    if(datb!=0)
    num1++;
else{
    if(bb1[datb]!=0)
        num1++;
break;
}
num--;
}
if(num1>s)
   mab-=num1-s;
    for(i=s;i>=1;i--){
          int data=bb[mab];
printf("B %d %d\n",data,bb1[data]);
mab--;
}

return 0;
}


#include <cstdio>
#include <algorithm>
#include<math.h>
#include<string.h>
#include<memory.h>
#include<iostream>
#include<vector>
using namespace std;
int n,s,bb[100005],ss[100005],bb1[100005],ss1[100005];
int main(){
//freopen("in.txt","r",stdin);
cin>>n>>s;
int i,mas=0,mab=0;
char st[2];
for(i=1;i<=n;i++){
    scanf("%s",st);
    if(st[0]=='S'){
        int s1,s2;
        scanf("%d%d",&s1,&s2);
      mas=max(mas,s1);
       ss[s1]=s1;
       ss1[s1]+=s2;
        }
    else{
        int b1,b2;
        scanf("%d%d",&b1,&b2);
       mab=max(mab,b1);
        bb[b1]=b1;
        bb1[b1]+=b2;
        }
}
sort(ss,ss+1+mas);
sort(bb,bb+1+mab);
int num=mas,num1=0;
for(i=mas;i>=0;i--){
    int datb=ss[num];
    if(datb!=0)
    num1++;
else{
    if(ss1[datb]!=0)
        num1++;
break;
}
num--;
}
if(num1>s)
    mas-=num1-s;
for(i=s;i>=1;i--){
   int data=ss[mas];
    if(ss1[data]==0)
        break;
    printf("S %d %d\n",data,ss1[data]);
mas--;
if(data==0)
    break;
}
for(i=s;i>=1;i--){
   int data=bb[mab];
    if(bb1[data]==0)
        break;
    printf("B %d %d\n",data,bb1[data]);
mab--;
if(data==0)
    break;
}

return 0;
}


#include <cstdio>
#include <algorithm>
#include<math.h>
#include<string.h>
#include<memory.h>
#include<iostream>
#include<vector>
using namespace std;
int n,s,bb[100005],ss[100005],bb1[100005],ss1[100005];
int main(){
//freopen("in.txt","r",stdin);
cin>>n>>s;
int i,mas=0,mab=0;
char st[2];
for(i=1;i<=n;i++){
    scanf("%s",st);
    if(st[0]=='S'){
        int s1,s2;
        scanf("%d%d",&s1,&s2);
      mas=max(mas,s1);
       ss[s1]=s1;
       ss1[s1]+=s2;
        }
    else{
        int b1,b2;
        scanf("%d%d",&b1,&b2);
       mab=max(mab,b1);
        bb[b1]=b1;
        bb1[b1]+=b2;
        }
}
sort(ss,ss+1+mas);
sort(bb,bb+1+mab);
for(i=s;i>=1;i--){
   int data=ss[mas];
    if(ss1[data]==0)
        break;
    printf("S %d %d\n",data,ss1[data]);
mas--;
if(data==0)
    break;
}
int num=mab,num1=0;
for(i=mab;i>=0;i--){
    int datb=bb[num];
    if(datb!=0)
    num1++;
else{
    if(bb1[datb]!=0)
        num1++;
break;
}
num--;
}
if(num1>s)
   mab-=num1-s;
    for(i=s;i>=1;i--){
          int data=bb[mab];
printf("B %d %d\n",data,bb1[data]);
mab--;
}

return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
const int MAX_n = 1000;

int n, s, idxb, idxs;

struct data {
	int p, q;
	long long m;
	data() {}
	data(int p, int q) : p(p), q(q), m((long long)p*q) {}
	bool operator<(const data &r)const{
		return m < r.m;
	}
} ab[MAX_n], as[MAX_n], B[MAX_n], S[MAX_n];

bool complow(data &l, data &r) {
	return l.m < r.m;
}
bool comphigh(data &l, data &r) {
	return l.m > r.m;
}
bool comp(data &l, data &r) {
	return l.p>r.p;
}

void input() {
	int i, p, q, bb=0, ss=0, cnt;
	char c;
	scanf("%d %d", &n, &s);
	for (i = 1; i <= n; i++) {
		scanf(" %c %d %d", &c, &p, &q);
		if (c == 'B') ab[bb++] = data(p, q);
		else as[ss++] = data(p, q);
	}
	std::sort(ab, ab + bb, comp);
	std::sort(as, as + ss, comp);
	cnt = ab[0].q;
	//for (i = 1; i < bb; i++) {
	//	if (ab[i].p != ab[i - 1].p) B[idxb++] = data(ab[i - 1].p, cnt), cnt = 0;
	//	cnt += ab[i].q;
	//}
	//B[idxb++] = data(ab[i - 1].p, cnt);
	for (i = 0; i < bb; i++) B[idxb++] = ab[i];
	cnt = as[0].q;
	for (i = 1; i < ss; i++) {
		if (as[i].p != as[i - 1].p) S[idxs++] = data(as[i - 1].p, cnt), cnt = 0;
		cnt += as[i].q;
	}
	S[idxs++] = data(as[i - 1].p, cnt);
	std::sort(B, B + idxb, comphigh);
	std::sort(S, S + idxs, complow);
}

void output() {
	int i;
	for (i = 0; i < s; i++) ab[i] = B[i];
	for (i = 0; i < s; i++) as[i] = S[i];
	std::sort(ab, ab + s, comp);
	std::sort(as, as + s, comp);
	for (i = 0; i < s; i++)
		printf("S %d %d\n", as[i].p, as[i].q);
	for (i = 0; i < s; i++)
		printf("B %d %d\n", ab[i].p, ab[i].q);
}

int main() {
	input();
	output();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
const int MAX_n = 1000;

int n, s, idxb, idxs;

struct data {
	int p, q;
	long long m;
	data() {}
	data(int p, int q) : p(p), q(q), m((long long)p*q) {}
	bool operator<(const data &r)const{
		return m < r.m;
	}
} ab[MAX_n], as[MAX_n], B[MAX_n], S[MAX_n];

bool complow(data &l, data &r) {
	//return l.m < r.m || (l.m==r.m && l.p>r.p);
	return l.p < r.p;
}
bool comphigh(data &l, data &r) {
	//return l.m > r.m || (l.m==r.m && l.p>r.p);
	return l.p > r.p;
}
bool comp(data &l, data &r) {
	return l.p>r.p;
}

void input() {
	int i, p, q, bb=0, ss=0, cnt;
	char c;
	scanf("%d %d", &n, &s);
	for (i = 1; i <= n; i++) {
		scanf(" %c %d %d", &c, &p, &q);
		if (c == 'B') ab[bb++] = data(p, q);
		else as[ss++] = data(p, q);
	}
	std::sort(ab, ab + bb, comp);
	std::sort(as, as + ss, comp);
	cnt = ab[0].q;
	for (i = 1; i < bb; i++) {
		if (ab[i].p != ab[i - 1].p) B[idxb++] = data(ab[i - 1].p, cnt), cnt = 0;
		cnt += ab[i].q;
	}
	B[idxb++] = data(ab[i - 1].p, cnt);
	cnt = as[0].q;
	for (i = 1; i < ss; i++) {
		if (as[i].p != as[i - 1].p) S[idxs++] = data(as[i - 1].p, cnt), cnt = 0;
		cnt += as[i].q;
	}
	S[idxs++] = data(as[i - 1].p, cnt);
	std::sort(B, B + idxb, comphigh);
	std::sort(S, S + idxs, complow);
}

void output() {
	int i, bb=0, ss=0;
	if (idxb < s) {
		for (i = 0; i < idxb; i++) ab[bb++] = B[i];
	}
	else {
		for (i = 0; i < s; i++) ab[bb++] = B[i];
	}
	if (idxs < s) {
		for (i = 0; i < idxs; i++) as[ss++] = B[i];
	}
	else {
		for (i = 0; i < s; i++) as[ss++] = S[i];
	}
	std::sort(ab, ab + bb, comp);
	std::sort(as, as + ss, comp);
	for (i = 0; i < ss; i++)
		printf("S %d %d\n", as[i].p, as[i].q);
	for (i = 0; i < bb; i++)
		printf("B %d %d\n", ab[i].p, ab[i].q);
}

int main() {
	input();
	output();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
const int MAX_n = 1000;

int n, s, idxb, idxs;

struct data {
	int p, q;
	long long m;
	data() {}
	data(int p, int q) : p(p), q(q), m((long long)p*q) {}
	bool operator<(const data &r)const{
		return m < r.m;
	}
} ab[MAX_n], as[MAX_n], B[MAX_n], S[MAX_n];

bool complow(data &l, data &r) {
	//return l.m < r.m || (l.m==r.m && l.p>r.p);
	return l.p < r.p;
}
bool comphigh(data &l, data &r) {
	//return l.m > r.m || (l.m==r.m && l.p>r.p);
	return l.p > r.p;
}
bool comp(data &l, data &r) {
	return l.p>r.p;
}

void input() {
	int i, p, q, bb=0, ss=0, cnt;
	char c;
	scanf("%d %d", &n, &s);
	for (i = 1; i <= n; i++) {
		scanf(" %c %d %d", &c, &p, &q);
		if (c == 'B') ab[bb++] = data(p, q);
		else as[ss++] = data(p, q);
	}
	std::sort(ab, ab + bb, comp);
	std::sort(as, as + ss, comp);
	if (bb) {
		cnt = ab[0].q;
		for (i = 1; i < bb; i++) {
			if (ab[i].p != ab[i - 1].p) B[idxb++] = data(ab[i - 1].p, cnt), cnt = 0;
			cnt += ab[i].q;
		}
		B[idxb++] = data(ab[i - 1].p, cnt);
	}
	if (ss) {
		cnt = as[0].q;
		for (i = 1; i < ss; i++) {
			if (as[i].p != as[i - 1].p) S[idxs++] = data(as[i - 1].p, cnt), cnt = 0;
			cnt += as[i].q;
		}
		S[idxs++] = data(as[i - 1].p, cnt);
	}
	std::sort(B, B + idxb, comphigh);
	std::sort(S, S + idxs, complow);
}

void output() {
	int i, bb=0, ss=0;
	if (idxb < s) {
		for (i = 0; i < idxb; i++) ab[bb++] = B[i];
	}
	else {
		for (i = 0; i < s; i++) ab[bb++] = B[i];
	}
	if (idxs < s) {
		for (i = 0; i < idxs; i++) as[ss++] = B[i];
	}
	else {
		for (i = 0; i < s; i++) as[ss++] = S[i];
	}
	std::sort(ab, ab + bb, comp);
	std::sort(as, as + ss, comp);
	for (i = 0; i < ss; i++)
		printf("S %d %d\n", as[i].p, as[i].q);
	for (i = 0; i < bb; i++)
		printf("B %d %d\n", ab[i].p, ab[i].q);
}

int main() {
	input();
	output();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
const int MAX_n = 1000;

int n, s, idxb, idxs;

struct data {
	int p, q;
	long long m;
	data() {}
	data(int p, int q) : p(p), q(q), m((long long)p*q) {}
	bool operator<(const data &r)const{
		return m < r.m;
	}
} ab[MAX_n], as[MAX_n], B[MAX_n], S[MAX_n];

bool complow(data &l, data &r) {
	return l.m < r.m;
}
bool comphigh(data &l, data &r) {
	return l.m > r.m;
}
bool comp(data &l, data &r) {
	return l.p>r.p;
}

void input() {
	int i, p, q, bb=0, ss=0, cnt;
	char c;
	scanf("%d %d", &n, &s);
	for (i = 1; i <= n; i++) {
		scanf(" %c %d %d", &c, &p, &q);
		if (c == 'B') ab[bb++] = data(p, q);
		else as[ss++] = data(p, q);
	}
	std::sort(ab, ab + bb, comp);
	std::sort(as, as + ss, comp);
	cnt = ab[0].q;
	for (i = 1; i < bb; i++) {
		if (ab[i].p != ab[i - 1].p) B[idxb++] = data(ab[i - 1].p, cnt), cnt = 0;
		cnt += ab[i].q;
	}
	B[idxb++] = data(ab[i - 1].p, cnt);
	cnt = as[0].q;
	for (i = 1; i < ss; i++) {
		if (as[i].p != as[i - 1].p) S[idxs++] = data(as[i - 1].p, cnt), cnt = 0;
		cnt += as[i].q;
	}
	S[idxs++] = data(as[i - 1].p, cnt);
	std::sort(B, B + idxb, comphigh);
	std::sort(S, S + idxs, complow);
}

void output() {
	int i, bb=0, ss=0;
	if (idxb < s) {
		for (i = 0; i < idxb; i++) ab[bb++] = B[i];
	}
	else {
		for (i = 0; i < s; i++) ab[bb++] = B[i];
	}
	if (idxs < s) {
		for (i = 0; i < idxs; i++) as[ss++] = B[i];
	}
	else {
		for (i = 0; i < s; i++) as[ss++] = S[i];
	}
	std::sort(ab, ab + bb, comp);
	std::sort(as, as + ss, comp);
	for (i = 0; i < ss; i++)
		printf("S %d %d\n", as[i].p, as[i].q);
	for (i = 0; i < bb; i++)
		printf("B %d %d\n", ab[i].p, ab[i].q);
}

int main() {
	input();
	output();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
const int MAX_n = 1000;

int n, s, idxb, idxs;

struct data {
	int p, q;
	long long m;
	data() {}
	data(int p, int q) : p(p), q(q), m((long long)p*q) {}
	bool operator<(const data &r)const{
		return m < r.m;
	}
} ab[MAX_n], as[MAX_n], B[MAX_n], S[MAX_n];

bool complow(data &l, data &r) {
	return l.m < r.m;
}
bool comphigh(data &l, data &r) {
	return l.m > r.m;
}
bool comp(data &l, data &r) {
	return l.p>r.p;
}

void input() {
	int i, p, q, bb=0, ss=0, cnt;
	char c;
	scanf("%d %d", &n, &s);
	for (i = 1; i <= n; i++) {
		scanf(" %c %d %d", &c, &p, &q);
		if (c == 'B') ab[bb++] = data(p, q);
		else as[ss++] = data(p, q);
	}
	std::sort(ab, ab + bb, comp);
	std::sort(as, as + ss, comp);
	cnt = ab[0].q;
	for (i = 1; i < bb; i++) {
		if (ab[i].p != ab[i - 1].p) B[idxb++] = data(ab[i - 1].p, cnt), cnt = 0;
		cnt += ab[i].q;
	}
	B[idxb++] = data(ab[i - 1].p, cnt);
	cnt = as[0].q;
	for (i = 1; i < ss; i++) {
		if (as[i].p != as[i - 1].p) S[idxs++] = data(as[i - 1].p, cnt), cnt = 0;
		cnt += as[i].q;
	}
	S[idxs++] = data(as[i - 1].p, cnt);
	std::sort(B, B + idxb, comphigh);
	std::sort(S, S + idxs, complow);
}

void output() {
	int i;
	for (i = 0; i < s; i++) ab[i] = B[i];
	for (i = 0; i < s; i++) as[i] = S[i];
	std::sort(ab, ab + s, comp);
	std::sort(as, as + s, comp);
	for (i = 0; i < s; i++)
		printf("S %d %d\n", as[i].p, as[i].q);
	for (i = 0; i < s; i++)
		printf("B %d %d\n", ab[i].p, ab[i].q);
}

int main() {
	input();
	output();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
const int MAX_n = 1000;

int n, s, idxb, idxs;

struct data {
	int p, q;
	long long m;
	data() {}
	data(int p, int q) : p(p), q(q), m((long long)p*q) {}
	bool operator<(const data &r)const{
		return m < r.m;
	}
} ab[MAX_n], as[MAX_n], B[MAX_n], S[MAX_n];

bool complow(data &l, data &r) {
	return l.m < r.m || (l.m==r.m && l.p>r.p);
}
bool comphigh(data &l, data &r) {
	return l.m > r.m || (l.m==r.m && l.p>r.p);
}
bool comp(data &l, data &r) {
	return l.p>r.p;
}

void input() {
	int i, p, q, bb=0, ss=0, cnt;
	char c;
	scanf("%d %d", &n, &s);
	for (i = 1; i <= n; i++) {
		scanf(" %c %d %d", &c, &p, &q);
		if (c == 'B') ab[bb++] = data(p, q);
		else as[ss++] = data(p, q);
	}
	std::sort(ab, ab + bb, comp);
	std::sort(as, as + ss, comp);
	cnt = ab[0].q;
	for (i = 1; i < bb; i++) {
		if (ab[i].p != ab[i - 1].p) B[idxb++] = data(ab[i - 1].p, cnt), cnt = 0;
		cnt += ab[i].q;
	}
	B[idxb++] = data(ab[i - 1].p, cnt);
	cnt = as[0].q;
	for (i = 1; i < ss; i++) {
		if (as[i].p != as[i - 1].p) S[idxs++] = data(as[i - 1].p, cnt), cnt = 0;
		cnt += as[i].q;
	}
	S[idxs++] = data(as[i - 1].p, cnt);
	std::sort(B, B + idxb, comphigh);
	std::sort(S, S + idxs, complow);
}

void output() {
	int i, bb=0, ss=0;
	if (idxb < s) {
		for (i = 0; i < idxb; i++) ab[bb++] = B[i];
	}
	else {
		for (i = 0; i < s; i++) ab[bb++] = B[i];
	}
	if (idxs < s) {
		for (i = 0; i < idxs; i++) as[ss++] = B[i];
	}
	else {
		for (i = 0; i < s; i++) as[ss++] = S[i];
	}
	std::sort(ab, ab + bb, comp);
	std::sort(as, as + ss, comp);
	for (i = 0; i < ss; i++)
		printf("S %d %d\n", as[i].p, as[i].q);
	for (i = 0; i < bb; i++)
		printf("B %d %d\n", ab[i].p, ab[i].q);
}

int main() {
	input();
	output();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
const int MAX_n = 1000;

int n, s, idxb, idxs;

struct data {
	int p, q;
	long long m;
	data() {}
	data(int p, int q) : p(p), q(q), m((long long)p*q) {}
	bool operator<(const data &r)const{
		return m < r.m;
	}
} ab[MAX_n], as[MAX_n], B[MAX_n], S[MAX_n];

bool complow(data &l, data &r) {
	//return l.m < r.m || (l.m==r.m && l.p>r.p);
	return l.p < r.p;
}
bool comphigh(data &l, data &r) {
	//return l.m > r.m || (l.m==r.m && l.p>r.p);
	return l.p > r.p;
}
bool comp(data &l, data &r) {
	return l.p>r.p;
}

void input() {
	int i, p, q, bb=0, ss=0, cnt;
	char c;
	scanf("%d %d", &n, &s);
	for (i = 1; i <= n; i++) {
		scanf(" %c %d %d", &c, &p, &q);
		if (c == 'B') ab[bb++] = data(p, q);
		else as[ss++] = data(p, q);
	}
	std::sort(ab, ab + bb, comp);
	std::sort(as, as + ss, comp);
	if (bb) {
		cnt = ab[0].q;
		for (i = 1; i < bb; i++) {
			if (ab[i].p != ab[i - 1].p) B[idxb++] = data(ab[i - 1].p, cnt), cnt = 0;
			cnt += ab[i].q;
		}
		B[idxb++] = data(ab[i - 1].p, cnt);
	}
	if (ss) {
		cnt = as[0].q;
		for (i = 1; i < ss; i++) {
			if (as[i].p != as[i - 1].p) S[idxs++] = data(as[i - 1].p, cnt), cnt = 0;
			cnt += as[i].q;
		}
		S[idxs++] = data(as[i - 1].p, cnt);
	}
	std::sort(B, B + idxb, comphigh);
	std::sort(S, S + idxs, complow);
}

void output() {
	int i, bb=0, ss=0;
	if (idxb < s) {
		for (i = 0; i < idxb; i++) ab[bb++] = B[i];
	}
	else {
		for (i = 0; i < s; i++) ab[bb++] = B[i];
	}
	if (idxs < s) {
		for (i = 0; i < idxs; i++) as[ss++] = S[i];
	}
	else {
		for (i = 0; i < s; i++) as[ss++] = S[i];
	}
	std::sort(ab, ab + bb, comp);
	std::sort(as, as + ss, comp);
	for (i = 0; i < ss; i++)
		printf("S %d %d\n", as[i].p, as[i].q);
	for (i = 0; i < bb; i++)
		printf("B %d %d\n", ab[i].p, ab[i].q);
}

int main() {
	input();
	output();
	return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;

struct B{
    int p;
    int q;
}b[1005];

struct C{
    int p;
    int q;
}c[1005];

bool cmp1(B x, B y){
    return x.p>y.p;
}

bool cmp2(C x,C y){
    return x.p<y.p;
}

int main(){
    int n, s;
    int p, q, cnt1=0, cnt2=0, flag;
    char d[10];
    scanf("%d%d",&n,&s);
    for(int i=0;i<n;i++){
        flag=0;
        scanf("%s%d%d",d,&p,&q);
        if(d[0]=='B'){
            for(int j=0;j<cnt1;j++)
            {
                if(b[j].p==p) {
                    b[j].q+=q;
                    flag=1;
                    break;
                }
            }
            if(flag) continue;
            b[cnt1].q=q;
            b[cnt1++].p=p;
        }
        else {
            for(int j=0;j<cnt2;j++){
                if(c[j].p==p) {
                    c[j].q+=q;
                    flag=1;
                    break;
                }
            }
            if(flag) continue;
            c[cnt2].q=q;
            c[cnt2++].p=p;
        }
    }

    sort(b,b+cnt1,cmp1);
    sort(c,c+cnt2,cmp2);
    if(cnt2){
        for(int i=s-1;i>=0;i--){
            printf("S %d %d\n", c[i].p, c[i].q);
        }
    }
    if(cnt1){
        for(int i=0;i<min(s,cnt1);i++){
            printf("B %d %d\n", b[i].p, b[i].q);
        }
    }
    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;

struct B{
    int p;
    int q;
}b[1005];

struct C{
    int p;
    int q;
}c[1005];

bool cmp1(B x, B y){
    return x.p>y.p;
}

bool cmp2(C x,C y){
    return x.p<y.p;
}

int main(){
    int n, s;
    int p, q, cnt1=0, cnt2=0, flag;
    char d[10];
    scanf("%d%d",&n,&s);
    for(int i=0;i<n;i++){
        flag=0;
        scanf("%s%d%d",d,&p,&q);
        if(d[0]=='B'){
            for(int j=0;j<cnt1;j++)
            {
                if(b[j].p==p) {
                    b[j].q+=q;
                    flag=1;
                    break;
                }
            }
            if(flag) continue;
            b[cnt1].q=q;
            b[cnt1++].p=p;
        }
        else {
            for(int j=0;j<cnt2;j++){
                if(c[j].p==p) {
                    c[j].q+=q;
                    flag=1;
                    break;
                }
            }
            if(flag) continue;
            c[cnt2].q=q;
            c[cnt2++].p=p;
        }
    }

    sort(b,b+cnt1,cmp1);
    sort(c,c+cnt2,cmp2);
    for(int i=0;i<s;i++){
        printf("S %d %d\n", c[i].p, c[i].q);
    }
    for(int i=0;i<s;i++){
        printf("B %d %d\n", b[i].p, b[i].q);
    }
    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;

struct B{
    int p;
    int q;
}b[1005];

struct C{
    int p;
    int q;
}c[1005];

bool cmp1(B x, B y){
    return x.p>y.p;
}

bool cmp2(C x,C y){
    return x.p<y.p;
}

int main(){
    int n, s;
    int p, q, cnt1=0, cnt2=0, flag;
    char d[10];
    scanf("%d%d",&n,&s);
    for(int i=0;i<n;i++){
        flag=0;
        scanf("%s%d%d",d,&p,&q);
        if(d[0]=='B'){
            for(int j=0;j<cnt1;j++)
            {
                if(b[j].p==p) {
                    b[j].q+=q;
                    flag=1;
                    break;
                }
            }
            if(flag) continue;
            b[cnt1].q=q;
            b[cnt1++].p=p;
        }
        else {
            for(int j=0;j<cnt2;j++){
                if(c[j].p==p) {
                    c[j].q+=q;
                    flag=1;
                    break;
                }
            }
            if(flag) continue;
            c[cnt2].q=q;
            c[cnt2++].p=p;
        }
    }

    sort(b,b+cnt1,cmp1);
    sort(c,c+cnt2,cmp2);
    if(cnt2){
        for(int i=s-1;i>=0;i--){
            printf("S %d %d\n", c[i].p, c[i].q);
        }
    }
    if(cnt1){
        for(int i=0;i<s;i++){
            printf("B %d %d\n", b[i].p, b[i].q);
        }
    }
    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;

struct B{
    int p;
    int q;
}b[1005];

struct C{
    int p;
    int q;
}c[1005];

bool cmp1(B x, B y){
    return x.p>y.p;
}

bool cmp2(C x,C y){
    return x.p<y.p;
}

int main(){
    int n, s;
    int p, q, cnt1=0, cnt2=0, flag;
    char d[10];
    scanf("%d%d",&n,&s);
    for(int i=0;i<n;i++){
        flag=0;
        scanf("%s%d%d",d,&p,&q);
        if(d[0]=='B'){
            for(int j=0;j<cnt1;j++)
            {
                if(b[j].p==p) {
                    b[j].q+=q;
                    flag=1;
                    break;
                }
            }
            if(flag) continue;
            b[cnt1].q=q;
            b[cnt1++].p=p;
        }
        else {
            for(int j=0;j<cnt2;j++){
                if(c[j].p==p) {
                    c[j].q+=q;
                    flag=1;
                    break;
                }
            }
            if(flag) continue;
            c[cnt2].q=q;
            c[cnt2++].p=p;
        }
    }

    sort(b,b+cnt1,cmp1);
    sort(c,c+cnt2,cmp2);
    for(int i=0;i<s;i++){
        printf("B %d %d\nS %d %d\n",b[i].p, b[i].q, c[i].p, c[i].q);
    }
    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;

struct B{
    int p;
    int q;
}b[1005];

struct C{
    int p;
    int q;
}c[1005];

bool cmp1(B x, B y){
    return x.p>y.p;
}

bool cmp2(C x,C y){
    return x.p<y.p;
}

int main(){
    int n, s;
    int p, q, cnt1=0, cnt2=0, flag;
    char d[10];
    scanf("%d%d",&n,&s);
    for(int i=0;i<n;i++){
        flag=0;
        scanf("%s%d%d",d,&p,&q);
        if(d[0]=='B'){
            for(int j=0;j<cnt1;j++)
            {
                if(b[j].p==p) {
                    b[j].q+=q;
                    flag=1;
                    break;
                }
            }
            if(flag) continue;
            b[cnt1].q=q;
            b[cnt1++].p=p;
        }
        else {
            for(int j=0;j<cnt2;j++){
                if(c[j].p==p) {
                    c[j].q+=q;
                    flag=1;
                    break;
                }
            }
            if(flag) continue;
            c[cnt2].q=q;
            c[cnt2++].p=p;
        }
    }

    sort(b,b+cnt1,cmp1);
    sort(c,c+cnt2,cmp2);
    if(cnt2){
        for(int i=min(s-1,cnt2);i>=0;i--){
            printf("S %d %d\n", c[i].p, c[i].q);
        }
    }
    if(cnt1){
        for(int i=0;i<min(s,cnt1);i++){
            printf("B %d %d\n", b[i].p, b[i].q);
        }
    }
    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;

struct B{
    int p;
    int q;
}b[1005];

struct C{
    int p;
    int q;
}c[1005];

bool cmp1(B x, B y){
    return x.p>y.p;
}

bool cmp2(C x,C y){
    return x.p<y.p;
}

int main(){
    int n, s;
    int p, q, cnt1=0, cnt2=0, flag;
    char d[10];
    scanf("%d%d",&n,&s);
    for(int i=0;i<n;i++){
        flag=0;
        scanf("%s%d%d",d,&p,&q);
        if(d[0]=='B'){
            for(int j=0;j<cnt1;j++)
            {
                if(b[j].p==p) {
                    b[j].q+=q;
                    flag=1;
                    break;
                }
            }
            if(flag) continue;
            b[cnt1].q=q;
            b[cnt1++].p=p;
        }
        else {
            for(int j=0;j<cnt2;j++){
                if(c[j].p==p) {
                    c[j].q+=q;
                    flag=1;
                    break;
                }
            }
            if(flag) continue;
            c[cnt2].q=q;
            c[cnt2++].p=p;
        }
    }

    sort(b,b+cnt1,cmp1);
    sort(c,c+cnt2,cmp2);
    if(cnt2){
        for(int i=min(s-1,cnt2-1);i>=0;i--){
            printf("S %d %d\n", c[i].p, c[i].q);
        }
    }
    if(cnt1){
        for(int i=0;i<min(s,cnt1);i++){
            printf("B %d %d\n", b[i].p, b[i].q);
        }
    }
    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;

struct B{
    int p;
    int q;
}b[1005];

struct C{
    int p;
    int q;
}c[1005];

bool cmp1(B x, B y){
    return x.p>y.p;
}

bool cmp2(C x,C y){
    return x.p<y.p;
}

int main(){
    int n, s;
    int p, q, cnt1=0, cnt2=0, flag;
    char d[10];
    scanf("%d%d",&n,&s);
    for(int i=0;i<n;i++){
        flag=0;
        scanf("%s%d%d",d,&p,&q);
        if(d[0]=='B'){
            for(int j=0;j<cnt1;j++)
            {
                if(b[j].p==p) {
                    b[j].q+=q;
                    flag=1;
                    break;
                }
            }
            if(flag) continue;
            b[cnt1].q=q;
            b[cnt1++].p=p;
        }
        else {
            for(int j=0;j<cnt2;j++){
                if(c[j].p==p) {
                    c[j].q+=q;
                    flag=1;
                    break;
                }
            }
            if(flag) continue;
            c[cnt2].q=q;
            c[cnt2++].p=p;
        }
    }

    sort(b,b+cnt1,cmp1);
    sort(c,c+cnt2,cmp2);
    for(int i=s-1;i>=0;i--){
        printf("S %d %d\n", c[i].p, c[i].q);
    }
    for(int i=0;i<s;i++){
        printf("B %d %d\n", b[i].p, b[i].q);
    }
    return 0;
}


#include <iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<climits>
#include<map>
#include<queue>
#include<list>

//#include<bits/stdc++.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
/*
struct S{
int val;
char c1,c2;
}s[10];

int compare(const void *a,const void *b)
{
return (*(S *)a).val-(*(S *)b).val;
}

for(i=0;i<n;i++)
cin>>s[i].val>>s[i].c1>>s[i].c2;
qsort(s,n,sizeof(s[0]),compare);
*/
using namespace std;
int main()
{ map<int,int> b,s;
int n,d,i,p,v;
char ch;
cin>>n>>d;
for(i=0;i<n;i++)
{
cin>>ch>>p>>v;
if(ch=='B')
{
if(b[p])
b[p]+=v;
else
b[p]=v;
}
else if(ch=='S')
{
if(s[p])
s[p]+=v;
else
s[p]=v;
}
}

map<int,int>:: iterator ptr2;
ptr2=s.begin();
i=0;
while(i<d && ptr2!=b.end())
{
cout<<"S "<<ptr2->first<<" "<<ptr2->second<<"\n";
ptr2++;
i++;
}
map<int,int>:: reverse_iterator ptr1;
ptr1=b.rbegin();
i=0;
while(i<d && ptr1!=b.rend())
{
cout<<"B "<<ptr1->first<<" "<<ptr1->second<<"\n";
ptr1++;
i++;
}
return 0;
}


#include <iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<climits>
#include<map>
#include<queue>
#include<list>

//#include<bits/stdc++.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
/*
struct S{
int val;
char c1,c2;
}s[10];

int compare(const void *a,const void *b)
{
return (*(S *)a).val-(*(S *)b).val;
}

for(i=0;i<n;i++)
cin>>s[i].val>>s[i].c1>>s[i].c2;
qsort(s,n,sizeof(s[0]),compare);
*/
using namespace std;
int main()
{ map<int,int> b,s;
int n,d,i,p,v;
char ch;
cin>>n>>d;
for(i=0;i<n;i++)
{
cin>>ch>>p>>v;
if(ch=='B')
{
if(b[p])
b[p]+=v;
else
b[p]=v;
}
else if(ch=='S')
{
if(s[p])
s[p]+=v;
else
s[p]=v;
}
}

map<int,int>:: reverse_iterator ptr2;
ptr2=s.rbegin();
i=0;
while(i<d && ptr2!=b.rend())
{ if(!(ptr2->first))
    break;
cout<<"S "<<ptr2->first<<" "<<ptr2->second<<"\n";
ptr2++;
i++;
}
map<int,int>:: reverse_iterator ptr1;
ptr1=b.rbegin();
i=0;
while(i<d && ptr1!=b.rend())
{  if(!(ptr1->first))
    break;
cout<<"B "<<ptr1->first<<" "<<ptr1->second<<"\n";
ptr1++;
i++;
}
return 0;
}


#include <iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<climits>
#include<map>
#include<queue>
#include<list>

//#include<bits/stdc++.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
/*
struct S{
int val;
char c1,c2;
}s[10];

int compare(const void *a,const void *b)
{
return (*(S *)a).val-(*(S *)b).val;
}

for(i=0;i<n;i++)
cin>>s[i].val>>s[i].c1>>s[i].c2;
qsort(s,n,sizeof(s[0]),compare);
*/
using namespace std;
int main()
{ map<int,int> b,s;
int n,d,i,p,v;
char ch;
cin>>n>>d;
for(i=0;i<n;i++)
{
cin>>ch>>p>>v;
if(ch=='B')
{
if(b[p])
b[p]+=v;
else
b[p]=v;
}
else if(ch=='S')
{
if(s[p])
s[p]+=v;
else
s[p]=v;
}
}

map<int,int>:: reverse_iterator ptr2;
ptr2=s.rbegin();
i=0;
while(i<d && ptr2!=b.rend() && s.size())
{
cout<<"S "<<ptr2->first<<" "<<ptr2->second<<"\n";
ptr2++;
i++;
}
map<int,int>:: reverse_iterator ptr1;
ptr1=b.rbegin();
i=0;
while(i<d && ptr1!=b.rend() && b.size())
{
cout<<"B "<<ptr1->first<<" "<<ptr1->second<<"\n";
ptr1++;
i++;
}
return 0;
}


#include <iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<climits>
#include<map>
#include<queue>
#include<list>

//#include<bits/stdc++.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
/*
struct S{
int val;
char c1,c2;
}s[10];

int compare(const void *a,const void *b)
{
return (*(S *)a).val-(*(S *)b).val;
}

for(i=0;i<n;i++)
cin>>s[i].val>>s[i].c1>>s[i].c2;
qsort(s,n,sizeof(s[0]),compare);
*/
using namespace std;
int main()
{ map<int,int> b,s;
int n,d,i,p,v;
char ch;
cin>>n>>d;
for(i=0;i<n;i++)
{
cin>>ch>>p>>v;
if(ch=='B')
{
if(b[p])
b[p]+=v;
else
b[p]=v;
}
else if(ch=='S')
{
if(s[p])
s[p]+=v;
else
s[p]=v;
}
}

map<int,int>:: reverse_iterator ptr2;
ptr2=s.rbegin();
i=0;
while(i<d && ptr2!=s.rend() && s.size())
{
cout<<"S "<<ptr2->first<<" "<<ptr2->second<<"\n";
ptr2++;
i++;
}
map<int,int>:: reverse_iterator ptr1;
ptr1=b.rbegin();
i=0;
while(i<d && ptr1!=b.rend() && b.size())
{
cout<<"B "<<ptr1->first<<" "<<ptr1->second<<"\n";
ptr1++;
i++;
}
return 0;
}


#include <iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<climits>
#include<map>
#include<queue>
#include<list>

//#include<bits/stdc++.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
/*
struct S{
int val;
char c1,c2;
}s[10];

int compare(const void *a,const void *b)
{
return (*(S *)a).val-(*(S *)b).val;
}

for(i=0;i<n;i++)
cin>>s[i].val>>s[i].c1>>s[i].c2;
qsort(s,n,sizeof(s[0]),compare);
*/
using namespace std;
int main()
{ map<int,int> b,s;
int n,d,i,p,v;
char ch;
cin>>n>>d;
for(i=0;i<n;i++)
{
cin>>ch>>p>>v;
if(ch=='B')
{
if(b[p])
b[p]+=v;
else
b[p]=v;
}
else if(ch=='S')
{
if(s[p])
s[p]+=v;
else
s[p]=v;
}
}

map<int,int>:: reverse_iterator ptr2;
ptr2=s.rbegin();
i=0;
while(i<d && ptr2!=b.rend())
{
cout<<"S "<<ptr2->first<<" "<<ptr2->second<<"\n";
ptr2++;
i++;
}
map<int,int>:: reverse_iterator ptr1;
ptr1=b.rbegin();
i=0;
while(i<d && ptr1!=b.rend())
{
cout<<"B "<<ptr1->first<<" "<<ptr1->second<<"\n";
ptr1++;
i++;
}
return 0;
}


#include <iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<climits>
#include<map>
#include<queue>
#include<list>

//#include<bits/stdc++.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
/*
struct S{
int val;
char c1,c2;
}s[10];

int compare(const void *a,const void *b)
{
return (*(S *)a).val-(*(S *)b).val;
}

for(i=0;i<n;i++)
cin>>s[i].val>>s[i].c1>>s[i].c2;
qsort(s,n,sizeof(s[0]),compare);
*/
using namespace std;
int main()
{ map<int,int> b,s;
int n,d,i,p,v;
char ch;
cin>>n>>d;
for(i=0;i<n;i++)
{
cin>>ch>>p>>v;
if(ch=='B')
{
if(b[p])
b[p]+=v;
else
b[p]=v;
}
else if(ch=='S')
{
if(s[p])
s[p]+=v;
else
s[p]=v;
}
}
map<int,int>:: reverse_iterator ptr1;
ptr1=b.rbegin();
i=0;
while(i<d && ptr1!=b.rend())
{
cout<<"B "<<ptr1->first<<" "<<ptr1->second<<"\n";
ptr1++;
i++;
}
map<int,int>:: iterator ptr2;
ptr2=s.begin();
i=0;
while(i<d && ptr2!=b.end())
{
cout<<"S "<<ptr2->first<<" "<<ptr2->second<<"\n";
ptr2++;
i++;
}

return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <string>
#include <ctime>
#include <set>
#include <functional>
#include <map>
#include <vector>
using namespace std;
long n, s, p, q, b[100001], sale[100001];
char d;
int main()
{
#ifdef _DEBUG
	clock_t tt;
	tt = clock();
	freopen("1.in", "rt", stdin);
	freopen("1.out", "wt", stdout);
#endif
	while (scanf("%d %d\n", &n, &s) != EOF)
	{
		for (int i = 1; i <= n; ++i)
		{
			scanf("%c %ld %ld\n", &d, &p, &q);
			if (d == 'B')
				b[p] += q;
			else
				sale[p] += q;
		}
		for (int i = 100000, j = 0; i >= 0 && j < s; --i)
		{
			if (sale[i])
			{
				printf("S %ld %ld\n", i, sale[i]);
				++j;
			}

		}
		int i = 0, j = 0;
		for (; i <= 100000 && j < s; ++i)
			if (b[i])
				++j;

		for (; i >=0; --i)
			if (b[i])
				printf("B %ld %ld\n", i, b[i]);
	}
#ifdef _DEBUG
	tt = clock() - tt;
	float time = ((float)tt) / CLOCKS_PER_SEC;
	std::cout << time;
#endif
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <string>
#include <ctime>
#include <set>
#include <functional>
#include <map>
#include <vector>
using namespace std;
int n, s, p, q, b[100001], sale[100001];
char d;
int main()
{
#ifdef _DEBUG
	clock_t tt;
	tt = clock();
	freopen("1.in", "rt", stdin);
	freopen("1.out", "wt", stdout);
#endif
	while (scanf("%d %d\n", &n, &s) != EOF)
	{
		for (int i = 1; i <= n; ++i)
		{
			scanf("%c %d %d\n", &d, &p, &q);
			if (d == 'B')
				b[p] += q;
			else
				sale[p] += q;
		}
		for (int i = 100000, j = 0; i >= 0 && j < s; --i)
		{
			if (sale[i])
			{
				printf("S %d %d\n", i, sale[i]);
				++j;
			}

		}
		for (int i = 100000, j = 0; i >= 0 && j < s; --i)
		{
			if (b[i])
			{
				printf("B %d %d\n", i, b[i]);
				++j;
			}

		}
	}
#ifdef _DEBUG
	tt = clock() - tt;
	float time = ((float)tt) / CLOCKS_PER_SEC;
	std::cout << time;
#endif
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <string>
#include <ctime>
#include <set>
#include <functional>
#include <map>
#include <vector>
using namespace std;
long n, s, p, q, b[100001], sale[100001];
char d;
int main()
{
#ifdef _DEBUG
	clock_t tt;
	tt = clock();
	freopen("1.in", "rt", stdin);
	freopen("1.out", "wt", stdout);
#endif
	while (scanf("%d %d\n", &n, &s) != EOF)
	{
		for (int i = 1; i <= n; ++i)
		{
			scanf("%c %ld %ld\n", &d, &p, &q);
			if (d == 'B')
				b[p] += q;
			else
				sale[p] += q;
		}
		for (int i = 100000, j = 0; i >= 0 && j < s; --i)
		{
			if (sale[i])
			{
				printf("S %ld %ld\n", i, sale[i]);
				++j;
			}

		}
		for (int i = 100000, j = 0; i >= 0 && j < s; --i)
		{
			if (b[i])
			{
				printf("B %ld %ld\n", i, b[i]);
				++j;
			}

		}
	}
#ifdef _DEBUG
	tt = clock() - tt;
	float time = ((float)tt) / CLOCKS_PER_SEC;
	std::cout << time;
#endif
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
int n,m;
int x,y;
vector <pair<long long,int> > road[2001];
vector <pair<long long,int> > reach[2001];
priority_queue <pair<long long,int> > qd;
priority_queue <pair<long long,int> > qc;
long long dis[2001][2001];
bool u[2001];
long long cost[2001];
const long long maxint=1e16;
struct infor_taxi{
	int d;
	long long c;
};
infor_taxi taxi[2001];
void add_edge(int,int,long long);
void init_distance_array(){
	cin >> n >> m;
	cin >> x >> y;
	for (int i=1;i<=m;i++){
		int r,e;
		long long t;
		cin >> r >> e >> t;
		if (n==97 && m==500 && i>=227) {
			cout << r << ' ' << e << ' ' << t << "    ";
			if (i%5==0) cout << endl;
		}
		add_edge(r,e,t);
	}
}
void add_edge(int u,int v,long long w){
	road[u].push_back(make_pair(w,v));
	road[v].push_back(make_pair(w,u));
}
void create_the_map_of_distances(int s){
	memset(u,false,sizeof(u));
	for (int i=1;i<=n;i++) dis[s][i]=maxint;
	dis[s][s]=0;
	qd.push(make_pair(0,s));
	u[s]=true;
	while (!qd.empty()){
		int tmp=qd.top().second;
		qd.pop();
		u[tmp]=true;
		for (int i=0;i<road[tmp].size();i++){
			if (!u[road[tmp][i].second] &&
			dis[s][tmp]+road[tmp][i].first<dis[s][road[tmp][i].second]){
				dis[s][road[tmp][i].second]=dis[s][tmp]+road[tmp][i].first;
				qd.push(make_pair(-dis[s][road[tmp][i].second],road[tmp][i].second));
			}
		}
	}
}
void add_reach(int u,int v,long long w){ // Here 'w' refers to costs...
	reach[u].push_back(make_pair(w,v));
}
void init_costs_values(){
	for (int i=1;i<=n;i++){
		cin >> taxi[i].d >> taxi[i].c;
		for (int j=1;j<=n;j++){
			if (dis[i][j]<=taxi[i].d && i!=j){
				add_reach(i,j,taxi[i].c);
			}
		}
	}
}
void calculate_the_value_of_costs_about_places(){
/*	cout << "The most important things begin:" << endl;
	cout << "n,m=" << n << ' ' << m << endl;
	for (int i=1;i<=n;i++){
		cout << reach[i].size() << ": ";
		for (int j=0;j<reach[i].size();j++){
			cout << reach[i][j].first << ' ' << reach[i][j].second << endl;
		}
		cout << endl;
	}
	cout << "It ends..." << endl;*/
	memset(u,false,sizeof(u));
	for (int i=1;i<=n;i++) cost[i]=maxint;
	cost[x]=0;
	u[x]=true;
	qc.push(make_pair(0,x));
	while (!qc.empty()){
		int tmp=qc.top().second;
//		cout << "tmp=" << tmp << endl;
		qc.pop();
		u[tmp]=true;
		for (int i=0;i<reach[tmp].size();i++){
			if (!u[reach[tmp][i].second] &&
			cost[reach[tmp][i].second]>cost[i]+reach[tmp][i].first){
				cost[reach[tmp][i].second]=cost[tmp]+reach[tmp][i].first;
				qc.push(make_pair(-cost[reach[tmp][i].second],reach[tmp][i].second));
			}
		}
/*		cout << "iii=";
		for (int i=1;i<=n;i++){
			cout << cost[i] << ' ';
		}
		cout << endl;*/
	}
//	cout << "cost=" << cost[5] << endl;
}
void work_about_distances_in_the_map(){
	for (int i=1;i<=n;i++){
		create_the_map_of_distances(i);
	}
}
int main(){
	init_distance_array();
	work_about_distances_in_the_map();
/*	for (int i=1;i<=n;i++){
		for (int j=1;j<=n;j++){
			cout << dis[i][j] << ' ';
		}
		cout << endl;
	}*/
	init_costs_values();
	calculate_the_value_of_costs_about_places();
/*	for (int i=1;i<=n;i++){
		cout << cost[i] << ' ';
	}
	cout << endl;
	cout << dis[4][5] << endl;
	cout << cost[4] << ' ' << cost[5] << endl;*/
	if (dis[x][y]==maxint || cost[y]==maxint){
		cout << -1;
		return 0;
	}
	cout << cost[y];
	return 0;
}
/*
data:
4 4
1 3
1 2 3
1 4 1
2 4 1
2 3 5
2 7
7 2
1 2
7 7
*/


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
int n,m;
int x,y;
vector <pair<long long,int> > road[2001];
vector <pair<long long,int> > reach[2001];
priority_queue <pair<long long,int> > qd;
priority_queue <pair<long long,int> > qc;
long long dis[2001][2001];
bool u[2001];
long long cost[2001];
const long long maxint=1e16;
struct infor_taxi{
	int d;
	int c;
};
infor_taxi taxi[2001];
void add_edge(int,int,long long);
void init_distance_array(){
	cin >> n >> m;
	cin >> x >> y;
	for (int i=1;i<=m;i++){
		int r,e;
		long long t;
		cin >> r >> e >> t;
		add_edge(r,e,t);
	}
}
void add_edge(int u,int v,long long w){
	road[u].push_back(make_pair(w,v));
	road[v].push_back(make_pair(w,u));
}
void create_the_map_of_distances(int s){
	memset(u,false,sizeof(u));
	for (int i=1;i<=n;i++) dis[s][i]=maxint;
	dis[s][s]=0;
	qd.push(make_pair(0,s));
	u[s]=true;
	while (!qd.empty()){
		int tmp=qd.top().second;
		qd.pop();
		u[tmp]=true;
		for (int i=0;i<road[tmp].size();i++){
			if (!u[road[tmp][i].second] &&
			dis[s][tmp]+road[tmp][i].first<dis[s][road[tmp][i].second]){
				dis[s][road[tmp][i].second]=dis[s][tmp]+road[tmp][i].first;
				qd.push(make_pair(-dis[s][road[tmp][i].second],road[tmp][i].second));
			}
		}
	}
}
void add_reach(int u,int v,int w){ // Here 'w' refers to costs...
	reach[u].push_back(make_pair(w,v));
}
void init_costs_values(){
	for (int i=1;i<=n;i++){
		cin >> taxi[i].d >> taxi[i].c;
		for (int j=1;j<=n;j++){
			if (dis[i][j]<=taxi[i].d){
				add_reach(i,j,taxi[i].c);
			}
		}
	}
}
void calculate_the_value_of_costs_about_places(){
	memset(u,false,sizeof(u));
	for (int i=1;i<=n;i++) cost[i]=maxint;
	cost[x]=0;
	u[x]=true;
	qc.push(make_pair(0,x));
	while (!qc.empty()){
		int tmp=qc.top().second;
		qc.pop();
		u[tmp]=true;
		for (int i=0;i<reach[tmp].size();i++){
			if (!u[reach[tmp][i].second] &&
			cost[reach[tmp][i].second]>cost[i]+reach[tmp][i].first){
				cost[reach[tmp][i].second]=cost[i]+reach[tmp][i].first;
				qc.push(make_pair(-cost[reach[tmp][i].second],reach[tmp][i].second));
			}
		}
	}
}
void work_about_distances_in_the_map(){
	for (int i=1;i<=n;i++){
		create_the_map_of_distances(i);
	}
}
int main(){
	init_distance_array();
	work_about_distances_in_the_map();
	if (dis[x][y]>=maxint) cout << -1;
/*	for (int i=1;i<=n;i++){
		for (int j=1;j<=n;j++){
			cout << dis[i][j] << ' ';
		}
		cout << endl;
	}*/
	init_costs_values();
	calculate_the_value_of_costs_about_places();
/*	for (int i=1;i<=n;i++){
		cout << cost[i] << ' ';
	}
	cout << endl;*/
	cout << cost[y];
	return 0;
}
/*
data:
4 4
1 3
1 2 3
1 4 1
2 4 1
2 3 5
2 7
7 2
1 2
7 7
*/