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

typedef long long int ll;

bool compare(const pair<ll,ll>&a,const pair<ll,ll>&b)
{
	if(a.first < b.first)
	{
		return 1;
	}
	else if(a.first ==b.first)
	{
		if(a.second<=b.second)
			return 1;
		else
			return 0;
	}
	else
		return 0;
}
bool comp(const pair<ll,ll>&a,const pair<ll,ll>&b)
{
	if(a.first < b.first )
		return 1;
	return 0;
}
int main()
{
	ios::sync_with_stdio(0);
	ll n,k,i,j;
	cin >> n >> k;
	vector< pair<ll,ll> > a(n);
	vector< pair<ll,ll> >b(n);
	for(i=0;i<n;i++)
	{
		ll x;
		cin >> x;
		a[i].first = x;
		b[i].first = x;
	}
	for(i=0;i<n;i++)
	{
		ll x;
		cin >> x;
		a[i].second = x;
		b[i].second = x;
	}
	sort(b.begin(),b.end(),compare);
	cout << a[0].second << " ";
	for(i=1;i<n;i++)
	{
		ll tem = a[i].first;
		pair<ll,ll>P;
		P = make_pair(tem,0);
		ll index = (ll)distance(b.begin(),lower_bound(b.begin(),b.end(),P,comp));
	//	cout << "index:" << index << "\n";
		ll cnt=0,sum=a[i].second;
		index--;
		while(index>=0 && cnt<k)
		{
			sum+=b[index].second;
			cnt++;
			index--;
		}
		cout << sum << " ";
	}
	cout << "\n";
	return 0;
}


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

typedef long long int ll;

int main()
{
	ios::sync_with_stdio(0);
	ll n,k;
	cin >> n >> k;
	map<ll,ll>M;
	ll i,j;
	vector< pair<ll,ll> >a(n);
	for(i=0;i<n;i++)
		cin >> a[i].first;
	for(i=0;i<n;i++)
	{
		cin >> a[i].second ;
		M.insert(make_pair(a[i].first,a[i].second));
	}
	multiset<ll>ans;
	map<ll,ll>fin;
	for(auto it=M.begin();it!=M.end();it++)
	{
		ll cnt=0,s=(*it).second;
		auto itr = ans.end();
		if(ans.empty())
		{
			ans.insert(s);
			fin[(*it).first]=s;
			continue;
		}
		itr--;
		while(cnt<k)
		{
			if(itr==ans.begin())
			{
				s+=(*itr);
				break;
			}
			cnt++;
			s+=(*itr);
			itr--;
		}
		ans.insert((*it).second);
		fin[(*it).first]=s;
	}
	for(i=0;i<n;i++)
	{
		cout << fin[a[i].first] << " ";
	}
	cout << "\n";
	return 0;
}


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

typedef long long int ll;

bool compare(const pair<ll,ll>&a,const pair<ll,ll>&b)
{
	if(a.first < b.first)
	{
		return 1;
	}
	else if(a.first ==b.first)
	{
		if(a.second<=b.second)
			return 1;
	}
	return 0;
}

int main()
{
	ios::sync_with_stdio(0);
	ll n,k,i,j;
	cin >> n >> k;
	vector< pair<ll,ll> > a(n);
	vector< pair<ll,ll> >b(n);
	multimap<ll,ll>M;
	for(i=0;i<n;i++)
	{
		ll x;
		cin >> x;
		a[i].first = x;
		b[i].first = x;
	}
	for(i=0;i<n;i++)
	{
		ll x;
		cin >> x;
		a[i].second = x;
		b[i].second = x;
	}
	sort(b.begin(),b.end(),compare);
	for(i=0;i<n;i++)
	{
		M.insert(make_pair(b[i].first,b[i].second));
	}
	cout << a[0].second << " ";
	for(i=1;i<n;i++)
	{
		ll tem = a[i].first;
		auto it = M.lower_bound(tem);
		ll cnt=0,sum=a[i].second;
		if(it==M.begin())
		{
			cout << sum << " ";
			continue;
		}
		it--;
		while(it!=M.begin() && cnt<k)
		{
			sum+=(*it).second;
			cnt++;
			it--;
		}
		if(cnt<k && it==M.begin())
			sum+=(*it).second;
		cout << sum << " ";
	}
	cout << "\n";
	return 0;
}


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

typedef long long int ll;

int main()
{
	ios::sync_with_stdio(0);
	ll n,k,i,j;
	cin >> n >> k;
	vector< pair<ll,ll> > a(n);
	multimap<ll,ll>M;
	for(i=0;i<n;i++)
	{
		ll x;
		cin >> x;
		a[i].first = x;
	}
	for(i=0;i<n;i++)
	{
		ll x;
		cin >> x;
		a[i].second = x;
		M.insert(make_pair(a[i].first,x));
	}
	cout << a[0].second << " ";
	for(i=1;i<n;i++)
	{
		ll tem = a[i].first;
		auto it = M.lower_bound(tem);
		it--;
		ll cnt=0,sum=a[i].second;
		while(it!=M.begin() && cnt<k)
		{
			sum+=(*it).second;
			cnt++;
			it--;
		}
		if(cnt<k)
			sum+=(*it).second;
		cout << sum << " ";
	}
	cout << "\n";
	return 0;
}


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

typedef long long int ll;

int main()
{
	ios::sync_with_stdio(0);
	ll n,k;
	cin >> n >> k;
	map<ll,ll>M;
	ll i,j;
	vector< pair<ll,ll> >a(n);
	for(i=0;i<n;i++)
		cin >> a[i].first;
	for(i=0;i<n;i++)
	{
		cin >> a[i].second ;
		M.insert(make_pair(a[i].first,a[i].second));
	}
	cout << a[0].second << " ";
	for(i=1;i<n;i++)
	{
		ll tem = a[i].first;
		auto it = M.lower_bound(tem);
		ll cnt=0,s=0;
		while(cnt<=k && it!=M.begin())
		{
			s+=(*it).second;
			it--;
			cnt++;
		}
		if(cnt<=k && it==M.begin())
		{
			s+=(*it).second;
		}
		cout << s << " ";
	}
	cout << "\n";
	return 0;
}


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

typedef long long int ll;

