#include <iostream>

using namespace std;

int buy[100007];
int sell[100007];
int sellout[100007];

int main()
{
    freopen("in.txt", "r", stdin);
    int n, s;
    cin >> n >> s;
    char d;
    int p, q;
    for(int i = 0; i < n; i++)
    {
        cin >> d >> p >> q;
        if(d == 'B')
            buy[p] += q;
        else
            sell[p] += q;
    }
    int sellnum = s;
    for(int i = 0; i <= 1e5 && sellnum; i++)
        if(sell[i])
        {
            sellout[i] = sell[i];
            sellnum--;
        }
    for(int i = 1e5; i >= 0; i--)
        if(sellout[i])
            cout << "S " << i << ' ' << sellout[i] << endl;
    int buynum = s;
    for(int i = 1e5; i >= 0 && buynum; i--)
        if(buy[i])
        {
            cout << "B " << i << ' ' << buy[i] << endl;
            buynum--;
        }
    return 0;
}


#include <iostream>

using namespace std;

int buy[100007];
int sell[100007];
int sellout[100007];
int main()
{
    int n, s;
    cin >> n >> s;
    char d;
    int p, q;
    for(int i = 0; i < n; i++)
    {
        cin >> d >> p >> q;
        if(d == 'B')
            buy[p] += q;
        else
            sell[p] += q;
    }
    int sellnum = s;
    for(int i = 0; i <= 1e5 && sellnum; i++)
        if(sell[i])
        {
            sellout[i] = sell[i];
            sellnum--;
        }
    for(int i = 1e5; i >= 0; i--)
        if(sell[i])
            cout << "S " << i << ' ' << sell[i] << endl;
    int buynum = s;
    for(int i = 1e5; i >= 0 && buynum; i--)
        if(buy[i])
        {
            cout << "B " << i << ' ' << buy[i] << endl;
            buynum--;
        }
    return 0;
}


#include <iostream>

using namespace std;

__int64 buy[100007];
__int64 sell[100007];

int main()
{
    //freopen("in.txt", "r", stdin);
    __int64 n, s;
    cin >> n >> s;
    char d;
    __int64 p, q;
    for(__int64 i = 0; i < n; i++)
    {
        cin >> d >> p >> q;
        if(d == 'B')
            buy[p] += q;
        else
            sell[p] += q;
    }
    __int64 sellnum = s;
    for(__int64 i = 1e5; i >= 0 && sellnum; i--)
        if(sell[i])
        {
            cout << "S " << i << ' ' << sell[i] << endl;
            sellnum--;
        }
    __int64 buynum = s;
    for(__int64 i = 1e5; i >= 0 && buynum; i--)
        if(buy[i])
        {
            cout << "B " << i << ' ' << buy[i] << endl;
            buynum--;
        }
    return 0;
}


#include <iostream>

using namespace std;

__int64 buy[100007];
__int64 sell[100007];

__int64 main()
{
    //freopen("in.txt", "r", stdin);
    __int64 n, s;
    cin >> n >> s;
    char d;
    __int64 p, q;
    for(__int64 i = 0; i < n; i++)
    {
        cin >> d >> p >> q;
        if(d == 'B')
            buy[p] += q;
        else
            sell[p] += q;
    }
    __int64 sellnum = s;
    for(__int64 i = 1e5; i >= 0 && sellnum; i--)
        if(sell[i])
        {
            cout << "S " << i << ' ' << sell[i] << endl;
            sellnum--;
        }
    __int64 buynum = s;
    for(__int64 i = 1e5; i >= 0 && buynum; i--)
        if(buy[i])
        {
            cout << "B " << i << ' ' << buy[i] << endl;
            buynum--;
        }
    return 0;
}


#include <iostream>

using namespace std;

int buy[100007];
int sell[100007];

int main()
{
    int n, s;
    cin >> n >> s;
    char d;
    int p, q;
    for(int i = 0; i < n; i++)
    {
        cin >> d >> p >> q;
        if(d == 'B')
            buy[p] += q;
        else
            sell[p] += q;
    }
    int sellnum = s;
    for(int i = 1e5; i >= 0 && sellnum; i--)
        if(sell[i])
        {
            cout << "S " << i << ' ' << sell[i] << endl;
            sellnum--;
        }
    int buynum = s;
    for(int i = 1e5; i >= 0 && buynum; i--)
        if(buy[i])
        {
            cout << "B " << i << ' ' << buy[i] << endl;
            buynum--;
        }
    return 0;
}


#include <iostream>

using namespace std;

int buy[100007];
int sell[100007];
int sellout[100007];

int main()
{
    int n, s;
    cin >> n >> s;
    char d;
    int p, q;
    for(int i = 0; i < n; i++)
    {
        cin >> d >> p >> q;
        if(d == 'B')
            buy[p] += q;
        else
            sell[p] += q;
    }
    int sellnum = s;
    for(int i = 0; i <= 1e5 && sellnum; i++)
        if(sell[i])
        {
            sellout[i] = sell[i];
            sellnum--;
        }
    for(int i = 1e5; i >= 0; i--)
        if(sellout[i])
            cout << "S " << i << ' ' << sellout[i] << endl;
    int buynum = s;
    for(int i = 1e5; i >= 0 && buynum; i--)
        if(buy[i])
        {
            cout << "B " << i << ' ' << buy[i] << endl;
            buynum--;
        }
    return 0;
}


/*
*
* solved by Kareem M. Mohsen
*/
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<cstdio>
#include<climits>
#include<cstdlib>
#include<cmath>
#include<iomanip>
#include<sstream>
#include<cstring>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<bitset>
#include<queue>
#include<stack>
#include<utility>
#include<algorithm>
#include<functional>
#include<unordered_map>
#include<iterator>
#include<ctype.h>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>
#include<numeric>
#include <math.h>

using namespace std;

