#include<bits/stdc++.h>
//#include<unordered_map>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<bitset>
#include<sstream>
using namespace std;
#define int long long
#define ull unsigned long long
#define ll long long
#define str string
#define db double
//#define ac() cout<<"Yes"<<endl
//#define wa()  cout<<"No"<<endl
void ac(int x){if(x)puts("YES");else puts("NO");}
#define ddd cout<<"---debug---"<<endl;
#define ff(a,b) for(signed long long (a)=1;(a)<=(b);(a)++)
#define fff(a,b) for(signed long long (a)=0;(a)<(b);(a)++)
#define re readdd()
#define pr(a) printtt(a)
#define pee puts("");
#define eee putchar(' ');
#define pss(a) push_back(a)
#define ps(a) push(a)
#define SZ(x) (int)x.size()
#define epp empty()
#define all(x) x.begin(),x.end()
#define asd signed QP;scanf("%d",&QP);while(QP--)
#define alll(x,y) x+1,x+1+y
typedef pair<int,int> PI;
typedef vector<int> VE;
#define fir first
#define sec second
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}//
int ppow(int a,int b,int mod){a%=mod;//
int ans=1%mod;while(b){if(b&1)ans=(long long)ans*a%mod;
a=(long long)a*a%mod;b>>=1;}return ans;}
int readdd(){int x=0,f=1;char c=getchar();//
while(!isdigit(c)&&c!='-')c=getchar();
if(c=='-')f=-1,c=getchar();
while(isdigit(c))x=x*10+c-'0',c=getchar();
return f*x;}
void printtt(int x){if(x<0)putchar('-'),x=-x;
if(x>=10)printtt(x/10);putchar(x%10+'0');}
bool addd(int a,int b){return a>b;}
int lowbit(int x){return x&-x;}
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
//
const int BN=2e6+5;
struct BIT{
    int c[BN];
    void init(){memset(c,0,sizeof c);}
    int lowbit(int i){return i&-i;}
    void add(int i,int t){while(i<BN)c[i]+=t,i+=lowbit(i);}
    int ask(int i){int ans=0;while(i)ans+=c[i],i-=lowbit(i);return ans;}
};
bool Isprime(int x){
    for(int i=2;i*i<=x;i++)if(x%i==0)return 0;
    return 1;
}
//
void init(){;}
const double Pi=acos(-1);
const double eps=1e-7;
const int mod=1e9+7;
//const int mod=998244353;
const int modd=mod-1;
//
const int maxm=2e5+5;
int a[maxm];
int b[maxm];
void solve(){
    int x=re,y=re,z=re;
    if(x==y){
        ac(1);
        cout<<x<<' '<<x<<' '<<z<<endl;
        return ;
    }
    if(x==z){
        ac(1);
        cout<<y<<' '<<x<<' '<<x<<endl;
        return ;
    }
    if(y==z){
        ac(1);
        cout<<x<<' '<<y<<' '<<y<<endl;
    }
    ac(0);
}
signed main(){
    init();
//    while(1)
    asd
        solve();
    return 0;
}
/*

*/


//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
using namespace std;
//#define ll long long
#define int long long
const int maxm=35;
int mark[maxm];
int cnt[maxm];
int a[maxm];
signed main(){
    ios::sync_with_stdio(0);
    int T;cin>>T;
    while(T--){
        int x,y,z;cin>>x>>y>>z;
        int ok=0;
        vector<int>ans;
        for(auto a:{x,y,z})
            for(auto b:{x,y,z})
                for(auto c:{x,y,z}){
                    if(max(a,b)==x&&max(a,c)==y&&max(b,c)==z){
                        ok=1;
                        ans.clear();
                        ans.push_back(a);
                        ans.push_back(b);
                        ans.push_back(c);
                    }
                }
        if(ok){
            puts("YES");
            for(auto i:ans){
                cout<<i<<' ';
            }
            cout<<endl;
        }else{
            puts("NO");
        }
    }
    return 0;
}


//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
using namespace std;
//#define ll long long
#define int long long
const int maxm=35;
int mark[maxm];
int cnt[maxm];
int a[maxm];
signed main(){
    ios::sync_with_stdio(0);
    int T;cin>>T;
    while(T--){
        int x,y,z;cin>>x>>y>>z;
        int ok=0;
        for(auto a:{x,y,z})
            for(auto b:{x,y,z})
                for(auto c:{x,y,z}){
                    if(max(a,b)==x&&max(a,c)==y&&max(b,c)==z){
                        ok=1;
                    }
                }
        if(ok){
            puts("YES");
        }else{
            puts("NO");
        }
    }
    return 0;
}


#include<bits/stdc++.h>
//#include<iostream>
//#include<cstdio>
using namespace std;
//#define ll long long
#define int long long
const int maxm=35;
int mark[maxm];
int cnt[maxm];
int a[maxm];
signed main(){
//    ios::sync_with_stdio(0);
    int T;cin>>T;
    while(T--){
        int x,y,z;cin>>x>>y>>z;
        int ok=0;
        vector<int>ans;
        for(auto a:{x,y,z})
            for(auto b:{x,y,z})
                for(auto c:{x,y,z}){
                    if(max(a,b)==x&&max(a,c)==y&&max(b,c)==z){
                        ok=1;
                        ans.clear();
                        ans.push_back(a);
                        ans.push_back(b);
                        ans.push_back(c);
                    }
                }
        if(ok){
            puts("YES");
            for(auto i:ans){
                cout<<i<<' ';
            }
            cout<<endl;
        }else{
            puts("NO");
        }
    }
    return 0;
}


#include<bits/stdc++.h>
//#include<iostream>
//#include<cstdio>
using namespace std;
//#define ll long long
#define int long long
const int maxm=35;
int mark[maxm];
int cnt[maxm];
int a[maxm];
signed main(){
    ios::sync_with_stdio(0);
    int T;cin>>T;
    while(T--){
        int x,y,z;cin>>x>>y>>z;
        int ok=0;
        vector<int>ans;
        for(auto a:{x,y,z})
            for(auto b:{x,y,z})
                for(auto c:{x,y,z}){
                    if(max(a,b)==x&&max(a,c)==y&&max(b,c)==z){
                        ok=1;
                        ans.clear();
                        ans.push_back(a);
                        ans.push_back(b);
                        ans.push_back(c);
                    }
                }
        if(ok){
            puts("YES");
            for(auto i:ans){
                cout<<i<<' ';
            }
            cout<<endl;
        }else{
            puts("NO");
        }
    }
    return 0;
}


#include<bits/stdc++.h>
//#include<unordered_map>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<bitset>
#include<sstream>
using namespace std;
#define int long long
#define ull unsigned long long
#define ll long long
#define str string
#define db double
//#define ac() cout<<"Yes"<<endl
//#define wa()  cout<<"No"<<endl
void ac(int x){if(x)puts("YES");else puts("NO");}
#define ddd cout<<"---debug---"<<endl;
#define ff(a,b) for(signed long long (a)=1;(a)<=(b);(a)++)
#define fff(a,b) for(signed long long (a)=0;(a)<(b);(a)++)
#define re readdd()
#define pr(a) printtt(a)
#define pee puts("");
#define eee putchar(' ');
#define pss(a) push_back(a)
#define ps(a) push(a)
#define SZ(x) (int)x.size()
#define epp empty()
#define all(x) x.begin(),x.end()
#define asd signed QP;scanf("%d",&QP);while(QP--)
#define alll(x,y) x+1,x+1+y
typedef pair<int,int> PI;
typedef vector<int> VE;
#define fir first
#define sec second
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}//
int ppow(int a,int b,int mod){a%=mod;//
int ans=1%mod;while(b){if(b&1)ans=(long long)ans*a%mod;
a=(long long)a*a%mod;b>>=1;}return ans;}
int readdd(){int x=0,f=1;char c=getchar();//
while(!isdigit(c)&&c!='-')c=getchar();
if(c=='-')f=-1,c=getchar();
while(isdigit(c))x=x*10+c-'0',c=getchar();
return f*x;}
void printtt(int x){if(x<0)putchar('-'),x=-x;
if(x>=10)printtt(x/10);putchar(x%10+'0');}
bool addd(int a,int b){return a>b;}
int lowbit(int x){return x&-x;}
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
//
const int BN=2e6+5;
struct BIT{
    int c[BN];
    void init(){memset(c,0,sizeof c);}
    int lowbit(int i){return i&-i;}
    void add(int i,int t){while(i<BN)c[i]+=t,i+=lowbit(i);}
    int ask(int i){int ans=0;while(i)ans+=c[i],i-=lowbit(i);return ans;}
};
bool Isprime(int x){
    for(int i=2;i*i<=x;i++)if(x%i==0)return 0;
    return 1;
}
//
void init(){;}
const double Pi=acos(-1);
const double eps=1e-7;
const int mod=1e9+7;
//const int mod=998244353;
const int modd=mod-1;
//
const int maxm=2e5+5;
int a[maxm];
int b[maxm];
void solve(){
    int x=re,y=re,z=re;
    int a,b,c;
    a=x;
    if(x!=y){
        c=y;
        if(c<a){
            ac(0);return ;
        }
    }else{
        b=c=z;
    }
    if(z!=y){
        b=z;
        if(b<c){
            ac(0);return ;
        }
    }else{
        b=c=z;
    }
    ac(1);
    cout<<a<<' '<<b<<' '<<c<<endl;
}
signed main(){
    init();
//    while(1)
    asd
        solve();
    return 0;
}
/*

*/