int main()
{
	ios::sync_with_stdio(0);
	ll n,k;
	cin >> n >> k;
	map<ll,ll>M;
	ll i,j;
	vector< pair<ll,ll> >a(n);
	for(i=0;i<n;i++)
		cin >> a[i].first;
	for(i=0;i<n;i++)
	{
		cin >> a[i].second ;
		M.insert(make_pair(a[i].first,a[i].second));
	}
	//cout << a[0].second << " ";
	for(i=0;i<n;i++)
	{
		ll tem = a[i].first;
		auto it = M.lower_bound(tem);
		ll cnt=0,s=0;
		while(cnt<=k && it!=M.begin())
		{
			s+=(*it).second;
			it--;
			cnt++;
		}
		if(cnt<=k && it==M.begin())
		{
			s+=(*it).second;
		}
		cout << s << " ";
	}
	cout << "\n";
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(c) c.begin(),c.end()
#define rap(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) rap(i,0,n)
#define efor(i,a) for(auto i : a)
#define rev1(i,n) for(ll i=n-1;i>=0;i--)
using namespace std;

int main() {
	ll n,k;
	cin>>n>>k;
	vector< pair<ll, pair<ll,ll> > > v(n);
	rep(i,n)
	{
		cin>>v[i].first;
		v[i].second.second=i;
	}
	rep(i,n)	cin>>v[i].second.first;
	sort(all(v));
	vector<ll> c(n);
	rep(i,n)
	{
		c[i]=v[i].second.first;
		swap(v[i].first,v[i].second.second);
	}
	rap(i,1,k)
	{
		if(k==0)	break;
		vector<ll> te(&c[0],&c[i]);
		for(ll j=0;j<min(k,i);j++)
		{
			v[i].second.first+=te[j];
			te.erase(te.begin()+j);
		}
	}
	vector<ll> te(&c[0],&c[k]);
	ll at;
	if(k!=0&&k<=n)
	{v[k].second.first=(c[k]+v[k-1].second.first);
	at=v[k-1].second.first;}
	//cout<<at<<endl;
	rap(i,k+1,n)
	{
		if(k==0)	break;
		ll l1=*min_element(all(te)),itl=min_element(all(te))-te.begin();
		if(c[i-1]>=l1)
		{
			at+=(c[i-1]-l1);
			te[itl]=c[i-1];
		}
		v[i].second.first+=at;
	}
	sort(all(v));
	rep(i,n)	cout<<v[i].second.first<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(c) c.begin(),c.end()
#define rap(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) rap(i,0,n)
#define efor(i,a) for(auto i : a)
#define rev1(i,n) for(ll i=n-1;i>=0;i--)
using namespace std;

int main() {
	ll n,k;
	cin>>n>>k;
	vector< pair<ll, pair<ll,ll> > > v(n);
	rep(i,n)
	{
		cin>>v[i].first;
		v[i].second.second=i;
	}
	rep(i,n)	cin>>v[i].second.first;
	sort(all(v));
	vector<ll> c(n);
	rep(i,n)
	{
		c[i]=v[i].second.first;
		swap(v[i].first,v[i].second.second);
	}
	rap(i,1,k)
	{
		if(k==0)	break;
		vector<ll> te(&c[0],&c[i]);
		for(ll j=0;j<min(k,i);j++)
		{
			v[i].second.first+=te[j];
			te.erase(te.begin()+j);
		}
	}
	vector<ll> te(&c[0],&c[k]);
	ll at;
	if(k!=0)
	{v[k].second.first=(c[k]+v[k-1].second.first);
	at=v[k-1].second.first;}
	//cout<<at<<endl;
	rap(i,k+1,n)
	{
		if(k==0)	break;
		ll l1=*min_element(all(te)),itl=min_element(all(te))-te.begin();
		if(c[i-1]>=l1)
		{
			at+=(c[i-1]-l1);
			te[itl]=c[i-1];
		}
		v[i].second.first+=at;
	}
	sort(all(v));
	rep(i,n)	cout<<v[i].second.first<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(c) c.begin(),c.end()
#define rap(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) rap(i,0,n)
#define efor(i,a) for(auto i : a)
#define rev1(i,n) for(ll i=n-1;i>=0;i--)
using namespace std;

int main() {
	ll n,k;
	cin>>n>>k;
	vector< pair<ll, pair<ll,ll> > > v(n);
	rep(i,n)
	{
		cin>>v[i].first;
		v[i].second.second=i;
	}
	rep(i,n)	cin>>v[i].second.first;
	sort(all(v));
	vector<ll> c(n);
	rep(i,n)
	{
		c[i]=v[i].second.first;
		swap(v[i].first,v[i].second.second);
	}
	rap(i,1,min(k,n))
	{
		if(k==0)	break;
		vector<ll> te(&c[0],&c[i]);
		for(ll j=0;j<min(k,i);j++)
		{
			v[i].second.first+=te[j];
			te.erase(te.begin()+j);
		}
	}
	vector<ll> te;
	ll at;
	if(k!=0&&k<=n)
	{v[k].second.first=(c[k]+v[k-1].second.first);
	at=v[k-1].second.first;
		vector<ll> t2(&c[0],&c[k]);
		te=t2;
	}
	//cout<<at<<endl;
	rap(i,k+1,n)
	{
		if(k==0)	break;
		ll l1=*min_element(all(te)),itl=min_element(all(te))-te.begin();
		if(c[i-1]>=l1)
		{
			at+=(c[i-1]-l1);
			te[itl]=c[i-1];
		}
		v[i].second.first+=at;
	}
	sort(all(v));
	rep(i,n)	cout<<v[i].second.first<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(c) c.begin(),c.end()
#define rap(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) rap(i,0,n)
#define efor(i,a) for(auto i : a)
#define rev1(i,n) for(ll i=n-1;i>=0;i--)
using namespace std;

int main() {
	ll n,k;
	cin>>n>>k;
	vector< pair<ll, pair<ll,ll> > > v(n);
	rep(i,n)
	{
		cin>>v[i].first;
		v[i].second.second=i;
	}
	rep(i,n)	cin>>v[i].second.first;
	sort(all(v));
	vector<ll> c(n);
	rep(i,n)
	{
		c[i]=v[i].second.first;
		swap(v[i].first,v[i].second.second);
	}
	rap(i,1,k)
	{
		if(k==0)	break;
		vector<ll> te(&c[0],&c[i]);
		for(ll j=0;j<min(k,i);j++)
		{
			v[i].second.first+=te[j];
			te.erase(te.begin()+j);
		}
	}
	vector<ll> te(&c[0],&c[k]);
	ll at;
	if(k!=0&&k<n)
	{v[k].second.first=(c[k]+v[k-1].second.first);
	at=v[k-1].second.first;}
	//cout<<at<<endl;
	rap(i,k+1,n)
	{
		if(k==0)	break;
		ll l1=*min_element(all(te)),itl=min_element(all(te))-te.begin();
		if(c[i-1]>=l1)
		{
			at+=(c[i-1]-l1);
			te[itl]=c[i-1];
		}
		v[i].second.first+=at;
	}
	sort(all(v));
	rep(i,n)	cout<<v[i].second.first<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(c) c.begin(),c.end()
#define rap(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) rap(i,0,n)
#define efor(i,a) for(auto i : a)
#define rev1(i,n) for(ll i=n-1;i>=0;i--)
using namespace std;

int main() {
	ll n,k;
	cin>>n>>k;
	vector< pair<ll, pair<ll,ll> > > v(n);
	rep(i,n)
	{
		cin>>v[i].first;
		v[i].second.second=i;
	}
	rep(i,n)	cin>>v[i].second.first;
	sort(all(v));
	vector<ll> c(n);
	rep(i,n)
	{
		c[i]=v[i].second.first;
		swap(v[i].first,v[i].second.second);
	}
	rap(i,1,min(k,n))
	{
		if(k==0)	break;
		vector<ll> te(&c[0],&c[i]);
		for(ll j=0;j<min(k,i);j++)
		{
			v[i].second.first+=te[j];
			te.erase(te.begin()+j);
		}
	}
	vector<ll> te;
	ll at;
	if(k!=0&&k<n)
	{v[k].second.first=(c[k]+v[k-1].second.first);
	at=v[k-1].second.first;
		vector<ll> t2(&c[0],&c[k]);
		te=t2;
	}
	//cout<<at<<endl;
	rap(i,k+1,n)
	{
		if(k==0)	break;
		ll l1=*min_element(all(te)),itl=min_element(all(te))-te.begin();
		if(c[i-1]>=l1)
		{
			at+=(c[i-1]-l1);
			te[itl]=c[i-1];
		}
		v[i].second.first+=at;
	}
	sort(all(v));
	rep(i,n)	cout<<v[i].second.first<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(c) c.begin(),c.end()
#define rap(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) rap(i,0,n)
#define efor(i,a) for(auto i : a)
#define rev1(i,n) for(ll i=n-1;i>=0;i--)
using namespace std;

int main() {
	ll n,k;
	cin>>n>>k;
	vector< pair<ll, pair<ll,ll> > > v(n);
	rep(i,n)
	{
		cin>>v[i].first;
		v[i].second.second=i;
	}
	rep(i,n)	cin>>v[i].second.first;
	sort(all(v));
	vector<ll> c(n);
	rep(i,n)
	{
		c[i]=v[i].second.first;
		swap(v[i].first,v[i].second.second);
	}
	rap(i,1,n)
	{
		if(k==0)	break;
		vector<ll> te(&c[0],&c[i]);
		for(ll j=0;j<min(k,i);j++)
		{
			ll add=*max_element(all(te)),it=max_element(all(te))-te.begin();
			v[i].second.first+=add;
			te.erase(te.begin()+it);
		}
	}
	sort(all(v));
	rep(i,n)	cout<<v[i].second.first<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(c) c.begin(),c.end()
#define rap(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) rap(i,0,n)
#define efor(i,a) for(auto i : a)
#define rev1(i,n) for(ll i=n-1;i>=0;i--)
using namespace std;

int main() {
	ll n,k;
	cin>>n>>k;
	vector< pair<ll, pair<ll,ll> > > v(n);
	rep(i,n)
	{
		cin>>v[i].first;
		v[i].second.second=i;
	}
	rep(i,n)	cin>>v[i].second.first;
	sort(all(v));
	vector<ll> c(n);
	rep(i,n)
	{
		c[i]=v[i].second.first;
		swap(v[i].first,v[i].second.second);
	}
	rap(i,1,k)
	{
		if(k==0)	break;
		vector<ll> te(&c[0],&c[i]);
		for(ll j=0;j<min(k,i);j++)
		{
			v[i].second.first+=te[j];
			te.erase(te.begin()+j);
		}
	}
	vector<ll> te;
	ll at;
	if(k!=0&&k<=n)
	{v[k].second.first=(c[k]+v[k-1].second.first);
	at=v[k-1].second.first;
		vector<ll> t2(&c[0],&c[k]);
		te=t2;
	}
	//cout<<at<<endl;
	rap(i,k+1,n)
	{
		if(k==0)	break;
		ll l1=*min_element(all(te)),itl=min_element(all(te))-te.begin();
		if(c[i-1]>=l1)
		{
			at+=(c[i-1]-l1);
			te[itl]=c[i-1];
		}
		v[i].second.first+=at;
	}
	sort(all(v));
	rep(i,n)	cout<<v[i].second.first<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(c) c.begin(),c.end()
#define rap(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) rap(i,0,n)
#define efor(i,a) for(auto i : a)
#define rev1(i,n) for(ll i=n-1;i>=0;i--)
using namespace std;

int main() {
	ll n,k;
	cin>>n>>k;
	vector< pair<ll, pair<ll,ll> > > v(n);
	rep(i,n)
	{
		cin>>v[i].first;
		v[i].second.second=i;
	}
	rep(i,n)	cin>>v[i].second.first;
	sort(all(v));
	vector<ll> c(n);
	rep(i,n)
	{
		c[i]=v[i].second.first;
		swap(v[i].first,v[i].second.second);
	}
	ll a1=min(n,k);
	//cout<<a1<<endl;
	rap(i,1,a1)
	{
		//cout<<i<<endl;
		if(k==0)	break;
		vector<ll> te(&c[0],&c[i]);
		for(ll j=0;j<min(k,i);j++)
		{
			v[i].second.first+=te[j];
			//te.erase(te.begin()+j);
		}
	}
	vector<ll> te;
	ll at;
	if(k!=0&&k<=n)
	{
		v[k].second.first=(c[k]+v[k-1].second.first);
		at=v[k-1].second.first;
		vector<ll> t2(&c[0],&c[k]);
		te=t2;
	}
	//cout<<at<<endl;
	rap(i,k+1,n)
	{
		if(k==0)	break;
		ll l1=*min_element(all(te)),itl=min_element(all(te))-te.begin();
		if(c[i-1]>=l1)
		{
			at+=(c[i-1]-l1);
			te[itl]=c[i-1];
		}
		v[i].second.first+=at;
	}
	sort(all(v));
	rep(i,n)	cout<<v[i].second.first<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(c) c.begin(),c.end()
#define rap(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) rap(i,0,n)
#define efor(i,a) for(auto i : a)
#define rev1(i,n) for(ll i=n-1;i>=0;i--)
using namespace std;

int main() {
	ll n,k;
	cin>>n>>k;
	vector< pair<ll, pair<ll,ll> > > v(n);
	rep(i,n)
	{
		cin>>v[i].first;
		v[i].second.second=i;
	}
	rep(i,n)	cin>>v[i].second.first;
	sort(all(v));
	vector<ll> c(n);
	rep(i,n)
	{
		c[i]=v[i].second.first;
		swap(v[i].first,v[i].second.second);
	}
	rap(i,1,k)
	{
		if(k==0)	break;
		vector<ll> te(&c[0],&c[i]);
		for(ll j=0;j<min(k,i);j++)
		{
			v[i].second.first+=te[j];
			te.erase(te.begin()+j);
		}
	}
	vector<ll> te(&c[0],&c[k]);
	ll at;
	v[k].second.first=(c[k]+v[k-1].second.first);
	at=v[k-1].second.first;
	//cout<<at<<endl;
	rap(i,k+1,n)
	{
		ll l1=*min_element(all(te)),itl=min_element(all(te))-te.begin();
		if(c[i-1]>=l1)
		{
			at+=(c[i-1]-l1);
			te[itl]=c[i-1];
		}
		v[i].second.first+=at;
	}
	sort(all(v));
	rep(i,n)	cout<<v[i].second.first<<" ";
	cout<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include <numeric>
using namespace std;
int c[100000];
long long a[100000];
pair<int,int>arr[100000];
int main()
{
	int i,j,n,k,x;
	cin>>n>>k;
	for(i=0;i<n;i++)
	{
		cin>>arr[i].first;
		arr[i].second=i;
	}

	for(i=0;i<n;i++)
		cin>>c[i];
	sort(arr,arr+n);
	vector<int>v;
	for(i=0;i<n;i++)
	{

		a[arr[i].second]=c[arr[i].second];
		a[arr[i].second]+=accumulate(v.begin(), v.end(), 0);
		if(v.size() == min(k,n-1) && min(k,n-1) != 0)
		{
			v[0]=max(v[0],c[arr[i].second]);
			sort(v.begin(),v.end());
		}
		else
			v.push_back(c[arr[i].second]);
	}
	for(i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;

}


#include<iostream>
#include<algorithm>
#include<vector>
#include <numeric>
using namespace std;
int c[100000];
long long a[100000];
pair<int,int>arr[100000];
int main()
{
	int i,j,n,k,x;
	cin>>n>>k;
	for(i=0;i<n;i++)
	{
		cin>>arr[i].first;
		arr[i].second=i;
	}

	for(i=0;i<n;i++)
		cin>>c[i];
	sort(arr,arr+n);
	vector<int>v;
	for(i=0;i<n;i++)
	{

		a[arr[i].second]=c[arr[i].second];
		a[arr[i].second]+=accumulate(v.begin(), v.end(), 0);
		if(v.size() == k && v.size() != 0)
		{
			v[0]=max(v[0],c[arr[i].second]);
			sort(v.begin(),v.end());
		}
		else
		{
			v.push_back(c[arr[i].second]);
			sort(v.begin(),v.end());
		}
	}
	for(i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;

}


#include<iostream>
#include<algorithm>
#include<vector>
#include <numeric>
using namespace std;
int c[100000];
long long a[100000];
pair<int,int>arr[100000];
int main()
{
	int i,j,n,k,x;
	cin>>n>>k;
	for(i=0;i<n;i++)
	{
		cin>>arr[i].first;
		arr[i].second=i;
	}

	for(i=0;i<n;i++)
		cin>>c[i];
	sort(arr,arr+n);
	vector<int>v;
	for(i=0;i<n;i++)
	{

		a[arr[i].second]=c[arr[i].second];
		for(j=0;j<v.size();j++)
			a[arr[i].second]+=v[j];
		if(v.size() == k && v.size() != 0)
		{
			v[0]=max(v[0],c[arr[i].second]);
			sort(v.begin(),v.end());
		}
		else if(v.size() < k)
		{
			v.push_back(c[arr[i].second]);
			sort(v.begin(),v.end());
		}
	}
	for(i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;

}


#include<iostream>
using namespace std;
int c[100000];
long long a[100000];
pair<int,int>arr[100000];
int main()
{
	int i,j,n,k,x;
	cin>>n>>k;
	for(i=0;i<n;i++)
	{
		cin>>arr[i].first;
		arr[i].second=i;
	}
	sort(arr,arr+n);
	for(i=0;i<n;i++)
		cin>>c[i];
	for(i=0;i<n;i++)
	{
		x=k;
		a[arr[i].second]=c[arr[i].second];
		for(j=i-1;j>=0 && x;j--)
			a[arr[i].second]+=c[arr[j].second],x--;

	}
	for(i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;

}


#include<iostream>
#include<algorithm>
#include<vector>
#include <numeric>
using namespace std;
int c[100000];
long long a[100000];
pair<int,int>arr[100000];
int main()
{
	int i,j,n,k,x;
	cin>>n>>k;
	for(i=0;i<n;i++)
	{
		cin>>arr[i].first;
		arr[i].second=i;
	}

	for(i=0;i<n;i++)
		cin>>c[i];
	sort(arr,arr+n);
	vector<int>v;
	for(i=0;i<n;i++)
	{

		a[arr[i].second]=c[arr[i].second];
		a[arr[i].second]+=accumulate(v.begin(), v.end(), 0);
		if(v.size() == k && v.size() != 0)
		{
			v[0]=max(v[0],c[arr[i].second]);
			sort(v.begin(),v.end());
		}
		else if(v.size() < k)
		{
			v.push_back(c[arr[i].second]);
			sort(v.begin(),v.end());
		}
	}
	for(i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;

}


#include<iostream>
#include<algorithm>
using namespace std;
int c[100000],a[100000];
pair<int,int>arr[100000];
int main()
{
	int i,j,n,k,x;
	cin>>n>>k;
	for(i=0;i<n;i++)
	{
		cin>>arr[i].first;
		arr[i].second=i;
	}
	sort(arr,arr+n);
	for(i=0;i<n;i++)
		cin>>c[i];
	for(i=0;i<n;i++)
	{
		x=k;
		a[arr[i].second]=c[arr[i].second];
		for(j=i-1;j>=0 && x;j--)
			a[arr[i].second]+=c[arr[j].second],x--;

	}
	for(i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;

}


#include<iostream>
#include<algorithm>
using namespace std;
int c[100000];
long long a[100000];
pair<int,int>arr[100000];
int main()
{
	int i,j,n,k,x;
	cin>>n>>k;
	for(i=0;i<n;i++)
	{
		cin>>arr[i].first;
		arr[i].second=i;
	}
	sort(arr,arr+n);
	for(i=0;i<n;i++)
		cin>>c[i];
	for(i=0;i<n;i++)
	{
		x=k;
		a[arr[i].second]=c[arr[i].second];
		for(j=i-1;j>=0 && x;j--)
			a[arr[i].second]+=c[arr[j].second],x--;

	}
	for(i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;

}


#include<iostream>
#include<algorithm>
#include<vector>
#include <numeric>
using namespace std;
int c[100000];
long long a[100000];
pair<int,int>arr[100000];
int main()
{
	int i,j,n,k,x;
	cin>>n>>k;
	for(i=0;i<n;i++)
	{
		cin>>arr[i].first;
		arr[i].second=i;
	}

	for(i=0;i<n;i++)
		cin>>c[i];
	sort(arr,arr+n);
	vector<int>v;
	for(i=0;i<n;i++)
	{

		a[arr[i].second]=c[arr[i].second];
		a[arr[i].second]+=accumulate(v.begin(), v.end(), 0);
		if(v.size() == min(k,n-1) && min(k,n) != 0)
		{
			v[0]=max(v[0],c[arr[i].second]);
			sort(v.begin(),v.end());
		}
		else
			v.push_back(c[arr[i].second]);
	}
	for(i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;

}


#include<iostream>
#include<algorithm>
#include<vector>
#include <numeric>
using namespace std;
int c[100000];
long long a[100000];
pair<int,int>arr[100000];
int main()
{
	int i,j,n,k,x;
	cin>>n>>k;
	for(i=0;i<n;i++)
	{
		cin>>arr[i].first;
		arr[i].second=i;
	}

	for(i=0;i<n;i++)
		cin>>c[i];
	sort(arr,arr+n);
	vector<int>v;
	for(i=0;i<n;i++)
	{

		a[arr[i].second]=c[arr[i].second];
		a[arr[i].second]+=accumulate(v.begin(), v.end(), 0);
		if(v.size() == min(k,n-1) && min(k,n-1) != 0)
		{
			v[0]=max(v[0],c[arr[i].second]);
			sort(v.begin(),v.end());
		}
		else
		{
			v.push_back(c[arr[i].second]);
			sort(v.begin(),v.end());
		}
	}
	for(i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;

}


#include<iostream>
using namespace std;
int c[100000],a[100000];
pair<int,int>arr[100000];
int main()
{
	int i,j,n,k,x;
	cin>>n>>k;
	for(i=0;i<n;i++)
	{
		cin>>arr[i].first;
		arr[i].second=i;
	}
	sort(arr,arr+n);
	for(i=0;i<n;i++)
		cin>>c[i];
	for(i=0;i<n;i++)
	{
		x=k;
		a[arr[i].second]=c[arr[i].second];
		for(j=i-1;j>=0 && x;j--)
			a[arr[i].second]+=c[arr[j].second],x--;

	}
	for(i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define se second
#define mp make_pair
typedef pair<int,int> ii;
typedef long long int ll;
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}
int main()
{
 int n,k;
 cin>>n>>k;
 int m=0;
 int p[n+1],c[n+1];
 for(int i=1;i<=n;i++)
    {cin>>p[i];m=max(m,p[i]);}
 for(int i=1;i<=n;i++)
    cin>>c[i];
 //   vector<int> a(m+1);
 vector<pair<int,int>> v;
 for(int i=1;i<=n;i++)
 {
     v.pb(make_pair(p[i],c[i]));
 }
 sort(v.begin(),v.end());
 vector<int> ans(m+1);
 cout<<v[0].second<<" ";
 for(int i=1;i<n;i++)
 {
     sort(v.begin(),v.begin()+i-1,sortbysec);
     int j=i-1,m1=k,sum=0;
     while(j>=0&&m1>0)
     {
         sum+=v[j].second;
         j--;
         m1--;
     }
     sum+=v[i].second;
     ans[v[i].first]=sum;
 }
 for(int i=2;i<=n;i++)
 cout<<ans[p[i]]<<" ";
}


/*
 D.M.BHIKADIYA
*/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define se second
#define mp make_pair
typedef long long int ll;
int main()
{
  ll n,k;
  cin>>n>>k;
  ll a[n],b[n];
  for(ll i=0;i<n;i++)
    cin>>a[i];
  for(ll i=0;i<n;i++)
    cin>>b[i];
  vector<pair<ll,ll>> v(n);
  map<ll,ll> m;
  for(ll i=0;i<n;i++)
  {
      v[i].f=a[i];
      v[i].se=b[i];
  }
  sort(v.begin(),v.end());
  if(k==0)
  {
      for(ll i=0;i<n;i++)
        cout<<b[i]<<" ";
      cout<<endl;
  }
  else
  {
      m[v[0].f]=v[0].se;
      vector<ll> ans;
      ans.pb(v[0].se);
      for(ll i=1;i<n;i++)
      {
         ll sum=0;
         for(ll j=0;j<ans.size();j++)
            sum+=ans[j];
         m[v[i].f]=sum+v[i].se;
         if(ans.size()==k)
         {
             sort(ans.begin(),ans.end());
             if(ans[0]<v[i].se)
                ans[0]=v[i].se;
         }
         else
            ans.pb(v[i].se);
      }
      for(ll i=0;i<n;i++)
        cout<<m[a[i]]<<" ";
  }
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define se second
#define mp make_pair
typedef pair<int,int> ii;
typedef long long int ll;
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}
int main()
{
 int n,k;
 cin>>n>>k;
 int p[n+1],c[n+1];
 for(int i=1;i<=n;i++)
    cin>>p[i];
 for(int i=1;i<=n;i++)
    cin>>c[i];
 vector<pair<int,int>> v;
 for(int i=1;i<=n;i++)
 {
     v.pb(make_pair(p[i],c[i]));
 }
 sort(v.begin(),v.end());
 vector<int> ans(n);
 cout<<v[0].second<<" ";
 for(int i=1;i<n;i++)
 {
     sort(v.begin(),v.begin()+i-1,sortbysec);
     int j=i-1,m1=k,sum=0;
     while(j>=0&&m1>0)
     {
         sum+=v[j].second;
         j--;
         m1--;
     }
     sum+=v[i].second;
     ans[i]=sum;
 }
 for(int i=1;i<n;i++)
    cout<<ans[i]<<" ";
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define se second
#define mp make_pair
typedef pair<int,int> ii;
typedef long long int ll;
int main()
{
  int n,k;
  cin>>n>>k;
  ll a[n],b[n];
  for(int i=0;i<n;i++)
    cin>>a[i];
  for(int i=0;i<n;i++)
    cin>>b[i];
  vector<pair<ll,ll>> v(n);
  map<ll,ll> m;
  for(int i=0;i<n;i++)
  {
      v[i].f=a[i];
      v[i].se=b[i];
  }
  sort(v.begin(),v.end());
  if(k==0)
  {
      for(int i=0;i<n;i++)
        cout<<b[i]<<" ";
      cout<<endl;
  }
  else
  {
      m[v[0].f]=v[0].se;
      vector<ll> ans;
      ans.pb(v[0].se);
      for(int i=1;i<n;i++)
      {
         int sum=0;
         for(int j=0;j<ans.size();j++)
            sum+=ans[j];
         m[v[i].f]=sum+v[i].se;
         if(ans.size()==k)
         {
             sort(ans.begin(),ans.end());
             if(ans[0]<v[i].se)
                ans[0]=v[i].se;
         }
         else
            ans.pb(v[i].se);
      }
      for(int i=0;i<n;i++)
        cout<<m[a[i]]<<" ";
  }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pi acos(-1)
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define P pair<ll,ll>
vector<P>vp,vp1;
vector<ll>v;
set<ll>st;
ll ara[500005];
map<ll,ll>mp;
ll Set(ll N,ll pos)
{
    return N=N | (1LL<<pos);
}
ll reset(ll N,ll pos)
{
    return N= N & ~(1LL<<pos);
}
bool check(ll N,ll pos)
{
    return (bool)(N & (1LL<<pos));
}
vector<ll>ans;
ll a[500005],b[500005];
int main()
{
    fast
    //    freopen("input.txt", "r", stdin);
    //    freopen("output.txt", "w", stdout);
    ll n,i,j,p=0,k;
    cin>>n>>k;
    for(i=0;i<n;i++)
        cin>>a[i];
    for(i=0;i<n;i++)
        cin>>b[i];
        for(i=0;i<n;i++)
        vp.pb({a[i],b[i]});

        sort(vp.begin(),vp.end());
        if(k==0){
                for(i=0;i<n;i++)
            cout<<b[i]<<" ";
        cout<<'\n';
        }

else{
    ll sum=0;
    mp[vp[0].first]=vp[0].second;
    ans.pb(vp[0].second);
    for(i=1;i<n;i++)
    {
        sum=0,p=0;
        for(j=0;j<ans.size();j++)
            sum+=ans[j];
        mp[vp[i].first]=sum+vp[i].second;
        if(ans.size()==k)
        {
            sort(ans.begin(),ans.end());
            if(ans[0]<vp[i].second)
                ans[0]=vp[i].second;
        }
        else
            ans.pb(vp[i].second);
    }
        for(i=0;i<n;i++)
            cout<<mp[a[i]]<<" ";
    cout<<'\n';
}
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define se second
#define mp make_pair
typedef pair<int,int> ii;
typedef long long int ll;
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second  <b.second);
}
int main()
{
 int n,k;
 cin>>n>>k;
 int m=0;
 int p[n+1],c[n+1];
 for(int i=1;i<=n;i++)
    {cin>>p[i];m=max(m,p[i]);}
 for(int i=1;i<=n;i++)
    cin>>c[i];
 //   vector<int> a(m+1);
 vector<pair<int,int>> v;
 for(int i=1;i<=n;i++)
 {
     v.pb(make_pair(p[i],c[i]));
 }
 sort(v.begin(),v.end());
 vector<int> ans(m+1);
 vector<int> v1;
 cout<<v[0].second<<" ";//<<endl;
 for(int i=1;i<n;i++)
 {
     for(int l=0;l<i;l++)
     v1.pb(v[l].second);
     sort(v1.begin(),v1.end());
   //  for(int l=0;l<=i;l++)
     //cout<<v[l].second<<" ";//endl;
     //cout<<endl;
     int j=i-1,m1=k,sum=0;
     while(j>=0&&m1>0)
     {
         sum+=v1[j];
         j--;
         m1--;
     }
     sum+=v[i].second;
     ans[v[i].first]=sum;
     v1.clear();
 }
 for(int i=2;i<=n;i++)
 cout<<ans[p[i]]<<" ";
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair

typedef long long ll;

const int Z = int(1e5) + 123;
const int inf = int(1e9) + 123;
const ll llinf = ll(1e18) + 123;

pair <int, ll> a[Z];
int c[Z], ans[Z];

int main () {
	ios_base::sync_with_stdio(false);
	int n, k;
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].first;
		a[i].second = i;
	}
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		ans[i] = c[i];
	}
	sort(a + 1, a + 1 + n);
	set <ll> mn;
	int l = 1;
	mn.insert(c[a[1].second]);
	ll sum = c[a[1].second];
	for (int i = 2; i <= n; i++) {
		ans[a[i].second] += sum;
		if (l < k) {
			l++;
			mn.insert(c[a[i].second]);
			sum += c[a[i].second];
		} else {
			if (c[a[i].second] > *mn.begin()) {
			sum -= *mn.begin();
			mn.erase(mn.begin());
			mn.insert(c[a[i].second]);
			sum += c[a[i].second];
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << ans[i] << ' ';
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair

typedef long long ll;

const int Z = int(1e5) + 123;
const int inf = int(1e9) + 123;
const ll llinf = ll(1e18) + 123;

pair <int, ll> a[Z];
int c[Z], ans[Z];

int main () {
	ios_base::sync_with_stdio(false);
	int n, k;
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].first;
		a[i].second = i;
	}
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		ans[i] = c[i];
	}
	sort(a + 1, a + 1 + n);
	set <ll> mn;
	int l = 1;
	mn.insert(c[a[1].second]);
	ll sum = c[a[1].second];
	for (int i = 2; i <= n; i++) {
		ans[a[i].second] += sum;
		if (l < k) {
			l++;
			mn.insert(c[a[i].second]);
			sum += c[a[i].second];
		} else {
			sum -= *mn.begin();
			mn.erase(mn.begin());
			mn.insert(c[a[i].second]);
			sum += c[a[i].second];
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << ans[i] << ' ';
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair

typedef long long ll;

const int Z = int(1e5) + 123;
const int inf = int(1e9) + 123;
const ll llinf = ll(1e18) + 123;

pair <int, ll> a[Z];
ll c[Z], ans[Z];

int main () {
	ios_base::sync_with_stdio(false);
	int n, k;
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].first;
		a[i].second = i;
	}
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		ans[i] = c[i];
	}
	sort(a + 1, a + 1 + n);
	set <ll> mn;
	int l = 1;
	if (k > 0) {
		mn.insert(c[a[1].second]);
	ll sum = c[a[1].second];
	for (int i = 2; i <= n; i++) {
		ans[a[i].second] += sum;
		if (l < k) {
			l++;
			mn.insert(c[a[i].second]);
			sum += c[a[i].second];
		} else {
			sum -= *mn.begin();
			mn.erase(mn.begin());
			mn.insert(c[a[i].second]);
			sum += c[a[i].second];
		}
	}
	}
	for (int i = 1; i <= n; i++) {
		cout << ans[i] << ' ';
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair

typedef long long ll;

const int Z = int(1e5) + 123;
const int inf = int(1e9) + 123;
const ll llinf = ll(1e18) + 123;

pair <int, ll> a[Z];
int c[Z], ans[Z];

int main () {
	ios_base::sync_with_stdio(false);
	int n, k;
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].first;
		a[i].second = i;
	}
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		ans[i] = c[i];
	}
	sort(a + 1, a + 1 + n);
	set <ll> mn;
	int l = 1;
	if (k > 0) {
		mn.insert(c[a[1].second]);
	ll sum = c[a[1].second];
	for (int i = 2; i <= n; i++) {
		ans[a[i].second] += sum;
		if (l < k) {
			l++;
			mn.insert(c[a[i].second]);
			sum += c[a[i].second];
		} else {
			sum -= *mn.begin();
			mn.erase(mn.begin());
			mn.insert(c[a[i].second]);
			sum += c[a[i].second];
		}
	}
	}
	for (int i = 1; i <= n; i++) {
		cout << ans[i] << ' ';
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair

typedef long long ll;

const int Z = int(1e5) + 123;
const int inf = int(1e9) + 123;
const ll llinf = ll(1e18) + 123;

pair <int, ll> a[Z];
ll c[Z], ans[Z];

int main () {
	ios_base::sync_with_stdio(false);
	int n, k;
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].first;
		a[i].second = i;
	}
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		ans[i] = c[i];
	}
	sort(a + 1, a + 1 + n);
	set <ll> mn;
	int l = 1;
	if (k > 0) {
		mn.insert(c[a[1].second]);
	ll sum = c[a[1].second];
	for (int i = 2; i <= n; i++) {
		ans[a[i].second] += sum;
		if (l < k) {
			l++;
			mn.insert(c[a[i].second]);
			sum += c[a[i].second];
		} else {
			if (c[a[i].second] > *mn.begin()) {
			sum -= *mn.begin();
			mn.erase(mn.begin());
			mn.insert(c[a[i].second]);
			sum += c[a[i].second];
			}
		}
	}
	}
	for (int i = 1; i <= n; i++) {
		cout << ans[i] << ' ';
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair

typedef long long ll;

const int Z = int(1e5) + 123;
const int inf = int(1e9) + 123;
const ll llinf = ll(1e18) + 123;

pair <int, ll> a[Z];
ll c[Z], ans[Z];

int main () {
	ios_base::sync_with_stdio(false);
	int n, k;
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].first;
		a[i].second = i;
	}
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		ans[i] = c[i];
	}
	sort(a + 1, a + 1 + n);
	multiset <ll> mn;
	int l = 1;
	if (k > 0) {
		mn.insert(c[a[1].second]);
	ll sum = c[a[1].second];
	for (int i = 2; i <= n; i++) {
		ans[a[i].second] += sum;
		if (l < k) {
			l++;
			mn.insert(c[a[i].second]);
			sum += c[a[i].second];
		} else {
			if (c[a[i].second] > *mn.begin()) {
			sum -= *mn.begin();
			mn.erase(mn.begin());
			mn.insert(c[a[i].second]);
			sum += c[a[i].second];
			}
		}
	}
	}
	for (int i = 1; i <= n; i++) {
		cout << ans[i] << ' ';
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mk make_pair

typedef long long ll;

const int Z = int(1e5) + 123;
const int inf = int(1e9) + 123;
const ll llinf = ll(1e18) + 123;

pair <int, ll> a[Z];
ll c[Z], ans[Z];

int main () {
	ios_base::sync_with_stdio(false);
	int n, k;
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].first;
		a[i].second = i;
	}
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		ans[i] = c[i];
	}
	sort(a + 1, a + 1 + n);
	multiset <ll> mn;
	int l = 1;
	if (k > 0) {
		mn.insert(c[a[1].second]);
	ll sum = c[a[1].second];
	for (int i = 2; i <= n; i++) {
		ans[a[i].second] += sum;
		if (l < k) {
			l++;
			mn.insert(c[a[i].second]);
			sum += c[a[i].second];
		} else {
			sum -= *mn.begin();
			mn.erase(mn.begin());
			mn.insert(c[a[i].second]);
			sum += c[a[i].second];
		}
	}
	}
	for (int i = 1; i <= n; i++) {
		cout << ans[i] << ' ';
	}
	return 0;
}


//#pragma GCC optimize("03")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx2,fma,sse2,sse3,ssse3,sse4,tune=native")

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#ifndef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1)
{
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args)
{
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1<<" | ";
    __f(comma+1, args...);
}
#else

#define debug(...)
#endif
#define Ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define sz(a) ((int)(a).size())
#define all(n) (n).begin(),(n).end()
#define ll long long
#define pb push_back
#define EPS 1e-9
#define space(n) for(int i = 0; i < n; ++i)printf(" ");

const int mod = int(1e9 + 7);
//const int mod = int(1e6 + 3);
int dcmp(const double &a, const double &b)
{
    if (fabs(a - b) < EPS)
        return 0;
    return ((a > b) << 1) - 1;
}
template<class T, class T2>
T _f_pow(T a, T2 b)
{
    T res = 1LL;
    //a%=mod;
    while (b > 0)
    {
        if (b & 1)
            res = (res*a);//res = (res * a)%mod;
        a = (a * a);
        //a%=mod;
        b >>= 1;
    }
    return (res);
}
//#define iter(x) for(int i = 0; i < (int)x; ++i)
ll OO = 0x3f3f3f3f;
const int N = 2e5 + 5;
const int M = 1e5 + 6;
/*
    Avril Lavigne is my Love
    Taylor Swift too
*/
bool cmp(pair<ll, ll>& f, pair<ll, ll>& s)
{
    if(f.first < s.first)
        return f.second > s.second;
    else
        return false;
}
int main()
{
    Ios;
    //freopen("in.in", "r",stdin);
    //freopen("out.out", "w",stdout);
    int n, k;
    cin >> n >> k;
    vector<pair<ll, ll>> knight(n);
    map<ll, ll> id;
    for(int i = 0; i < n; ++i)
    {
        cin >> knight[i].first;
        id[knight[i].first] = i;
    }
    for(int i = 0; i < n; ++i)
    {
        cin >> knight[i].second;
    }
    sort(all(knight), cmp);
    vector<ll> ans(n, 0);
    for(int i = 0; i < n; ++i)
    {
        for(int j = i; j >= 0 && j >= i - k - 1; --j)
        {
            ans[id[knight[i].first]] += knight[j].second;
        }
    }
    for(auto v: ans)
        cout << v << " ";
    cout << endl;
    //main();
    //system("sleep 50");
    return 0;
}


//#pragma GCC optimize("03")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx2,fma,sse2,sse3,ssse3,sse4,tune=native")

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#ifndef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1)
{
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args)
{
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1<<" | ";
    __f(comma+1, args...);
}
#else

#define debug(...)
#endif
#define Ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define sz(a) ((int)(a).size())
#define all(n) (n).begin(),(n).end()
#define ll long long
#define pb push_back
#define EPS 1e-9
#define space(n) for(int i = 0; i < n; ++i)printf(" ");

const int mod = int(1e9 + 7);
//const int mod = int(1e6 + 3);
int dcmp(const double &a, const double &b)
{
    if (fabs(a - b) < EPS)
        return 0;
    return ((a > b) << 1) - 1;
}
template<class T, class T2>
T _f_pow(T a, T2 b)
{
    T res = 1LL;
    //a%=mod;
    while (b > 0)
    {
        if (b & 1)
            res = (res*a);//res = (res * a)%mod;
        a = (a * a);
        //a%=mod;
        b >>= 1;
    }
    return (res);
}
//#define iter(x) for(int i = 0; i < (int)x; ++i)
ll OO = 0x3f3f3f3f;
const int N = 2e5 + 5;
const int M = 1e5 + 6;
/*
    Avril Lavigne is my Love
    Taylor Swift too
*/
bool cmp(pair<ll, ll>& f, pair<ll, ll>& s)
{
    if(f.first <= s.first)
        return f.second >= s.second;
    else
        return false;
}
int main()
{
    Ios;
    //freopen("in.in", "r",stdin);
    //freopen("out.out", "w",stdout);
    int n, k;
    cin >> n >> k;
    vector<pair<ll, ll>> knight(n);
    map<ll, ll> id;
    for(int i = 0; i < n; ++i)
    {
        cin >> knight[i].first;
        id[knight[i].first] = i;
    }
    for(int i = 0; i < n; ++i)
    {
        cin >> knight[i].second;
    }
    sort(all(knight), cmp);
    vector<ll> ans(n, 0);
    priority_queue<ll> dos, rdos;
    int ii, jj;
    for(auto [ii, jj]: knight)
        debug(ii, jj);
    for(int i = 0; i < n; ++i)
    {
        ans[id[knight[i].first]] += knight[i].second;
        rdos.push(knight[i].second);
        int kk = k;
        while(kk-- && dos.size())
        {
            ans[id[knight[i].first]] += dos.top();
            rdos.push(dos.top());
            dos.pop();
        }
        while(rdos.size())
        {
            dos.push(rdos.top());
            rdos.pop();
        }
    }
    for(auto v: ans)
        cout << v << " ";
    cout << endl;
    //main();
    //system("sleep 50");
    return 0;
}


//#pragma GCC optimize("03")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx2,fma,sse2,sse3,ssse3,sse4,tune=native")

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#ifndef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1)
{
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args)
{
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1<<" | ";
    __f(comma+1, args...);
}
#else

#define debug(...)
#endif
#define Ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define sz(a) ((int)(a).size())
#define all(n) (n).begin(),(n).end()
#define ll long long
#define pb push_back
#define EPS 1e-9
#define space(n) for(int i = 0; i < n; ++i)printf(" ");

const int mod = int(1e9 + 7);
//const int mod = int(1e6 + 3);
int dcmp(const double &a, const double &b)
{
    if (fabs(a - b) < EPS)
        return 0;
    return ((a > b) << 1) - 1;
}
template<class T, class T2>
T _f_pow(T a, T2 b)
{
    T res = 1LL;
    //a%=mod;
    while (b > 0)
    {
        if (b & 1)
            res = (res*a);//res = (res * a)%mod;
        a = (a * a);
        //a%=mod;
        b >>= 1;
    }
    return (res);
}
//#define iter(x) for(int i = 0; i < (int)x; ++i)
ll OO = 0x3f3f3f3f;
const int N = 2e5 + 5;
const int M = 1e5 + 6;
/*
    Avril Lavigne is my Love
    Taylor Swift too
*/
bool cmp(pair<ll, ll>& f, pair<ll, ll>& s)
{
    return f.first <= s.first;
}
int main()
{
    Ios;
    //freopen("in.in", "r",stdin);
    //freopen("out.out", "w",stdout);
    int n, k;
    cin >> n >> k;
    vector<pair<ll, ll>> knight(n);
    map<ll, int> id;
    for(int i = 0; i < n; ++i)
    {
        cin >> knight[i].first;
        id[knight[i].first] = i;
    }
    for(int i = 0; i < n; ++i)
    {
        cin >> knight[i].second;
    }
    sort(all(knight), cmp);
    vector<ll> ans(n, 0);
    priority_queue<ll> dos, rdos;
//    int ii, jj;
//    for(auto [ii, jj]: knight)
//        debug(ii, jj);
    for(int i = 0; i < n; ++i)
    {
        ans[id[knight[i].first]] += knight[i].second;
        rdos.push(knight[i].second);
        int kk = k;
        while(kk > 0 && dos.size())
        {
            ans[id[knight[i].first]] += dos.top();
            rdos.push(dos.top());
            dos.pop();
            --kk;
        }
        while(rdos.size())
        {
            dos.push(rdos.top());
            rdos.pop();
        }
    }
    for(auto v: ans)
        cout << v << " ";
    cout << endl;
    //main();
    //system("sleep 50");
    return 0;
}


//#pragma GCC optimize("03")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx2,fma,sse2,sse3,ssse3,sse4,tune=native")

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#ifndef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1)
{
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args)
{
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1<<" | ";
    __f(comma+1, args...);
}
#else

#define debug(...)
#endif
#define Ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define sz(a) ((int)(a).size())
#define all(n) (n).begin(),(n).end()
#define ll long long
#define pb push_back
#define EPS 1e-9
#define space(n) for(int i = 0; i < n; ++i)printf(" ");

const int mod = int(1e9 + 7);
//const int mod = int(1e6 + 3);
int dcmp(const double &a, const double &b)
{
    if (fabs(a - b) < EPS)
        return 0;
    return ((a > b) << 1) - 1;
}
template<class T, class T2>
T _f_pow(T a, T2 b)
{
    T res = 1LL;
    //a%=mod;
    while (b > 0)
    {
        if (b & 1)
            res = (res*a);//res = (res * a)%mod;
        a = (a * a);
        //a%=mod;
        b >>= 1;
    }
    return (res);
}
//#define iter(x) for(int i = 0; i < (int)x; ++i)
ll OO = 0x3f3f3f3f;
const int N = 2e5 + 5;
const int M = 1e5 + 6;
/*
    Avril Lavigne is my Love
    Taylor Swift too
*/
bool cmp(pair<int, int>& f, pair<int, int>& s)
{
    if(f.first < s.first)
        return f.second > s.second;
    else
        return false;
}
int main()
{
    Ios;
    //freopen("in.in", "r",stdin);
    //freopen("out.out", "w",stdout);
    int n, k;
    cin >> n >> k;
    vector<pair<int, int>> knight(n);
    map<int, int> id;
    for(int i = 0; i < n; ++i)
    {
        cin >> knight[i].first;
        id[knight[i].first] = i;
    }
    for(int i = 0; i < n; ++i)
    {
        cin >> knight[i].second;
    }
    sort(all(knight), cmp);
    vector<int> ans(n, 0);
    for(int i = 0; i < n; ++i)
    {
        for(int j = i; j >= 0 && j >= i - k; --j)
        {
            ans[id[knight[i].first]] += knight[j].second;
        }
    }
    for(auto v: ans)
        cout << v << " ";
    cout << endl;
    //main();
    //system("sleep 50");
    return 0;
}


//#pragma GCC optimize("03")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx2,fma,sse2,sse3,ssse3,sse4,tune=native")

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#ifndef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1)
{
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args)
{
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1<<" | ";
    __f(comma+1, args...);
}
#else

#define debug(...)
#endif
#define Ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define sz(a) ((int)(a).size())
#define all(n) (n).begin(),(n).end()
#define ll long long
#define pb push_back
#define EPS 1e-9
#define space(n) for(int i = 0; i < n; ++i)printf(" ");

const int mod = int(1e9 + 7);
//const int mod = int(1e6 + 3);
int dcmp(const double &a, const double &b)
{
    if (fabs(a - b) < EPS)
        return 0;
    return ((a > b) << 1) - 1;
}
template<class T, class T2>
T _f_pow(T a, T2 b)
{
    T res = 1LL;
    //a%=mod;
    while (b > 0)
    {
        if (b & 1)
            res = (res*a);//res = (res * a)%mod;
        a = (a * a);
        //a%=mod;
        b >>= 1;
    }
    return (res);
}
//#define iter(x) for(int i = 0; i < (int)x; ++i)
ll OO = 0x3f3f3f3f;
const int N = 2e5 + 5;
const int M = 1e5 + 6;
/*
    Avril Lavigne is my Love
    Taylor Swift too
*/
bool cmp(pair<ll, ll>& f, pair<ll, ll>& s)
{
    if(f.first < s.first)
        return f.second > s.second;
    else
        return false;
}
int main()
{
    Ios;
    //freopen("in.in", "r",stdin);
    //freopen("out.out", "w",stdout);
    int n, k;
    cin >> n >> k;
    vector<pair<ll, ll>> knight(n);
    map<ll, ll> id;
    for(int i = 0; i < n; ++i)
    {
        cin >> knight[i].first;
        id[knight[i].first] = i;
    }
    for(int i = 0; i < n; ++i)
    {
        cin >> knight[i].second;
    }
    sort(all(knight), cmp);
    vector<ll> ans(n, 0);
    for(int i = 0; i < n; ++i)
    {
        for(int j = i; j >= 0 && j >= i - k; --j)
        {
            ans[id[knight[i].first]] += knight[j].second;
        }
    }
    for(auto v: ans)
        cout << v << " ";
    cout << endl;
    //main();
    //system("sleep 50");
    return 0;
}


//#pragma GCC optimize("03")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx2,fma,sse2,sse3,ssse3,sse4,tune=native")

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#ifndef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1)
{
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args)
{
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1<<" | ";
    __f(comma+1, args...);
}
#else

#define debug(...)
#endif
#define Ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define sz(a) ((int)(a).size())
#define all(n) (n).begin(),(n).end()
#define ll long long
#define pb push_back
#define EPS 1e-9
#define space(n) for(int i = 0; i < n; ++i)printf(" ");

const int mod = int(1e9 + 7);
//const int mod = int(1e6 + 3);
int dcmp(const double &a, const double &b)
{
    if (fabs(a - b) < EPS)
        return 0;
    return ((a > b) << 1) - 1;
}
template<class T, class T2>
T _f_pow(T a, T2 b)
{
    T res = 1LL;
    //a%=mod;
    while (b > 0)
    {
        if (b & 1)
            res = (res*a);//res = (res * a)%mod;
        a = (a * a);
        //a%=mod;
        b >>= 1;
    }
    return (res);
}
//#define iter(x) for(int i = 0; i < (int)x; ++i)
ll OO = 0x3f3f3f3f;
const int N = 2e5 + 5;
const int M = 1e5 + 6;
/*
    Avril Lavigne is my Love
    Taylor Swift too
*/
bool cmp(pair<ll, ll>& f, pair<ll, ll>& s)
{
    if(f.first <= s.first)
        return f.second >= s.second;
    else
        return false;
}
int main()
{
    Ios;
    //freopen("in.in", "r",stdin);
    //freopen("out.out", "w",stdout);
    int n, k;
    cin >> n >> k;
    vector<pair<ll, ll>> knight(n);
    map<ll, ll> id;
    for(int i = 0; i < n; ++i)
    {
        cin >> knight[i].first;
        id[knight[i].first] = i;
    }
    for(int i = 0; i < n; ++i)
    {
        cin >> knight[i].second;
    }
    sort(all(knight), cmp);
    vector<ll> ans(n, 0);
    priority_queue<ll> dos, rdos;

    for(int i = 0; i < n; ++i)
    {
        dos.push(knight[i].second);
        int kk = k + 1;
        while(kk-- && dos.size())
        {
            ans[id[knight[i].first]] += dos.top();
            rdos.push(dos.top());
            dos.pop();
        }
        while(rdos.size())
        {
            dos.push(rdos.top());
            rdos.pop();
        }
    }
    for(auto v: ans)
        cout << v << " ";
    cout << endl;
    //main();
    //system("sleep 50");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ll n,k,a,b;
    cin>>n>>k;
    ll p[n],c[n],i,ans,temp=0;
    vector <pair <ll,ll> > v;
    map <ll,ll> mp,mp2;

    for(i=0;i<n;i++)
        cin>>p[i];
    for(i=0;i<n;i++)
        cin>>c[i];

    for(i=0;i<n;i++)
      v.push_back(make_pair(p[i],c[i]));
    sort(v.begin(),v.end());

    for(i=0;i<n;i++)
    {
        ans = v[i].second;
         b=k;
        if(mp.size()>k)
        {
            auto it=mp.end();
            while(b>0)
            {
              it--;
              a=it->second;
              while(a>0&&b>0)
              {
               ans=ans+it->first;
               b--;
               a--;
              }
            }
        }
        else
        {
            for(auto it=mp.begin();it!=mp.end();it++)
            {
                a=it->second;
                while(a>0&&b>0)
                {
                  ans=ans+it->first;
                  b--;
                  a--;
                }
                if(b<=0)
                break;
            }
        }
        mp2[v[i].first]=ans;
        mp[v[i].second]++;
    }

    for(i=0;i<n;i++)
        cout<<mp2[p[i]]<<" ";
    cout<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ll n,k,a,b;
    cin>>n>>k;
    ll p[n],c[n],i,ans,temp=0;
    vector <pair <ll,ll> > v;
    map <ll,ll> mp,mp2;

    for(i=0;i<n;i++)
        cin>>p[i];
    for(i=0;i<n;i++)
        cin>>c[i];

    for(i=0;i<n;i++)
      v.push_back(make_pair(p[i],c[i]));
    sort(v.begin(),v.end());

    for(i=0;i<n;i++)
    {
        ans = v[i].second;
        if(mp.size()>k)
        {
            b=k;
            auto it=mp.end();
            while(b--)
            {
              it--;
              ans=ans+it->second;
            }
        }
        else
        {
            for(auto it=mp.begin();it!=mp.end();it++)
                ans=ans+it->second;
        }
        mp2[v[i].first]=ans;
        mp[v[i].first] = v[i].second;
    }

    for(i=0;i<n;i++)
        cout<<mp2[p[i]]<<" ";
    cout<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ll n,k,a,b;
    cin>>n>>k;
    ll p[n],c[n],i,ans,temp=0;
    vector <pair <ll,ll> > v;
    map <ll,ll> mp,mp2;

    for(i=0;i<n;i++)
        cin>>p[i];
    for(i=0;i<n;i++)
        cin>>c[i];

    for(i=0;i<n;i++)
      v.push_back(make_pair(p[i],c[i]));
    sort(v.begin(),v.end());

    for(i=0;i<n;i++)
    {
        ans = v[i].second;
        if(mp.size()>k)
        {
            b=k;
            auto it=mp.end();
            while(b--)
            {
              it--;
              ans=ans+it->first;
            }
        }
        else
        {
            for(auto it=mp.begin();it!=mp.end();it++)
                ans=ans+it->first;
        }
        mp2[v[i].first]=ans;
        mp[v[i].second] = v[i].first;
    }

    for(i=0;i<n;i++)
        cout<<mp2[p[i]]<<" ";
    cout<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ll n,k,a,b;
    cin>>n>>k;
    ll p[n],c[n],i,ans,temp=0;
    vector <pair <ll,ll> > v;
    map <ll,ll> mp,mp2;

    for(i=0;i<n;i++)
        cin>>p[i];
    for(i=0;i<n;i++)
        cin>>c[i];

    for(i=0;i<n;i++)
      v.push_back(make_pair(p[i],c[i]));
    sort(v.begin(),v.end());

    for(i=0;i<n;i++)
    {
        ans = v[i].second;
         b=k;
        if(mp.size()>k)
        {
            auto it=mp.end();
            while(b>0)
            {
              it--;
              a=it->second;
              while(a>0&&b>0)
              {
               ans=ans+it->first;
               b--;
              }
            }
        }
        else
        {
            for(auto it=mp.begin();it!=mp.end();it++)
            {
                a=it->second;
                while(a>0&&b>0)
                {
                  ans=ans+it->first;
                  b--;
                }
                if(b<=0)
                break;
            }
        }
        mp2[v[i].first]=ans;
        mp[v[i].second]++;
    }

    for(i=0;i<n;i++)
        cout<<mp2[p[i]]<<" ";
    cout<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ll n,k,a,b;
    cin>>n>>k;
    ll p[n],c[n],i,ans,temp=0;
    vector <pair <ll,ll> > v;
    map <ll,ll> mp,mp2;

    for(i=0;i<n;i++)
        cin>>p[i];
    for(i=0;i<n;i++)
        cin>>c[i];

    for(i=0;i<n;i++)
      v.push_back(make_pair(p[i],c[i]));
    sort(v.begin(),v.end());

    for(i=0;i<n;i++)
    {
        ans = v[i].second;
        //cout<<"ans:  "<<ans<<endl;
        if(mp.size()>k)
        {
            b=k;
            auto it=mp.end();
            it--;
            while(b--)
            {
              ans=ans+it->first;
              it--;
              //cout<<"ans: "<<ans<<endl;
            }
        }
        else
        {
            for(auto a:mp)
            {
                ans=ans+a.first;
               // cout<<"ans:  "<<ans<<endl;
            }
        }
        mp2[v[i].first]=ans;
        mp[v[i].second]++;
    }

    for(i=0;i<n;i++)
        cout<<mp2[p[i]]<<" ";
    cout<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ll n,k,a,b;
    cin>>n>>k;
    ll p[n],c[n],i,ans,temp=0;
    vector <pair <ll,ll> > v;
    map <ll,ll> mp,mp2;

    for(i=0;i<n;i++)
        cin>>p[i];
    for(i=0;i<n;i++)
        cin>>c[i];

    for(i=0;i<n;i++)
      v.push_back(make_pair(p[i],c[i]));
    sort(v.begin(),v.end());

    for(i=0;i<n;i++)
    {
        ans = v[i].second;
        b=k;

        auto it=mp.end();
        while(b>0)
        {
            if(it==mp.begin())
              break;
            it--;
              a=it->second;
              while(a>0&&b>0)
              {
               ans=ans+it->first;
               b--;
               a--;
              }
        }

        mp2[v[i].first]=ans;
        mp[v[i].second]++;
    }

    for(i=0;i<n;i++)
        cout<<mp2[p[i]]<<" ";
    cout<<endl;
    return 0;
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define sz(v) ((ll)v.size())
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define pii pair<ll,ll>
const ll mod = 1000000007;
const ll mod1= 1000000009;
const ll base= 2323233;
const ll base1=3424424;
#define MAXN 2000004
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
ll a[3000002];
bool cmp(const pair<pii,ll> &x,const pair<pii,ll> &y){
  if(x.F.F==y.F.F) return x.S>y.S;
  return x.F.F<y.F.F;
}
int main() {
   ios::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   ll n,m,i;
   cin>>n>>m;
   vector<pair<pii,ll>> v;
   for(i=0;i<m;i++){
    ll x,y;
    cin>>x>>y;
    v.pb({{x,y},i});
   }
   vector<pii> ans(m);
   sort(all(v),cmp);
   ll curr=1;
   ll base=1;
   for(auto it: v){
     if(it.F.S==1){
      ans[it.S]={1,curr+1};
      curr++;
      a[curr]=curr-1;
     }
     else{
         if(a[base]==0) base++;
         if(base>curr) {
          cout<<-1;
          return 0;
         }
         ans[it.S]={base,a[base]--};
     }
   }
   for(auto it: ans) cout<<it.F<<" "<<it.S<<'\n';
 }


#include <bits/stdc++.h>

#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define CLR(t, value) memset(t, value, sizeof(t))
#define ALL(v) v.begin(), v.end()
#define SZ(v) ((int)(v).size())
#define TEST(x) cerr << "test " << #x << " " << x << endl;
#define sc(x) scanf("%d", &x)

using namespace std;

typedef long long Long;
typedef vector<int> vInt;
typedef pair<int,int> Pair;

const int N = 1e5 + 2;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;

void fast_io() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}

/************************************/

int C[N];

int main() {
    fast_io();
    int n,m, w, in;
    cin>>n>>m;
    vector<pair<Pair, int> > v;
    REP(i, m){
        cin>>w>>in;
        v.push_back({{w, in}, i});
    }
    sort(ALL(v));
    int nc=2, ncc=2, ne=0;
    bool ok = 1;
    vector <pair<int, Pair> > ans;
    REP(i,n+1)C[i]=i;
    for (auto &p: v) {
        if (p.first.second) {
            if (nc>n) {
                ok = 1;
                break;
            }
            ne++;
            ans.push_back({p.second, {1, nc++}});
        } else {
            if (nc < 10000 && ne == ((nc-1)*(nc-2))/2) {
                ok=0;
                break;
            }
            ne++;
            while (ncc < nc && nc - C[ncc]<2)ncc++;
            if (ncc == nc) {
                ncc = 2;
                while (nc - C[ncc]<2)ncc++;
            }
            C[ncc]++;
            ans.push_back({p.second, {ncc, C[ncc]}});
        }
    }
    if (!ok) {
        puts("-1");
        return 0;
    }
    sort(ALL(ans));
    for (auto &p: ans) {
        cout<<p.second.first<<" "<<p.second.second<<endl;
    }

    return 0 ;
}


#include <bits/stdc++.h>

#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define CLR(t, value) memset(t, value, sizeof(t))
#define ALL(v) v.begin(), v.end()
#define SZ(v) ((int)(v).size())
#define TEST(x) cerr << "test " << #x << " " << x << endl;
#define sc(x) scanf("%d", &x)

using namespace std;

typedef long long Long;
typedef vector<int> vInt;
typedef pair<int,int> Pair;

const int N = 1e5 + 2;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;

void fast_io() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}

/************************************/

int C[N];

int main() {
    fast_io();
    int n,m, w, in;
    cin>>n>>m;
    vector<pair<Pair, int> > v;
    REP(i, m){
        cin>>w>>in;
        v.push_back({{w, -in}, i});
    }
    sort(ALL(v));
    int nc=2, ncc=2, ne=0;
    bool ok = 1;
    vector <pair<int, Pair> > ans;
    REP(i,n+1)C[i]=i;
    for (auto &p: v) {
        if (p.first.second) {
            if (nc>n) {
                ok = 1;
                break;
            }
            ne++;
            ans.push_back({p.second, {1, nc++}});
        } else {
            if (nc < 10000 && ne == ((nc-1)*(nc-2))/2) {
                ok=0;
                break;
            }
            ne++;
            while (ncc < nc && nc - C[ncc]<2)ncc++;
            if (ncc == nc) {
                ncc = 2;
                while (nc - C[ncc]<2)ncc++;
            }
            C[ncc]++;
            ans.push_back({p.second, {ncc, C[ncc]}});
        }
    }
    if (!ok) {
        puts("-1");
        return 0;
    }
    sort(ALL(ans));
    for (auto &p: ans) {
        cout<<p.second.first<<" "<<p.second.second<<endl;
    }

    return 0 ;
}


#include <bits/stdc++.h>

#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define CLR(t, value) memset(t, value, sizeof(t))
#define ALL(v) v.begin(), v.end()
#define SZ(v) ((int)(v).size())
#define TEST(x) cerr << "test " << #x << " " << x << endl;
#define sc(x) scanf("%d", &x)

using namespace std;

typedef long long Long;
typedef vector<int> vInt;
typedef pair<int,int> Pair;

const int N = 1e5 + 2;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;

void fast_io() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}

/************************************/

int C[N];

int main() {
    fast_io();
    int n,m, w, in;
    cin>>n>>m;
    vector<pair<Pair, int> > v;
    REP(i, m){
        cin>>w>>in;
        v.push_back({{w, in}, i});
    }
    sort(ALL(v));
    int nc=2, ncc=2, ne=0;
    bool ok = 1;
    vector <pair<int, Pair> > ans;
    REP(i,n+1)C[i]=i;
    for (auto &p: v) {
        if (p.first.second) {
            if (nc>n) {
                ok = 1;
                break;
            }
            ne++;
            ans.push_back({p.second, {1, nc++}});
        } else {
            if (ne == ((nc-1)*(nc-2))/2) {
                ok=0;
                break;
            }
            ne++;
            while (ncc < nc && nc - C[ncc]<2)ncc++;
            if (ncc == nc) {
                ncc = 2;
                while (nc - C[ncc]<2)ncc++;
            }
            C[ncc]++;
            ans.push_back({p.second, {ncc, C[ncc]}});
        }
    }
    if (!ok) {
        puts("-1");
        return 0;
    }
    sort(ALL(ans));
    for (auto &p: ans) {
        cout<<p.second.first<<" "<<p.second.second<<endl;
    }

    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+9;
int n,m,p1=1,p2=2,p3=1;
pair<int,int> ans[MAX];
vector<pair<pair<int,int>,int> > edge;
int main()
{
	cin>>n>>m;
	for (int i=0,w,t;i<m;i++) cin>>w>>t,edge.push_back({{w,-t},i});
	sort(edge.begin(),edge.end());
	for (auto e:edge)
	{
		if (e.first.second==-1) ans[e.second]={0,p1},p1++;
		else
		{
			if (p3==0) p2++,p3=p2-1;
			if (p2>=p1 || p3==0) return cout<<-1,0;
			ans[e.second]={p2,p3},p3--;
		}
	}
	for (int i=0;i<m;i++) cout<<ans[i].first+1<<" "<<ans[i].second+1<<'\n';
	return 0;
}


//Be Name Khoda
//no comment...
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using ordered_multiset =  tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using mordered_set =  tree<T, null_type, greater<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)1e5 + 7;
const int infint = (int)1e9;
const ll inf = (ll)1e18;
pair<pair<int, int>, int> P[MAXN];
pair<int, int> ans[MAXN];
void add_edge(int u, int v, int e)
{
	ans[e] = {u, v};
	return;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
    	cin >> P[i].first.first >> P[i].first.second;
    	P[i].first.second = 1 - P[i].first.second;
    	P[i].second = i;
	}
	sort(P, P + m);
	set<pair<int, int> > S;
	int extra = 0, sz = 1;
	for (int i = 0; i < m; i++)
	{
		if(P[i].first.second == 0)
		{
			sz++;
			add_edge(sz, sz - 1, P[i].second);
			for (int j = 1; j < sz - 1 && extra < m - (n - 1); j++)
				extra++, S.insert({sz, j});
			continue;
		}
		else
		{
			if(S.size() == 0)
				return cout << -1, 0;
			pair<int, int> Q = *S.begin();
			S.erase(*S.begin());
			add_edge(Q.first, Q.second, P[i].second);
		}
	}
	for (int i = 0; i < m; i++)
		cout << ans[i].first << " " << ans[i].second << "\n";
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 7;
struct edge{
	int s, e;
	int dis;
	int id;
}mp[MAXN];
int f[MAXN];
int n, m;
int find(int p){
	if (f[p] == p)return p;
	else return f[p] = find(f[p]);
}
bool merge(int a, int b){
	int ta = find(a);
	int tb = find(b);
	if (ta != tb){
		f[ta] = tb;
		return true;
	}
	return false;
}
bool cmp(edge a, edge b){
	return a.id < b.id;
}
void init(){
	for (int i = 1; i <= n; i++)f[i] = i;
}
struct Edge{
	int w, id;
	int isIn;
}mpp[MAXN];
bool Cmp(Edge a, Edge b){
	if (a.w == b.w)
		return a.isIn>b.isIn;
	return a.w < b.w;
}
int main(){
	int n, m;
	scanf("%d %d", &n, &m);
	for (int i = 0; i < m; i++){
		scanf("%d %d", &mpp[i].w, &mpp[i].isIn);
		mpp[i].id = i;
	}
	sort(mpp, mpp + m, Cmp);
	//for (int i = 0; i < m; i++)
	//	printf("**%d %d\n", mpp[i].w, mpp[i].isIn);
	bool flag = true;
	int cntedge = 0;
	int cnt = 1;
	int lasts = 2, laste = 1;
	for (int i = 0; i < m; i++){
		if (mpp[i].isIn){
			mp[cntedge].s = 1;
			mp[cntedge].e = ++cnt;
		}
		else{
			if (laste + 1 >= lasts){
				lasts++, laste = 1;
				if (lasts > cnt){
					flag = false;
					break;
				}
			}
			mp[cntedge].s = lasts;
			mp[cntedge].e = ++laste;
		}
		mp[cntedge++].id = mpp[i].id;
	}
	if (!flag)
		printf("-1\n");
	else{
		sort(mp, mp + cntedge, cmp);
		for (int i = 0; i < cntedge; i++)
			printf("%d %d\n", mp[i].s, mp[i].e);
	}
	return 0;
}


#include <bits/stdc++.h>

#define pb push_back
#define ft first
#define sd second

using namespace std;
typedef pair <int, int> pii;

const int N = 100000;

int n, m, k, l, p, t = 1, nz = 1;

vector <pii> a, b;

pii o[N];

int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		cin >> k >> l;
		if (l == 1)
			a.pb({k, i});
		else
			b.pb({k, i});
	}
	sort (a.begin(), a.end());
	sort (b.begin(), b.end());
	for (auto i : b)
	{
		if (nz == 0)
		{
			t++;
			nz = p + 1;
			p = 0;
		}
		if (a[t].ft <= i.ft)
			o[i.sd] = {t + 2, t + 1 - p};
		else
		{
			cout << -1;
			return 0;
		}
		p++;
		nz--;
	}
	for (int i = 0; i < a.size(); i++)
		o[a[i].sd] = {1, i + 2};
	for (int i = 0; i < m; i++)
			cout << o[i].ft << " " << o[i].sd << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define f first
#define s second
const int N = 1e5 + 3;
pair < int , int > p[N];
pair < int , int > ans[N];
int parent[N] , height[N];
vector < int > v;
struct DSU {
      int parent[N];
      int sz[N];
      DSU() {
            for(int i = 0; i < N; ++i)
                  parent[i] = i;

            fill(sz , sz+N , 1);
      }

      int findParent(int i) {
            if(parent[i] == i)
            return i;
            return parent[i] = findParent(parent[i]);
      }

      bool areTeam(int x , int y) {
            int leader1 = findParent(x);
            int leader2 = findParent(y);
            return leader1 == leader2;
      }

      void mergeTeams(int x , int y) {
            int leader1 = findParent(x);
            int leader2 = findParent(y);

            if(leader1 == leader2)
            return;

            if(sz[leader1] > sz[leader2]) {
                  parent[leader2] = leader1;
                  sz[leader1] += sz[leader2];
                  sz[leader2] = 0;
            }
            else {
                  parent[leader1] = leader2;
                  sz[leader2] += sz[leader1];
                  sz[leader1] = 0;
            }
      }
};

int main()
{   ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n , m , w , o ;
    cin >> n >> m ;
    ll sum1 = 0 , sum2 = 0;
    for(int i = 0 ; i < m ; ++i){
        cin >> p[i].f >> p[i].s;
        if(p[i].s){
            sum1+=p[i].f;
        }
    }
    int u1 = 1 , v1 = 2 , u2 = 2 , v2 = 3 ;
    vector <  pair < int , int >   > pv;
    for(int i = 0 ; i <  m ; ++i){
        pv.push_back({p[i].f , i});
    }
    DSU d;
    sort(pv.begin() , pv.end());
    for(int i = 0 ; i < m ; ++i){
        int ff = pv[i].f , ss = pv[i].s;
        if(p[ss].s){
            if(!d.areTeam(u1 , v1)){
                d.mergeTeams(u1 , v1);
                sum2+=pv[i].f;
            }
            ans[ss] = {u1 , v1++};
        }
        else{
            if(u2 == v2){
                u2 = 2 ;
                v2++;
            }
            if(v2 > n)
                v2 = n;
            if(!d.areTeam(u2 , v2)){
                d.mergeTeams(u2 , v2);
                sum2+=pv[i].f;
            }
            ans[ss] = {u2++ , v2};
    }
    }
    if(sum1 <=sum2){
        for(int i = 0 ; i < m ; ++i) cout << ans[i].f << ' ' << ans[i].s << endl;
    }
    else
        cout << -1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define f first
#define s second
const int N = 1e5 + 3;
pair < int , int > p[N];
pair < int , int > ans[N];
int parent[N] , height[N];
vector < int > v;
struct DSU {
      int parent[N];
      int sz[N];
      DSU() {
            for(int i = 0; i < N; ++i)
                  parent[i] = i;

            fill(sz , sz+N , 1);
      }

      int findParent(int i) {
            if(parent[i] == i)
            return i;
            return parent[i] = findParent(parent[i]);
      }

      bool areTeam(int x , int y) {
            int leader1 = findParent(x);
            int leader2 = findParent(y);
            return leader1 == leader2;
      }

      void mergeTeams(int x , int y) {
            int leader1 = findParent(x);
            int leader2 = findParent(y);

            if(leader1 == leader2)
            return;

            if(sz[leader1] > sz[leader2]) {
                  parent[leader2] = leader1;
                  sz[leader1] += sz[leader2];
                  sz[leader2] = 0;
            }
            else {
                  parent[leader1] = leader2;
                  sz[leader2] += sz[leader1];
                  sz[leader1] = 0;
            }
      }
};

int main()
{   ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n , m , w , o ;
    cin >> n >> m ;
    ll sum1 = 0 , sum2 = 0;
    for(int i = 0 ; i < m ; ++i){
        cin >> p[i].f >> p[i].s;
        if(p[i].s){
            sum1+=p[i].f;
        }
    }
    int u1 = 1 , v1 = 2 , u2 = 2 , v2 = 3 ;
    vector <  pair < int , int >   > pv;
    for(int i = 0 ; i <  m ; ++i){
        pv.push_back({p[i].f , i});
    }
    DSU d;
    sort(pv.begin() , pv.end());
    for(int i = 0 ; i < m ; ++i){
        int ff = pv[i].f , ss = pv[i].s;
        if(p[ss].s){
            if(!d.areTeam(u1 , v1)){
                d.mergeTeams(u1 , v1);
                sum2+=pv[i].f;
            }
            ans[ss] = {u1 , v1++};
        }
        else{
            if(v2 > n){
                u2++;
                v2 = u2 + 1;
            }
            if(!d.areTeam(u2 , v2)){
                d.mergeTeams(u2 , v2);
                sum2+=pv[i].f;
            }
            ans[ss] = {u2 , v2++};
        }
    }
    if(sum1<=sum2){
        for(int i = 0 ; i < m ; ++i) cout << ans[i].f << ' ' << ans[i].s << endl;
    }
    else
        cout << -1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define f first
#define s second
const int N = 1e5 + 3;
pair < int , int > p[N];
int parent[N] , height[N];
vector < int > v;
//int find_parent(int node){
//    if(node == parent[node])
//        return node;
//    return parent[node] = find_parent(parent[node]);
//}
//bool connected(int u , int v){
//    return(find_parent(u) == find_parent(v));
//}
//void connect(int u , int v){
//    if(height[u] > height[v]){
//        parent[v] = u ;
//        height[v]+=height[u];
//        height[u] = 0;
//    }
//    else{
//        parent[u] = v ;
//        height[u]+=height[v];
//        height[v] = 0;
//    }
//}
struct DSU {
      int parent[N];
      int sz[N];
      DSU() {
            for(int i = 0; i < N; ++i)
                  parent[i] = i;

            fill(sz , sz+N , 1);
      }

      int findParent(int i) {
            if(parent[i] == i)
            return i;
            return parent[i] = findParent(parent[i]);
      }

      bool areTeam(int x , int y) {
            int leader1 = findParent(x);
            int leader2 = findParent(y);
            return leader1 == leader2;
      }

      void mergeTeams(int x , int y) {
            int leader1 = findParent(x);
            int leader2 = findParent(y);

            if(leader1 == leader2)
            return;

            if(sz[leader1] > sz[leader2]) {
                  parent[leader2] = leader1;
                  sz[leader1] += sz[leader2];
                  sz[leader2] = 0;
            }
            else {
                  parent[leader1] = leader2;
                  sz[leader2] += sz[leader1];
                  sz[leader1] = 0;
            }
      }
};

int main()
{   ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n , m , w , o ;
    cin >> n >> m ;
    ll sum1 = 0 , sum2 = 0;
    for(int i = 0 ; i < m ; ++i){
        cin >> p[i].f >> p[i].s;
        if(p[i].s){
            sum1+=p[i].f;
        }
    }
    int u1 = 1 , v1 = 2 , u2 = 2 , v2 = 3 ;
    vector < pair < int , int > > ans;
    vector < pair < int , pair < int , int > > > pv;
    for(int i = 0 ; i < m ; ++i){
        if(p[i].s){
           // adj[u1].push_back({v1 , p[i].f});
            pv.push_back({p[i].f , {u1 , v1}});
            ans.push_back({u1 , v1++});
           // cout << u1 << ' ' << v1++ << endl;

        }
        else{
            if(v2 > n){
                u2++;
                v2 = u2 + 1;
            }
            pv.push_back({p[i].f , {u2 , v2}});
            ans.push_back({u2 , v2++});
            //cout << u2 << ' ' << v2++ << endl;
        }
    }
    sort(pv.begin() , pv.end());
    DSU d;
    for(int i = 0 ; i < (int)pv.size() ; ++i){
        if(!d.areTeam(pv[i].s.f , pv[i].s.s)){
           // cout << pv[i].f << endl;
            sum2+=pv[i].f;
            d.mergeTeams(pv[i].s.f , pv[i].s.s);
        }
    }
    if(sum2!=sum1) {
        cout << -1;
    }
    else
        for(auto i : ans) cout << i.f << ' ' << i.s << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define f first
#define s second
const int N = 1e5 + 3;
pair < int , int > p[N];
vector < int > v;
int main()
{   ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n , m , w , o ;
    cin >> n >> m ;
    ll sum1 = 0 , sum2 = 0;
    for(int i = 0 ; i < m ; ++i){
        cin >> p[i].f >> p[i].s;
        if(p[i].s){
            sum1+=p[i].f;
        }
        v.push_back(p[i].f);
    }
    sort(v.begin() , v.end());
    for(int i = 0 ; i < n -1 ; ++i) sum2+=v[i];
    if(sum1 !=sum2){
        cout << -1 ;
        return 0;
    }
    int u1 = 1 , v1 = 2 , u2 = 1 , v2 = 3 ;
    for(int i = 0 ; i < m ; ++i){
        if(p[i].s){
            cout << u1++ << ' ' << v1++ << endl;

        }
        else{
            if(v2 > n){
                u2++;
                v2 = u2 + 2;
            }
            cout << u2 << ' ' << v2++ << endl;
        }
    }

    return 0;
}


#include<bits/stdc++.h>
#define In_the_name_of_allah_the_merciful ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Alhamdulillah return 0;
#define pb push_back
#define ins(x) insert(x)
#define fi first
#define se second
#define ub upper_bound
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) ((ll)((v).size()))
#define reset(v,val) memset(v,val,sizeof(v))
#define erep(i, x, n) for ( i = x; i<=(ll)(n); i++)
#define rep(i, x, n) for( i = x; i<(ll)(n); i++)
using namespace std;
typedef   long long  int   ll;
long long int mod=1e9+7;
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,0};
ll m=0,n,res=0,k,ans=0;
string s2,ch="",s,t,s1="";
vector< pair<string,ll> >vm;
vector< pair< pair<ll,ll>,ll > > vvv;
vector<pair<ll,ll> >vv,vv2;
vector< ll > v,v2,v1;
set<ll >st,st1;
set<pair<ll,ll> > stp;
pair<ll,ll> a[200005];

struct Edge {
    ll  weight,type,id;
    bool operator<(Edge const& other) {
        if(weight==other.weight)return type>other.type;
        return weight < other.weight;
    }
};
vector<Edge> edges;
vector<Edge> result;

int main(){
/******************************************/In_the_name_of_allah_the_merciful/****************************************************/

ll i=0,x=0,z=0,y=0,j=0,q,mx=0,idx=0,ok=0,l=0,r=0,negatif=0,positif=0,l1,r1,d,p,t;
set< pair<ll,ll>  > ::iterator itp;
set< ll > ::iterator it;
//freopen("a.txt","r",stdin);
cin>>n>>m;
res=0;
ll node=1;
for(i=1;i<=m;i++){
    cin>>x>>z;
    Edge e;
    e.weight=x;
    e.type=z;
    e.id=i;
    edges.pb(e);
}

sort(edges.begin(),edges.end());
node=1;
x=2;y=3;
st.clear();
for(i=0;i<m;i++){
    ll w=edges[i].weight;
    ll t=edges[i].type;
    ll id=edges[i].id;
    if(t==1){
        a[id]={1,++node};
    }
    else{
        if(x<2){
            y++;
            x=y-1;
            if(y>node||x<2)return cout<<"-1",0;
        }
         a[id]={x,y};
         x--;
    }
}
for(i=1;i<=m;i++)cout<<a[i].first<<" "<<a[i].second<<endl;

/*****************************************************/Alhamdulillah/**************************************************************/
 }


#include<bits/stdc++.h>
#define In_the_name_of_allah_the_merciful ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Alhamdulillah return 0;
#define pb push_back
#define ins(x) insert(x)
#define fi first
#define se second
#define ub upper_bound
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) ((ll)((v).size()))
#define reset(v,val) memset(v,val,sizeof(v))
#define erep(i, x, n) for ( i = x; i<=(ll)(n); i++)
#define rep(i, x, n) for( i = x; i<(ll)(n); i++)
using namespace std;
typedef   long long  int   ll;
long long int mod=1e9+7;
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,0};
ll m=0,n,res=0,k,ans=0;
string s2,ch="",s,t,s1="";
vector< pair<string,ll> >vm;
vector< pair< pair<ll,ll>,ll > > vvv;
vector<pair<ll,ll> >vv,vv2;
vector< ll > v,v2,v1;
set<ll >st,st1;
set<pair<ll,ll> > stp;
ll parent[100005],amount[100005],a[200005];

void make_set(ll v) {
    parent[v] = v;
    amount[v] =0;
}

ll find_set(ll v) {
    if (v == parent[v])
        return v;
    return parent[v] = find_set(parent[v]);
}

void union_sets(ll a, ll b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) {
        if (amount[a] < amount[b])
            swap(a, b);
        parent[b] = a;
        if (amount[a] == amount[b])
            amount[a]++;
    }
}

struct Edge {
    ll u, v, weight;
    bool operator<(Edge const& other) {
        return weight < other.weight;
    }
};
vector<Edge> edges,edges2;
vector<Edge> result;
ll mst(){
	ll cost = 0;
    for (ll i = 1; i <=n; i++)
        make_set(i);
     sort(edges.begin(), edges.end());

    for (Edge e : edges) {
        if (find_set(e.u) != find_set(e.v)) {
           cost += e.weight;
           result.push_back(e);
           union_sets(e.u, e.v);
        }
    }
    return cost;
}

int main(){
/******************************************/In_the_name_of_allah_the_merciful/****************************************************/

ll i=0,x=0,z=0,y=0,j=0,q,mx=0,idx=0,ok=0,l=0,r=0,negatif=0,positif=0,l1,r1,d,p,t;
set< pair<ll,ll>  > ::iterator itp;
set< ll > ::iterator it;
//freopen("a.txt","r",stdin);
cin>>n>>m;
res=0;
ll node=1;
for(i=1;i<=m;i++){
    cin>>x>>z;
    a[i]=z;
    if(z){
        res+=x;
        Edge e;
		e.u=node;
		e.v=(node+1);
		e.weight=x;
		edges.push_back(e);
		node++;
    }
    else{
        v.pb(x);
    }
}
node=1;
ll nxt=(node+2);
for(i=0;i<v.size();i++){
    if(nxt>=(n+1)){
        node++;
        nxt=(node+2);
        if(nxt>=(n+1))break;
    }
    if(node==n)return cout<<"-1",0;
    Edge e;
    e.u=node;
    e.v=(nxt);
    e.weight=v[i];
    edges.push_back(e);
    edges2.pb(e);
    nxt++;

}
ans=mst();
if(ans!=res)return cout<<"-1",0;
i=0;j=0;
for(k=1;k<=m;k++){
    if(a[k]){
        if(i>edges.size())return cout<<"-1",0;
        cout<<edges[i].u<<" "<<edges[i].v<<endl;
        i++;
    }
    else{
        if(j>edges2.size())return cout<<"-1",0;
        cout<<edges2[j].u<<" "<<edges2[j].v<<endl;
        j++;
    }
}

/*****************************************************/Alhamdulillah/**************************************************************/
 }


#include<bits/stdc++.h>
#define In_the_name_of_allah_the_merciful ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Alhamdulillah return 0;
#define pb push_back
#define ins(x) insert(x)
#define fi first
#define se second
#define ub upper_bound
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) ((ll)((v).size()))
#define reset(v,val) memset(v,val,sizeof(v))
#define erep(i, x, n) for ( i = x; i<=(ll)(n); i++)
#define rep(i, x, n) for( i = x; i<(ll)(n); i++)
using namespace std;
typedef   long long  int   ll;
long long int mod=1e9+7;
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,0};
ll m=0,n,res=0,k,ans=0;
string s2,ch="",s,t,s1="";
vector< pair<string,ll> >vm;
vector< pair< pair<ll,ll>,ll > > vvv;
vector<pair<ll,ll> >vv,vv2;
vector< ll > v,v2,v1;
set<ll >st,st1;
set<pair<ll,ll> > stp;
pair<ll,ll> a[200005];

struct Edge {
    ll  weight,type,id;
    bool operator<(Edge const& other) {
        if(weight==other.weight)return type>other.type;
        return weight < other.weight;
    }
};
vector<Edge> edges;
vector<Edge> result;

int main(){
/******************************************/In_the_name_of_allah_the_merciful/****************************************************/

ll i=0,x=0,z=0,y=0,j=0,q,mx=0,idx=0,ok=0,l=0,r=0,negatif=0,positif=0,l1,r1,d,p,t;
set< pair<ll,ll>  > ::iterator itp;
set< ll > ::iterator it;
//freopen("a.txt","r",stdin);
cin>>n>>m;
res=0;
ll node=1;
for(i=1;i<=m;i++){
    cin>>x>>z;
    Edge e;
    e.weight=x;
    e.type=z;
    e.id=i;
    edges.pb(e);
}

sort(edges.begin(),edges.end());
node=1;
x=2;y=3;
st.clear();
for(i=0;i<m;i++){
    ll w=edges[i].weight;
    ll t=edges[i].type;
    ll id=edges[i].id;
    if(t==1){
        a[id]={1,++node};
    }
    else{
        if(x<2){
            y++;
            x=y-1;
            if(y>node||x<2)return cout<<"-1",0;
        }
        if(y>node||x<2)return cout<<"-1",0;
         a[id]={x,y};
         x--;
    }
}
for(i=1;i<=m;i++)cout<<a[i].first<<" "<<a[i].second<<endl;

/*****************************************************/Alhamdulillah/**************************************************************/
 }


#include<bits/stdc++.h>
#define In_the_name_of_allah_the_merciful ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Alhamdulillah return 0;
#define pb push_back
#define ins(x) insert(x)
#define fi first
#define se second
#define ub upper_bound
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) ((ll)((v).size()))
#define reset(v,val) memset(v,val,sizeof(v))
#define erep(i, x, n) for ( i = x; i<=(ll)(n); i++)
#define rep(i, x, n) for( i = x; i<(ll)(n); i++)
using namespace std;
typedef   long long  int   ll;
long long int mod=1e9+7;
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,0};
ll m=0,n,res=0,k,ans=0;
string s2,ch="",s,t,s1="";
vector< pair<string,ll> >vm;
vector< pair< pair<ll,ll>,ll > > vvv;
vector<pair<ll,ll> >vv,vv2;
vector< ll > v,v2,v1;
set<ll >st,st1;
set<pair<ll,ll> > stp;
ll parent[100005],amount[100005],a[200005];

void make_set(ll v) {
    parent[v] = v;
    amount[v] =0;
}

ll find_set(ll v) {
    if (v == parent[v])
        return v;
    return parent[v] = find_set(parent[v]);
}

void union_sets(ll a, ll b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) {
        if (amount[a] < amount[b])
            swap(a, b);
        parent[b] = a;
        if (amount[a] == amount[b])
            amount[a]++;
    }
}

struct Edge {
    ll u, v, weight;
    bool operator<(Edge const& other) {
        return weight < other.weight;
    }
};
vector<Edge> edges,edges2,edges1;
vector<Edge> result;
ll mst(){
	ll cost = 0;
    for (ll i = 1; i <=n; i++)
        make_set(i);
     sort(edges.begin(), edges.end());

    for (Edge e : edges) {
        if (find_set(e.u) != find_set(e.v)) {
           cost += e.weight;
           result.push_back(e);
           union_sets(e.u, e.v);
        }
    }
    return cost;
}

int main(){
/******************************************/In_the_name_of_allah_the_merciful/****************************************************/

ll i=0,x=0,z=0,y=0,j=0,q,mx=0,idx=0,ok=0,l=0,r=0,negatif=0,positif=0,l1,r1,d,p,t;
set< pair<ll,ll>  > ::iterator itp;
set< ll > ::iterator it;
//freopen("a.txt","r",stdin);
cin>>n>>m;
res=0;
ll node=1;
for(i=1;i<m;i++)a[i]=0;
for(i=1;i<=m;i++){
    cin>>x>>z;
    a[i]=z;
    vv.pb({x,z});
}
sort(vv.begin(),vv.end());
res=0;
for(i=0;i<vv.size();i++){
    z=vv[i].second;
    if(z){
        res+=vv[i].first;
        Edge e;
		e.u=node;
		e.v=(node+1);
		e.weight=vv[i].first;
		edges1.pb(e);
		edges.push_back(e);
		node++;
    }
}
node=n;
ll nxt=(node-2);
for(i=vv.size()-1;i>=0;i--){
    if(vv[i].second)continue;
    if(nxt<1){
        node--;
        nxt=(node-2);
        if(nxt<1)break;
    }
    Edge e;
    e.u=node;
    e.v=(nxt);
    e.weight=vv[i].first;
    edges.push_back(e);
    edges2.pb(e);
    nxt--;

}
ans=mst();
if(ans!=res)return cout<<"-1",0;
i=0;j=0;
for(k=1;k<=m;k++){
    if(a[k]){
        if(i>edges1.size())return cout<<"-1",0;
        cout<<edges1[i].u<<" "<<edges1[i].v<<endl;
        i++;
    }
    else{
        if(j>edges2.size())return cout<<"-1",0;
        cout<<edges2[j].u<<" "<<edges2[j].v<<endl;
        j++;
    }
}

/*****************************************************/Alhamdulillah/**************************************************************/
 }


#include<bits/stdc++.h>
#define In_the_name_of_allah_the_merciful ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Alhamdulillah return 0;
#define pb push_back
#define ins(x) insert(x)
#define fi first
#define se second
#define ub upper_bound
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) ((ll)((v).size()))
#define reset(v,val) memset(v,val,sizeof(v))
#define erep(i, x, n) for ( i = x; i<=(ll)(n); i++)
#define rep(i, x, n) for( i = x; i<(ll)(n); i++)
using namespace std;
typedef   long long  int   ll;
long long int mod=1e9+7;
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,0};
ll m=0,n,res=0,k,ans=0;
string s2,ch="",s,t,s1="";
vector< pair<string,ll> >vm;
vector< pair< pair<ll,ll>,ll > > vvv;
vector<pair<ll,ll> >vv,vv2;
vector< ll > v,v2,v1;
set<ll >st,st1;
set<pair<ll,ll> > stp;
ll parent[100005],amount[100005],a[200005];

void make_set(ll v) {
    parent[v] = v;
    amount[v] =0;
}

ll find_set(ll v) {
    if (v == parent[v])
        return v;
    return parent[v] = find_set(parent[v]);
}

void union_sets(ll a, ll b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) {
        if (amount[a] < amount[b])
            swap(a, b);
        parent[b] = a;
        if (amount[a] == amount[b])
            amount[a]++;
    }
}

struct Edge {
    ll u, v, weight;
    bool operator<(Edge const& other) {
        return weight < other.weight;
    }
};
vector<Edge> edges,edges2,edges1;
vector<Edge> result;
ll mst(){
	ll cost = 0;
    for (ll i = 1; i <=n; i++)
        make_set(i);
     sort(edges.begin(), edges.end());

    for (Edge e : edges) {
        if (find_set(e.u) != find_set(e.v)) {
           cost += e.weight;
           result.push_back(e);
           union_sets(e.u, e.v);
        }
    }
    return cost;
}

int main(){
/******************************************/In_the_name_of_allah_the_merciful/****************************************************/

ll i=0,x=0,z=0,y=0,j=0,q,mx=0,idx=0,ok=0,l=0,r=0,negatif=0,positif=0,l1,r1,d,p,t;
set< pair<ll,ll>  > ::iterator itp;
set< ll > ::iterator it;
//freopen("a.txt","r",stdin);
cin>>n>>m;
res=0;
ll node=1;
for(i=1;i<m;i++)a[i]=0;
for(i=1;i<=m;i++){
    cin>>x>>z;
    a[i]=z;
    vv.pb({x,z});
}
sort(vv.begin(),vv.end());
res=0;
for(i=0;i<vv.size();i++){
    z=vv[i].second;
    if(z){
        res+=vv[i].first;
        Edge e;
		e.u=node;
		e.v=(node+1);
		e.weight=vv[i].first;
		edges1.pb(e);
		edges.push_back(e);
		node++;
    }
}
node=1;
ll nxt=(node+2);
for(i=0;i<vv.size();i++){
    if(vv[i].second)continue;
    if(nxt>=(n+1)){
        node++;
        nxt=(node+2);
        if(nxt>=(n+1))break;
    }
    Edge e;
    e.u=node;
    e.v=(nxt);
    e.weight=vv[i].first;
    edges.push_back(e);
    edges2.pb(e);
    nxt++;

}
ans=mst();
if(ans!=res)return cout<<"-1",0;
i=0;j=0;
for(k=1;k<=m;k++){
    if(a[k]){
        if(i>edges1.size())return cout<<"-1",0;
        cout<<edges1[i].u<<" "<<edges1[i].v<<endl;
        i++;
    }
    else{
        if(j>edges2.size())return cout<<"-1",0;
        cout<<edges2[j].u<<" "<<edges2[j].v<<endl;
        j++;
    }
}

/*****************************************************/Alhamdulillah/**************************************************************/
 }


#include<bits/stdc++.h>
#define In_the_name_of_allah_the_merciful ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Alhamdulillah return 0;
#define pb push_back
#define ins(x) insert(x)
#define fi first
#define se second
#define ub upper_bound
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) ((ll)((v).size()))
#define reset(v,val) memset(v,val,sizeof(v))
#define erep(i, x, n) for ( i = x; i<=(ll)(n); i++)
#define rep(i, x, n) for( i = x; i<(ll)(n); i++)
using namespace std;
typedef   long long  int   ll;
long long int mod=1e9+7;
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,0};
ll m=0,n,res=0,k,ans=0;
string s2,ch="",s,t,s1="";
vector< pair<string,ll> >vm;
vector< pair< pair<ll,ll>,ll > > vvv;
vector<pair<ll,ll> >vv,vv2;
vector< ll > v,v2,v1;
set<ll >st,st1;
set<pair<ll,ll> > stp;
ll parent[100005],amount[100005],a[200005];

void make_set(ll v) {
    parent[v] = v;
    amount[v] =0;
}

ll find_set(ll v) {
    if (v == parent[v])
        return v;
    return parent[v] = find_set(parent[v]);
}

void union_sets(ll a, ll b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) {
        if (amount[a] < amount[b])
            swap(a, b);
        parent[b] = a;
        if (amount[a] == amount[b])
            amount[a]++;
    }
}