typedef long long int      ll;
typedef vector<double>     vd;
typedef vector<string>     vs;
typedef pair <ll, ll >     ii;
typedef pair<string, ll >  si;
typedef pair<ll, string>   is;
typedef pair<char, ll >    chi;
typedef vector<ll >        vi;
typedef vector<vi>         vvi;
typedef vector< ii >       vii;
#define KareemMohsen	   int main()
#define NoSync_NoTie	   ios_base::sync_with_stdio(0); cin.tie(0);
#define x				   first
#define y                  second
#define all(v)             v.begin(),v.end()
#define sz(V)              ((ll )((V).size()))
#define pp(x)              push_back(x)
#define lop(i,n)           for(ll i=0;i<n;++i)
#define loop(i,f,l)        for(ll i=f;i<l;++i)
#define PI                 3.141592653589793
#define print(v)           lop(i, v.size()) cout<<v[i]<<" ";
#define read(v)            lop(i, (ll)v.size()) cin>>v[i];
#define INF                (1<<59)
#define CLR(v, d)          memset(v, d, sizeof(v))
double const EPS = 2.22045e-016;
unsigned long long int fact(unsigned long long int n){ if (n == 1 || n == 0)return 1; return n*fact(n - 1); }
ll  gcd(ll  a, ll  b) { return (b == 0 ? a : gcd(b, a % b)); }
ll  lcm(ll  a, ll  b){ return ((a*b) / gcd(a, b)); }
bool comp(ll x, ll y){ return x>y; }
string int2str(ll n){ std::ostringstream stm; stm << n; return stm.str(); }
ll  str2int(string s){ istringstream ss(s); ll t; ss >> t; return t; }
ll  pw(ll  b, ll  p){ if (!p) return 1; ll  sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll  sd(ll  x){ return x<10 ? x : x % 10 + sd(x / 10); }
ll  sq(ll  x){ lop(i, x)if ((ll)i*i > x) return (i - 1); return ll(1); }
bool is_integer(float k){ return std::floor(k) == k; }

ll nCr(ll n, ll r)
{
	ll res = 1;
	lop(i, r) res = res * (n - i) / (i + 1);
	return res;
}
vector<bool> vis;
vi arr, conc; ll n, x;
ll ans = 0;

ll pww(ll n, ll mx)
{
	if (n == 0)	return 2;
	if (n == mx)return 2 * pww(n - 1, mx) - 1;
	else return 2 * pww(n - 1, mx);
}

KareemMohsen{
	NoSync_NoTie
#ifndef ONLINE_JUDGE
	ifstream cin("input.txt");
	ofstream cout("output.txt");
#endif
	ll n, s; cin >> n >> s;
	map<ll, ll> S, B;
	lop(i, n){
		char dummy; cin >> dummy;
		if (dummy == 'S'){
			ll x, y;
			cin >> x>> y;
			S[x] += y;
		}
		else{
			ll x, y;
			cin >> x>> y;
			B[x] += y;
		}
	}
	map<ll, ll>::iterator it = S.begin();
	vii ss, bb;
	for (it; it != S.end(); it++){
		ss.push_back(make_pair(it->first, it->second));
	}
	it = B.begin();
	for (it; it != B.end(); it++){
		bb.push_back(make_pair(it->first, it->second));
	}
	reverse(all(bb));
	for (ll q = min(s, (ll)ss.size()) - 1; q >= 0; q--){
		cout << "S " << ss[q].first << " " << ss[q].second << endl;
	}
	lop(w, min(s, (ll)bb.size())){
		cout << "B " << bb[w].first << " " << bb[w].second << endl;
	}
	return false;
}


/*
*
* solved by Kareem M. Mohsen
*/
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<cstdio>
#include<climits>
#include<cstdlib>
#include<cmath>
#include<iomanip>
#include<sstream>
#include<cstring>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<bitset>
#include<queue>
#include<stack>
#include<utility>
#include<algorithm>
#include<functional>
#include<unordered_map>
#include<iterator>
#include<ctype.h>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>
#include<numeric>
#include <math.h>

using namespace std;

typedef long long int      ll;
typedef vector<double>     vd;
typedef vector<string>     vs;
typedef pair <ll, ll >     ii;
typedef pair<string, ll >  si;
typedef pair<ll, string>   is;
typedef pair<char, ll >    chi;
typedef vector<ll >        vi;
typedef vector<vi>         vvi;
typedef vector< ii >       vii;
#define KareemMohsen	   int main()
#define NoSync_NoTie	   ios_base::sync_with_stdio(0); cin.tie(0);
#define x				   first
#define y                  second
#define all(v)             v.begin(),v.end()
#define sz(V)              ((ll )((V).size()))
#define pp(x)              push_back(x)
#define lop(i,n)           for(ll i=0;i<n;++i)
#define loop(i,f,l)        for(ll i=f;i<l;++i)
#define PI                 3.141592653589793
#define print(v)           lop(i, v.size()) cout<<v[i]<<" ";
#define read(v)            lop(i, (ll)v.size()) cin>>v[i];
#define INF                (1<<59)
#define CLR(v, d)          memset(v, d, sizeof(v))
double const EPS = 2.22045e-016;
unsigned long long int fact(unsigned long long int n){ if (n == 1 || n == 0)return 1; return n*fact(n - 1); }
ll  gcd(ll  a, ll  b) { return (b == 0 ? a : gcd(b, a % b)); }
ll  lcm(ll  a, ll  b){ return ((a*b) / gcd(a, b)); }
bool comp(ll x, ll y){ return x>y; }
string int2str(ll n){ std::ostringstream stm; stm << n; return stm.str(); }
ll  str2int(string s){ istringstream ss(s); ll t; ss >> t; return t; }
ll  pw(ll  b, ll  p){ if (!p) return 1; ll  sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll  sd(ll  x){ return x<10 ? x : x % 10 + sd(x / 10); }
ll  sq(ll  x){ lop(i, x)if ((ll)i*i > x) return (i - 1); return ll(1); }
bool is_integer(float k){ return std::floor(k) == k; }

ll nCr(ll n, ll r)
{
	ll res = 1;
	lop(i, r) res = res * (n - i) / (i + 1);
	return res;
}
vector<bool> vis;
vi arr, conc; ll n, x;
ll ans = 0;

ll pww(ll n, ll mx)
{
	if (n == 0)	return 2;
	if (n == mx)return 2 * pww(n - 1, mx) - 1;
	else return 2 * pww(n - 1, mx);
}

KareemMohsen{
	NoSync_NoTie
#ifndef ONLINE_JUDGE
	ifstream cin("input.txt");
	ofstream cout("output.txt");
#endif
	ll n, s; cin >> n >> s;
	map<ll, ll> S, B;
	lop(i, n){
		char dummy; cin >> dummy;
		if (dummy == 'S'){
			ll x, y;
			cin >> x>> y;
			S[x] += y;
		}
		else{
			ll x, y;
			cin >> x>> y;
			B[x] += y;
		}
	}
	map<ll, ll>::iterator it = S.begin();
	lop(i, s - 1)it++;
	for (; it != S.begin(); it--){
		cout << "S " << it->first << " " << it->second << endl;
	}
	cout << "S " << it->first << " " << it->second << endl;
	it = B.end();
	for (it--; it != B.begin(); it--){
		cout << "B " << it->first << " " << it->second << endl;
	}
	return false;
}


/*
*
* solved by Kareem M. Mohsen
*/
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<cstdio>
#include<climits>
#include<cstdlib>
#include<cmath>
#include<iomanip>
#include<sstream>
#include<cstring>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<bitset>
#include<queue>
#include<stack>
#include<utility>
#include<algorithm>
#include<functional>
#include<unordered_map>
#include<iterator>
#include<ctype.h>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>
#include<numeric>
#include <math.h>

using namespace std;

typedef long long int      ll;
typedef vector<double>     vd;
typedef vector<string>     vs;
typedef pair <ll, ll >     ii;
typedef pair<string, ll >  si;
typedef pair<ll, string>   is;
typedef pair<char, ll >    chi;
typedef vector<ll >        vi;
typedef vector<vi>         vvi;
typedef vector< ii >       vii;
#define KareemMohsen	   int main()
#define NoSync_NoTie	   ios_base::sync_with_stdio(0); cin.tie(0);
#define x				   first
#define y                  second
#define all(v)             v.begin(),v.end()
#define sz(V)              ((ll )((V).size()))
#define pp(x)              push_back(x)
#define lop(i,n)           for(ll i=0;i<n;++i)
#define loop(i,f,l)        for(ll i=f;i<l;++i)
#define PI                 3.141592653589793
#define print(v)           lop(i, v.size()) cout<<v[i]<<" ";
#define read(v)            lop(i, (ll)v.size()) cin>>v[i];
#define INF                (1<<59)
#define CLR(v, d)          memset(v, d, sizeof(v))
double const EPS = 2.22045e-016;
unsigned long long int fact(unsigned long long int n){ if (n == 1 || n == 0)return 1; return n*fact(n - 1); }
ll  gcd(ll  a, ll  b) { return (b == 0 ? a : gcd(b, a % b)); }
ll  lcm(ll  a, ll  b){ return ((a*b) / gcd(a, b)); }
bool comp(ll x, ll y){ return x>y; }
string int2str(ll n){ std::ostringstream stm; stm << n; return stm.str(); }
ll  str2int(string s){ istringstream ss(s); ll t; ss >> t; return t; }
ll  pw(ll  b, ll  p){ if (!p) return 1; ll  sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll  sd(ll  x){ return x<10 ? x : x % 10 + sd(x / 10); }
ll  sq(ll  x){ lop(i, x)if ((ll)i*i > x) return (i - 1); return ll(1); }
bool is_integer(float k){ return std::floor(k) == k; }

ll nCr(ll n, ll r)
{
	ll res = 1;
	lop(i, r) res = res * (n - i) / (i + 1);
	return res;
}
vector<bool> vis;
vi arr, conc; ll n, x;
ll ans = 0;

ll pww(ll n, ll mx)
{
	if (n == 0)	return 2;
	if (n == mx)return 2 * pww(n - 1, mx) - 1;
	else return 2 * pww(n - 1, mx);
}

KareemMohsen{
	NoSync_NoTie
#ifndef ONLINE_JUDGE
	ifstream cin("input.txt");
	ofstream cout("output.txt");
#endif
	ll n, s; cin >> n >> s;
	map<ll, ll> S, B;
	lop(i, n){
		char dummy; cin >> dummy;
		if (dummy == 'S'){
			ll x, y;
			cin >> x>> y;
			S[x] += y;
		}
		else{
			ll x, y;
			cin >> x>> y;
			B[x] += y;
		}
	}
	map<ll, ll>::iterator it = S.begin(), a;
	lop(i, s - 1)it++;
	for (; it != S.begin(); it--){
		cout << "S " << it->first << " " << it->second << endl;
	}
	cout << "S " << it->first << " " << it->second << endl;
	it = B.end(); a = B.begin();
	lop(i, s - 1)a++;
	for (it--; it != a; it--){
		cout << "B " << it->first << " " << it->second << endl;
	}
	cout << "B " << it->first << " " << it->second << endl;
	return false;
}


/*
*
* solved by Kareem M. Mohsen
*/
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<cstdio>
#include<climits>
#include<cstdlib>
#include<cmath>
#include<iomanip>
#include<sstream>
#include<cstring>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<bitset>
#include<queue>
#include<stack>
#include<utility>
#include<algorithm>
#include<functional>
#include<unordered_map>
#include<iterator>
#include<ctype.h>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>
#include<numeric>
#include <math.h>

using namespace std;

typedef long long int      ll;
typedef vector<double>     vd;
typedef vector<string>     vs;
typedef pair <ll, ll >     ii;
typedef pair<string, ll >  si;
typedef pair<ll, string>   is;
typedef pair<char, ll >    chi;
typedef vector<ll >        vi;
typedef vector<vi>         vvi;
typedef vector< ii >       vii;
#define KareemMohsen	   int main()
#define NoSync_NoTie	   ios_base::sync_with_stdio(0); cin.tie(0);
#define x				   first
#define y                  second
#define all(v)             v.begin(),v.end()
#define sz(V)              ((ll )((V).size()))
#define pp(x)              push_back(x)
#define lop(i,n)           for(ll i=0;i<n;++i)
#define loop(i,f,l)        for(ll i=f;i<l;++i)
#define PI                 3.141592653589793
#define print(v)           lop(i, v.size()) cout<<v[i]<<" ";
#define read(v)            lop(i, (ll)v.size()) cin>>v[i];
#define INF                (1<<59)
#define CLR(v, d)          memset(v, d, sizeof(v))
double const EPS = 2.22045e-016;
unsigned long long int fact(unsigned long long int n){ if (n == 1 || n == 0)return 1; return n*fact(n - 1); }
ll  gcd(ll  a, ll  b) { return (b == 0 ? a : gcd(b, a % b)); }
ll  lcm(ll  a, ll  b){ return ((a*b) / gcd(a, b)); }
bool comp(ll x, ll y){ return x>y; }
string int2str(ll n){ std::ostringstream stm; stm << n; return stm.str(); }
ll  str2int(string s){ istringstream ss(s); ll t; ss >> t; return t; }
ll  pw(ll  b, ll  p){ if (!p) return 1; ll  sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll  sd(ll  x){ return x<10 ? x : x % 10 + sd(x / 10); }
ll  sq(ll  x){ lop(i, x)if ((ll)i*i > x) return (i - 1); return ll(1); }
bool is_integer(float k){ return std::floor(k) == k; }

ll nCr(ll n, ll r)
{
	ll res = 1;
	lop(i, r) res = res * (n - i) / (i + 1);
	return res;
}
vector<bool> vis;
vi arr, conc; ll n, x;
ll ans = 0;

ll pww(ll n, ll mx)
{
	if (n == 0)	return 2;
	if (n == mx)return 2 * pww(n - 1, mx) - 1;
	else return 2 * pww(n - 1, mx);
}

KareemMohsen{
	NoSync_NoTie
#ifndef ONLINE_JUDGE
	ifstream cin("input.txt");
	ofstream cout("output.txt");
#endif
	ll n, s; cin >> n >> s;
	map<ll, ll> S, B;
	lop(i, n){
		char dummy; cin >> dummy;
		if (dummy == 'S'){
			ll x, y;
			cin >> x>> y;
			S[x] += y;
		}
		else{
			ll x, y;
			cin >> x>> y;
			B[x] += y;
		}
	}
	map<ll, ll>::iterator it = S.begin(), a;
	lop(i, s - 1)it++;
	if (S.size() > 0)
	{
		for (; it != S.begin(); it--){
			cout << "S " << it->first << " " << it->second << endl;
		}
		cout << "S " << it->first << " " << it->second << endl;

	}
	it = B.end(); a = B.begin();
	lop(i, s - 1)a++;
	if (B.size() > 0){
		for (it--; it != a; it--){
			cout << "B " << it->first << " " << it->second << endl;
		}
		cout << "B " << it->first << " " << it->second << endl;
	}
	return false;
}


#include <iostream>
#include <set>
using namespace std;
int s[100002];
int b[100002];
set < int > ss, bb;
pair < int , int > sans[52];
char c;
int n;
int a, q;
int S;
int main(){
    cin >> n >> S;
    for(int i = 1; i <= n; ++i){
        cin >> c >> a >> q;
        if(c == 'S')
            ss.insert(a), s[a] += q;
        else
            bb.insert(-a), b[a] += q;
    }
    int t = 0;
    for(set < int > ::iterator it = ss.begin(); it != ss.end(); ++it){
        int k = *(it);
        t++;
        if(t > S)
            break;
        else
            sans[t] = make_pair(k,s[k]);
    }
    for(int i = t; i > 0; --i)
        cout << "S " << sans[i].first << " " << sans[i].second << endl;
    t = 0;
    for(set < int > ::iterator it = bb.begin(); it != bb.end(); ++it){
        int k = *(it);
        t++;
        if(t > S)
            break;
        else
            cout << "B " << -k << " " << b[-k] << endl;
    }
    return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
map < int , int > s, b;
set < int > ss, bb;
pair < int , int > sans[100002];
char c;
int n;
int a, q;
int S;
int main(){
    cin >> n >> S;
    for(int i = 1; i <= n; ++i){
        cin >> c >> a >> q;
        if(c == 'S')
            ss.insert(a), s[a] += q;
        else
            bb.insert(-a), b[a] += q;
    }
    int t = 0;
    for(set < int > ::iterator it = ss.begin(); it != ss.end(); ++it){
        int k = *(it);
        t++;
        if(t > S)
            break;
        else
            sans[t] = make_pair(k,s[k]);
    }
    for(int i = t; i > 0; --i)
        cout << "S " << sans[i].first << " " << sans[i].second << endl;
    t = 0;
    for(set < int > ::iterator it = bb.begin(); it != bb.end(); ++it){
        int k = *(it);
        t++;
        if(t > S)
            break;
        else
            cout << "B " << -k << " " << b[-k] << endl;
    }
    return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
map < int , int > s, b;
set < int > ss, bb;
pair < int , int > sans[100002], bans[100002];
char c;
int n;
int a, q;
int S;
int main(){
    cin >> n >> S;
    for(int i = 1; i <= n; ++i){
        cin >> c >> a >> q;
        if(c == 'S')
            ss.insert(a), s[a] += q;
        else
            bb.insert(a), b[a] += q;
    }
    int t = 0;
    for(set < int > ::iterator it = ss.begin(); it != ss.end(); ++it){
        int k = *(it);
        t++;
        sans[t] = make_pair(s[k],k);
    }
    sort(sans+1, sans + ss.size() + 1);
    for(int i = 1; i <= min(int(ss.size()),S); ++i)
        cout << "S " << sans[i].second << " " << sans[i].first << endl;
    t = 0;
    for(set < int > ::iterator it = bb.begin(); it != bb.end(); ++it){
        int k = *(it);
        t++;
        bans[t] = make_pair(-b[k],k);
    }
    sort(bans+1, bans + bb.size() + 1);
    for(int i = 1; i <= min(int(bb.size()),S); ++i)
        cout << "B " << bans[i].second << " " << -bans[i].first << endl;
    return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
map < int , int > s, b;
set < int > ss, bb;
pair < int , int > sans[100002], bans[100002];
char c;
int n;
int a, q;
int S;
int main(){
    cin >> n >> S;
    for(int i = 1; i <= n; ++i){
        cin >> c >> a >> q;
        if(c == 'S')
            ss.insert(a), s[a] += q;
        else
            bb.insert(-a), b[a] += q;
    }
    int t = 0;
    for(set < int > ::iterator it = ss.begin(); it != ss.end(); ++it){
        int k = *(it);
        t++;
        if(t > S)
            break;
        else
            cout << "S " << k << " " << s[k] << endl;
    }
    t = 0;
    for(set < int > ::iterator it = bb.begin(); it != bb.end(); ++it){
        int k = *(it);
        t++;
        if(t > S)
            break;
        else
            cout << "B " << -k << " " << b[-k] << endl;
    }
    return 0;
}


#include <iostream>
#include <set>
using namespace std;
int s[100002];
int b[100002];
set < int > ss, bb;
pair < int , int > sans[52];
char c;
int n;
int a, q;
int S;
int main(){
    cin >> n >> S;
    for(int i = 1; i <= n; ++i){
        cin >> c >> a >> q;
        if(c == 'S')
            ss.insert(a), s[a] += q;
        else
            bb.insert(-a), b[a] += q;
    }
    int t = 0;
    for(set < int > ::iterator it = ss.begin(); it != ss.end(); ++it){
        int k = *(it);
        t++;
        if(t > S)
            break;
        else
            sans[t] = make_pair(k,s[k]);
    }
    for(int i = min(t,S); i > 0; --i)
        cout << "S " << sans[i].first << " " << sans[i].second << endl;
    t = 0;
    for(set < int > ::iterator it = bb.begin(); it != bb.end(); ++it){
        int k = *(it);
        t++;
        if(t > S)
            break;
        else
            cout << "B " << -k << " " << b[-k] << endl;
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
typedef __int64 ll;
struct node
{
    char str[2];
    int val;
    friend bool operator<(const node& a,const node& b){
        return a.str[0]>b.str[0]||(a.str[0]==b.str[0]&&a.val>b.val);
    }
};
map<node,int> a;
map<node,int>::iterator it,itt;
int main()
{
    /*freopen("input.txt","r",stdin);*/
    int n,m,j,k,ma,cnt;
    while(~scanf("%d%d",&n,&m))
    {
        node s;ma=0x3f3f3f3f;cnt=0;
        while(n--)
        {
            scanf("%s%d%d",&s.str,&s.val,&k);if(s.str[0]=='S'&&!a[s])cnt++;
            a[s]+=k;
        }
        for(j=0,itt=it=a.begin();it!=a.end()&&j<2*m;++it){
            if(cnt>m){cnt--;continue;}
            printf("%c %d %d\n",it->first.str[0],it->first.val,it->second);
            j++;
        }
        printf("\n");
        a.clear();
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
typedef __int64 ll;
struct node
{
    char str[2];
    int val;
    friend bool operator<(const node& a,const node& b){
        return a.str[0]>b.str[0]||(a.str[0]==b.str[0]&&a.val>b.val);
    }
};
map<node,int> a;
map<node,int>::iterator it,itt;
int main()
{
   /* freopen("input.txt","r",stdin);*/
    int n,m,j,k,ma,cnt;
    while(~scanf("%d%d",&n,&m))
    {
        node s;ma=0x3f3f3f3f;cnt=0;
        while(n--)
        {
            scanf("%s%d%d",&s.str,&s.val,&k);if(s.str[0]=='S'&&!a[s])cnt++;
            a[s]+=k;
        }
        for(j=0,k=0,itt=it=a.begin();it!=a.end()&&k<m;++it){
            if(cnt>m){cnt--;continue;}
            if(it->first.str[0]=='B')k++;
            printf("%c %d %d\n",it->first.str[0],it->first.val,it->second);

        }
        printf("\n");
        a.clear();
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
typedef __int64 ll;
struct node
{
    char str[2];
    int val;
    friend bool operator<(const node& a,const node& b){
        return a.str[0]>b.str[0]||(a.str[0]==b.str[0]&&a.val>b.val);
    }
};
map<node,int> a;
map<node,int>::iterator it,itt;
int main()
{
    /*freopen("input.txt","r",stdin);*/
    int n,m,j,k,ma;
    while(~scanf("%d%d",&n,&m))
    {
        node s;ma=0x3f3f3f3f;
        while(n--)
        {
            scanf("%s%d%d",&s.str,&s.val,&k);
            a[s]+=k;
            /*printf("%c %d %d\n",s.str[0],s.val,a[s]);*/
        }
        for(j=0,itt=it=a.begin();it!=a.end();++it){
            if(it->first.str[0]!=itt->first.str[0])
                j=0;
            if(!j||it->first.str[0]==itt->first.str[0]&&j<m){
                if(it->first.str[0]=='S'||it->first.str[0]=='B'&&ma>it->first.val)
            printf("%c %d %d\n",it->first.str[0],it->first.val,it->second),j++;
                if(it->first.str[0]=='S'){
                    ma=min(ma,it->first.val);
                }
            }
            itt=it;
        }
        printf("\n");
        a.clear();
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
typedef __int64 ll;
struct node
{
    char str[2];
    int val;
    friend bool operator<(const node& a,const node& b){
        return a.str[0]>b.str[0]||(a.str[0]==b.str[0]&&a.val>b.val);
    }
};
map<node,int> a;
map<node,int>::iterator it,itt;
int main()
{
    /*freopen("input.txt","r",stdin);*/
    int n,m,j,k,ma;
    while(~scanf("%d%d",&n,&m))
    {
        node s;ma=0x3f3f3f3f;
        while(n--)
        {
            scanf("%s%d%d",&s.str,&s.val,&k);
            a[s]+=k;
            /*printf("%c %d %d\n",s.str[0],s.val,a[s]);*/
        }
        for(j=0,itt=it=a.begin();it!=a.end();++it){
            if(j<2*m){
              if(it->first.str[0]=='S'||it->first.str[0]=='B'&&ma>it->first.val)
            printf("%c %d %d\n",it->first.str[0],it->first.val,it->second),j++;
                if(it->first.str[0]=='S'){
                    ma=min(ma,it->first.val);
                }
            }
        }
        printf("\n");
        a.clear();
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
typedef __int64 ll;
struct node
{
    char str[2];
    int val;
    friend bool operator<(const node& a,const node& b){
        return a.str[0]>b.str[0]||(a.str[0]==b.str[0]&&a.val>b.val);
    }
};
map<node,int> a;
map<node,int>::iterator it,itt;
int main()
{
    /*freopen("input.txt","r",stdin);*/
    int n,m,j,k;
    while(~scanf("%d%d",&n,&m))
    {
        node s;
        while(n--)
        {
            scanf("%s%d%d",&s.str,&s.val,&k);
            a[s]+=k;
            /*printf("%c %d %d\n",s.str[0],s.val,a[s]);*/
        }
        for(j=0,itt=it=a.begin();it!=a.end();++it){
            if(it->first.str[0]!=itt->first.str[0])
                j=0;
            if(!j||it->first.str[0]==itt->first.str[0]&&j<m)
            printf("%c %d %d\n",it->first.str[0],it->first.val,it->second),j++;
            itt=it;
        }
        a.clear();
    }
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <memory.h>
#include <math.h>
#include <functional>

#define _CRT_SECURE_NO_WARNINGS

using namespace std;
// Input macros
#define s(n)                        scanf("%d",&n)
#define sf(n)                       scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful container manipulation / traversal macros
#define FOR(i,a,b)               for(int i=a;i<b;i++)
#define REP(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())
#define pb                          push_back
#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((int)(a.size()))
#define mp                          make_pair

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))
#define checkbit(n,b)                ( (n >> b) & 1)
#define DREP(a)                      sort(all(a)); a.erase(unique(all(a)),a.end())
#define INDEX(arr,ind)               (lower_bound(all(arr),ind)-arr.begin())

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

#define INF 2147483647
#define UINF 0xffffffff

int buy[1009][2], sell[1009][2];

int main()
{
	int n, sss, buylen = 0, selllen = 0;
	fill(buy, 0);
	fill(sell, 0);
	s(n); s(sss);
	FOR(i, 0, n)
	{
		char temp[10];
		ss(temp);
		if (temp[0] == 'B')
		{
			s(buy[buylen][0]);
			s(buy[buylen][1]);
			bool flag = true;
			FOR(j, 0, buylen)
			{
				if (buy[j][0] == buy[buylen][0])
				{
					buy[j][1] += buy[buylen][1];
					flag = false;
					break;
				}
			}
			if (flag)
				buylen++;
		}
		else
		{
			s(sell[selllen][0]);
			s(sell[selllen][1]);
			bool flag = true;
			FOR(j, 0, selllen)
			{
				if (sell[j][0] == sell[selllen][0])
				{
					sell[j][1] += sell[selllen][1];
					flag = false;
					break;
				}
			}
			if (flag)
				selllen++;
		}
	}
	FOR(i, 0, buylen)
	{
		FOR(j, 0, i)
		{
			if (buy[i][0] > buy[j][0])
			{
				int temp;
				temp = buy[i][0];
				buy[i][0] = buy[j][0];
				buy[j][0] = temp;

				temp = buy[i][1];
				buy[i][1] = buy[j][1];
				buy[j][1] = temp;
			}
		}
	}
	FOR(i, 0, selllen)
	{
		FOR(j, 0, i)
		{
			if (sell[i][0] > sell[j][0])
			{
				int temp;
				temp = sell[i][0];
				sell[i][0] = sell[j][0];
				sell[j][0] = temp;

				temp = sell[i][1];
				sell[i][1] = sell[j][1];
				sell[j][1] = temp;
			}
		}
	}

	for (int i = selllen - sss; i < sss; i++)
	{
		if (i < 0)
			continue;
		if (i >= selllen)
			break;
		printf("S %d %d\n", sell[i][0], sell[i][1]);
	}

	FOR(i, 0, sss)
	{
		if (i >= buylen)
			break;
		printf("B %d %d\n", buy[i][0], buy[i][1]);
	}

}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <memory.h>
#include <math.h>
#include <functional>

#define _CRT_SECURE_NO_WARNINGS

using namespace std;
// Input macros
#define s(n)                        scanf("%d",&n)
#define sf(n)                       scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful container manipulation / traversal macros
#define FOR(i,a,b)               for(int i=a;i<b;i++)
#define REP(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())
#define pb                          push_back
#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((int)(a.size()))
#define mp                          make_pair

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))
#define checkbit(n,b)                ( (n >> b) & 1)
#define DREP(a)                      sort(all(a)); a.erase(unique(all(a)),a.end())
#define INDEX(arr,ind)               (lower_bound(all(arr),ind)-arr.begin())

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

#define INF 2147483647
#define UINF 0xffffffff

int buy[1009][2], sell[1009][2];

int main()
{
	int n, ss, buylen = 0, selllen = 0;
	fill(buy, 0);
	fill(sell, 0);
	s(n); s(ss);
	FOR(i, 0, n)
	{
		char temp[10];
		ss(temp);
		if (temp[0] == 'B')
		{
			s(buy[buylen][0]);
			s(buy[buylen][1]);
			bool flag = true;
			FOR(j, 0, buylen)
			{
				if (buy[j][0] == buy[buylen][0])
				{
					buy[j][1] += buy[buylen][1];
					flag = false;
					break;
				}
			}
			if (flag)
				buylen++;
		}
		else
		{
			s(sell[selllen][0]);
			s(sell[selllen][1]);
			bool flag = true;
			FOR(j, 0, selllen)
			{
				if (sell[j][0] == sell[selllen][0])
				{
					sell[j][1] += sell[selllen][1];
					flag = false;
					break;
				}
			}
			if (flag)
				selllen++;
		}
	}
	FOR(i, 0, buylen)
	{
		FOR(j, 0, i)
		{
			if (buy[i][0] > buy[j][0])
			{
				int temp;
				temp = buy[i][0];
				buy[i][0] = buy[j][0];
				buy[j][0] = temp;

				temp = buy[i][1];
				buy[i][1] = buy[j][1];
				buy[j][1] = temp;
			}
		}
	}
	FOR(i, 0, selllen)
	{
		FOR(j, 0, i)
		{
			if (sell[i][0] > sell[j][0])
			{
				int temp;
				temp = sell[i][0];
				sell[i][0] = sell[j][0];
				sell[j][0] = temp;

				temp = sell[i][1];
				sell[i][1] = sell[j][1];
				sell[j][1] = temp;
			}
		}
	}

	FOR(i, 0, ss)
	{
		if (i >= selllen)
			break;
		printf("S %d %d\n", sell[i][0], sell[i][1]);
	}

	FOR(i, 0, ss)
	{
		if (i >= buylen)
			break;
		printf("B %d %d\n", buy[i][0], buy[i][1]);
	}

}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <memory.h>
#include <math.h>
#include <functional>

#define _CRT_SECURE_NO_WARNINGS

using namespace std;
// Input macros
#define s(n)                        scanf("%d",&n)
#define sf(n)                       scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful container manipulation / traversal macros
#define FOR(i,a,b)               for(int i=a;i<b;i++)
#define REP(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())
#define pb                          push_back
#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((int)(a.size()))
#define mp                          make_pair

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))
#define checkbit(n,b)                ( (n >> b) & 1)
#define DREP(a)                      sort(all(a)); a.erase(unique(all(a)),a.end())
#define INDEX(arr,ind)               (lower_bound(all(arr),ind)-arr.begin())

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

#define INF 2147483647
#define UINF 0xffffffff

int buy[1009][2], sell[1009][2];

int main()
{
	int n, ss, buylen = 0, selllen = 0;
	fill(buy, 0);
	fill(sell, 0);
	s(n); s(ss);
	FOR(i, 0, n)
	{
		char temp[10];
		ss(temp);
		if (temp[0] == 'B')
		{
			s(buy[buylen][0]);
			s(buy[buylen][1]);
			bool flag = true;
			FOR(j, 0, buylen)
			{
				if (buy[j][0] == buy[buylen][0])
				{
					buy[j][1] += buy[buylen][1];
					flag = false;
					break;
				}
			}
			if (flag)
				buylen++;
		}
		else
		{
			s(sell[selllen][0]);
			s(sell[selllen][1]);
			bool flag = true;
			FOR(j, 0, selllen)
			{
				if (sell[j][0] == sell[selllen][0])
				{
					sell[j][1] += sell[selllen][1];
					flag = false;
					break;
				}
			}
			if (flag)
				selllen++;
		}
	}
	FOR(i, 0, buylen)
	{
		FOR(j, 0, i)
		{
			if (buy[i][0] > buy[j][0])
			{
				int temp;
				temp = buy[i][0];
				buy[i][0] = buy[j][0];
				buy[j][0] = temp;

				temp = buy[i][1];
				buy[i][1] = buy[j][1];
				buy[j][1] = temp;
			}
		}
	}
	FOR(i, 0, selllen)
	{
		FOR(j, 0, i)
		{
			if (sell[i][0] > sell[j][0])
			{
				int temp;
				temp = sell[i][0];
				sell[i][0] = sell[j][0];
				sell[j][0] = temp;

				temp = sell[i][1];
				sell[i][1] = sell[j][1];
				sell[j][1] = temp;
			}
		}
	}

	for (int i = selllen - ss; i < ss; i++)
	{
		if (i < 0)
			continue;
		printf("S %d %d\n", sell[i][0], sell[i][1]);
	}

	FOR(i, 0, ss)
	{
		if (i >= buylen)
			break;
		printf("B %d %d\n", buy[i][0], buy[i][1]);
	}

}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <memory.h>
#include <math.h>
#include <functional>

#define _CRT_SECURE_NO_WARNINGS

using namespace std;
// Input macros
#define s(n)                        scanf("%d",&n)
#define sf(n)                       scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful container manipulation / traversal macros
#define FOR(i,a,b)               for(int i=a;i<b;i++)
#define REP(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())
#define pb                          push_back
#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((int)(a.size()))
#define mp                          make_pair

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))
#define checkbit(n,b)                ( (n >> b) & 1)
#define DREP(a)                      sort(all(a)); a.erase(unique(all(a)),a.end())
#define INDEX(arr,ind)               (lower_bound(all(arr),ind)-arr.begin())

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

#define INF 2147483647
#define UINF 0xffffffff

int buy[1009][2], sell[1009][2];

int main()
{
	int n, ss, buylen = 0, selllen = 0;
	fill(buy, 0);
	fill(sell, 0);
	s(n); s(ss);
	FOR(i, 0, n)
	{
		char temp[10];
		ss(temp);
		if (temp[0] == 'B')
		{
			s(buy[buylen][0]);
			s(buy[buylen][1]);
			bool flag = true;
			FOR(j, 0, buylen)
			{
				if (buy[j][0] == buy[buylen][0])
				{
					buy[j][1] += buy[buylen][1];
					flag = false;
					break;
				}
			}
			if (flag)
				buylen++;
		}
		else
		{
			s(sell[selllen][0]);
			s(sell[selllen][1]);
			bool flag = true;
			FOR(j, 0, selllen)
			{
				if (sell[j][0] == sell[selllen][0])
				{
					sell[j][1] += sell[selllen][1];
					flag = false;
					break;
				}
			}
			if (flag)
				selllen++;
		}
	}
	FOR(i, 0, buylen)
	{
		FOR(j, 0, i)
		{
			if (buy[i][0] > buy[j][0])
			{
				int temp;
				temp = buy[i][0];
				buy[i][0] = buy[j][0];
				buy[j][0] = temp;

				temp = buy[i][1];
				buy[i][1] = buy[j][1];
				buy[j][1] = temp;
			}
		}
	}
	FOR(i, 0, selllen)
	{
		FOR(j, 0, i)
		{
			if (sell[i][0] > sell[j][0])
			{
				int temp;
				temp = sell[i][0];
				sell[i][0] = sell[j][0];
				sell[j][0] = temp;

				temp = sell[i][1];
				sell[i][1] = sell[j][1];
				sell[j][1] = temp;
			}
		}
	}

	FOR(i, 0, ss)
		printf("S %d %d\n", sell[i][0], sell[i][1]);

	FOR(i, 0, ss)
		printf("B %d %d\n", buy[i][0], buy[i][1]);

}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <memory.h>
#include <math.h>
#include <functional>

#define _CRT_SECURE_NO_WARNINGS

using namespace std;
// Input macros
#define s(n)                        scanf("%d",&n)
#define sf(n)                       scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful container manipulation / traversal macros
#define FOR(i,a,b)               for(int i=a;i<b;i++)
#define REP(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())
#define pb                          push_back
#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((int)(a.size()))
#define mp                          make_pair

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))
#define checkbit(n,b)                ( (n >> b) & 1)
#define DREP(a)                      sort(all(a)); a.erase(unique(all(a)),a.end())
#define INDEX(arr,ind)               (lower_bound(all(arr),ind)-arr.begin())

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

#define INF 2147483647
#define UINF 0xffffffff

int buy[1009][2], sell[1009][2];

int main()
{
	int n, ss, buylen = 0, selllen = 0;
	fill(buy, 0);
	fill(sell, 0);
	s(n); s(ss);
	FOR(i, 0, n)
	{
		char temp[10];
		ss(temp);
		if (temp[0] == 'B')
		{
			s(buy[buylen][0]);
			s(buy[buylen][1]);
			bool flag = true;
			FOR(j, 0, buylen)
			{
				if (buy[j][0] == buy[buylen][0])
				{
					buy[j][1] += buy[buylen][1];
					flag = false;
					break;
				}
			}
			if (flag)
				buylen++;
		}
		else
		{
			s(sell[selllen][0]);
			s(sell[selllen][1]);
			bool flag = true;
			FOR(j, 0, selllen)
			{
				if (sell[j][0] == sell[selllen][0])
				{
					sell[j][1] += sell[selllen][1];
					flag = false;
					break;
				}
			}
			if (flag)
				selllen++;
		}
	}
	FOR(i, 0, buylen)
	{
		FOR(j, 0, i)
		{
			if (buy[i][0] > buy[j][0])
			{
				int temp;
				temp = buy[i][0];
				buy[i][0] = buy[j][0];
				buy[j][0] = temp;

				temp = buy[i][1];
				buy[i][1] = buy[j][1];
				buy[j][1] = temp;
			}
		}
	}
	FOR(i, 0, selllen)
	{
		FOR(j, 0, i)
		{
			if (sell[i][0] > sell[j][0])
			{
				int temp;
				temp = sell[i][0];
				sell[i][0] = sell[j][0];
				sell[j][0] = temp;

				temp = sell[i][1];
				sell[i][1] = sell[j][1];
				sell[j][1] = temp;
			}
		}
	}

	for (int i = selllen - ss; i < ss; i++)
	{
		if (i < 0)
			continue;
		if (i >= selllen)
			break;
		printf("S %d %d\n", sell[i][0], sell[i][1]);
	}

	FOR(i, 0, ss)
	{
		if (i >= buylen)
			break;
		printf("B %d %d\n", buy[i][0], buy[i][1]);
	}

}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <memory.h>
#include <math.h>
#include <functional>

#define _CRT_SECURE_NO_WARNINGS

using namespace std;
// Input macros
#define s(n)                        scanf("%d",&n)
#define sf(n)                       scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful container manipulation / traversal macros
#define FOR(i,a,b)               for(int i=a;i<b;i++)
#define REP(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())
#define pb                          push_back
#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((int)(a.size()))
#define mp                          make_pair

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))
#define checkbit(n,b)                ( (n >> b) & 1)
#define DREP(a)                      sort(all(a)); a.erase(unique(all(a)),a.end())
#define INDEX(arr,ind)               (lower_bound(all(arr),ind)-arr.begin())

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

#define INF 2147483647
#define UINF 0xffffffff

int buy[1009][2], sell[1009][2];

int main()
{
	int n, sss, buylen = 0, selllen = 0;
	fill(buy, 0);
	fill(sell, 0);
	s(n); s(sss);
	FOR(i, 0, n)
	{
		char temp[10];
		ss(temp);
		if (temp[0] == 'B')
		{
			s(buy[buylen][0]);
			s(buy[buylen][1]);
			bool flag = true;
			FOR(j, 0, buylen)
			{
				if (buy[j][0] == buy[buylen][0])
				{
					buy[j][1] += buy[buylen][1];
					flag = false;
					break;
				}
			}
			if (flag)
				buylen++;
		}
		else
		{
			s(sell[selllen][0]);
			s(sell[selllen][1]);
			bool flag = true;
			FOR(j, 0, selllen)
			{
				if (sell[j][0] == sell[selllen][0])
				{
					sell[j][1] += sell[selllen][1];
					flag = false;
					break;
				}
			}
			if (flag)
				selllen++;
		}
	}
	FOR(i, 0, buylen)
	{
		FOR(j, 0, i)
		{
			if (buy[i][0] > buy[j][0])
			{
				int temp;
				temp = buy[i][0];
				buy[i][0] = buy[j][0];
				buy[j][0] = temp;

				temp = buy[i][1];
				buy[i][1] = buy[j][1];
				buy[j][1] = temp;
			}
		}
	}
	FOR(i, 0, selllen)
	{
		FOR(j, 0, i)
		{
			if (sell[i][0] > sell[j][0])
			{
				int temp;
				temp = sell[i][0];
				sell[i][0] = sell[j][0];
				sell[j][0] = temp;

				temp = sell[i][1];
				sell[i][1] = sell[j][1];
				sell[j][1] = temp;
			}
		}
	}

	for (int i = selllen - sss; i < selllen; i++)
	{
		if (i < 0)
			continue;
		printf("S %d %d\n", sell[i][0], sell[i][1]);
	}

	FOR(i, 0, sss)
	{
		if (i >= buylen)
			break;
		printf("B %d %d\n", buy[i][0], buy[i][1]);
	}

}


//#include "functions.h"

//#include "geo.h"
#include<iostream>
#include<fstream>
#include<vector>
#include<list>
#include<algorithm>
#include<string>
#include <cstdint>
#include<memory>
#include<cmath>
#include<queue>
#include<deque>
#include<stack>
#include<numeric>
#include<ctime>
#include<set>
#include<map>
#include<sstream>
#include<bitset>

//#include"functions.h"

using namespace std;

typedef unsigned long long ll;
typedef unsigned int uint;
typedef pair<int, int> ii;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long double, int> di;

#define INF 1e9
#define EPS 1e-9
#define MOD 1000000007

#define REP(i,n) for(int i=0; i < n; i++)
#define REPR(i,x,y) for(int i = x; i < y; i++)
#define CINVEC(n, arr) REP(i,n) cin >> arr[i];
#define eqd(x,y) (fabs(x-y) < EPS)

#define pback push_back
#define F first
#define S second
#define mp make_pair

vi pset[3]; // remember: vi is vector<int>
void initSet(int N, int ind) { pset[ind].assign(N, 0);
for (int i = 0; i < N; i++) pset[ind][i] = i; }
int findSet(int i, int ind) { return (pset[ind][i] == i) ? i : (pset[ind][i] = findSet(pset[ind][i],ind)); }
bool isSameSet(int i, int j, int ind) { return findSet(i, ind) == findSet(j, ind); }
void unionSet(int i, int j, int ind) { pset[ind][findSet(i,ind)] = findSet(j,ind); }

#define NUM 4100

#define SIZE 100
#define ERR 1e6
#define RND 0.0089
#define RND2 19.572

int main()
{
    int n, s;

    cin >> n >> s;

    map<int, ii> buys, sells;

    for(int i = 0; i < n; i++)
    {
        char type;
        int price, quant;

        cin >> type >> price >> quant;
        map<int, ii> * active;
        if(type == 'B')
            active =  &buys;
        else
            active = &sells;

        auto iter = active->find(price);
        if(iter != active->end())
        {
            iter->second.second += quant;
            iter->second.first = i;
        }
        else
        {
            ii pr = mp(i,quant);
            (*active)[price] = pr;
        }
    }

    while(buys.size() > s)
        buys.erase(buys.begin());
    while(sells.size() > s)
        sells.erase(sells.begin());

    vector<pair<int, ii>> buysvec, sellsvec;

    for(auto iter = buys.begin(); iter != buys.end(); ++iter)
        buysvec.push_back(mp(iter->second.first, mp(iter->first, iter->second.second)));

    for(auto iter = sells.begin(); iter != sells.end(); ++iter)
        sellsvec.push_back(mp(iter->second.first, mp(iter->first, iter->second.second)));

    sort(buysvec.begin(), buysvec.end());
    sort(sellsvec.begin(), sellsvec.end());

    for(int i = 0; i < sellsvec.size(); i++)
        cout << "S " << sellsvec[i].second.first << sellsvec[i].second.second << endl;

    for(int i = 0; i < buysvec.size(); i++)
        cout << "B " << buysvec[i].second.first << buysvec[i].second.second << endl;

    cin.get();
    cin.get();

}


//#include "functions.h"

//#include "geo.h"
#include<iostream>
#include<fstream>
#include<vector>
#include<list>
#include<algorithm>
#include<string>
#include <cstdint>
#include<memory>
#include<cmath>
#include<queue>
#include<deque>
#include<stack>
#include<numeric>
#include<ctime>
#include<set>
#include<map>
#include<sstream>
#include<bitset>

//#include"functions.h"

using namespace std;

typedef unsigned long long ll;
typedef unsigned int uint;
typedef pair<int, int> ii;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long double, int> di;

#define INF 1e9
#define EPS 1e-9
#define MOD 1000000007

#define REP(i,n) for(int i=0; i < n; i++)
#define REPR(i,x,y) for(int i = x; i < y; i++)
#define CINVEC(n, arr) REP(i,n) cin >> arr[i];
#define eqd(x,y) (fabs(x-y) < EPS)

#define pback push_back
#define F first
#define S second
#define mp make_pair

vi pset[3]; // remember: vi is vector<int>
void initSet(int N, int ind) { pset[ind].assign(N, 0);
for (int i = 0; i < N; i++) pset[ind][i] = i; }
int findSet(int i, int ind) { return (pset[ind][i] == i) ? i : (pset[ind][i] = findSet(pset[ind][i],ind)); }
bool isSameSet(int i, int j, int ind) { return findSet(i, ind) == findSet(j, ind); }
void unionSet(int i, int j, int ind) { pset[ind][findSet(i,ind)] = findSet(j,ind); }

#define NUM 4100

#define SIZE 100
#define ERR 1e6
#define RND 0.0089
#define RND2 19.572

int main()
{
    int n, s;

    cin >> n >> s;

    map<int, ii> buys, sells;

    for(int i = 0; i < n; i++)
    {
        char type;
        int price, quant;

        cin >> type >> price >> quant;
        map<int, ii> * active;
        if(type == 'B')
            active =  &buys;
        else
            active = &sells;

        auto iter = active->find(price);
        if(iter != active->end())
        {
            iter->second.second += quant;
            iter->second.first = i;
        }
        else
        {
            ii pr = mp(i,quant);
            (*active)[price] = pr;
        }
    }

    while(buys.size() > s)
        buys.erase(buys.begin());
    while(sells.size() > s)
        sells.erase(sells.begin());

    auto iter = sells.end();

    int count = s;
    while(iter != sells.begin() && count > 0)
    {
        --iter;
        count--;
        cout << "S " << iter->first << " " << iter->second.second << endl;

    }

    iter = buys.end();
    count = s;
    while(iter != buys.begin() && count > 0)
    {
        --iter;
        count--;
        cout << "B " << iter->first << " " << iter->second.second << endl;

    }

    cin.get();
    cin.get();

}


//#include "functions.h"

//#include "geo.h"
#include<iostream>
#include<fstream>
#include<vector>
#include<list>
#include<algorithm>
#include<string>
#include <cstdint>
#include<memory>
#include<cmath>
#include<queue>
#include<deque>
#include<stack>
#include<numeric>
#include<ctime>
#include<set>
#include<map>
#include<sstream>
#include<bitset>

//#include"functions.h"

using namespace std;

typedef unsigned long long ll;
typedef unsigned int uint;
typedef pair<int, int> ii;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long double, int> di;

#define INF 1e9
#define EPS 1e-9
#define MOD 1000000007

#define REP(i,n) for(int i=0; i < n; i++)
#define REPR(i,x,y) for(int i = x; i < y; i++)
#define CINVEC(n, arr) REP(i,n) cin >> arr[i];
#define eqd(x,y) (fabs(x-y) < EPS)

#define pback push_back
#define F first
#define S second
#define mp make_pair

vi pset[3]; // remember: vi is vector<int>
void initSet(int N, int ind) { pset[ind].assign(N, 0);
for (int i = 0; i < N; i++) pset[ind][i] = i; }
int findSet(int i, int ind) { return (pset[ind][i] == i) ? i : (pset[ind][i] = findSet(pset[ind][i],ind)); }
bool isSameSet(int i, int j, int ind) { return findSet(i, ind) == findSet(j, ind); }
void unionSet(int i, int j, int ind) { pset[ind][findSet(i,ind)] = findSet(j,ind); }

#define NUM 4100

#define SIZE 100
#define ERR 1e6
#define RND 0.0089
#define RND2 19.572

int main()
{
    int n, s;

    cin >> n >> s;

    map<int, ii> buys, sells;

    for(int i = 0; i < n; i++)
    {
        char type;
        int price, quant;

        cin >> type >> price >> quant;
        map<int, ii> * active;
        if(type == 'B')
            active =  &buys;
        else
            active = &sells;

        auto iter = active->find(price);
        if(iter != active->end())
        {
            iter->second.second += quant;
            iter->second.first = i;
        }
        else
        {
            ii pr = mp(i,quant);
            (*active)[price] = pr;
        }
    }

    while(buys.size() > s)
        buys.erase(buys.begin());
    while(sells.size() > s)
        sells.erase(sells.begin());

    vector<pair<int, ii>> buysvec, sellsvec;

    for(auto iter = buys.begin(); iter != buys.end(); ++iter)
        buysvec.push_back(mp(iter->second.first, mp(iter->first, iter->second.second)));

    for(auto iter = sells.begin(); iter != sells.end(); ++iter)
        sellsvec.push_back(mp(iter->second.first, mp(iter->first, iter->second.second)));

    reverse(buysvec.begin(), buysvec.end());
    reverse(sellsvec.begin(), sellsvec.end());

    for(int i = 0; i < sellsvec.size(); i++)
        cout << "S " << sellsvec[i].second.first << " " << sellsvec[i].second.second << endl;

    for(int i = 0; i < buysvec.size(); i++)
        cout << "B " << buysvec[i].second.first << " " << buysvec[i].second.second << endl;

    cin.get();
    cin.get();

}


//#include "functions.h"

//#include "geo.h"
#include<iostream>
#include<fstream>
#include<vector>
#include<list>
#include<algorithm>
#include<string>
#include <cstdint>
#include<memory>
#include<cmath>
#include<queue>
#include<deque>
#include<stack>
#include<numeric>
#include<ctime>
#include<set>
#include<map>
#include<sstream>
#include<bitset>

//#include"functions.h"

using namespace std;

typedef unsigned long long ll;
typedef unsigned int uint;
typedef pair<int, int> ii;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long double, int> di;

#define INF 1e9
#define EPS 1e-9
#define MOD 1000000007

#define REP(i,n) for(int i=0; i < n; i++)
#define REPR(i,x,y) for(int i = x; i < y; i++)
#define CINVEC(n, arr) REP(i,n) cin >> arr[i];
#define eqd(x,y) (fabs(x-y) < EPS)

#define pback push_back
#define F first
#define S second
#define mp make_pair

vi pset[3]; // remember: vi is vector<int>
void initSet(int N, int ind) { pset[ind].assign(N, 0);
for (int i = 0; i < N; i++) pset[ind][i] = i; }
int findSet(int i, int ind) { return (pset[ind][i] == i) ? i : (pset[ind][i] = findSet(pset[ind][i],ind)); }
bool isSameSet(int i, int j, int ind) { return findSet(i, ind) == findSet(j, ind); }
void unionSet(int i, int j, int ind) { pset[ind][findSet(i,ind)] = findSet(j,ind); }

#define NUM 4100

#define SIZE 100
#define ERR 1e6
#define RND 0.0089
#define RND2 19.572

int main()
{
    int n, s;

    cin >> n >> s;

    map<int, pll> buys, sells;

    for(int i = 0; i < n; i++)
    {
        char type;
        long long price, quant;

        cin >> type >> price >> quant;
        map<int, pll> * active;
        if(type == 'B')
            active =  &buys;
        else
            active = &sells;

        auto iter = active->find(price);
        if(iter != active->end())
        {
            iter->second.second += quant;
            iter->second.first = i;
        }
        else
        {
            pll pr = mp(i,quant);
            (*active)[price] = pr;
        }
    }

    while(buys.size() > s)
        buys.erase(buys.begin());
    while(sells.size() > s)
        sells.erase(sells.begin());

    auto iter = sells.end();

    int count = s;
    while(iter != sells.begin() && count > 0)
    {
        --iter;
        count--;
        cout << "S " << iter->first << " " << iter->second.second << endl;

    }

    iter = buys.end();
    count = s;
    while(iter != buys.begin() && count > 0)
    {
        --iter;
        count--;
        cout << "B " << iter->first << " " << iter->second.second << endl;

    }

    cin.get();
    cin.get();

}


//#include "functions.h"

//#include "geo.h"
#include<iostream>
#include<fstream>
#include<vector>
#include<list>
#include<algorithm>
#include<string>
#include <cstdint>
#include<memory>
#include<cmath>
#include<queue>
#include<deque>
#include<stack>
#include<numeric>
#include<ctime>
#include<set>
#include<map>
#include<sstream>
#include<bitset>

//#include"functions.h"

using namespace std;

typedef unsigned long long ll;
typedef unsigned int uint;
typedef pair<int, int> ii;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long double, int> di;

#define INF 1e9
#define EPS 1e-9
#define MOD 1000000007

#define REP(i,n) for(int i=0; i < n; i++)
#define REPR(i,x,y) for(int i = x; i < y; i++)
#define CINVEC(n, arr) REP(i,n) cin >> arr[i];
#define eqd(x,y) (fabs(x-y) < EPS)

#define pback push_back
#define F first
#define S second
#define mp make_pair

vi pset[3]; // remember: vi is vector<int>
void initSet(int N, int ind) { pset[ind].assign(N, 0);
for (int i = 0; i < N; i++) pset[ind][i] = i; }
int findSet(int i, int ind) { return (pset[ind][i] == i) ? i : (pset[ind][i] = findSet(pset[ind][i],ind)); }
bool isSameSet(int i, int j, int ind) { return findSet(i, ind) == findSet(j, ind); }
void unionSet(int i, int j, int ind) { pset[ind][findSet(i,ind)] = findSet(j,ind); }

#define NUM 4100

#define SIZE 100
#define ERR 1e6
#define RND 0.0089
#define RND2 19.572

int main()
{
    int n, s;

    cin >> n >> s;

    map<int, pll> buys, sells;

    for(int i = 0; i < n; i++)
    {
        char type;
        int price;
        long long quant;

        cin >> type >> price >> quant;
        //type = 'S';
        //price = 1e5;
        //quant = 1e4;
        map<int, pll> * active;
        if(type == 'B')
            active =  &buys;
        else
            active = &sells;

        auto iter = active->find(price);
        if(iter != active->end())
        {
            iter->second.second += quant;
            iter->second.first = i;
        }
        else
        {
            pll pr = mp(i,quant);
            (*active)[price] = pr;
        }
    }

    while(buys.size() > s)
        buys.erase(buys.begin());
    while(sells.size() > s)
        sells.erase((--sells.end()));

    auto iter = sells.end();

    int count = s;
    while(iter != sells.begin() && count > 0)
    {
        --iter;
        count--;
        cout << "S " << iter->first << " " << iter->second.second << endl;

    }

    auto iter2 = buys.end();
    count = s;
    while(iter2 != buys.begin() && count > 0)
    {
        --iter2;
        count--;
        cout << "B " << iter2->first << " " << iter2->second.second << endl;

    }

    cin.get();
    cin.get();

}


//#include "functions.h"

//#include "geo.h"
#include<iostream>
#include<fstream>
#include<vector>
#include<list>
#include<algorithm>
#include<string>
#include <cstdint>
#include<memory>
#include<cmath>
#include<queue>
#include<deque>
#include<stack>
#include<numeric>
#include<ctime>
#include<set>
#include<map>
#include<sstream>
#include<bitset>

//#include"functions.h"

using namespace std;

typedef unsigned long long ll;
typedef unsigned int uint;
typedef pair<int, int> ii;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long double, int> di;

#define INF 1e9
#define EPS 1e-9
#define MOD 1000000007

#define REP(i,n) for(int i=0; i < n; i++)
#define REPR(i,x,y) for(int i = x; i < y; i++)
#define CINVEC(n, arr) REP(i,n) cin >> arr[i];
#define eqd(x,y) (fabs(x-y) < EPS)

#define pback push_back
#define F first
#define S second
#define mp make_pair

vi pset[3]; // remember: vi is vector<int>
void initSet(int N, int ind) { pset[ind].assign(N, 0);
for (int i = 0; i < N; i++) pset[ind][i] = i; }
int findSet(int i, int ind) { return (pset[ind][i] == i) ? i : (pset[ind][i] = findSet(pset[ind][i],ind)); }
bool isSameSet(int i, int j, int ind) { return findSet(i, ind) == findSet(j, ind); }
void unionSet(int i, int j, int ind) { pset[ind][findSet(i,ind)] = findSet(j,ind); }

#define NUM 4100

#define SIZE 100
#define ERR 1e6
#define RND 0.0089
#define RND2 19.572

int main()
{
    int n, s;

    cin >> n >> s;

    map<int, pll> buys, sells;

    for(int i = 0; i < n; i++)
    {
        char type;
        int price, quant;

        cin >> type >> price >> quant;
        map<int, pll> * active;
        if(type == 'B')
            active =  &buys;
        else
            active = &sells;

        auto iter = active->find(price);
        if(iter != active->end())
        {
            iter->second.second += quant;
            iter->second.first = i;
        }
        else
        {
            ii pr = mp(i,quant);
            (*active)[price] = pr;
        }
    }

    while(buys.size() > s)
        buys.erase(buys.begin());
    while(sells.size() > s)
        sells.erase(sells.begin());

    auto iter = sells.end();

    int count = s;
    while(iter != sells.begin() && count > 0)
    {
        --iter;
        count--;
        cout << "S " << iter->first << " " << iter->second.second << endl;

    }

    iter = buys.end();
    count = s;
    while(iter != buys.begin() && count > 0)
    {
        --iter;
        count--;
        cout << "B " << iter->first << " " << iter->second.second << endl;

    }

    cin.get();
    cin.get();

}


//#include "functions.h"

//#include "geo.h"
#include<iostream>
#include<fstream>
#include<vector>
#include<list>
#include<algorithm>
#include<string>
#include <cstdint>
#include<memory>
#include<cmath>
#include<queue>
#include<deque>
#include<stack>
#include<numeric>
#include<ctime>
#include<set>
#include<map>
#include<sstream>
#include<bitset>

//#include"functions.h"

using namespace std;

typedef unsigned long long ll;
typedef unsigned int uint;
typedef pair<int, int> ii;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long double, int> di;

#define INF 1e9
#define EPS 1e-9
#define MOD 1000000007

#define REP(i,n) for(int i=0; i < n; i++)
#define REPR(i,x,y) for(int i = x; i < y; i++)
#define CINVEC(n, arr) REP(i,n) cin >> arr[i];
#define eqd(x,y) (fabs(x-y) < EPS)

#define pback push_back
#define F first
#define S second
#define mp make_pair

vi pset[3]; // remember: vi is vector<int>
void initSet(int N, int ind) { pset[ind].assign(N, 0);
for (int i = 0; i < N; i++) pset[ind][i] = i; }
int findSet(int i, int ind) { return (pset[ind][i] == i) ? i : (pset[ind][i] = findSet(pset[ind][i],ind)); }
bool isSameSet(int i, int j, int ind) { return findSet(i, ind) == findSet(j, ind); }
void unionSet(int i, int j, int ind) { pset[ind][findSet(i,ind)] = findSet(j,ind); }

#define NUM 4100

#define SIZE 100
#define ERR 1e6
#define RND 0.0089
#define RND2 19.572

int main()
{
    int n, s;

    cin >> n >> s;

    map<int, pll> buys, sells;

    for(int i = 0; i < n; i++)
    {
        char type;
        int price;
        long long quant;

        //cin >> type >> price >> quant;
        type = 'S';
        price = 1e5;
        quant = 1e4;
        map<int, pll> * active;
        if(type == 'B')
            active =  &buys;
        else
            active = &sells;

        auto iter = active->find(price);
        if(iter != active->end())
        {
            iter->second.second += quant;
            iter->second.first = i;
        }
        else
        {
            pll pr = mp(i,quant);
            (*active)[price] = pr;
        }
    }

    while(buys.size() > s)
        buys.erase(buys.begin());
    while(sells.size() > s)
        sells.erase((--sells.end()));

    auto iter = sells.end();

    int count = s;
    while(iter != sells.begin() && count > 0)
    {
        --iter;
        count--;
        cout << "S " << iter->first << " " << iter->second.second << endl;

    }

    auto iter2 = buys.end();
    count = s;
    while(iter2 != buys.begin() && count > 0)
    {
        --iter2;
        count--;
        cout << "B " << iter2->first << " " << iter2->second.second << endl;

    }

    cin.get();
    cin.get();

}


//#include "functions.h"

//#include "geo.h"
#include<iostream>
#include<fstream>
#include<vector>
#include<list>
#include<algorithm>
#include<string>
#include <cstdint>
#include<memory>
#include<cmath>
#include<queue>
#include<deque>
#include<stack>
#include<numeric>
#include<ctime>
#include<set>
#include<map>
#include<sstream>
#include<bitset>

//#include"functions.h"

using namespace std;

typedef unsigned long long ll;
typedef unsigned int uint;
typedef pair<int, int> ii;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long double, int> di;

#define INF 1e9
#define EPS 1e-9
#define MOD 1000000007

#define REP(i,n) for(int i=0; i < n; i++)
#define REPR(i,x,y) for(int i = x; i < y; i++)
#define CINVEC(n, arr) REP(i,n) cin >> arr[i];
#define eqd(x,y) (fabs(x-y) < EPS)

#define pback push_back
#define F first
#define S second
#define mp make_pair

vi pset[3]; // remember: vi is vector<int>
void initSet(int N, int ind) { pset[ind].assign(N, 0);
for (int i = 0; i < N; i++) pset[ind][i] = i; }
int findSet(int i, int ind) { return (pset[ind][i] == i) ? i : (pset[ind][i] = findSet(pset[ind][i],ind)); }
bool isSameSet(int i, int j, int ind) { return findSet(i, ind) == findSet(j, ind); }
void unionSet(int i, int j, int ind) { pset[ind][findSet(i,ind)] = findSet(j,ind); }

#define NUM 4100

#define SIZE 100
#define ERR 1e6
#define RND 0.0089
#define RND2 19.572

int main()
{
    int n, s;

    cin >> n >> s;

    map<int, pll> buys, sells;

    for(int i = 0; i < n; i++)
    {
        char type;
        int price;
        long long quant;

        cin >> type >> price >> quant;
        map<int, pll> * active;
        if(type == 'B')
            active =  &buys;
        else
            active = &sells;

        auto iter = active->find(price);
        if(iter != active->end())
        {
            iter->second.second += quant;
            iter->second.first = i;
        }
        else
        {
            pll pr = mp(i,quant);
            (*active)[price] = pr;
        }
    }

    while(buys.size() > s)
        buys.erase(buys.begin());
    while(sells.size() > s)
        sells.erase(sells.begin());

    auto iter = sells.end();

    int count = s;
    while(iter != sells.begin() && count > 0)
    {
        --iter;
        count--;
        cout << "S " << iter->first << " " << iter->second.second << endl;

    }

    auto iter2 = buys.end();
    count = s;
    while(iter2 != buys.begin() && count > 0)
    {
        --iter2;
        count--;
        cout << "B " << iter2->first << " " << iter2->second.second << endl;

    }

    cin.get();
    cin.get();

}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:256000000")
#define _USE_MATH_DEFINES
#include<iostream>
#include<vector>
#include<string>
#include<stack>
#include<algorithm>
#include<cmath>
#include<set>
#include<queue>
#include<sstream>
#include<utility>
#include<map>
#include<ctime>
#include<cstdio>
#include<cassert>
#include<functional>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<char, char> pcc;
typedef pair<double, double> pdd;

#define show(x) cerr << x
#define debug(x) show(#x << ": " << (x) << endl)

const long double PI = 3.14159265358979323846;
const long double gammama = 0.57721566490153286060;
const long double eps = 1e-5;
const int INF = 1000 * 1000 * 1000 + 1;
const ll LINF = (ll)1000 * 1000 * 1000 * 1000 * 1000 * 1000;
const ll mod = 1000 * 1000 * 1000 + 7;
const int N = 1000000;

int main () {
    int n, s, x ,y;
    char c;
    bool flag = false;
    cin >> n >> s;
    std::vector<pair<int, int>> B;
    std::vector<pair<int, int>> S;
    for (int i = 0; i < n; ++i)
    {
        cin >> c >> x >> y;
        if (c == 'B') {
            for (int i = 0; i < B.size(); ++i)
            {
                if(B[i].first == x) {
                    B[i].second += y;
                    flag = true;
                }
            }
            if (!flag) {
                B.push_back(make_pair(x, y));
            } else {
                flag = false;
            }

        } else if (c == 'S') {
            for (int i = 0; i < S.size(); ++i)
            {
                if(S[i].first == x) {
                    S[i].second += y;
                    flag = true;
                }
            }
            if (!flag) {
                S.push_back(make_pair(x, y));
            } else {
                flag = false;
            }
        }
    }
    if (B.size() > 0) {
        sort(B.begin(), B.end());
        reverse(B.begin(), B.end());
    }
    if (S.size() > 0) {
        sort(S.begin(), S.end());
    }

    int k = s > S.size() ? S.size() : s;
    if (S.size() > 0) {
        for (int i = k - 1; i >= 0; --i)
        {
            cout << "S" << " " << S[i].first << " " << S[i].second << endl;
        }
    }
    k = s > B.size() ? B.size() : s;
    for (int i = 0; i < k; ++i)
    {
        cout << "B" << " " << B[i].first << " " << B[i].second << endl;
    }

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:256000000")
#define _USE_MATH_DEFINES
#include<iostream>
#include<vector>
#include<string>
#include<stack>
#include<algorithm>
#include<cmath>
#include<set>
#include<queue>
#include<sstream>
#include<utility>
#include<map>
#include<ctime>
#include<cstdio>
#include<cassert>
#include<functional>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<char, char> pcc;
typedef pair<double, double> pdd;

#define show(x) cerr << x
#define debug(x) show(#x << ": " << (x) << endl)

const long double PI = 3.14159265358979323846;
const long double gammama = 0.57721566490153286060;
const long double eps = 1e-5;
const int INF = 1000 * 1000 * 1000 + 1;
const ll LINF = (ll)1000 * 1000 * 1000 * 1000 * 1000 * 1000;
const ll mod = 1000 * 1000 * 1000 + 7;
const int N = 1000000;

int main () {
    int n, s, x ,y;
    char c;
    bool flag = false;
    cin >> n >> s;
    std::vector<pair<int, int>> B;
    std::vector<pair<int, int>> S;
    for (int i = 0; i < n; ++i)
    {
        cin >> c >> x >> y;
        if (c == 'B') {
            for (int i = 0; i < B.size(); ++i)
            {
                if(B[i].first == x) {
                    B[i].second += y;
                    flag = true;
                }
            }
            if (!flag) {
                B.push_back(make_pair(x, y));
            } else {
                flag = false;
            }

        } else if (c == 'S') {
            for (int i = 0; i < S.size(); ++i)
            {
                if(S[i].first == x) {
                    S[i].second += y;
                    flag = true;
                }
            }
            if (!flag) {
                S.push_back(make_pair(x, y));
            } else {
                flag = false;
            }
        }
    }
    if (B.size() > 0) {
        sort(B.begin(), B.end());
        reverse(B.begin(), B.end());
    }
    if (S.size() > 0) {
        sort(S.begin(), S.end());
    }

    int k = s > S.size() ? S.size() : s;
    if (S.size() > 0) {
        for (int i = s - 1; i >= 0; --i)
        {
            cout << "S" << " " << S[i].first << " " << S[i].second << endl;
        }
    }
    k = s > B.size() ? B.size() : s;
    for (int i = 0; i < s; ++i)
    {
        cout << "B" << " " << B[i].first << " " << B[i].second << endl;
    }

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:256000000")
#define _USE_MATH_DEFINES
#include<iostream>
#include<vector>
#include<string>
#include<stack>
#include<algorithm>
#include<cmath>
#include<set>
#include<queue>
#include<sstream>
#include<utility>
#include<map>
#include<ctime>
#include<cstdio>
#include<cassert>
#include<functional>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<char, char> pcc;
typedef pair<double, double> pdd;

#define show(x) cerr << x
#define debug(x) show(#x << ": " << (x) << endl)

const long double PI = 3.14159265358979323846;
const long double gammama = 0.57721566490153286060;
const long double eps = 1e-5;
const int INF = 1000 * 1000 * 1000 + 1;
const ll LINF = (ll)1000 * 1000 * 1000 * 1000 * 1000 * 1000;
const ll mod = 1000 * 1000 * 1000 + 7;
const int N = 1000000;

int main () {
    int n, s, x ,y;
    char c;
    bool flag = false;
    cin >> n >> s;
    std::vector<pair<int, int>> B;
    std::vector<pair<int, int>> S;
    for (int i = 0; i < n; ++i)
    {
        cin >> c >> x >> y;
        if (c == 'B') {
            for (int i = 0; i < B.size(); ++i)
            {
                if(B[i].first == x) {
                    B[i].second += y;
                    flag = true;
                }
            }
            if (!flag) {
                B.push_back(make_pair(x, y));
            } else {
                flag = false;
            }

        } else if (c == 'S') {
            for (int i = 0; i < S.size(); ++i)
            {
                if(S[i].first == x) {
                    S[i].second += y;
                    flag = true;
                }
            }
            if (!flag) {
                S.push_back(make_pair(x, y));
            } else {
                flag = false;
            }
        }
    }
    sort(B.begin(), B.end());
    sort(S.begin(), S.end());
    reverse(B.begin(), B.end());
    int k = s > S.size() ? S.size() : s;
    for (int i = s - 1; i >= 0; --i)
    {
        cout << "S" << " " << S[i].first << " " << S[i].second << endl;
    }

    k = s > B.size() ? B.size() : s;
    for (int i = 0; i < s; ++i)
    {
        cout << "B" << " " << B[i].first << " " << B[i].second << endl;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
int n, s1;

struct A
{
    char d;
    int p, q;
};
A a[100000];
A p[100000];
A r[100000];
A t;
bool cmp(A &x, A &y)
{
    return (x.p > y.p);
}
int main()

{
    cin >> n >> s1;
    int ua = 0, up = 0;
    for (int i = 0; i < n; ++i)
    {
        cin >> t.d >> t.p >> t.q;
        if (t.d == 'S') a[ua++] = t; else p[up++] = t;
    }

    sort(a, a + ua, cmp);
    sort(p, p + up, cmp);
 long long s;
    int kol = 0;

    int e = 0;
    for (int i = 0; i < ua; ++i)
    {
        r[e].d= a[i].d;
        r[e].p = a[i].p;
        s = a[i].q;
        while (i + 1 < ua && a[i].p == a[i + 1].p && a[i].d == a[i+1].d)  {
            s += a[i + 1].q; ++i;
        }
        r[e].q = s;
        ++e;
    }

    for (int i = e - s1 ; i < e; ++i)
        cout << r[i].d << ' ' << r[i].p << ' ' << r[i].q << '\n';

    for (int i = 0; i < up; ++i)
    {
        s = p[i].q;
        while (i + 1 < up && p[i].p == p[i + 1].p && p[i].d == p[i+1].d)  {
            s += p[i + 1].q; ++i;
        }
        ++kol;
        if (kol <= s1) cout << p[i].d << ' ' << p[i].p << ' ' << s << '\n';
    }

    //system("PAUSE");
    return 0;
}


#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
int n, s1;

struct A
{
    char d;
    int p, q;
};
A a[100000];
A p[100000];
A r[100000];
A t;
bool cmp(A &x, A &y)
{
    return (x.p > y.p);
}
int main()

{
    cin >> n >> s1;
    int ua = 0, up = 0;
    for (int i = 0; i < n; ++i)
    {
        cin >> t.d >> t.p >> t.q;
        if (t.d == 'S') a[ua++] = t; else p[up++] = t;
    }

    sort(a, a + ua, cmp);
    sort(p, p + up, cmp);
 long long s;
    int kol = 0;
    for (int i = 0; i < ua; ++i)
    {
        s = a[i].q;
        while (i + 1 < ua && a[i].p == a[i + 1].p && a[i].d == a[i+1].d)  {
            s += a[i + 1].q; ++i;
        }
        ++kol;
        if (kol <= s1) cout << a[i].d << ' ' << a[i].p << ' ' << s << '\n';
    }
    int e = 0;
    for (int i = 0; i < up; ++i)
    {
        r[e].d= p[i].d;
        r[e].p = p[i].p;
        s = p[i].q;
        while (i + 1 < up && p[i].p == p[i + 1].p && p[i].d == p[i+1].d)  {
            s += p[i + 1].q; ++i;
        }
        r[e].q = s;
        ++e;
    }

    for (int i = e - s1 - 1; i < e -1; ++i)
        cout << r[i].d << ' ' << r[i].p << ' ' << r[i].q << '\n';
    //system("PAUSE");
    return 0;
}


#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
int n, s1;

struct A
{
    char d;
    int p, q;
};
A a[100000];
A p[100000];
A r[100000];
A t;
bool cmp(A &x, A &y)
{
    return (x.p > y.p);
}
int main()

{
    cin >> n >> s1;
    int ua = 0, up = 0;
    for (int i = 0; i < n; ++i)
    {
        cin >> t.d >> t.p >> t.q;
        if (t.d == 'S') a[ua++] = t; else p[up++] = t;
    }

    sort(a, a + ua, cmp);
    sort(p, p + up, cmp);
    int s;
    int kol = 0;
    for (int i = 0; i < ua; ++i)
    {
        s = a[i].q;
        while (i + 1 < ua && a[i].p == a[i + 1].p)  {
            s += a[i + 1].q; ++i;
        }
        ++kol;
        if (kol <= s1) cout << a[i].d << ' ' << a[i].p << ' ' << s << '\n';
    }
    int e = 0;
    for (int i = 0; i < up; ++i)
    {
        r[e].d= p[i].d;
        r[e].p = p[i].p;
        s = p[i].q;
        while (i + 1 < up && p[i].p == p[i + 1].p)  {
            s += p[i + 1].q; ++i;
        }
        r[e].q = s;
        ++e;
    }
    for (int i = e - s; i < e -1; ++i)
        cout << r[i].d << ' ' << r[i].p << ' ' << r[i].q << '\n';
//  system("PAUSE");
    return 0;
}


#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
int n, s1;

struct A
{
    char d;
    int p, q;
};
A a[100000];
A p[100000];

A t;
bool cmp(A &x, A &y)
{
    return (x.p > y.p);
}
int main()

{
    cin >> n >> s1;
    int ua = 0, up = 0;
    for (int i = 0; i < n; ++i)
    {
        cin >> t.d >> t.p >> t.q;
        if (t.d == 'S') a[ua++] = t; else p[up++] = t;
    }

    sort(a, a + ua, cmp);
    sort(p, p + up, cmp);
    int s;
    int kol = 0;
    for (int i = 0; i < ua; ++i)
    {
        s = a[i].q;
        while (i + 1 < ua && a[i].p == a[i + 1].p)  {
            s += a[i + 1].q; ++i;
        }
        ++kol;
        if (kol <= s1) cout << a[i].d << ' ' << a[i].p << ' ' << s << '\n';
    }
    kol = 0;
    for (int i = 0; i < up; ++i)
    {
        s = p[i].q;
        while (i + 1 < up && p[i].p == p[i + 1].p)
        {
            s += p[i + 1].q; ++i;
        }
        ++kol;
        if (kol <= s1)
            cout << p[i].d << ' ' << p[i].p << ' ' << s << '\n';
    }

    //system("PAUSE");
    return 0;
}


#include <iostream>

int tc(char a){
  return a-int('0');
}

using namespace std;

int main(){
  string aux="0123456789";
  string st;
  cin >> st;
  int n=st.size();
  if (n & 1){
    for (int i=0;i<n/2+1;i++)
      cout << '4';
    for (int i=0;i<n/2+1;i++)
      cout << '7';
    cout << endl;
    return 0;
  }

  bool convertir=0;
  int cf=n/2,cs=n/2,uf=-1;
  string ans;
  for (int c=0;c<n;c++){
    int i=tc(st[c]);
    if (i<4){
      break;
    }
    if (i==4){
      if (cf==0){
        break;
      }
      uf=c;
      ans.push_back('4');
      cf--;
    } else {
      if (i<7){
        if (cs==0){
          convertir=1;
          break;
        }
        ans.push_back('7');
        cs--;
        break;
      }
      if ((i==7 && cs==0) || i>7) {
          convertir=1;
          break;
      }
      ans.push_back('7');
      cs--;
    }
  }
  if (convertir){
    if (uf!=-1){
      cf=cs=n/2;
      for (int i=0;i<uf;i++){
        cout << ans[i];
        if (ans[i]=='4'){
          cf--;
        } else {
          cs--;
        }
      }
      cout << '7';
      cs--;
      while (cf>0){
        cout << '4';
        cf--;
      }
      while (cs>0){
        cout << '7';
        cs--;
      }
      cout << endl;
      return 0;
    }
    for (int i=0;i<n/2+1;i++)
      cout << '4';
    for (int i=0;i<n/2+1;i++)
      cout << '7';
    cout << endl;
    return 0;
  }
  while (cf>0){
    ans.push_back('4');
    cf--;
  }
  while (cs>0){
    ans.push_back('7');
    cs--;
  }
  cout << ans << endl;
  return 0;
}


#include <iostream>
#include <stdio.h>

int tc(char a){
  return a-int('0');
}

using namespace std;

int main(){
  string st;
  cin >> st;
  int n=st.size();
  if (n & 1){
    for (int i=0;i<n/2+1;i++)
      cout << '4';
    for (int i=0;i<n/2+1;i++)
      cout << '7';
    cout << endl;
    return 0;
  }

  bool convertir=0;
  int cf=n/2,cs=n/2,uf=-1;
  string ans;
  for (int c=0;c<n;c++){
    int i=tc(st[c]);
    if (i<4){
      break;
    }
    if (i==4){
      if (cf==0){
        break;
      }
      uf=c;
      ans.push_back('4');
      cf--;
    } else {
      if (i<7){
        if (cs==0){
          convertir=1;
          break;
        }
        ans.push_back('7');
        cs--;
        break;
      }
      if ((i==7 && cs==0) || i>7) {
          convertir=1;
          break;
      }
      ans.push_back('7');
      cs--;
    }
  }
  if (convertir){
    string ans1;
    if (uf!=-1){
      cf=cs=n/2;
      for (int i=0;i<uf;i++){
        ans1.push_back(ans[i]);
        if (ans[i]=='4'){
          cf--;
        } else {
          cs--;
        }
      }
      ans1.push_back('7');
      cs--;
      if (cs<0){
        for (int i=0;i<n/2+1;i++)
          cout << '4';
        for (int i=0;i<n/2+1;i++)
          cout << '7';
        cout << endl;
        return 0;
      }
      while (cf>0){
        ans1.push_back('4');
        cf--;
      }
      while (cs>0){
        ans1.push_back('7');
        cs--;
      }
      cout << ans1 << endl;
      return 0;
    }
    for (int i=0;i<n/2+1;i++)
      cout << '4';
    for (int i=0;i<n/2+1;i++)
      cout << '7';
    cout << endl;
    return 0;
  }
  while (cf>0){
    ans.push_back('4');
    cf--;
  }
  while (cs>0){
    ans.push_back('7');
    cs--;
  }
  cout << ans << endl;
  return 0;
}


#include <iostream>
#include <stdio.h>

int tc(char a){
  return a-int('0');
}

using namespace std;

int main(){
  string st;
  cin >> st;
  int n=st.size();
  if (n & 1){
    for (int i=0;i<n/2+1;i++)
      cout << '4';
    for (int i=0;i<n/2+1;i++)
      cout << '7';
    cout << endl;
    return 0;
  }

  bool convertir=0;
  int cf=n/2,cs=n/2,uf=-1;
  string ans;
  for (int c=0;c<n;c++){
    int i=tc(st[c]);
    if (i<4){
      break;
    }
    if (i==4){
      if (cf==0){
        break;
      }
      if (cs>0)
        uf=c;
      ans.push_back('4');
      cf--;
    } else {
      if (i<7){
        if (cs==0){
          convertir=1;
          break;
        }
        ans.push_back('7');
        cs--;
        break;
      }
      if ((i==7 && cs==0) || i>7) {
          convertir=1;
          break;
      }
      ans.push_back('7');
      cs--;
    }
  }
  if (convertir){
    string ans1;
    if (uf!=-1){
      cf=cs=n/2;
      for (int i=0;i<uf;i++){
        ans1.push_back(ans[i]);
        if (ans[i]=='4'){
          cf--;
        } else {
          cs--;
        }
      }
      ans1.push_back('7');
      cs--;
      /*if (cs<0){
        for (int i=0;i<n/2+1;i++)
          cout << '4';
        for (int i=0;i<n/2+1;i++)
          cout << '7';
        cout << endl;
        return 0;
      }*/
      while (cf>0){
        ans1.push_back('4');
        cf--;
      }
      while (cs>0){
        ans1.push_back('7');
        cs--;
      }
      cout << ans1 << endl;
      return 0;
    }
    for (int i=0;i<n/2+1;i++)
      cout << '4';
    for (int i=0;i<n/2+1;i++)
      cout << '7';
    cout << endl;
    return 0;
  }
  while (cf>0){
    ans.push_back('4');
    cf--;
  }
  while (cs>0){
    ans.push_back('7');
    cs--;
  }
  cout << ans << endl;
  return 0;
}


#include <iostream>

int tc(char a){
  return a-int('0');
}

using namespace std;

int main(){
  string st;
  cin >> st;
  int n=st.size();
  if (n & 1){
    for (int i=0;i<n/2+1;i++)
      cout << '4';
    for (int i=0;i<n/2+1;i++)
      cout << '7';
    cout << endl;
    return 0;
  }
  int c=1;
  bool convertir=0;
  int cf=n/2,cs=n/2,uf=-1;
  string ans;
  for (int i=tc(st[0]);c<=n;i=tc(st[c++])){
    if (i<3){
      break;
    }
    if (i==4){
      if (cf==0){
        break;
      }
      uf=c-1;
      ans.push_back('4');
      cf--;
    } else {
      if (i<7){
        if (cs==0){
          convertir=1;
        }
        ans.push_back('7');
        cs--;
        break;
      }
      if ((i==7 && cs==0) || i>7) {
          convertir=1;
          break;
      }
      ans.push_back('7');
      cs--;
    }
  }
  if (convertir){
    if (uf!=-1){
      cf=cs=n/2;
      for (int i=0;i<uf;i++){
        cout << ans[i];
        if (ans[i]=='4'){
          cf--;
        } else {
          cs--;
        }
      }
      cout << '7';
      cs--;
      for (int i=uf+1;i<n;i++){
        if (cf>0){
          cout << '4';
          cf--;
        } else {
          cout << '7';
        }
      }
      cout << endl;
      return 0;
    }
    for (int i=0;i<n/2+1;i++)
      cout << '4';
    for (int i=0;i<n/2+1;i++)
      cout << '7';
    cout << endl;
    return 0;
  }
  while (cf>0){
    ans.push_back('4');
    cf--;
  }
  while (cs>0){
    ans.push_back('7');
    cs--;
  }
  cout << ans << endl;
  return 0;
}


#include <iostream>

int tc(char a){
  return a-int('0');
}

using namespace std;

int main(){
  string st;
  cin >> st;
  int n=st.size();
  if (n & 1){
    for (int i=0;i<n/2+1;i++)
      cout << '4';
    for (int i=0;i<n/2+1;i++)
      cout << '7';
    cout << endl;
    return 0;
  }
  int c=1;
  bool convertir=0;
  int cf=n/2,cs=n/2,uf=-1;
  string ans;
  for (int i=tc(st[0]);c<=n;i=tc(st[c++])){
    if (i<4){
      break;
    }
    if (i==4){
      if (cf==0){
        break;
      }
      uf=c-1;
      ans.push_back('4');
      cf--;
    } else {
      if (i<7){
        if (cs==0){
          convertir=1;
        }
        ans.push_back('7');
        cs--;
        break;
      }
      if ((i==7 && cs==0) || i>7) {
          convertir=1;
          break;
      }
      ans.push_back('7');
      cs--;
    }
  }
  if (convertir){
    if (uf!=-1){
      cf=cs=n/2;
      for (int i=0;i<uf;i++){
        cout << ans[i];
        if (ans[i]=='4'){
          cf--;
        } else {
          cs--;
        }
      }
      cout << '7';
      cs--;
      for (int i=uf+1;i<n;i++){
        if (cf>0){
          cout << '4';
          cf--;
        } else {
          cout << '7';
        }
      }
      cout << endl;
      return 0;
    }
    for (int i=0;i<n/2+1;i++)
      cout << '4';
    for (int i=0;i<n/2+1;i++)
      cout << '7';
    cout << endl;
    return 0;
  }
  while (cf>0){
    ans.push_back('4');
    cf--;
  }
  while (cs>0){
    ans.push_back('7');
    cs--;
  }
  cout << ans << endl;
  return 0;
}


#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>

using namespace std;

string smenor(int f,int s){
  string ans;
  for (int i=0;i<f;i++)
    ans.push_back('4');
  for (int i=0;i<s;i++)
    ans.push_back('7');
  return ans;
}
string smayor(int f,int s){
  string ans;
  for (int i=0;i<s;i++)
    ans.push_back('7');
  for (int i=0;i<f;i++)
    ans.push_back('4');
  return ans;
}

int main (){
  string st;
  cin >> st;
  int n=st.size();
  int uf=-1;
  int f=n/2;
  int s=n/2;
  if (n & 1 || atoll(st.c_str())>atoll(smayor(f,s).c_str()) ){
    cout << smenor(n/2+1,n/2+1) << endl;
    return 0;
  }
  string ans;
  for (int i=0;i<n;i++){
    string mayor,menor;
    menor=ans+smenor(f,s);
    mayor=ans+smayor(f,s);
    if(atoll(menor.c_str())>=atoll(st.c_str())){
      cout << menor << endl;
      return 0;
    }
    if(atoll(mayor.c_str())==atoll(st.c_str())){
      cout << mayor << endl;
      return 0;
    }
    if(atoll(mayor.c_str())>atoll(st.c_str())){
      ans.push_back(menor[i]);
      if (menor[i]=='4'){
        uf=i;
        f--;
      } else {
        s--;
      }
    }
    if(atoll(mayor.c_str())<atoll(st.c_str())){
      int f1=0,s1=0;
      string ans1;
      for (int j=0;j<uf;j++){
        if (ans[j]=='4') f1++;
        else s1++;
        ans1.push_back(ans[j]);
      }
      ans1.push_back('7');
      s1++;
      ans=ans1;
      i=ans1.size()-1;
      f=n/2-f1;
      s=n/2-s1;
    }
  }
  cout << ans << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
string s,num;
void dfs(bool f,int sum4,int sum7)
{
	int rem=s.size()-num.size();
	if(sum4+rem<s.size()/2||sum7+rem<s.size()/2)
		return;
	if(num.size()>=s.size())
	{
		cout<<num<<endl;
		exit(0);
	}
	if(f)
	{
		num+='4';dfs(1,sum4+1,sum7);num.pop_back();
		num+='7';dfs(1,sum4,sum7+1);num.pop_back();
	}
	int p=num.size();
	if(s[p]<='4')
	{
		num+='4';dfs(s[p]<'4',sum4+1,sum7);num.pop_back();
	}
	if(s[p]<='7')
	{
		num+='7';dfs(s[p]<'7',sum4,sum7+1);num.pop_back();
	}
	return;
}
int main()
{
	cin>>s;
	if(s.size()%2)
		s='0'+s;
	for(int i=s.size();;i+=2)
	{
		num="";
		dfs(0,0,0);
		s="00"+s;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
string s;
void dfs(string num,bool f,int sum4,int sum7)
{
	int rem=s.size()-num.size();
	if(sum4+rem<s.size()/2||sum7+rem<s.size()/2)
		return;
	if(num.size()>=s.size())
	{
		cout<<num<<endl;
		exit(0);
	}
	if(f)
	{
		dfs(num+'4',1,sum4+1,sum7);
		dfs(num+'7',1,sum4,sum7+1);
	}
	int p=num.size();
	if(s[p]<='4')
		dfs(num+'4',s[p]<'4',sum4+1,sum7);
	if(s[p]<='7')
		dfs(num+'7',s[p]<'7',sum4,sum7+1);
	return;
}
int main()
{
	cin>>s;
	if(s.size()%2)
		s='0'+s;
	for(int i=s.size();;i+=2)
	{
		dfs("",0,0,0);
		s="00"+s;
	}
	return 0;
}


//g++  5.4.0

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define M 1000000007
void pt(ll n)
{
    ll d=n/2;
    ll t=4;
    for(int i=1;i<=n;++i)
    {
        cout<<t;
        if(i==d)
            t=7;
    }
}
int main()
{
    string s;
    cin>>s;
    if(s.length()%2==1)
    {
        pt(s.length()+1);
        return 0;
    }
    ll n=s.length();
    ll fo=0,se=0;
    for(int i=0;i<s.length();++i)
    {
        if(s[i]!='4' && s[i]!='7')
        {
            if(s[i]<'4')
            {
                char b='4';
                for(int j=i,k=fo;j<s.length();++j,++k)
                {
                    if(k==n/2)
                        b='7';
                    s[j]=b;
                }
                cout<<s;
                return 0;
            }
            else if(s[i]<'7')
            {
                if(se==n/2)
                {
                    ll f=0;
                    for(int j=i-2;j>=0;--j)
                        if(s[j]=='4' && s[j+1]=='7')
                        {
                            f=1;
                            s[j]='7';
                            s[j+1]='4';
                            break;
                        }
                    if(f==0)
                    {
                        pt(s.length()+2);
                        return 0;
                    }
                    for(int j=i;j<s.length();++j)
                        s[j]='4';
                    cout<<s;
                    return 0;
                }
                se++;
                s[i]='7';
                char b='4';
                for(int j=i+1,k=fo;j<s.length();++j,++k)
                {
                    if(k==n/2)
                        b='7';
                    s[j]=b;
                }
                cout<<s;
                return 0;
            }
            else
            {
                if(se==n/2)
                {
                    ll f=0;
                    for(int j=i-2;j>=0;--j)
                        if(s[j]=='4' && s[j+1]=='7')
                        {
                            f=1;
                            s[j]='7';
                            s[j+1]='4';
                            break;
                        }
                    if(f==0)
                    {
                        pt(s.length()+2);
                        return 0;
                    }
                    for(int j=i;j<s.length();++j)
                        s[j]='4';
                    cout<<s;
                    return 0;
                }
                ll f=0;
                for(int j=i-1;j>=0;--j)
                    if(s[j]=='4')
                    {
                        s[j]='7';
                        se++;
                        fo--;
                        f=1;
                        break;
                    }
                if(f==0)
                {
                    pt(n+2);
                    return 0;
                }
                char b='4';
                for(int j=i,k=fo;j<s.length();++j,++k)
                {
                    if(k==n/2)
                        b='7';
                    s[j]=b;
                }
                cout<<s;
                return 0;
            }
        }
        else if(s[i]=='4')
            ++fo;
        else
            ++se;
        if(fo==s.length()/2+1)
        {
            for(int j=i;j<s.length();++j)
                s[j]='7';
            cout<<s;
            return 0;
        }
        else if(se==s.length()/2+1)
        {
            ll f=0;
            for(int j=i-2;j>=0;--j)
                if(s[j]=='4' && s[j+1]=='7')
                {
                    f=1;
                    s[j]='7';
                    s[j+1]='4';
                    break;
                }
            if(f==0)
            {
                pt(s.length()+2);
                return 0;
            }
            for(int j=i;j<s.length();++j)
                s[j]='4';
            cout<<s;
            return 0;
        }
    }
    cout<<s;
}