#include<bits/stdc++.h>
using namespace std;
int T;
long long x,y,z;
long long a,b,c;
int main()
{
    while(scanf("%d",&T)!=EOF)
    {
        while(T--)
        {
            scanf("%lld%lld%lld",&x,&y,&z);
            if(x==y&&z<=x)
            {
                if(z<=x)
                printf("YES\n%lld %lld %lld\n",x,z,z-1);
                else printf("NO\n");
            }
           else
           {
               if(x>y)y=x;
               if(z==y)printf("YES\n%lld %lld %lld\n",x,x,y);
               else printf("NO\n");
           }

        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int T;
long long x,y,z;
long long a,b,c;
int main()
{
    while(scanf("%d",&T)!=EOF)
    {
        while(T--)
        {
            scanf("%lld%lld%lld",&x,&y,&z);
            if(x==y&&z<=x)
            {
                if(z<=x)printf("YES\n%lld %lld %lld\n",x,z,z-1);
                else printf("NO\n");
            }
           else
           {
               int t=y;
               if(x>y)t=x;
               if(z==t)printf("YES\n%lld %lld %lld\n",x,(x>y)?y-1:x-1,y);
               else printf("NO\n");
           }

        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int T;
long long x,y,z;
long long a,b,c;
int main()
{
    while(scanf("%d",&T)!=EOF)
    {
        while(T--)
        {
            scanf("%lld%lld%lld",&x,&y,&z);
            if(x==y)
            {
                if(z<=x)printf("YES\n%lld %lld %lld\n",x,z,y);
                else printf("NO\n");
            }
           else
           {
               int t=y;
               if(x>y)t=x;
               if(z==t)printf("YES\n%lld %lld %lld\n",x,y,(x>y)?y:x);
               else printf("NO\n");
           }

        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int T;
long long x,y,z;
long long a,b,c;
int main()
{
    while(scanf("%d",&T)!=EOF)
    {
        while(T--)
        {
            scanf("%lld%lld%lld",&x,&y,&z);
            if(x==y&&z<=x)
            {
                if(z<=x)printf("YES\n%lld %lld %lld\n",x,z,z-1);
                else printf("NO\n");
            }
           else
           {
               int t=y;
               if(x>y)t=x;
               if(z==t)printf("YES\n%lld %lld %lld\n",x,y,(x>y)?y-1:x-1);
               else printf("NO\n");
           }

        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int T;
long long x,y,z;
long long a,b,c;
int main()
{
    while(scanf("%d",&T)!=EOF)
    {
        while(T--)
        {
            scanf("%lld%lld%lld",&x,&y,&z);
            if(x==y&&z<=x)
            {
                if(z<=x)printf("YES\n%lld %lld %lld\n",x,z,z-1);
                else printf("NO\n");
            }
           else
           {
               int t=y;
               if(x>y)t=x;
               if(z==t)printf("YES\n%lld %lld %lld\n",x,x,y);
               else printf("NO\n");
           }

        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int T;
long long x,y,z;
long long a,b,c;
int main()
{
    while(scanf("%d",&T)!=EOF)
    {
        while(T--)
        {
            scanf("%lld%lld%lld",&x,&y,&z);
            if(x==y&&z<=x)
            {
                if(z<=x)printf("YES\n%lld %lld %lld\n",x,z,y);
                else printf("NO\n");
            }
           else
           {
               int t=y;
               if(x>y)t=x;
               if(z==t)printf("YES\n%lld %lld %lld\n",x,y,(x>y)?y:x);
               else printf("NO\n");
           }

        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int T;
long long x,y,z;
long long a,b,c;
int main()
{
    while(scanf("%d",&T)!=EOF)
    {
        while(T--)
        {
            scanf("%lld%lld%lld",&x,&y,&z);
            if(x==y&&z<=x)
            {
                if(z<=x)printf("YES\n%lld %lld %lld\n",x,z,z-1);
                else printf("NO\n");
            }
           else
           {
               int t=y;
               if(x>y)t=x;
               if(z==t)printf("YES\n%lld %lld %lld\n",x,y,(x>y)?y:x);
               else printf("NO\n");
           }

        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int T;
long long x,y,z;
long long a,b,c;
int main()
{
    while(scanf("%d",&T)!=EOF)
    {
        while(T--)
        {
            scanf("%lld%lld%lld",&x,&y,&z);
            if(x==y)
            {
                if(z<=x)printf("YES\n%lld %lld %lld\n",x,z,z);
                else printf("NO\n");
            }
           else
           {
               int t=y;
               if(x>y)t=x;
               if(z==t)printf("YES\n%lld %lld %lld\n",x,y,(x>y)?y:x);
               else printf("NO\n");
           }

        }
    }
}


#include <bits/stdc++.h>

using namespace std;
int main()
{
    int t=0;
    cin>>t;
    for(int i=1;i<=t;i++)
    {
     int x=0,y=0,z=0,a=0,b=0,c=0;
     cin>>x>>y>>z;
     if(x==y)
     {
        b=x;
        if(z>b)
        {
            cout<<"NO\n";
        }
        else
        {
            cout<<"YES\n";
            c=z;
            a=z-1;
            cout<<a<<b<<c;
        }
     }
     else if(y==z)
     {
         c=y;
        if(x>c)
        {
            cout<<"NO\n";
        }
        else
        {
            cout<<"YES\n";
            b=x;
            a=x-1;
            cout<<a<<b<<c;
        }

     }
     else if(z==x)
     {
         a=z;
        if(y>a)
        {
            cout<<"NO\n";
        }
        else
        {
            cout<<"YES\n";
            c=y;
            b=y-1;
            cout<<a<<b<<c;
        }
     }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define optimizar_io ios_base::sync_with_stdio(0); cin.tie(0);
#define fore(i, a, b) for(int i = a; i < b; i++)
#define fori(i, a, b) for(int i = a; i <= b; i++)
#define pb push_back
#define popb pop_back
#define mk make_pair
#define fi first
#define se second
#define endl '\n'
#define MAX 100005
typedef long long int lli;
typedef long double ld;
const double PI = acos(-1.0);
ld eps = 1e-9;

void solve() {
	lli x, y, z;
	cin >> x >> y >> z;
	vector<lli> A = {x, y}, B = {x, z}, C = {y, z};
	lli a, b, c;
	bool flag = false;
	fore(i, 0, 2) {
		fore(j, 0, 2) {
			fore (k, 0, 2) {
				a = A[i], b = B[j], c = C[k];
				lli auxX = max(a, b);
				lli auxY = max(a, c);
				lli auxZ = max(b, c);
				if(auxX == x && auxY == y && auxZ == z) {
					flag = true;
				}
			}
		}
	}
	if(flag) cout << "YES" << endl << a << " " << b << " " << c << endl;
	cout << "NO" << endl;

}
int main() {
	optimizar_io
	int t; cin >> t;
	while(t--) {
		solve();
	}
}

/*
	x = 3
		a = 3 or b = 3
	y = 2
		a = 2 or c = 2
	z = 3
		b = 3 or c = 3

	a = x, b = z, c = y
	a = x, b = z, c =

*/


#include<bits/stdc++.h>
using namespace std;
#define optimizar_io ios_base::sync_with_stdio(0); cin.tie(0);
#define fore(i, a, b) for(int i = a; i < b; i++)
#define fori(i, a, b) for(int i = a; i <= b; i++)
#define pb push_back
#define popb pop_back
#define mk make_pair
#define fi first
#define se second
#define endl '\n'
#define MAX 100005
typedef long long int lli;
typedef long double ld;
const double PI = acos(-1.0);
ld eps = 1e-9;

void solve() {
	lli x, y, z;
	cin >> x >> y >> z;
	vector<lli> A = {x, y}, B = {x, z}, C = {y, z};
	lli a, b, c;
	bool flag = false;
	fore(i, 0, 2) {
		fore(j, 0, 2) {
			fore (k, 0, 2) {
				lli auxX = max(A[i], B[j]);
				lli auxY = max(A[i], C[k]);
				lli auxZ = max(B[j], C[k]);
				if(auxX == x && auxY == y && auxZ == z) {
					a = A[i], b = B[j], c = C[k];
					flag = true;
					break;
				}
			}
		}
	}
	if(flag) cout << "YES" << endl << a << " " << b << " " << c << endl;
	else cout << "NO" << endl;
}
int main() {
	optimizar_io
	int t; cin >> t;
	while(t--) {
		solve();
	}
}

/*
	x = 3
		a = 3 or b = 3
	y = 2
		a = 2 or c = 2
	z = 3
		b = 3 or c = 3

	a = x, b = z, c = y
	a = x, b = z, c =

*/


#include<bits/stdc++.h>
using namespace std;
#define optimizar_io ios_base::sync_with_stdio(0); cin.tie(0);
#define fore(i, a, b) for(int i = a; i < b; i++)
#define fori(i, a, b) for(int i = a; i <= b; i++)
#define pb push_back
#define popb pop_back
#define mk make_pair
#define fi first
#define se second
#define endl '\n'
#define MAX 100005
typedef long long int lli;
typedef long double ld;
const double PI = acos(-1.0);
ld eps = 1e-9;

void solve() {
	lli x, y, z;
	cin >> x >> y >> z;
	vector<lli> A = {x, y}, B = {x, z}, C = {y, z};
	lli a, b, c;
	bool flag = false;
	fore(i, 0, 2) {
		fore(j, 0, 2) {
			fore (k, 0, 2) {
				a = A[i], b = B[j], c = C[k];
				lli auxX = max(a, b);
				lli auxY = max(a, c);
				lli auxZ = max(b, c);
				if(auxX == x && auxY == y && auxZ == z) {
					flag = true;
					break;
				}
			}
		}
	}
	if(flag) cout << "YES" << endl << a << " " << b << " " << c << endl;
	else cout << "NO" << endl;

}
int main() {
	optimizar_io
	int t; cin >> t;
	while(t--) {
		solve();
	}
}

/*
	x = 3
		a = 3 or b = 3
	y = 2
		a = 2 or c = 2
	z = 3
		b = 3 or c = 3

	a = x, b = z, c = y
	a = x, b = z, c =

*/


#include<bits/stdc++.h>
using namespace std;
#define optimizar_io ios_base::sync_with_stdio(0); cin.tie(0);
#define fore(i, a, b) for(int i = a; i < b; i++)
#define fori(i, a, b) for(int i = a; i <= b; i++)
#define pb push_back
#define popb pop_back
#define mk make_pair
#define fi first
#define se second
#define endl '\n'
#define MAX 100005
typedef long long int lli;
typedef long double ld;
const double PI = acos(-1.0);
ld eps = 1e-9;

void solve() {
	lli x, y, z;
	cin >> x >> y >> z;
	vector<lli> A = {x, y}, B = {x, z}, C = {y, z};
	lli a, b, c;
	bool flag = false;
	fore(i, 0, 2) {
		fore(j, 0, 2) {
			fore (k, 0, 2) {
				a = A[i], b = B[j], c = C[k];
				lli auxX = max(a, b);
				lli auxY = max(a, c);
				lli auxZ = max(b, c);
				if(auxX == x && auxY == y && auxZ == z) {
					flag = true;
				}
			}
		}
	}
	if(flag) cout << "YES" << endl << a << " " << b << " " << c << endl;
	else cout << "NO" << endl;

}
int main() {
	optimizar_io
	int t; cin >> t;
	while(t--) {
		solve();
	}
}

/*
	x = 3
		a = 3 or b = 3
	y = 2
		a = 2 or c = 2
	z = 3
		b = 3 or c = 3

	a = x, b = z, c = y
	a = x, b = z, c =

*/


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

int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
	ll x,y,z;
		cin>>x>>y>>z;

		if(x==y && y==z && z==x)
		cout<<"YES\n"<<x<<" "<<y<<" "<<z<<endl;

		else if( y==z && x<y)
		{
			cout<<"YES\n";
			cout<<x<<" "<<x<<" "<<y<<endl;
		}
		else if(x==z && y<x)
		{
			cout<<"YES\n";
			cout<<x<<" "<<y<<" "<<"1\n";
		}
		else if(x>y & y>z)
		{
			cout<<"YES\n";
			cout<<x<<" "<<y<<" "<<z<<endl;
		}
				else if(x<y & y<z)
		{
			cout<<"YES\n";
			cout<<x<<" "<<y<<" "<<z<<endl;
		}
		else
		cout<<"NO\n";
	}

	return 0;
}


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

int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
	ll x,y,z;
		cin>>x>>y>>z;

		if(x==y && y==z && z==x)
		cout<<"YES\n"<<x<<" "<<y<<" "<<z<<endl;

		else if( y==z && x<y)
		{
			cout<<"YES\n";
			cout<<"1"<<" "<<x<<" "<<y<<endl;
		}
		else if(x==z && y<x)
		{
			cout<<"YES\n";
			cout<<x<<" "<<y<<" "<<"1\n";
		}
		else if(x>y & y>z)
		{
			cout<<"YES\n";
			cout<<x<<" "<<y<<" "<<"1"<<endl;
		}
				else if(x<y & y<z)
		{
			cout<<"YES\n";
			cout<<"1"<<" "<<y<<" "<<z<<endl;
		}
		else
		cout<<"NO\n";
	}

	return 0;
}


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

int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
	ll x,y,z;
		cin>>x>>y>>z;

		if(x==y && y==z && z==x)
		cout<<"YES\n"<<x<<" "<<y<<" "<<z<<endl;

		else if( y==z && x<y)
		{
			cout<<"YES\n";
			cout<<"1"<<" "<<x<<" "<<y<<endl;
		}
		else if(x==z && y<x)
		{
			cout<<"YES\n";
			cout<<x<<" "<<y<<" "<<"1\n";
		}

				else if(y==x && z<x)
		{
			cout<<"YES\n";
			cout<<x<<" "<<z<<" "<<"1\n";
		}
		else if(x>y & y>z)
		{
			cout<<"YES\n";
			cout<<x<<" "<<y<<" "<<z<<endl;
		}
				else if(x<y & y<z)
		{
			cout<<"YES\n";
			cout<<x<<" "<<y<<" "<<z<<endl;
		}
		else
		cout<<"NO\n";
	}

	return 0;
}


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

int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
	ll x,y,z;
		cin>>x>>y>>z;

		if(x==y && y==z && z==x)
		cout<<"YES\n"<<x<<" "<<y<<" "<<z<<endl;

		else if( y==z && x<y)
		{
			cout<<"YES\n";
			cout<<"1"<<" "<<x<<" "<<y<<endl;
		}
		else if(x==z && y<x)
		{
			cout<<"YES\n";
			cout<<x<<" "<<y<<" "<<"1\n";
		}

				else if(y==x && z<x)
		{
			cout<<"YES\n";
			cout<<x<<" "<<z<<" "<<"1\n";
		}
		else
		cout<<"NO\n";
	}

	return 0;
}


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

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int x,y,z;
		cin>>x>>y>>z;

		if(x==y && y==z && z==x)
		cout<<"YES\n"<<x<<" "<<y<<" "<<z<<endl;

		else if( y==z && x<y)
		{
			cout<<"YES\n";
			cout<<x<<" "<<x<<" "<<y<<endl;
		}
		else if(x==z && y<x)
		{
			cout<<"YES\n";
			cout<<x<<" "<<y<<" "<<"1\n";
		}
		else if(x>y & y>z)
		{
			cout<<"YES\n";
			cout<<x<<" "<<y<<" "<<z<<endl;
		}
				else if(x<y & y<z)
		{
			cout<<"YES\n";
			cout<<x<<" "<<y<<" "<<z<<endl;
		}
		else
		cout<<"NO\n";
	}

	return 0;
}


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

int main() {
    int T;
    cin >> T;

    while (T--)
    {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);

        int x, y, z;
        int a, b, c;
        string result = "";
        cin >> x >> y >> z;

        if(x == y && y == z) {
            result = "YES";
            a = b = c = x;
        }
        //3 2 3
        else if(x == z && x > y && y < z) {
            result = "YES";
            b = x;
            a = y;
            c = 1;
        }
        // 3 3 2
        else if(x == y && y > z) {
            result = "YES";
            a = x;
            b = 1;
            c = z;
        }
        //2 3 3
        else if(y == z && x < y) {
            result = "YES";
            a = x;
            b = 1;
            c = y;
        }
        // 1 2 1
        else if(x == z && x < y) {
            result = "NO";
        }
        //1 1 2
        else if(x == y && x < z) {
            result = "NO";
        }
        else if(x > y && y == z) {
            result = "NO";
        }
        else {
            result = "NO";
        }

        if(result == "NO") {
            cout << "NO\n";
        }else {
            cout << "YES\n";
            cout << b << " " << a << " " << c << "\n";
        }

    }

}


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

int main() {
    int T;
    cin >> T;

    while (T--)
    {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);

        int x, y, z;
        int a, b, c;
        string result = "";
        cin >> x >> y >> z;

        if(x == y && y == z) {
            result = "YES";
            a = b = c = x;
        }
        //3 2 3
        else if(x == z && x > y && y < z) {
            result = "YES";
            b = x;
            a = y;
            c = INT32_MIN;
        }
        // 3 3 2
        else if(x == y && y > z) {
            result = "NO";
        }
        //2 3 3
        else if(y == z && x < y) {
            result = "YES";
            a = x;
            b = INT32_MIN;
            c = y;
        }
        // 1 2 1
        else if(x == z && x < y) {
            result = "NO";
        }
        //1 1 2
        else if(x == y && x < z) {
            result = "NO";
        }
        else if(x > y && y == z) {
            result = "NO";
        }
        else {
            result = "NO";
        }

        if(result == "NO") {
            cout << "NO\n";
        }else {
            cout << "YES\n";
            cout << a << " " << b << " " << c << "\n";
        }

    }

}


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

int main() {
    int T;
    cin >> T;

    while (T--)
    {
        // ios_base::sync_with_stdio(false);
        // cin.tie(NULL);

        int x, y, z;
        int a, b, c;
        cin >> x >> y >> z;

       if(x== y && y==z)
        {
           cout << "YES\n";
            cout << x << " " << x << " " <<x << "\n";
        }
        else if(x!=y && y!=z && z!=x)
        cout << "NO\n";
        else if(x==y && y<z)
        {
            cout << "NO\n";
        }
        else if(x==y && y>z)
        {
            cout << "YES\n";
        cout << x << " " << z << " " << "1\n";
        }
        else if(y==z && x<y)
        {
            cout << "YES\n";
        cout << x << " " << y << " " << "1\n";
        }

        else if(y==z && y<x)
        cout << "NO\n";
        else if (x==z && x>y)
        {
            cout << "YES\n";
        cout << "1" << " " << x << " " << y << "\n";
        }
        else if(x==z && y>x)
        {
            cout << "NO\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

	int t;
	cin >> t;
	while (t--) {
		vector<int> a(3);
		for (auto &it : a) cin >> it;
		sort(a.begin(), a.end());
		if (a[1] != a[2]) {
			cout << "NO" << endl;
		} else {
			cout << "YES" << endl << a[0] << " " << a[0] << " " << a[2] << endl;
		}
	}

	return 0;
}


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

int main() {
    int T;
    cin >> T;

    while (T--)
    {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);

        int x, y, z;
		cin >> x >> y >> z;
		int arr[3];
		arr[0] = x; arr[1] = y; arr[2] = z;

		sort(arr, arr + 3);

		if(arr[1] != arr[2]) {
			cout << "NO\n";
		} else
		{
			cout << "YES\n";
			cout << arr[0] << " " << arr[0] << " " << arr[1] << "\n";
		}

    }
    return 0;
}


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

int main() {
    int T;
    cin >> T;

    while (T--)
    {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);

        int x, y, z;
        int a, b, c;
        cin >> x >> y >> z;

       if(x== y && y==z)
        {
           cout << "YES\n";
            cout << x << " " << x << " " <<x << "\n";
        }
        else if(x!=y && y!=z && z!=x)
        cout << "NO\n";
        else if(x==y && y<z)
        {
            cout << "NO\n";
        }
        else if(x==y && y>z)
        {
            cout << "YES\n";
        cout << x << " " << z << " " << "1\n";
        }
        else if(y==z && x<y)
        {
            cout << "YES\n";
        cout << x << " " << y << " " << "1\n";
        }

        else if(y==z && y<x)
        cout << "NO\n";
        else if (x==z && x>y)
        {
            cout << "YES\n";
        cout << "1" << " " << x << " " << y << "\n";
        }
        else if(x==z && y>x)
        {
            cout << "NO\n";
        }
    }
    return 0;
}


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

int main() {
    int T;
    cin >> T;

    while (T--)
    {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);

        int x, y, z;
        int a, b, c;
        string result = "";
        cin >> x >> y >> z;

        if(x == y && y == z) {
            result = "YES";
            a = b = c = x;
        }
        //3 2 3
        else if(x == z && x > y && y < z) {
            result = "YES";
            b = x;
            a = y;
            c = 1;
        }
        // 3 3 2
        else if(x == y && y > z) {
            result = "NO";
        }
        //2 3 3
        else if(y == z && x < y) {
            result = "YES";
            a = x;
            b = 1;
            c = y;
        }
        // 1 2 1
        else if(x == z && x < y) {
            result = "NO";
        }
        //1 1 2
        else if(x == y && x < z) {
            result = "NO";
        }
        else if(x > y && y == z) {
            result = "NO";
        }
        else {
            result = "NO";
        }

        if(result == "NO") {
            cout << "NO\n";
        }else {
            cout << "YES\n";
            cout << a << " " << b << " " << c << "\n";
        }

    }

}


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

int main() {
    int T;
    cin >> T;

    while (T--)
    {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);

        int x, y, z;
        int a, b, c;
        string result = "";
        cin >> x >> y >> z;

       if(x== y && y==z)
        {
           cout << "YES\n";
            cout << x << " " << x << " " <<x << "\n";
        }
        else if(x!=y && y!=z && z!=x)
        cout << "NO\n";
        else if(x==y && y<z)
        {
            cout << "NO\n";
        }
        else if(x==y && y>z)
        {
            cout << "YES\n";
        cout << x << " " << z << " " << "1\n";
        }
        else if(y==z && x<y)
        {
            cout << "YES\n";
        cout << x << " " << y << " " << "1\n";
        }

        else if(y==z && y<x)
        cout << "NO\n";
        else if (x==z && x>y)
        {
            cout << "YES\n";
        cout << "1" << " " << x << " " << y << "\n";
        }
        else if(x==z && y>x)
        {
            cout << "NO\n";
        }
    }

}


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

int main() {
    int T;
    cin >> T;

    while (T--)
    {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);

        int x, y, z;
        int a, b, c;
        string result = "";
        cin >> x >> y >> z;

        if(x == y && y == z) {
            result = "YES";
            a = b = c = x;
        }
        //3 2 3
        else if(x == z && x > y && y < z) {
            result = "YES";
            b = x;
            a = y;
            c = 1;
        }
        // 3 3 2
        else if(x == y && y > z) {
            result = "NO";
        }
        //2 3 3
        else if(y == z && x < y) {
            result = "YES";
            a = x;
            b = 1;
            c = y;
        }
        // 1 2 1
        else if(x == z && x < y) {
            result = "NO";
        }
        //1 1 2
        else if(x == y && x < z) {
            result = "NO";
        }
        else if(x > y && y == z) {
            result = "NO";
        }
        else {
            result = "NO";
        }

        if(result == "NO") {
            cout << "NO\n";
        }else {
            cout << "YES\n";
            cout << b << " " << a << " " << c << "\n";
        }

    }

}


#include <cstdio>
#include <utility>
#include <queue>
#define mp make_pair
#define X first
#define Y second
#define REP(i,n) for (int i=1;i<=n;++i)
using namespace std;

int n,w;
priority_queue< pair<int,int> > pq[2];

int main() {
	scanf("%d",&n);
	REP(i,n) {
		int a,b;
		scanf("%d%d",&a,&b);
		pq[a].push(mp(-b,i));
	}
	w=0;
	REP(i,n-1) {
		if (-pq[w].top().X<-pq[w^1].top().X) w^=1;
		if ((pq[w].top().X==pq[w^1].top().X)&&(pq[w].size()>pq[w^1].size())) w^=1;
		int a=-pq[w].top().X;
		int b=pq[w].top().Y;
		printf("%d %d %d\n",pq[w].top().Y,pq[w^1].top().Y,-pq[w^1].top().X);
		a-=-pq[w^1].top().X;
		pq[0].pop(); pq[1].pop();
		pq[w].push(mp(-a,b));
	}
	return 0;
}


#include <cstdio>
#include <utility>
#include <queue>
#define mp make_pair
#define X first
#define Y second
#define REP(i,n) for (int i=1;i<=n;++i)
using namespace std;

int n,w;
priority_queue< pair<int,int> > pq[2];

int main() {
	scanf("%d",&n);
	REP(i,n) {
		int a,b;
		scanf("%d%d",&a,&b);
		pq[a].push(mp(i,b));
	}
	w=0;
	REP(i,n-1) {
		if (pq[w].top().Y<pq[w^1].top().Y) w^=1;
		int a=pq[w].top().X;
		int b=pq[w].top().Y;
		printf("%d %d %d\n",pq[w].top().X,pq[w^1].top().X,pq[w^1].top().Y);
		b-=pq[w^1].top().Y;
		pq[0].pop(); pq[1].pop();
		pq[w].push(mp(a,b));
	}
	return 0;
}


#include <cstdio>
#include <utility>
#include <queue>
#define mp make_pair
#define X first
#define Y second
#define REP(i,n) for (int i=1;i<=n;++i)
using namespace std;

int n,w;
priority_queue< pair<int,int> > pq[2];

int main() {
	scanf("%d",&n);
	REP(i,n) {
		int a,b;
		scanf("%d%d",&a,&b);
		pq[a].push(mp(-b,i));
	}
	w=0;
	REP(i,n-1) {
		if (-pq[w].top().X<-pq[w^1].top().X) w^=1;
		int a=-pq[w].top().X;
		int b=pq[w].top().Y;
		printf("%d %d %d\n",pq[w].top().Y,pq[w^1].top().Y,-pq[w^1].top().X);
		a-=-pq[w^1].top().Y;
		pq[0].pop(); pq[1].pop();
		pq[w].push(mp(-a,b));
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define UP upper_bound
#define LB lower_bound
#define LL long long
#define Pi 3.14159265358
#define si size()
#define en end()
#define be begin()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ii set<int>::iterator
#define Tree int ind, int L, int R
#define Left 2*ind,L,(L+R)/2
#define Right 2*ind+1,(L+R)/2+1,R
using namespace std;
int n, m, k, i, j, c, s, l, r;
vector < pair < int , int > > v, g, v1, g1;
vector < pair < pair < int , int > , int > > res;
main(){
       cin>>n;
       for(i=1;i<=n;i++)
        {
         cin>>c>>s;
         if(c==0) v.pb(mp(s,i));
         else  g.pb(mp(s,i));
        }
       sort(v.be,v.en);
       sort(g.be,g.en);
       v1=v;
       g1=g;
       l=0; r=0;
       while(l<v.si && r<g.si)
        {
         //while(r<g.si && g[r].fi<v[l].fi)r++;
         if(g[r].fi>=v[l].fi){
         res.pb(mp(mp(v[l].se,g[r].se),v[l].fi));
         g[r].fi-=v[l].fi;
         l++;  }
         else
          {

                  res.pb(mp(mp(v[l].se,g[r].se),g[r].fi));
         v[l].fi-=g[r].fi;
         r++;
          }
        }
       /*if(res.si!=n-1)
        {
         res.clear();
         l=0; r=0;
       while(l<v1.si && r<g1.si)
        {
         while(l<v1.si && g1[r].fi>v1[l].fi)l++;
         if(g1[r].fi<=v1[l].fi){
         res.pb(mp(mp(g1[r].se,v1[l].se),g1[r].fi));
         v1[l].fi-=g1[r].fi;}
         r++;
        }}*/

       sort(res.be,res.en);
       for(i=0;i<res.si;i++)
         cout<<res[i].fi.fi<<" "<<res[i].fi.se<<" "<<res[i].se<<endl;
       }


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define UP upper_bound
#define LB lower_bound
#define LL long long
#define Pi 3.14159265358
#define si size()
#define en end()
#define be begin()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ii set<int>::iterator
#define Tree int ind, int L, int R
#define Left 2*ind,L,(L+R)/2
#define Right 2*ind+1,(L+R)/2+1,R
using namespace std;
int n, m, k, i, j, c, s, l, r;
vector < pair < int , int > > v, g;
vector < pair < pair < int , int > , int > > res;
main(){
       cin>>n;
       for(i=1;i<=n;i++)
        {
         cin>>c>>s;
         if(c==0) v.pb(mp(s,i));
         else  g.pb(mp(s,i));
        }
       sort(v.be,v.en);
       sort(g.be,g.en);
       l=0; r=0;
       while(l<v.si-1 && r<g.si)
        {
         while(r<g.si && g[r].fi<v[l].fi)r++;
         if(g[r].fi>=v[l].fi){
         res.pb(mp(mp(v[l].se,g[r].se),v[l].fi));
         g[r].fi-=v[l].fi; }
         l++;
        }
       for(i=0;i<g.si;i++)
        res.pb(mp(mp(v.back().se,g[i].se),g[i].fi));
       for(i=0;i<res.si;i++)
         cout<<res[i].fi.fi<<" "<<res[i].fi.se<<" "<<res[i].se<<endl;
       }


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define UP upper_bound
#define LB lower_bound
#define LL long long
#define Pi 3.14159265358
#define si size()
#define en end()
#define be begin()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ii set<int>::iterator
#define Tree int ind, int L, int R
#define Left 2*ind,L,(L+R)/2
#define Right 2*ind+1,(L+R)/2+1,R
using namespace std;
int n, m, k, i, j, c, s, l, r;
vector < pair < int , int > > v, g, v1, g1;
vector < pair < pair < int , int > , int > > res;
main(){
       cin>>n;
       for(i=1;i<=n;i++)
        {
         cin>>c>>s;
         if(c==0) v.pb(mp(s,i));
         else  g.pb(mp(s,i));
        }
       v1=v;
       g1=g;
       sort(v.be,v.en);
       sort(g.be,g.en);
       l=0; r=0;
       while(l<v.si && r<g.si)
        {
         while(r<g.si && g[r].fi<v[l].fi)r++;
         if(g[r].fi>=v[l].fi){
         res.pb(mp(mp(v[l].se,g[r].se),v[l].fi));
         g[r].fi-=v[l].fi;}
         l++;
        }
       for(i=0;i<g.si;i++)
        res.pb(mp(mp(v.back().se,g[i].se),g[i].fi));
       if(res.si!=n-1)
        {
         res.clear();
         l=0; r=0;
       while(l<v1.si && r<g1.si)
        {
         while(l<v1.si && g1[r].fi>v1[l].fi)l++;
         if(g1[r].fi<=v1[l].fi){
         res.pb(mp(mp(g1[r].se,v1[l].se),g1[r].fi));
         v1[l].fi-=g1[r].fi;}
         r++;
        }}

       sort(res.be,res.en);
       for(i=0;i<res.si;i++)
         cout<<res[i].fi.fi<<" "<<res[i].fi.se<<" "<<res[i].se<<endl;
       }


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define UP upper_bound
#define LB lower_bound
#define LL long long
#define Pi 3.14159265358
#define si size()
#define en end()
#define be begin()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ii set<int>::iterator
#define Tree int ind, int L, int R
#define Left 2*ind,L,(L+R)/2
#define Right 2*ind+1,(L+R)/2+1,R
using namespace std;
int n, m, k, i, j, c, s, l, r;
vector < pair < int , int > > v, g, v1, g1;
vector < pair < pair < int , int > , int > > res;
main(){
       cin>>n;
       for(i=1;i<=n;i++)
        {
         cin>>c>>s;
         if(c==0) v.pb(mp(s,i));
         else  g.pb(mp(s,i));
        }
       v1=v;
       g1=g;
       sort(v.be,v.en);
       sort(g.be,g.en);
       l=0; r=0;
       while(l<v.si && r<g.si)
        {
         while(g[r].fi<v[l].fi)r++;
         res.pb(mp(mp(v[l].se,g[r].se),v[l].fi));
         g[r].fi-=v[l].fi;
         l++;
        }
       for(i=0;i<g.si;i++)
        res.pb(mp(mp(v.back().se,g[i].se),g[i].fi));
       if(res.si!=n-1)
        {
         res.clear();
         l=0; r=0;
       while(l<v1.si && r<g1.si)
        {
         while(g1[r].fi>v1[l].fi)l++;
         res.pb(mp(mp(g1[r].se,v1[l].se),g1[r].fi));
         v1[l].fi-=g1[r].fi;
         r++;
        }}

       sort(res.be,res.en);
       for(i=0;i<res.si;i++)
         cout<<res[i].fi.fi<<" "<<res[i].fi.se<<" "<<res[i].se<<endl;
       }


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define UP upper_bound
#define LB lower_bound
#define LL long long
#define Pi 3.14159265358
#define si size()
#define en end()
#define be begin()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ii set<int>::iterator
#define Tree int ind, int L, int R
#define Left 2*ind,L,(L+R)/2
#define Right 2*ind+1,(L+R)/2+1,R
using namespace std;
int n, m, k, i, j, c, s, l, r;
vector < pair < int , int > > v, g, v1, g1;
vector < pair < pair < int , int > , int > > res;
main(){
       cin>>n;
       for(i=1;i<=n;i++)
        {
         cin>>c>>s;
         if(c==0) v.pb(mp(s,i));
         else  g.pb(mp(s,i));
        }
       sort(v.be,v.en);
       sort(g.be,g.en);
       v1=v;
       g1=g;
       l=0; r=0;
       while(l<v.si && r<g.si)
        {
         if(g[r].fi>=v[l].fi){
         res.pb(mp(mp(v[l].se,g[r].se),v[l].fi));
         g[r].fi-=v[l].fi;
         l++;  }
         else
          {

                  res.pb(mp(mp(v[l].se,g[r].se),g[r].fi));
         v[l].fi-=g[r].fi;
         r++;
          }
        }
       while(r<g.si-1)
        {
          res.pb(mp(mp(v[l-1].se,g[r].se),0));
          r++;
        }
       while(l<v.si-1)
        {
          res.pb(mp(mp(v[l].se,g[r-1].se),0));
          l++;
        }
       sort(res.be,res.en);
       for(i=0;i<res.si;i++)
         cout<<res[i].fi.fi<<" "<<res[i].fi.se<<" "<<res[i].se<<endl;
       }


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define UP upper_bound
#define LB lower_bound
#define LL long long
#define Pi 3.14159265358
#define si size()
#define en end()
#define be begin()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ii set<int>::iterator
#define Tree int ind, int L, int R
#define Left 2*ind,L,(L+R)/2
#define Right 2*ind+1,(L+R)/2+1,R
using namespace std;
int n, m, k, i, j, c, s, l, r;
vector < pair < int , int > > v, g, v1, g1;
vector < pair < pair < int , int > , int > > res;
main(){
       cin>>n;
       for(i=1;i<=n;i++)
        {
         cin>>c>>s;
         if(c==0) v.pb(mp(s,i));
         else  g.pb(mp(s,i));
        }
       sort(v.be,v.en);
       sort(g.be,g.en);
       v1=v;
       g1=g;
       l=0; r=0;
       while(l<v.si && r<g.si)
        {
         if(g[r].fi>=v[l].fi){
         res.pb(mp(mp(v[l].se,g[r].se),v[l].fi));
         g[r].fi-=v[l].fi;
         l++;  }
         else
          {

                  res.pb(mp(mp(v[l].se,g[r].se),g[r].fi));
         v[l].fi-=g[r].fi;
         r++;
          }
        }
       while(r<g.si-1)
        {r++;
          res.pb(mp(mp(v[l-1].se,g[r].se),0));

        }
       while(l<v.si-1)
        {l++;
          res.pb(mp(mp(v[l].se,g[r-1].se),0));

        }
       sort(res.be,res.en);
       for(i=0;i<res.si;i++)
         cout<<res[i].fi.fi<<" "<<res[i].fi.se<<" "<<res[i].se<<endl;
       }


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define UP upper_bound
#define LB lower_bound
#define LL long long
#define Pi 3.14159265358
#define si size()
#define en end()
#define be begin()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ii set<int>::iterator
#define Tree int ind, int L, int R
#define Left 2*ind,L,(L+R)/2
#define Right 2*ind+1,(L+R)/2+1,R
using namespace std;
int n, m, k, i, j, c, s, l, r;
vector < pair < int , int > > v, g;
vector < pair < pair < int , int > , int > > res;
main(){
       cin>>n;
       for(i=1;i<=n;i++)
        {
         cin>>c>>s;
         if(c==0) v.pb(mp(s,i));
         else  g.pb(mp(s,i));
        }
       sort(v.be,v.en);
       sort(g.be,g.en);
       l=0; r=0;
       while(l<v.si-1 && r<g.si)
        {
         while(g[r].fi<v[l].fi)r++;
         res.pb(mp(mp(v[l].se,g[r].se),v[l].fi));
         g[r].fi-=v[l].fi;
         l++;
        }
       for(i=0;i<g.si;i++)
        res.pb(mp(mp(v.back().se,g[i].se),g[i].fi));
       for(i=0;i<res.si;i++)
         cout<<res[i].fi.fi<<" "<<res[i].fi.se<<" "<<res[i].se<<endl;
       }


/*************************************************************************
    > File Name: d.cpp
    > Author: implus
    > Mail: 674592809@qq.com
    > Created Time: 一 12/16 13:04:59 2013
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
#include<string>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define ls (rt<<1)
#define rs (rt<<1|1)
#define lson l,m,ls
#define rson m+1,r,rs

struct node{
    int a,b,v;
    node(){}
    node(int a,int b,int v):a(a),b(b),v(v){}
};
int main(){
    int n;
    while(cin>>n){
        vector<pii> a[2];
        int type,val;
        for(int i = 0; i <n; i++){
            cin>>type>>val;
            a[type].push_back(pii(val,i+1));
        }
        for(int i = 0; i < 2; i++)
            sort(a[i].begin(),a[i].end());
        int p0 = 0,p1 = 0;
        vector<node> v;
        while(1){
            if(p0 == a[0].size() || p1 == a[1].size())break;
            pii& t0 = a[0][p0];
            pii& t1 = a[1][p1];
            if(t0.first < t1.first){
                v.push_back(node(t0.second,t1.second,t0.first));
                t1.first -= t0.first;
                p0++;
            }else if(t0.first >= t1.first){
                v.push_back(node(t0.second,t1.second,t1.first));
                t0.first -= t1.first;
                p1++;
            }
        }
        for(int i = 0; i < v.size(); i++){
            cout<<v[i].a<<" "<<v[i].b<<" "<<v[i].v<<endl;
        }
    }
    return 0;
}


/*************************************************************************
    > File Name: d.cpp
    > Author: implus
    > Mail: 674592809@qq.com
    > Created Time: 一 12/16 13:04:59 2013
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
#include<string>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define ls (rt<<1)
#define rs (rt<<1|1)
#define lson l,m,ls
#define rson m+1,r,rs

struct node{
    int a,b,v;
    node(){}
    node(int a,int b,int v):a(a),b(b),v(v){}
};
int main(){
    int n;
    while(cin>>n){
        vector<pii> a[2];
        int type,val;
        for(int i = 0; i <n; i++){
            cin>>type>>val;
            a[type].push_back(pii(val,i+1));
        }
        for(int i = 0; i < 2; i++)
            sort(a[i].begin(),a[i].end());
        int p0 = 0,p1 = 0;
        vector<node> v;
        while(1){
            if(p0 == a[0].size() || p1 == a[1].size())break;
            pii& t0 = a[0][p0];
            pii& t1 = a[1][p1];
            if(t0.first < t1.first){
                v.push_back(node(t0.second,t1.second,t0.first));
                t1.first -= t0.first;
                p0++;
            }else if(t0.first > t1.first){
                v.push_back(node(t0.second,t1.second,t1.first));
                t0.first -= t1.first;
                p1++;
            }else {
                v.push_back(node(t0.second,t1.second,t1.first));
                if(a[0].size() - p0 > a[1].size() - p1){
                    t1.first -= t0.first;
                    p0++;
                }else{
                    t0.first -= t1.first;
                    p1++;
                }
            }
        }
        for(int i = 0; i < v.size(); i++){
            cout<<v[i].a<<" "<<v[i].b<<" "<<v[i].v<<endl;
        }
    }
    return 0;
}


/*************************************************************************
    > File Name: d.cpp
    > Author: implus
    > Mail: 674592809@qq.com
    > Created Time: 一 12/16 13:04:59 2013
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
#include<string>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define ls (rt<<1)
#define rs (rt<<1|1)
#define lson l,m,ls
#define rson m+1,r,rs

struct node{
    int a,b,v;
    node(){}
    node(int a,int b,int v):a(a),b(b),v(v){}
};
int main(){
    int n;
    while(cin>>n){
        vector<pii> a[2];
        int type,val;
        for(int i = 0; i <n; i++){
            cin>>type>>val;
            a[type].push_back(pii(val,i+1));
        }
        for(int i = 0; i < 2; i++)
            sort(a[i].begin(),a[i].end());
        int p0 = 0,p1 = 0;
        vector<node> v;
        while(1){
            if(p0 == a[0].size() || p1 == a[1].size())break;
            pii& t0 = a[0][p0],t1 = a[1][p1];
            if(t0.first < t1.first){
                v.push_back(node(t0.second,t1.second,t0.first));
                t1.first -= t0.first;
                p0++;
            }else if(t0.first > t1.first){
                v.push_back(node(t0.second,t1.second,t1.first));
                t0.first -= t1.first;
                p1++;
            }else {
                v.push_back(node(t0.second,t1.second,t1.first));
                if(a[0].size() - p0 > a[1].size() - p1){
                    t1.first -= t0.first;
                    p0++;
                }else{
                    t0.first -= t1.first;
                    p1++;
                }
            }
        }
        for(int i = 0; i < v.size(); i++){
            cout<<v[i].a<<" "<<v[i].b<<" "<<v[i].v<<endl;
        }
    }
    return 0;
}


/*************************************************************************
    > File Name: d.cpp
    > Author: implus
    > Mail: 674592809@qq.com
    > Created Time: 一 12/16 13:04:59 2013
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
#include<string>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define ls (rt<<1)
#define rs (rt<<1|1)
#define lson l,m,ls
#define rson m+1,r,rs

struct node{
    int a,b,v;
    node(){}
    node(int a,int b,int v):a(a),b(b),v(v){}
};
int main(){
    int n;
    while(cin>>n){
        vector<pii> a[2];
        int type,val;
        for(int i = 0; i <n; i++){
            cin>>type>>val;
            a[type].push_back(pii(val,i+1));
        }
        for(int i = 0; i < 2; i++)
            sort(a[i].begin(),a[i].end());
        int p0 = 0,p1 = 0;
        vector<node> v;
        while(1){
            if(p0 == a[0].size() || p1 == a[1].size())break;
            pii& t0 = a[0][p0],&t1 = a[1][p1];
            if(t0.first < t1.first){
                v.push_back(node(t0.second,t1.second,t0.first));
                t1.first -= t0.first;
                p0++;
            }else if(t0.first > t1.first){
                v.push_back(node(t0.second,t1.second,t1.first));
                t0.first -= t1.first;
                p1++;
            }else {
                v.push_back(node(t0.second,t1.second,t1.first));
                if(a[0].size() - p0 > a[1].size() - p1){
                    t1.first -= t0.first;
                    p0++;
                }else{
                    t0.first -= t1.first;
                    p1++;
                }
            }
        }
        for(int i = 0; i < v.size(); i++){
            cout<<v[i].a<<" "<<v[i].b<<" "<<v[i].v<<endl;
        }
    }
    return 0;
}


#include <cstdio> //PROG: CF260D
#include <cstring>
#include <algorithm>
#include <queue>
struct Node{
	int n,id;
	Node(){}
	Node(int ni,int ii){n=ni,id=ii;}
	bool operator<(const Node &B)const{
		return n<B.n;
	}
};
std::priority_queue<Node> Jack[2];
int main(){
	#ifdef JACK1_NOTEBOOK
	freopen("CF260D_in.txt","r",stdin);
	freopen("CF260D_out.h","w",stdout);
	#endif
	int N,i,j,k,val,u,last[2];
	scanf("%d",&N);
	for (i=1;i<=N;i++){
		scanf("%d%d",&j,&k);
		Jack[j].push(Node(k,i));
		last[j]=i;
	}
	j=1,k=0;
	do{
		if (Jack[j].top() < Jack[k].top())j^=k^=j^=k;
		val=Jack[j].top().n;
		u=Jack[j].top().id;
		Jack[j].pop();
		while (!Jack[k].empty() && val>=Jack[k].top().n){
			val-=Jack[k].top().n;
			Jack[k].pop();
		}
		if (val>0)
			Jack[j].push(Node(val,u));
	}while (!Jack[j].empty() && !Jack[k].empty());

	//process zero edge
	for (j=0;j<2;j++)
		while (!Jack[j].empty()){
			//printf("elements %d\n",j);
			printf("%d %d 0\n",Jack[j].top().id,last[j^1]);
			Jack[j].pop();
		}
}


#include <cstdio> //PROG : CF260D, By Jack1
#include <cstring>
#include <algorithm>
#include <queue>
struct Node{
	int id,w;
	Node(){}Node(int idi,int wi){id=idi,w=wi;}
	bool operator<(const Node &B)const{
		return w<B.w;
	}
}use,adder;
int main(){
	#ifdef JACK1_PC
	freopen("CF260D_in.txt","r",stdin);
	freopen("CF260D_out.h","w",stdout);
	#endif
	int N,i,a,b;
	while (~scanf("%d",&N)){
		std::priority_queue<Node> W,B;
		for (i=1;i<=N;i++){
			scanf("%d%d",&a,&b);
			if (a==0)W.push(Node(i,b));
			if (a==1)B.push(Node(i,b));
		}
		while (!W.empty()&&!B.empty()){
			if (B.empty() || (!W.empty() && W.top().w > B.top().w)){
				//white main
				use=W.top(); W.pop();
				while (!B.empty() && use.w>=B.top().w){
					adder=B.top(); B.pop();
					use.w-=adder.w;
					printf("%d %d %d\n",use.id,adder.id,adder.w);
				}
				W.push(use);
			}else{
				//black main
				use=B.top(); B.pop();
				while (!W.empty() && use.w>=W.top().w){
					adder=W.top(); W.pop();
					use.w-=adder.w;
					printf("%d %d %d\n",use.id,adder.id,adder.w);
				}
				B.push(use);
			}
		}
	}
}


#include <cstdio> //PROG: CF260D
#include <cstring>
#include <algorithm>
#include <queue>
struct Node{
	int n,id;
	Node(){}
	Node(int ni,int ii){n=ni,id=ii;}
	bool operator<(const Node &B)const{
		return n<B.n;
	}
};
std::priority_queue<Node> Jack[2];
int main(){
	#ifdef JACK1_NOTEBOOK
	freopen("CF260D_in.txt","r",stdin);
	freopen("CF260D_out.h","w",stdout);
	#endif
	int N,i,j,k,val,u,last[2];
	scanf("%d",&N);
	for (i=1;i<=N;i++){
		scanf("%d%d",&j,&k);
		Jack[j].push(Node(k,i));
	}
	j=1,k=0;
	do{
		if (Jack[j].top() < Jack[k].top())j^=k^=j^=k;
		val=Jack[j].top().n;
		u=Jack[j].top().id;
		last[j]=Jack[j].top().id;
		Jack[j].pop();
		while (!Jack[k].empty() && val>=Jack[k].top().n){
			val-=Jack[k].top().n;
			printf("%d %d %d\n",u,Jack[k].top().id,Jack[k].top().n);
			last[k]=Jack[k].top().id;
			Jack[k].pop();
		}
		Jack[j].push(Node(val,u));
	}while (!Jack[j].empty() && !Jack[k].empty());

	//process zero edge
	for (j=0;j<2;j++)
		while (!Jack[j].empty()){
			if (Jack[j].size()+Jack[j^1].size() == 1)break;
			printf("%d %d 0\n",Jack[j].top().id,last[j^1]);
			Jack[j].pop();
		}
}


#include <cstdio> //PROG: CF260D
#include <cstring>
#include <algorithm>
#include <queue>
struct Node{
	int n,id;
	Node(){}
	Node(int ni,int ii){n=ni,id=ii;}
	bool operator<(const Node &B)const{
		return n>B.n;
	}
}use,goal;
std::priority_queue<Node> Jack[2];
int main(){
	#ifdef JACK1_NOTEBOOK
	freopen("CF260D_in.txt","r",stdin);
	freopen("CF260D_out.h","w",stdout);
	#endif
	int N,i,j,k,val,u,last[2];
	scanf("%d",&N);
	for (i=1;i<=N;i++){
		scanf("%d%d",&j,&k);
		Jack[j].push(Node(k,i));
	}
	j=1,k=0;
	do{
		if (Jack[j].top().n >= Jack[k].top().n)j^=k^=j^=k;
		use=Jack[j].top(); Jack[j].pop();
		goal=Jack[k].top(); Jack[k].pop();
		printf("%d %d %d\n",use.id,goal.id,use.n);
		goal.n-=use.n;
		Jack[k].push(goal);
		last[j]=use.id;
	}while (!Jack[j].empty() && !Jack[k].empty());

	//process zero edge
	for (j=0;j<2;j++)
		if (!Jack[j].empty()){
			k=j^1;
			while (Jack[j].size()>1){
				use=Jack[j].top();
				Jack[j].pop();
				printf("%d %d %d\n",use.id,last[k],0);
			}
		}
}


#include <cstdio> //PROG: CF260D
#include <cstring>
#include <algorithm>
#include <queue>
struct Node{
	int n,id;
	Node(){}
	Node(int ni,int ii){n=ni,id=ii;}
	bool operator<(const Node &B)const{
		return n>B.n;
	}
}use,goal;
std::priority_queue<Node> Jack[2];
int main(){
	#ifdef JACK1_NOTEBOOK
	freopen("CF260D_in.txt","r",stdin);
	freopen("CF260D_out.h","w",stdout);
	#endif
	int N,i,j,k,val,u,last[2];
	scanf("%d",&N);
	for (i=1;i<=N;i++){
		scanf("%d%d",&j,&k);
		Jack[j].push(Node(k,i));
	}
	j=1,k=0;
	do{
		if (Jack[j].top().n > Jack[k].top().n)j^=k^=j^=k;
		use=Jack[j].top(); Jack[j].pop();
		goal=Jack[k].top(); Jack[k].pop();
		printf("%d %d %d\n",use.id,goal.id,use.n);
		goal.n-=use.n;
		Jack[k].push(goal);
		last[j]=use.id;
		last[k]=goal.id;
	}while (!Jack[j].empty() && !Jack[k].empty());

	//process zero edge
	for (j=0;j<2;j++)
		if (!Jack[j].empty()){
			k=j^1;
			while (Jack[j].size()>1){
				use=Jack[j].top();
				Jack[j].pop();
				printf("%d %d %d\n",use.id,last[k],0);
			}
		}
}


#include <cstdio> //PROG: CF260D
#include <cstring>
#include <algorithm>
#include <queue>
struct Node{
	int n,id;
	Node(){}
	Node(int ni,int ii){n=ni,id=ii;}
	bool operator<(const Node &B)const{
		return n<B.n;
	}
};
std::priority_queue<Node> Jack[2];
int main(){
	#ifdef JACK1_NOTEBOOK
	freopen("CF260D_in.txt","r",stdin);
	freopen("CF260D_out.h","w",stdout);
	#endif
	int N,i,j,k,val,u,last[2];
	scanf("%d",&N);
	for (i=1;i<=N;i++){
		scanf("%d%d",&j,&k);
		Jack[j].push(Node(k,i));
		last[j]=i;
	}
	j=1,k=0;
	do{
		if (Jack[j].top() < Jack[k].top())j^=k^=j^=k;
		val=Jack[j].top().n;
		u=Jack[j].top().id;
		Jack[j].pop();
		while (!Jack[k].empty() && val>=Jack[k].top().n){
			val-=Jack[k].top().n;
			printf("%d %d %d\n",u,Jack[k].top().id,Jack[k].top().n);
			Jack[k].pop();
		}
		if (val>0)
			Jack[j].push(Node(val,u));
	}while (!Jack[j].empty() && !Jack[k].empty());

	//process zero edge
	for (j=0;j<2;j++)
		while (!Jack[j].empty()){
			printf("%d %d 0\n",Jack[j].top().id,last[j^1]);
			Jack[j].pop();
		}
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <queue>

using namespace std;

bool pred (pair<pair<int,int>,int> i,pair<pair<int,int>,int> j) { return (i>j); }
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    vector<pair<pair<int,int>,int> > white;
    vector<pair<pair<int,int>,int> > black;
    pair<int,int> temp;
    for(int i=0;i<n;i++){
        cin>>temp.first>>temp.second;
        if(temp.first){
            black.push_back(make_pair(temp,i+1));
        }
        else{
            white.push_back(make_pair(temp,i+1));
        }
    }

    //sort(black.begin(),black.end(),pred);
    //sort(white.begin(),white.end(),pred);

    while(black.size() && white.size() && (!black[black.size()-1].first.second || !white[white.size()-1].first.second)){

        if(black.size() && white.size() && !black[black.size()-1].first.second){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            black.pop_back();
        }

        else if(black.size() && white.size()){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            white.pop_back();
        }

    }
    while(black.size() && white.size()){
        if(black[black.size()-1].first.second > white[white.size()-1].first.second){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            black[black.size()-1].first.second -= white[white.size()-1].first.second;
            white.pop_back();
            if(!black[black.size()-1].first.second) black.pop_back();
        }
        else {
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            white[white.size()-1].first.second-=black[black.size()-1].first.second;
            black.pop_back();
            if(!white[white.size()-1].first.second) white.pop_back();
        }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <queue>

using namespace std;

bool pred (pair<pair<int,int>,int> i,pair<pair<int,int>,int> j) { return (i>j); }
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    vector<pair<pair<int,int>,int> > white;
    vector<pair<pair<int,int>,int> > black;
    pair<int,int> temp;
    for(int i=0;i<n;i++){
        cin>>temp.first>>temp.second;
        if(temp.first){
            black.push_back(make_pair(temp,i+1));
        }
        else{
            white.push_back(make_pair(temp,i+1));
        }
    }

    //sort(black.begin(),black.end(),pred);
    //sort(white.begin(),white.end(),pred);

    while(black.size() && white.size()){
        if(black[black.size()-1].first.second > white[white.size()-1].first.second
            || (black[black.size()-1].first.second == white[white.size()-1].first.second &&  black.size() < white.size())){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            black[black.size()-1].first.second -= white[white.size()-1].first.second;
            white.pop_back();
        }
        else {
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            white[white.size()-1].first.second-=black[black.size()-1].first.second;
            black.pop_back();
        }
    }
    system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <queue>

using namespace std;

bool pred (pair<pair<int,int>,int> i,pair<pair<int,int>,int> j) { return (i>j); }
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    vector<pair<pair<int,int>,int> > white;
    vector<pair<pair<int,int>,int> > black;
    pair<int,int> temp;
    for(int i=0;i<n;i++){
        cin>>temp.first>>temp.second;
        if(temp.first){
            black.push_back(make_pair(temp,i+1));
        }
        else{
            white.push_back(make_pair(temp,i+1));
        }
    }

    sort(black.begin(),black.end(),pred);
    sort(white.begin(),white.end(),pred);

    while(black.size() && white.size()){
        if(black[black.size()-1].first.second > white[white.size()-1].first.second
            || (black[black.size()-1].first.second == white[white.size()-1].first.second &&  black.size() < white.size())){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            black[black.size()-1].first.second -= white[white.size()-1].first.second;
            white.pop_back();
        }
        else {
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            white[white.size()-1].first.second-=black[black.size()-1].first.second;
            black.pop_back();
        }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <queue>

using namespace std;

bool pred (pair<pair<int,int>,int> i,pair<pair<int,int>,int> j) { return (i>j); }
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    vector<pair<pair<int,int>,int> > white;
    vector<pair<pair<int,int>,int> > black;
    pair<int,int> temp;
    for(int i=0;i<n;i++){
        cin>>temp.first>>temp.second;
        if(temp.first){
            black.push_back(make_pair(temp,i+1));
        }
        else{
            white.push_back(make_pair(temp,i+1));
        }
    }

    //sort(black.begin(),black.end(),pred);
    //sort(white.begin(),white.end(),pred);

    while(black.size() && white.size() && (!black[black.size()-1].first.second || !white[white.size()-1].first.second)){

        if(black.size() && white.size() && !black[black.size()-1].first.second){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            black.pop_back();
        }

        else if(black.size() && white.size()){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            white.pop_back();
        }

    }
    while(black.size() && white.size()){
        if(black[black.size()-1].first.second > white[white.size()-1].first.second){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            black[black.size()-1].first.second -= white[white.size()-1].first.second;
            white.pop_back();
            //if(!black[black.size()-1].first.second) black.pop_back();
        }
        else {
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            white[white.size()-1].first.second-=black[black.size()-1].first.second;
            black.pop_back();
            //if(!white[white.size()-1].first.second) white.pop_back();
        }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <queue>

using namespace std;

bool pred (pair<pair<int,int>,int> i,pair<pair<int,int>,int> j) { return (i>j); }
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    vector<pair<pair<int,int>,int> > white;
    vector<pair<pair<int,int>,int> > black;
    pair<int,int> temp;
    for(int i=0;i<n;i++){
        cin>>temp.first>>temp.second;
        if(temp.first){
            black.push_back(make_pair(temp,i+1));
        }
        else{
            white.push_back(make_pair(temp,i+1));
        }
    }

    //sort(black.begin(),black.end(),pred);
    //sort(white.begin(),white.end(),pred);

    int b=1;
    while(black.size() && white.size() && (!black[black.size()-1].first.second || !white[white.size()-1].first.second)){

        if(black.size() && white.size() && !black[black.size()-1].first.second && b==1){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            if(black.size()>1 || white.size() == 1)black.pop_back();
        }

        else if(black.size() && white.size() && !white[white.size()-1].first.second && b==-1){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            if(white.size()>1 || black.size() == 1)white.pop_back();
        }
        b*=-1;
    }
    while(black.size() && white.size()){
        if(black[black.size()-1].first.second > white[white.size()-1].first.second){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            black[black.size()-1].first.second -= white[white.size()-1].first.second;
            white.pop_back();
        }
        else {
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            white[white.size()-1].first.second-=black[black.size()-1].first.second;
            black.pop_back();
        }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <queue>

using namespace std;
long long a[100047];
bool pred (pair<pair<int,int>,int> i,pair<pair<int,int>,int> j) { return (i>j); }
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    vector<pair<pair<int,int>,int> > white;
    vector<pair<pair<int,int>,int> > black;
    pair<int,int> temp;
    for(int i=0;i<n;i++){
        cin>>temp.first>>temp.second;
        if(temp.first){
            black.push_back(make_pair(temp,i+1));
        }
        else{
            white.push_back(make_pair(temp,i+1));
        }
    }
    sort(black.begin(),black.end(),pred);
    sort(white.begin(),white.end(),pred);
    while(!black[black.size()-1].first.second || !white[white.size()-1].first.second){
        if(!black[black.size()-1].first.second){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            black.pop_back();
        }
        else{
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            white.pop_back();
        }

    }
    while(black.size() || white.size()){
        if(black[black.size()-1].first.second > white[white.size()-1].first.second){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            black[black.size()-1].first.second -= white[white.size()-1].first.second;
            white.pop_back();
            if(!black[black.size()-1].first.second) black.pop_back();
        }
        else {
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            white[white.size()-1].first.second-=black[black.size()-1].first.second;
            black.pop_back();
            if(!white[white.size()-1].first.second) white.pop_back();
        }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <queue>

using namespace std;
long long a[100047];
bool pred (pair<pair<int,int>,int> i,pair<pair<int,int>,int> j) { return (i>j); }
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    vector<pair<pair<int,int>,int> > white;
    vector<pair<pair<int,int>,int> > black;
    pair<int,int> temp;
    for(int i=0;i<n;i++){
        cin>>temp.first>>temp.second;
        if(temp.first){
            black.push_back(make_pair(temp,i+1));
        }
        else{
            white.push_back(make_pair(temp,i+1));
        }
    }
    sort(black.begin(),black.end(),pred);
    sort(white.begin(),white.end(),pred);
    while(black.size() && white.size() && (!black[black.size()-1].first.second || !white[white.size()-1].first.second)){
        if(black.size() && white.size() && !black[black.size()-1].first.second ){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            black.pop_back();
        }
        else if(black.size() && white.size()){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            white.pop_back();
        }

    }
    while(black.size() && white.size()){
        if(black[black.size()-1].first.second > white[white.size()-1].first.second){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            black[black.size()-1].first.second -= white[white.size()-1].first.second;
            white.pop_back();
            if(!black[black.size()-1].first.second) black.pop_back();
        }
        else {
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            white[white.size()-1].first.second-=black[black.size()-1].first.second;
            black.pop_back();
            if(!white[white.size()-1].first.second) white.pop_back();
        }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <queue>

using namespace std;

bool pred (pair<pair<int,int>,int> i,pair<pair<int,int>,int> j) { return (i>j); }
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    vector<pair<pair<int,int>,int> > white;
    vector<pair<pair<int,int>,int> > black;
    pair<int,int> temp;
    for(int i=0;i<n;i++){
        cin>>temp.first>>temp.second;
        if(temp.first){
            black.push_back(make_pair(temp,i+1));
        }
        else{
            white.push_back(make_pair(temp,i+1));
        }
    }

    //sort(black.begin(),black.end(),pred);
    //sort(white.begin(),white.end(),pred);

    while(black.size() && white.size()){
        if(black[black.size()-1].first.second > white[white.size()-1].first.second
            || (black[black.size()-1].first.second == white[white.size()-1].first.second &&  black.size() < white.size())){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            black[black.size()-1].first.second -= white[white.size()-1].first.second;
            white.pop_back();
        }
        else {
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            white[white.size()-1].first.second-=black[black.size()-1].first.second;
            black.pop_back();
        }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <queue>

using namespace std;
long long a[100047];
bool pred (pair<pair<int,int>,int> i,pair<pair<int,int>,int> j) { return (i>j); }
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    vector<pair<pair<int,int>,int> > white;
    vector<pair<pair<int,int>,int> > black;
    pair<int,int> temp;
    for(int i=0;i<n;i++){
        cin>>temp.first>>temp.second;
        if(temp.first){
            black.push_back(make_pair(temp,i+1));
        }
        else{
            white.push_back(make_pair(temp,i+1));
        }
    }
    sort(black.begin(),black.end(),pred);
    sort(white.begin(),white.end(),pred);
    while(!black[black.size()-1].first.second || !white[white.size()-1].first.second){
        if(!black[black.size()-1].first.second){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            black.pop_back();
        }
        else{
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            white.pop_back();
        }

    }
    while(black.size() && white.size()){
        if(black[black.size()-1].first.second > white[white.size()-1].first.second){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            black[black.size()-1].first.second -= white[white.size()-1].first.second;
            white.pop_back();
            if(!black[black.size()-1].first.second) black.pop_back();
        }
        else {
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            white[white.size()-1].first.second-=black[black.size()-1].first.second;
            black.pop_back();
            if(!white[white.size()-1].first.second) white.pop_back();
        }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <queue>

using namespace std;

bool pred (pair<pair<int,int>,int> i,pair<pair<int,int>,int> j) { return (i>j); }
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    vector<pair<pair<int,int>,int> > white;
    vector<pair<pair<int,int>,int> > black;
    pair<int,int> temp;
    for(int i=0;i<n;i++){
        cin>>temp.first>>temp.second;
        if(temp.first){
            black.push_back(make_pair(temp,i+1));
        }
        else{
            white.push_back(make_pair(temp,i+1));
        }
    }

    sort(black.begin(),black.end(),pred);
    sort(white.begin(),white.end(),pred);

    int b=1;
    while(black.size() && white.size() && (!black[black.size()-1].first.second || !white[white.size()-1].first.second)){

        if(black.size() && white.size() && !black[black.size()-1].first.second && b==1){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            black.pop_back();
        }

        else if(black.size() && white.size() && b==-1){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            white.pop_back();
        }
        b*=-1;
    }
    while(black.size() && white.size()){
        if(black[black.size()-1].first.second > white[white.size()-1].first.second){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            black[black.size()-1].first.second -= white[white.size()-1].first.second;
            white.pop_back();
        }
        else {
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            white[white.size()-1].first.second-=black[black.size()-1].first.second;
            black.pop_back();
        }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <queue>

using namespace std;

bool pred (pair<pair<int,int>,int> i,pair<pair<int,int>,int> j) { return (i>j); }
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    vector<pair<pair<int,int>,int> > white;
    vector<pair<pair<int,int>,int> > black;
    pair<int,int> temp;
    for(int i=0;i<n;i++){
        cin>>temp.first>>temp.second;
        if(temp.first){
            black.push_back(make_pair(temp,i+1));
        }
        else{
            white.push_back(make_pair(temp,i+1));
        }
    }

    //sort(black.begin(),black.end(),pred);
    //sort(white.begin(),white.end(),pred);

    int b=1;
    while(black.size() && white.size() && (!black[black.size()-1].first.second || !white[white.size()-1].first.second)){

        if(black.size() && white.size() && !black[black.size()-1].first.second && b==1){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            black.pop_back();
        }

        else if(black.size() && white.size() && !white[white.size()-1].first.second && b==-1){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            white.pop_back();
        }
        b*=-1;
    }
    while(black.size() && white.size()){
        if(black[black.size()-1].first.second > white[white.size()-1].first.second){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            black[black.size()-1].first.second -= white[white.size()-1].first.second;
            white.pop_back();
        }
        else {
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            white[white.size()-1].first.second-=black[black.size()-1].first.second;
            black.pop_back();
        }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <queue>

using namespace std;
long long a[100047];
bool pred (pair<pair<int,int>,int> i,pair<pair<int,int>,int> j) { return (i>j); }
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    vector<pair<pair<int,int>,int> > white;
    vector<pair<pair<int,int>,int> > black;
    pair<int,int> temp;
    for(int i=0;i<n;i++){
        cin>>temp.first>>temp.second;
        if(temp.first){
            black.push_back(make_pair(temp,i+1));
        }
        else{
            white.push_back(make_pair(temp,i+1));
        }
    }
    sort(black.begin(),black.end(),pred);
    sort(white.begin(),white.end(),pred);
    while(black.size() && white.size() && !black[black.size()-1].first.second || !white[white.size()-1].first.second){
        if(!black[black.size()-1].first.second && black.size()){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            black.pop_back();
        }
        else if(white.size()){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            white.pop_back();
        }

    }
    while(black.size() && white.size()){
        if(black[black.size()-1].first.second > white[white.size()-1].first.second){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            black[black.size()-1].first.second -= white[white.size()-1].first.second;
            white.pop_back();
            if(!black[black.size()-1].first.second) black.pop_back();
        }
        else {
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            white[white.size()-1].first.second-=black[black.size()-1].first.second;
            black.pop_back();
            if(!white[white.size()-1].first.second) white.pop_back();
        }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <queue>

using namespace std;
long long a[100047];
bool pred (pair<pair<int,int>,int> i,pair<pair<int,int>,int> j) { return (i>j); }
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    vector<pair<pair<int,int>,int> > white;
    vector<pair<pair<int,int>,int> > black;
    pair<int,int> temp;
    for(int i=0;i<n;i++){
        cin>>temp.first>>temp.second;
        if(temp.first){
            black.push_back(make_pair(temp,i+1));
        }
        else{
            white.push_back(make_pair(temp,i+1));
        }
    }
    sort(black.begin(),black.end(),pred);
    sort(white.begin(),white.end(),pred);
    while(!black[black.size()-1].first.second || !white[white.size()-1].first.second){
        if(!black[black.size()-1].first.second && black.size()){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            black.pop_back();
        }
        else if(white.size()){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            white.pop_back();
        }

    }
    while(black.size() && white.size()){
        if(black[black.size()-1].first.second > white[white.size()-1].first.second){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            black[black.size()-1].first.second -= white[white.size()-1].first.second;
            white.pop_back();
            if(!black[black.size()-1].first.second) black.pop_back();
        }
        else {
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            white[white.size()-1].first.second-=black[black.size()-1].first.second;
            black.pop_back();
            if(!white[white.size()-1].first.second) white.pop_back();
        }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <queue>

using namespace std;

bool pred (pair<pair<int,int>,int> i,pair<pair<int,int>,int> j) { return (i>j); }
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    vector<pair<pair<int,int>,int> > white;
    vector<pair<pair<int,int>,int> > black;
    pair<int,int> temp;
    for(int i=0;i<n;i++){
        cin>>temp.first>>temp.second;
        if(temp.first){
            black.push_back(make_pair(temp,i+1));
        }
        else{
            white.push_back(make_pair(temp,i+1));
        }
    }

    //sort(black.begin(),black.end(),pred);
    //sort(white.begin(),white.end(),pred);

    int b=1;
    while(black.size() && white.size() && (!black[black.size()-1].first.second || !white[white.size()-1].first.second)){

        if(black.size() && white.size() && !black[black.size()-1].first.second && b==1){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            black.pop_back();
        }

        else if(black.size() && white.size() && b==-1){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            white.pop_back();
        }
        b*=-1;
    }
    while(black.size() && white.size()){
        if(black[black.size()-1].first.second > white[white.size()-1].first.second){
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<white[white.size()-1].first.second<<endl;
            black[black.size()-1].first.second -= white[white.size()-1].first.second;
            white.pop_back();
        }
        else {
            cout<<black[black.size()-1].second<<" "<<white[white.size()-1].second<<" "<<black[black.size()-1].first.second<<endl;
            white[white.size()-1].first.second-=black[black.size()-1].first.second;
            black.pop_back();
        }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <bitset>

using namespace std;

struct stru {
    int color;
    int value;
    int node;
    bool operator<(const stru& a) const {
        if (value != a.value)
            return value < a.value;
        return node < a.node;
    }
} a[110000];

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    multiset<stru> st[2];
    multiset<stru> all;
    for (int i = 0; i < n; i++) {
        cin >> a[i].color >> a[i].value;
        a[i].node = i;
        st[a[i].color].insert(a[i]);
        all.insert(a[i]);
    }
    for (int i = 1; i < n; i++) {
        stru b;
        if (st[0].size() > st[1].size())
            b = *st[0].begin();
        else
            b = *st[1].begin();
        all.erase(all.begin());
        st[b.color].erase(st[b.color].find(b));
        stru b2 = *(--st[1 - b.color].end());
        all.erase(all.find(b2));
        st[b2.color].erase(st[b2.color].find(b2));
        cout << b.node + 1 << " " << b2.node + 1 << " " << b.value << "\n";
        b2.value -= b.value;
        all.insert(b2);
        st[b2.color].insert(b2);
    }

    return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <bitset>

using namespace std;

struct stru {
    int color;
    int value;
    int node;
    bool operator<(const stru& a) const {
        if (value != a.value)
            return value < a.value;
        return node < a.node;
    }
} a[110000];

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    multiset<stru> st[2];
    multiset<stru> all;
    for (int i = 0; i < n; i++) {
        cin >> a[i].color >> a[i].value;
        a[i].node = i;
        st[a[i].color].insert(a[i]);
        all.insert(a[i]);
    }
    while (all.size() > 1) {
        stru b = *all.begin();
        all.erase(all.begin());
        st[b.color].erase(st[b.color].find(b));
        stru b2 = *(--st[1 - b.color].end());
        all.erase(all.find(b2));
        st[b2.color].erase(st[b2.color].find(b2));
        cout << b.node << " " << b2.node << " " << b.value << endl;
        b2.value -= b.value;
        all.insert(b2);
        st[b2.color].insert(b2);
    }

    return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <bitset>

using namespace std;

struct stru {
    int color;
    int value;
    int node;
    bool used;
    bool operator<(const stru& a) const {
        if (used != a.used)
            return !used;
        if (value != a.value)
            return value < a.value;
        return node < a.node;
    }
} a[110000];

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    multiset<stru> st[2];
    multiset<stru> all;
    for (int i = 0; i < n; i++) {
        cin >> a[i].color >> a[i].value;
        a[i].node = i;
        st[a[i].color].insert(a[i]);
        all.insert(a[i]);
    }
    for (int i = 1; i < n; i++) {
        stru b = *all.begin();
        all.erase(all.begin());
        st[b.color].erase(st[b.color].find(b));
        stru b2 = *(--st[1 - b.color].end());
        all.erase(all.find(b2));
        st[b2.color].erase(st[b2.color].find(b2));
        cout << b.node + 1 << " " << b2.node + 1 << " " << b.value << "\n";
        b.used = true;
        all.insert(b);
        st[b.color].insert(b);
        b2.value -= b.value;
        all.insert(b2);
        st[b2.color].insert(b2);
    }

    return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <bitset>

using namespace std;

struct stru {
    int color;
    int value;
    int node;
    bool operator<(const stru& a) const {
        if (value != a.value)
            return value < a.value;
        return node < a.node;
    }
} a[110000];

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    multiset<stru> st[2];
    multiset<stru> all;
    for (int i = 0; i < n; i++) {
        cin >> a[i].color >> a[i].value;
        a[i].node = i;
        st[a[i].color].insert(a[i]);
        all.insert(a[i]);
    }
    for (int i = 1; i < n; i++) {
        stru b;
        if (st[0].size() > st[1].size())
            b = *st[0].begin();
        else
            b = *st[1].begin();
        //all.erase(all.begin());
        st[b.color].erase(st[b.color].find(b));
        stru b2 = *(--st[1 - b.color].end());
        //all.erase(all.find(b2));
        st[b2.color].erase(st[b2.color].find(b2));
        cout << b.node + 1 << " " << b2.node + 1 << " " << b.value << "\n";
        b2.value -= b.value;
        //all.insert(b2);
        st[b2.color].insert(b2);
    }

    return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <bitset>

using namespace std;

struct stru {
    int color;
    int value;
    int node;
    bool operator<(const stru& a) const {
        if (value != a.value)
            return value < a.value;
        return node < a.node;
    }
} a[110000];

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    multiset<stru> st[2];
    multiset<stru> all;
    for (int i = 0; i < n; i++) {
        cin >> a[i].color >> a[i].value;
        a[i].node = i;
        st[a[i].color].insert(a[i]);
        all.insert(a[i]);
    }
    while (all.size() > 1) {
        stru b = *all.begin();
        all.erase(all.begin());
        st[b.color].erase(st[b.color].find(b));
        stru b2 = *(--st[1 - b.color].end());
        all.erase(all.find(b2));
        st[b2.color].erase(st[b2.color].find(b2));
        cout << b.node+1 << " " << b2.node+1 << " " << b.value << "\n";
        b2.value -= b.value;
        all.insert(b2);
        st[b2.color].insert(b2);
    }

    return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <bitset>

using namespace std;

struct stru {
    int color;
    int value;
    int node;
    bool operator<(const stru& a) const {
        if (value != a.value)
            return value < a.value;
        return node < a.node;
    }
} a[110000];

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    multiset<stru> st[2];
    multiset<stru> all;
    int node[2];
    for (int i = 0; i < n; i++) {
        cin >> a[i].color >> a[i].value;
        a[i].node = i;
        st[a[i].color].insert(a[i]);
        all.insert(a[i]);
        node[a[i].color] = i;
    }
    while (all.size() > 1) {
        stru b = *all.begin();
        all.erase(all.begin());
        st[b.color].erase(st[b.color].find(b));
        if (b.value == 0) {
            cout << node[1 - b.color] << " " << b.node << " " << 0 << "\n";
            continue;
        }
        stru b2 = *(--st[1 - b.color].end());
        all.erase(all.find(b2));
        st[b2.color].erase(st[b2.color].find(b2));
        cout << b.node+1 << " " << b2.node+1 << " " << b.value << "\n";
        b2.value -= b.value;
        all.insert(b2);
        st[b2.color].insert(b2);
    }

    return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <bitset>

using namespace std;

struct stru {
    int color;
    int value;
    int node;
    bool operator<(const stru& a) const {
        if (value != a.value)
            return value < a.value;
        return node < a.node;
    }
} a[110000];

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    multiset<stru> st[2];
    multiset<stru> all;
    int node[2];
    for (int i = 0; i < n; i++) {
        cin >> a[i].color >> a[i].value;
        a[i].node = i;
        st[a[i].color].insert(a[i]);
        all.insert(a[i]);
        node[a[i].color] = i;
    }
    while (all.size() > 1) {
        stru b = *all.begin();
        all.erase(all.begin());
        st[b.color].erase(st[b.color].find(b));
        if (b.value == 0) {
            cout << node[1 - b.color] << " " << b.node << " " << 0 << "\n"
            continue;
        }
        stru b2 = *(--st[1 - b.color].end());
        all.erase(all.find(b2));
        st[b2.color].erase(st[b2.color].find(b2));
        cout << b.node+1 << " " << b2.node+1 << " " << b.value << "\n";
        b2.value -= b.value;
        all.insert(b2);
        st[b2.color].insert(b2);
    }

    return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <bitset>

using namespace std;

struct stru {
    int color;
    int value;
    int node;
    bool operator<(const stru& a) const {
        if (value != a.value)
            return value < a.value;
        return node < a.node;
    }
} a[110000];

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    multiset<stru> st[2];
    multiset<stru> all;
    int node[2];
    for (int i = 0; i < n; i++) {
        cin >> a[i].color >> a[i].value;
        a[i].node = i;
        st[a[i].color].insert(a[i]);
        all.insert(a[i]);
        node[a[i].color] = i;
    }
    while (all.size() > 1) {
        stru b = *all.begin();
        all.erase(all.begin());
        st[b.color].erase(st[b.color].find(b));
        if (b.value == 0) {
            cout << node[1 - b.color]+1 << " " << b.node+1 << " " << 0 << "\n";
            continue;
        }
        stru b2 = *(--st[1 - b.color].end());
        all.erase(all.find(b2));
        st[b2.color].erase(st[b2.color].find(b2));
        cout << b.node+1 << " " << b2.node+1 << " " << b.value << "\n";
        b2.value -= b.value;
        all.insert(b2);
        st[b2.color].insert(b2);
    }

    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define X first
#define Y second
typedef pair<int, int> PII;

const int MAXN = 100010;

priority_queue<PII> que;
int sum[MAXN], col[MAXN], del[MAXN];
int cnt[2], ans, n;
int white, black;

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) scanf("%d%d", &col[i], &sum[i]), black += col[i];
    white = n - black;
    for(int i = 1; i <= n; ++i) que.push(make_pair(-sum[i], i));
    cnt[0] = cnt[1] = 1;
    while(!que.empty()) {
        if(white == 1 || black == 1) {
            int t = (black == 1);
            int &u = cnt[t];
            while(col[u] != t || del[u]) ++u;
            for(int v = cnt[t ^ 1]; v <= n; ++v)
                if(col[v] == (t ^ 1) && !del[v]) printf("%d %d %d\n", u, v, sum[v]);
            break;
        }

        int u = que.top().Y, t = -que.top().X; que.pop();
        if(t != sum[u] || del[u]) continue;
        int &v = cnt[col[u] ^ 1];
        while(col[v] != (col[u] ^ 1) || del[v]) ++v;
        printf("%d %d %d\n", u, v, t);
        del[u] = true;
        if(col[u] == 0) --white; else --black;
        sum[u] -= t, sum[v] -= t;
        que.push(make_pair(-sum[v], v));
        if(++ans == n - 1) break;
    }
    return 0;
}