struct Edge {
    ll u, v, weight;
    bool operator<(Edge const& other) {
        return weight < other.weight;
    }
};
vector<Edge> edges,edges2,edges1;
vector<Edge> result;
ll mst(){
	ll cost = 0;
    for (ll i = 1; i <=n; i++)
        make_set(i);
     sort(edges.begin(), edges.end());

    for (Edge e : edges) {
        if (find_set(e.u) != find_set(e.v)) {
           cost += e.weight;
           result.push_back(e);
           union_sets(e.u, e.v);
        }
    }
    return cost;
}

int main(){
/******************************************/In_the_name_of_allah_the_merciful/****************************************************/

ll i=0,x=0,z=0,y=0,j=0,q,mx=0,idx=0,ok=0,l=0,r=0,negatif=0,positif=0,l1,r1,d,p,t;
set< pair<ll,ll>  > ::iterator itp;
set< ll > ::iterator it;
//freopen("a.txt","r",stdin);
cin>>n>>m;
res=0;
ll node=1;
for(i=1;i<m;i++)a[i]=0;
for(i=1;i<=m;i++){
    cin>>x>>z;
    a[i]=z;
    if(z){
        res+=x;
        Edge e;
		e.u=node;
		e.v=(node+1);
		e.weight=x;
		edges1.pb(e);
		edges.push_back(e);
		node++;
    }
    else{
        v.pb(x);
    }
}
node=1;
ll nxt=(node+2);
for(i=0;i<v.size();i++){
    if(nxt>=(n+1)){
        node++;
        nxt=(node+2);
        if(nxt>=(n+1))break;
    }
    if(node==n)return cout<<"-1",0;
    Edge e;
    e.u=node;
    e.v=(nxt);
    e.weight=v[i];
    edges.push_back(e);
    edges2.pb(e);
    nxt++;

}
ans=mst();
if(ans!=res)return cout<<"-1",0;
i=0;j=0;
for(k=1;k<=m;k++){
    if(a[k]){
        if(i>edges1.size())return cout<<"-1",0;
        cout<<edges1[i].u<<" "<<edges1[i].v<<endl;
        i++;
    }
    else{
        if(j>edges2.size())return cout<<"-1",0;
        cout<<edges2[j].u<<" "<<edges2[j].v<<endl;
        j++;
    }
}

/*****************************************************/Alhamdulillah/**************************************************************/
 }


#include<bits/stdc++.h>
#define In_the_name_of_allah_the_merciful ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Alhamdulillah return 0;
#define pb push_back
#define ins(x) insert(x)
#define fi first
#define se second
#define ub upper_bound
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) ((ll)((v).size()))
#define reset(v,val) memset(v,val,sizeof(v))
#define erep(i, x, n) for ( i = x; i<=(ll)(n); i++)
#define rep(i, x, n) for( i = x; i<(ll)(n); i++)
using namespace std;
typedef   long long  int   ll;
long long int mod=1e9+7;
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,0};
ll m=0,n,res=0,k,ans=0;
string s2,ch="",s,t,s1="";
vector< pair<string,ll> >vm;
vector< pair< pair<ll,ll>,ll > > vvv;
vector<pair<ll,ll> >vv,vv2;
vector< ll > v,v2,v1;
set<ll >st,st1;
set<pair<ll,ll> > stp;
ll parent[100005],amount[100005],a[200005];

void make_set(ll v) {
    parent[v] = v;
    amount[v] =0;
}

ll find_set(ll v) {
    if (v == parent[v])
        return v;
    return parent[v] = find_set(parent[v]);
}

void union_sets(ll a, ll b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) {
        if (amount[a] < amount[b])
            swap(a, b);
        parent[b] = a;
        if (amount[a] == amount[b])
            amount[a]++;
    }
}

struct Edge {
    ll u, v, weight;
    bool operator<(Edge const& other) {
        return weight < other.weight;
    }
};
vector<Edge> edges,edges2,edges1;
vector<Edge> result;
ll mst(){
	ll cost = 0;
    for (ll i = 1; i <=n; i++)
        make_set(i);
     sort(edges.begin(), edges.end());

    for (Edge e : edges) {
        if (find_set(e.u) != find_set(e.v)) {
           cost += e.weight;
           result.push_back(e);
           union_sets(e.u, e.v);
        }
    }
    return cost;
}

int main(){
/******************************************/In_the_name_of_allah_the_merciful/****************************************************/

ll i=0,x=0,z=0,y=0,j=0,q,mx=0,idx=0,ok=0,l=0,r=0,negatif=0,positif=0,l1,r1,d,p,t;
set< pair<ll,ll>  > ::iterator itp;
set< ll > ::iterator it;
freopen("a.txt","r",stdin);
cin>>n>>m;
res=0;
ll node=1;
for(i=1;i<m;i++)a[i]=0;
for(i=1;i<=m;i++){
    cin>>x>>z;
    a[i]=z;
    vv.pb({x,z});
}
sort(vv.begin(),vv.end());
res=0;
for(i=0;i<vv.size();i++){
    z=vv[i].second;
    if(z){
        res+=vv[i].first;
        Edge e;
		e.u=node;
		e.v=(node+1);
		e.weight=vv[i].first;
		edges1.pb(e);
		edges.push_back(e);
		node++;
    }
}
node=1;
ll nxt=(node+2);
for(i=0;i<vv.size();i++){
    if(vv[i].second)continue;
    if(nxt>=(n+1)){
        node++;
        nxt=(node+2);
        if(nxt>=(n+1))break;
    }
    Edge e;
    e.u=node;
    e.v=(nxt);
    e.weight=vv[i].first;
    edges.push_back(e);
    edges2.pb(e);
    nxt++;

}
ans=mst();
if(ans!=res)return cout<<"-1",0;
i=0;j=0;
for(k=1;k<=m;k++){
    if(a[k]){
        if(i>edges1.size())return cout<<"-1",0;
        cout<<edges1[i].u<<" "<<edges1[i].v<<endl;
        i++;
    }
    else{
        if(j>edges2.size())return cout<<"-1",0;
        cout<<edges2[j].u<<" "<<edges2[j].v<<endl;
        j++;
    }
}

/*****************************************************/Alhamdulillah/**************************************************************/
 }


	#include <bits/stdc++.h>
	#include <ext/pb_ds/assoc_container.hpp>
	using namespace __gnu_pbds;
	using namespace std;
	#define mod 998244353
	#define fi first
	#define se second
	#define mp make_pair
	#define pb push_back
	#define ps push
	#define pll pair<long,long>
	typedef long long ll;
	#define PI 3.14159265
	#define vi vector<pll>

	typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

	const ll INF=(ll)1e16;

	long long binpow(long long a, long long b, long long m) {
		a %= m;
		long long res = 1;
		while (b > 0) {
			if (b & 1)
				res = res * a % m;
			a = a * a % m;
			b >>= 1;
		}
		return res;
	}
	ll modinverse(ll a,ll m)
	{
		return binpow(a,m-2,m);
	}

	int main()
	{
		ios_base::sync_with_stdio(false);
		cin.tie(NULL);

		ll n,m;
		cin>>n>>m;

		vector<tuple<ll,ll,ll> > edge;
		for(ll i=0;i<m;i++)
		{
			ll a,b;
			cin>>a>>b;
			b=b^1;
			edge.emplace_back(a,b,i);
		}
		ll total_edges=0;
		sort(edge.begin(),edge.end());
		vector<pll> ans(m);
		vector<ll> fin(n+1);
		for(ll i=1;i<=n;i++)
		{
			fin[i]=i+2;
		}
		ll pres=2;
		ll idx1=1;
		for(ll i=0;i<m;i++)
		{
			if(get<1>(edge[i])==0)
			{
				ans[get<2>(edge[i])]={pres,pres-1};
				pres++;
				total_edges++;
			}
			else
			{
				if((i+1)>(total_edges*(total_edges+1))/2)
				{
					cout<<-1;
					exit(0);
				}
				if(fin[idx1]==pres)
				{
					idx1++;
					if(idx1>=(pres-2))
					{
						idx1=1;
					}

				}
				ans[get<2>(edge[i])]={idx1,fin[idx1]};
				fin[idx1]++;
			}
		}
		for(auto i:ans)
			cout<<i.fi<<" "<<i.se<<endl;

	}


#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define ff first
#define ss second
#define endl "\n";
using namespace std;

const double pi=acos(-1);
const int N=2e5+7,mod=1000000007,M=2e9;

int n,m,u,v;
struct edge{
    int w,id;
    bool st;

    operator<(const edge& a)const{
        if(a.w==w)
            return (!a.st);
        return a.w<w;
    }
}a[N],t;

priority_queue<edge> q;
pii ans[N];
stack<pii> tmp;

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>u>>v;
        a[i]={u,i,v};
        q.push(a[i]);
    }
    v=2;

    while(!q.empty()){
        t=q.top();
        q.pop();
        if(t.st){
            ans[t.id]={v-1,v};
            u=v-2;
            while(u&&tmp.size()<m)
                tmp.push({u--,v});
            v++;
        }
        else{
            if(tmp.empty())
                return puts("-1");
            else{
                ans[t.id]=tmp.top();
                tmp.pop();
            }
        }
    }
    for(int i=0;i<m;i++){
        cout<<ans[i].ff<<' '<<ans[i].ss<<endl;
    }
}


#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define ff first
#define ss second
#define endl "\n";
using namespace std;

const double pi=acos(-1);
const int N=2e5+7,mod=1000000007,M=2e9;

int n,m,u,v;
struct edge{
    int w,id;
    bool st;

    operator<(const edge& a)const{
        if(a.w==w)
            return a.st;
        return a.w<w;
    }
}a[N],t;

priority_queue<edge> q;
pii ans[N];
stack<pii> tmp;

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>u>>v;
        a[i]={u,i,v};
        q.push(a[i]);
    }
    v=2;

    while(!q.empty()){
        t=q.top();
        q.pop();
        if(t.st){
            ans[t.id]={v-1,v};
            u=v-2;
            while(u&&tmp.size()<m)
                tmp.push({u--,v});
            v++;
        }
        else{
            if(tmp.empty())
                return puts("-1");
            else{
                ans[t.id]=tmp.top();
                tmp.pop();
            }
        }
    }
    for(int i=0;i<m;i++){
        cout<<ans[i].ff<<' '<<ans[i].ss<<endl;
    }
}