#include<vector>
#include<queue>
#include<cstdio>
#define rep(i,n) for(int i=0;i<n;i++)
#define pb push_back
#define Queue priority_queue
#define ll long long
using namespace std;
struct node{
	int u;
	ll s;
	friend bool operator < (node a,node b){
		return a.s > b.s;
	}
};
const int Maxn=300000;
Queue<node> Q[2];
int n;
void Init(){
//	freopen("in.txt","r",stdin);
	scanf("%d",&n);
	rep(i,n){
		int c;
		node p;p.u=i;
		scanf("%d%I64d",&c,&p.s);
		Q[c].push(p);
	}

	/*
	rep(i,2){
		printf("--------\n");
		while(!Q[i].empty()){
			printf("%d %I64d\n",Q[i].top().u,Q[i].top().s);Q[i].pop();
		}
	}
	*/
}
void Work(){
	while((Q[0].size()+Q[1].size())>0){
		node p=Q[0].top(),q=Q[1].top();
		Q[0].pop();Q[1].pop();
		ll ss;
		if (p<q||Q[0].size()==0){
			ss=q.s;p.s-=q.s;Q[0].push(p);
			//printf("%d Q[0] %d Q[1] %d\n",q.u+1,Q[0].size(),Q[1].size());
		}else{
			ss=p.s;q.s-=p.s;Q[1].push(q);
			//printf("%d Q[0] %d Q[1] %d\n",p.u+1,Q[0].size(),Q[1].size());
		}
		if ((Q[0].size()+Q[1].size())>0) printf("%d %d %I64d\n",p.u+1,q.u+1,ss);
	}
}
int main(){
	Init();
	Work();
//	for(;;);
}


#include<vector>
#include<queue>
#include<cstdio>
#define rep(i,n) for(int i=0;i<n;i++)
#define pb push_back
#define Queue priority_queue
#define ll long long
using namespace std;
struct node{
	int u;
	ll s;
	friend bool operator < (node a,node b){
		return a.s > b.s;
	}
}B[2];
const int Maxn=300000;
Queue<node> Q[2];
int n;
void Init(){
//	freopen("in.txt","r",stdin);
	scanf("%d",&n);
	B[0].u=-1;
	rep(i,n){
		int c;
		node p;p.u=i;
		scanf("%d%I64d",&c,&p.s);
		Q[c].push(p);
		if (p.s) B[c]=p;
	}
//	printf("%d %d\n",B[0].u,B[1].u);
	/*
	rep(i,2){
		printf("--------\n");
		while(!Q[i].empty()){
			printf("%d %I64d\n",Q[i].top().u,Q[i].top().s);Q[i].pop();
		}
	}
	*/
}
void Work(){
	if (B[0].u>-1){
		while(Q[0].size()>1&&Q[0].top().s==0) {printf("%d %d 0\n",B[1].u+1,Q[0].top().u+1);Q[0].pop();}
		while(Q[1].size()>1&&Q[1].top().s==0) {printf("%d %d 0\n",B[0].u+1,Q[1].top().u+1);Q[1].pop();}
	}
	while((Q[0].size()+Q[1].size())>0){
		node p=Q[0].top(),q=Q[1].top();
		Q[0].pop();Q[1].pop();
		ll ss;
		if (p<q||Q[0].size()==0){
			ss=q.s;p.s-=ss;Q[0].push(p);
			//printf("%d Q[0] %d Q[1] %d\n",q.u+1,Q[0].size(),Q[1].size());
		}else{
			ss=p.s;q.s-=ss;Q[1].push(q);
			//printf("%d Q[0] %d Q[1] %d\n",p.u+1,Q[0].size(),Q[1].size());
		}
		if ((Q[0].size()+Q[1].size())>0) printf("%d %d %I64d\n",p.u+1,q.u+1,ss);
	}
}
int main(){
	Init();
	Work();
//	for(;;);
}


#include<iostream>
#include<vector>
#include<set>
#include<cmath>
#include<algorithm>
#include<fstream>
#include<string>
#include<queue>
#include<iomanip>
#include<cstring>
#include<cstdio>

using namespace std;

typedef pair<int,int> pie;
#define PB push_back
#define X first
#define Y second

int n,oneB,oneW,c;

set<pie> w,b;

int main(){
	ios_base::sync_with_stdio(false) ;
	cin>>n;
	for(int i=1;i<=n;i++){
		int x,y;
		cin>>x>>y;
		if(x)b.insert(pie(y,i));
		else w.insert(pie(y,i));

		if(x)oneB=i;
		else oneW=i;
	}
	while(b.size()){
		pie B=*(b.begin());
		if(B.X==0){
			b.erase(B);
			cout<<oneW<<" "<<B.Y<<" "<<0<<endl;
			c++;
			if(c==n-1)return false;
		}
	}
	while(w.size()){
		pie W=*(w.begin());
		if(W.X==0){
			w.erase(W);
			cout<<oneB<<" "<<W.Y<<" "<<0<<endl;
			c++;
			if(c==n-1)return false;
		}
	}

	while(b.size()+w.size()!=1){
		pie B=*(b.begin()),W=*(w.begin());

		if(B.X<W.X){
			b.erase(B);// !!
			w.erase(W);
			w.insert(pie(W.X-B.X,W.Y));
			cout<<B.Y<<" "<<W.Y<<" "<<B.X<<endl;
			c++;
			if(c==n-1)return false;
		}else{
			b.erase(B);// !!
			w.erase(W);
			b.insert(pie(B.X-W.X,B.Y));
			cout<<B.Y<<" "<<W.Y<<" "<<W.X<<endl;
			c++;
			if(c==n-1)return false;
		}
	}
}


#include<iostream>
#include<vector>
#include<set>
#include<cmath>
#include<algorithm>
#include<fstream>
#include<string>
#include<queue>
#include<iomanip>
#include<cstring>
#include<cstdio>

using namespace std;

typedef pair<int,int> pie;
#define PB push_back
#define X first
#define Y second

int n,oneB,oneW,c;

set<pie> w,b;

int main(){
	ios_base::sync_with_stdio(false) ;
	cin>>n;
	for(int i=1;i<=n;i++){
		int x,y;
		cin>>x>>y;
		if(x)b.insert(pie(y,i));
		else w.insert(pie(y,i));

		if(x)oneB=i;
		else oneW=i;
	}
	while(b.size()){
		pie B=*(b.begin());
		if(B.X==0){
			b.erase(B);
			cout<<oneW<<" "<<B.Y<<" "<<0<<endl;
			c++;
			if(c==n-1)return false;
		}else break;
	}
	while(w.size()){
		pie W=*(w.begin());
		if(W.X==0){
			w.erase(W);
			cout<<oneB<<" "<<W.Y<<" "<<0<<endl;
			c++;
			if(c==n-1)return false;
		}else break;
	}

	while(b.size()+w.size()!=1){
		pie B=*(b.begin()),W=*(w.begin());

		if(B.X<W.X){
			b.erase(B);// !!
			w.erase(W);
			w.insert(pie(W.X-B.X,W.Y));
			cout<<B.Y<<" "<<W.Y<<" "<<B.X<<endl;
			c++;
			if(c==n-1)return false;
		}else{
			b.erase(B);// !!
			w.erase(W);
			b.insert(pie(B.X-W.X,B.Y));
			cout<<B.Y<<" "<<W.Y<<" "<<W.X<<endl;
			c++;
			if(c==n-1)return false;
		}
	}
}


#include<iostream>
#include<vector>
#include<set>
#include<cmath>
#include<algorithm>
#include<fstream>
#include<string>
#include<queue>
#include<iomanip>
#include<cstring>
#include<cstdio>

using namespace std;

typedef pair<int,int> pie;
#define PB push_back
#define X first
#define Y second

int n;

set<pie> w,b;

int main(){
	ios_base::sync_with_stdio(false) ;
	cin>>n;
	for(int i=1;i<=n;i++){
		int x,y;
		cin>>x>>y;
		if(x)b.insert(pie(y,i));
		else w.insert(pie(y,i));
	}
	while(b.size()+w.size()!=1){
		pie B=*(b.begin()),W=*(w.begin());
		if(B.X<W.X){
			b.erase(B);// !!
			w.erase(W);
			w.insert(pie(W.X-B.X,W.Y));
			cout<<B.Y<<" "<<W.Y<<" "<<W.X-B.X<<endl;
		}else{
			b.erase(B);// !!
			w.erase(W);
			b.insert(pie(B.X-W.X,B.Y));
			cout<<B.Y<<" "<<W.Y<<" "<<B.X-W.X<<endl;
		}
	}
}


#include<iostream>
#include<vector>
#include<set>
#include<cmath>
#include<algorithm>
#include<fstream>
#include<string>
#include<queue>
#include<iomanip>
#include<cstring>
#include<cstdio>

using namespace std;

typedef pair<int,int> pie;
#define PB push_back
#define X first
#define Y second

int n;

set<pie> w,b;

int main(){
	ios_base::sync_with_stdio(false) ;
	cin>>n;
	for(int i=1;i<=n;i++){
		int x,y;
		cin>>x>>y;
		if(x)b.insert(pie(y,i));
		else w.insert(pie(y,i));
	}
	while(b.size()+w.size()!=1){
		pie B=*(b.begin()),W=*(w.begin());
		if(B.X<W.X){
			b.erase(B);// !!
			w.erase(W);
			w.insert(pie(W.X-B.X,W.Y));
			cout<<B.Y<<" "<<W.Y<<" "<<B.X<<endl;
		}else{
			b.erase(B);// !!
			w.erase(W);
			b.insert(pie(B.X-W.X,B.Y));
			cout<<B.Y<<" "<<W.Y<<" "<<W.X<<endl;
		}
	}
}


#include<iostream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<fstream>
#define MAX 100001
using namespace std;
typedef long long ll;
vector<int>v[2];
int n,k=1,w[MAX],ans[MAX][3],col[MAX];

int ali()
{
   int wh=v[1].size()-1,bl=v[0].size()-1,x ;
    x=min(w[v[1][wh]],w[v[0][bl]] );
   if(v[0].empty())
       for(int i=1;i<=n;i++)
            if(col[i]==0)
            {
                ans[k][0]=v[1][wh];
                ans[k][1]=i;
                ans[k][2]=0;
                k++;
                return 0;
            }
   else if(v[1].empty())
    for(int i=1;i<=n;i++)
                if(col[i]==1)
                {
                    ans[k][0]=v[0][bl];
                    ans[k][1]=i;
                    ans[k][2]=0;
                    k++;
                    return 0;
                }
    if(w[v[0][bl]]==x)
    {
        w[v[1][wh]]-=w[v[0][bl]];
        ans[k][0]=v[0][bl];
        ans[k][1]=v[1][wh];
        ans[k][2]=x;
        v[0].pop_back();

    }
    else
    {
        w[v[0][bl]]-=w[v[1][wh]];
        ans[k][0]=v[0][bl];
        ans[k][1]=v[1][wh];
        ans[k][2]=x;
        v[1].pop_back();

    }
    k++;
    return 0;
}
int main()
{

    cin>>n;
    for(int i=1;i<=n;i++)
     {
        cin>>col[i]>>w[i];
         v[col[i]].push_back(i);
     }
    for(int i=1;i<=n-1;i++)
        ali();

    for(int i=1;i<k;i++)
        cout<<ans[i][0]<<" "<<ans[i][1]<<" "<<ans[i][2]<<endl;
}


#include<iostream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<fstream>
#define MAX 100001
using namespace std;
typedef long long ll;
int n,k=1,w[MAX],col[MAX],ans[MAX][3],mark[MAX]={0};

int ali()
{
    ll mi=1000000001,t;
    for(int i=1;i<=n;i++)
        if( mark[i]==0 && w[i]<mi )
        {
            mi=w[i];
            t=i;
        }
    for(int i=1;i<=n;i++)
        if(mark[i]==0 && col[i]!=col[t] )
          {
            mark[t]=1;
            w[i]-=w[t];
            ans[k][0]=i;
            ans[k][1]=t;
            ans[k][2]=w[t];
            k++;
            break;
          }

}
int main()
{

    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>col[i]>>w[i];
    for(int i=1;i<=n-1;i++)
        ali();

    for(int i=1;i<=n-1;i++)
        cout<<ans[i][0]<<" "<<ans[i][1]<<" "<<ans[i][2]<<endl;
}


#include<iostream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<fstream>
#define MAX 100001
using namespace std;
typedef long long ll;
vector<int>v[2];
int n,k=1,w[MAX],ans[MAX][3],col[MAX];

int ali()
{

   if(v[0].size()==0)
     {  for(int i=1;i<=n;i++)
            if(col[i]==0)
            {
                ans[k][0]=v[1][0];
                ans[k][1]=i;
                ans[k][2]=0;
                k++;
                v[1].pop_back();
                return 0;
            }
      }
   if(v[1].size()==0)
   { for(int i=1;i<=n;i++)
                if(col[i]==1)
                {
                    ans[k][0]=v[0][0];
                    ans[k][1]=i;
                    ans[k][2]=0;
                    k++;
                    v[0].pop_back();
                    return 0;
                }
    }
    int wh=v[1].size()-1,bl=v[0].size()-1,x ;
        x=min(w[v[1][wh]],w[v[0][bl]] );
    if(w[v[0][bl]]==x)
    {
        w[v[1][wh]]-=w[v[0][bl]];
        ans[k][0]=v[0][bl];
        ans[k][1]=v[1][wh];
        ans[k][2]=x;
        v[0].pop_back();

    }
    else
    {
        w[v[0][bl]]-=w[v[1][wh]];
        ans[k][0]=v[0][bl];
        ans[k][1]=v[1][wh];
        ans[k][2]=x;
        v[1].pop_back();

    }
    k++;
    return 0;
}
int main()
{

    cin>>n;
    for(int i=1;i<=n;i++)
     {
        cin>>col[i]>>w[i];
         v[col[i]].push_back(i);
     }
    for(int i=1;i<=n-1;i++)
        ali();

    for(int i=1;i<k;i++)
        cout<<ans[i][0]<<" "<<ans[i][1]<<" "<<ans[i][2]<<endl;
}


#include<iostream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<fstream>
#define MAX 100001
using namespace std;
typedef long long ll;
vector<int>v[2];
int n,k=1,w[MAX],ans[MAX][3],col[MAX];

int ali()
{

   if(v[0].size()==0)
     {  for(int i=1;i<=n;i++)
            if(col[i]==0)
            {
                v[1].pop_back();
                int t=v[1].size()-1;
                ans[k][0]=v[1][t];
                ans[k][1]=i;
                ans[k][2]=0;
                k++;

                return 0;
            }
      }
   if(v[1].size()==0)
   { for(int i=1;i<=n;i++)
                if(col[i]==1)
                {
                    v[0].pop_back();
                    int t=v[0].size()-1;
                    ans[k][0]=v[0][t];
                    ans[k][1]=i;
                    ans[k][2]=0;
                    k++;

                    return 0;
                }
    }
    int wh=v[1].size()-1,bl=v[0].size()-1,x ;
        x=min(w[v[1][wh]],w[v[0][bl]] );
    if(w[v[0][bl]]==x)
    {
        w[v[1][wh]]-=w[v[0][bl]];
        ans[k][0]=v[0][bl];
        ans[k][1]=v[1][wh];
        ans[k][2]=x;
        v[0].pop_back();

    }
    else
    {
        w[v[0][bl]]-=w[v[1][wh]];
        ans[k][0]=v[0][bl];
        ans[k][1]=v[1][wh];
        ans[k][2]=x;
        v[1].pop_back();

    }
    k++;
    return 0;
}
int main()
{

    cin>>n;
    for(int i=1;i<=n;i++)
     {
        cin>>col[i]>>w[i];
         v[col[i]].push_back(i);
     }
    for(int i=1;i<=n-1;i++)
        ali();

    for(int i=1;i<k;i++)
        cout<<ans[i][0]<<" "<<ans[i][1]<<" "<<ans[i][2]<<endl;
}


#include<cstdio>
#include<list>

#define MAXX  100111

std::list<std::pair<int,int> >pnt[2];
int i,j,k,n;

int main()
{
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        scanf("%d %d",&j,&k);
        pnt[j].push_back(std::make_pair(k,i));
    }
    pnt[0].sort();
    pnt[1].sort();
    while(!pnt[0].empty())
    {
        j=std::min(pnt[0].front().first,pnt[1].front().first);
        pnt[0].front().first-=j;
        pnt[1].front().first-=j;
        printf("%d %d %d\n",pnt[0].front().second,pnt[1].front().second,j);
        if(pnt[1].front().first==0 && pnt[1].size()>1)
            pnt[1].pop_front();
        else
            pnt[0].pop_front();
    }
    return 0;
}


#include<cstdio>
#include<list>

#define MAXX  100111

std::list<std::pair<int,int> >pnt[2];
int i,j,k,n;

int main()
{
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        scanf("%d %d",&j,&k);
        pnt[j].push_back(std::make_pair(k,i));
    }
    pnt[0].sort();
    pnt[1].sort();
    while(!pnt[0].empty() && !pnt[1].empty())
    {

        j=std::min(pnt[0].front().first,pnt[1].front().first);
        pnt[0].front().first-=j;
        pnt[1].front().first-=j;
        printf("%d %d %d\n",pnt[0].front().second,pnt[1].front().second,j);
        if(pnt[1].front().first==0 && pnt[0].front().first==0)
            if(pnt[1].size()>pnt[0].size())
                pnt[1].pop_front();
            else
                pnt[0].pop_front();
        else
            if(pnt[1].front().first==0)
                pnt[1].pop_front();
            else
                pnt[0].pop_front();
    }
    return 0;
}


#include<cstdio>
#include<list>

#define MAXX  100111

std::list<std::pair<int,int> >pnt[2];
int i,j,k,n;

int main()
{
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        scanf("%d %d",&j,&k);
        pnt[j].push_back(std::make_pair(k,i));
    }
    pnt[0].sort();
    pnt[1].sort();
    while(!pnt[0].empty() && !pnt[1].empty())
    {
        j=std::min(pnt[0].front().first,pnt[1].front().first);
        pnt[0].front().first-=j;
        pnt[1].front().first-=j;
        printf("%d %d %d\n",pnt[0].front().second,pnt[1].front().second,j);
        if(pnt[1].front().first==pnt[0].front().first)
            if(pnt[1].size()==1 && pnt[0].size()==1)
                pnt[1].pop_front(),pnt[0].pop_front();
            else
           if(pnt[1].size()>1)
           //     if(pnt[1].size()>pnt[0].size())
                    pnt[1].pop_front();
                else
                    pnt[0].pop_front();
        /*
           if(pnt[1].size()>1)
           pnt[1].pop_front();
           else
           if(pnt[0].size()>1)
           pnt[0].pop_front();
           else
           pnt[1].pop_front(),pnt[0].pop_front();
           */
        else
            if(pnt[1].front().first==0)
                pnt[1].pop_front();
            else
                pnt[0].pop_front();
    }
    return 0;
}


#include<cstdio>
#include<list>

#define MAXX  100111

std::list<std::pair<int,int> >pnt[2];
int i,j,k,n;

int main()
{
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        scanf("%d %d",&j,&k);
        pnt[j].push_back(std::make_pair(k,i));
    }
    pnt[0].sort();
    pnt[1].sort();
    while(!pnt[0].empty() && !pnt[1].empty())
    {
        j=std::min(pnt[0].front().first,pnt[1].front().first);
        pnt[0].front().first-=j;
        pnt[1].front().first-=j;
        printf("%d %d %d\n",pnt[0].front().second,pnt[1].front().second,j);
        if(pnt[1].front().first==pnt[0].front().first)
            if(pnt[1].size()==1 && pnt[0].size()==1)
                pnt[1].pop_front(),pnt[0].pop_front();
            else
                if(pnt[1].size()>pnt[0].size())
                    pnt[1].pop_front();
                else
                    pnt[0].pop_front();
        /*
           if(pnt[1].size()>1)
           pnt[1].pop_front();
           else
           if(pnt[0].size()>1)
           pnt[0].pop_front();
           else
           pnt[1].pop_front(),pnt[0].pop_front();
           */
        else
            if(pnt[1].front().first==0)
                pnt[1].pop_front();
            else
                pnt[0].pop_front();
    }
    return 0;
}


#include<cstdio>
#include<list>

#define MAXX  100111

std::list<std::pair<int,int> >pnt[2];
int i,j,k,n;

int main()
{
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        scanf("%d %d",&j,&k);
        pnt[j].push_back(std::make_pair(k,i));
    }
    pnt[0].sort();
    pnt[1].sort();
    while(!pnt[0].empty() && !pnt[1].empty())
    {
        j=std::min(pnt[0].front().first,pnt[1].front().first);
        pnt[0].front().first-=j;
        pnt[1].front().first-=j;
        printf("%d %d %d\n",pnt[0].front().second,pnt[1].front().second,j);
        if(pnt[1].front().first==0 && pnt[1].size()>1)
            pnt[1].pop_front();
        else
            pnt[0].pop_front();
    }
    return 0;
}


#include<cstdio>
#include<list>

#define MAXX  100111

std::list<std::pair<int,int> >pnt[2];
int i,j,k,n;

int main()
{
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        scanf("%d %d",&j,&k);
        pnt[j].push_back(std::make_pair(k,i));
    }
    pnt[0].sort();
    pnt[1].sort();
    while(!pnt[0].empty() && !pnt[1].empty())
    {
        j=std::min(pnt[0].front().first,pnt[1].front().first);
        pnt[0].front().first-=j;
        pnt[1].front().first-=j;
        printf("%d %d %d\n",pnt[0].front().second,pnt[1].front().second,j);
        if(pnt[1].front().first==pnt[0].front().first)
            if(pnt[1].size()<=1 && pnt[0].size()<=1)
                pnt[1].pop_front(),pnt[0].pop_front();
            else
                if(pnt[1].size()>1)
                    //     if(pnt[1].size()>pnt[0].size())
                    pnt[1].pop_front();
                else
                    pnt[0].pop_front();
        /*
           if(pnt[1].size()>1)
           pnt[1].pop_front();
           else
           if(pnt[0].size()>1)
           pnt[0].pop_front();
           else
           pnt[1].pop_front(),pnt[0].pop_front();
           */
        else
            if(pnt[1].front().first==0)
                pnt[1].pop_front();
            else
                pnt[0].pop_front();
    }
    return 0;
}


#include<cstdio>
#include<list>

#define MAXX  100111

std::list<std::pair<int,int> >pnt[2];
int i,j,k,n;

int main()
{
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        scanf("%d %d",&j,&k);
        pnt[j].push_back(std::make_pair(k,i));
    }
    pnt[0].sort();
    pnt[1].sort();
    while(!pnt[0].empty() && !pnt[1].empty())
    {
        j=std::min(pnt[0].front().first,pnt[1].front().first);
        pnt[0].front().first-=j;
        pnt[1].front().first-=j;
        printf("%d %d %d\n",pnt[0].front().second,pnt[1].front().second,j);
        if(pnt[1].front().first==pnt[0].front().first)
            if(pnt[1].size()>pnt[0].size())
                pnt[1].pop_front();
            else
                pnt[0].pop_front();
            /*
            if(pnt[1].size()>1)
                pnt[1].pop_front();
            else
                if(pnt[0].size()>1)
                    pnt[0].pop_front();
                else
                    pnt[1].pop_front(),pnt[0].pop_front();
                    */
        else
            if(pnt[1].front().first==0)
                pnt[1].pop_front();
            else
                pnt[0].pop_front();
    }
    return 0;
}


#include<cstdio>
#include<list>

#define MAXX  100111

std::list<std::pair<int,int> >pnt[2];
int i,j,k,n;

int main()
{
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        scanf("%d %d",&j,&k);
        pnt[j].push_back(std::make_pair(k,i));
    }
    pnt[0].sort();
    pnt[1].sort();
    while(!pnt[0].empty() && !pnt[1].empty())
    {
        j=std::min(pnt[0].front().first,pnt[1].front().first);
        pnt[0].front().first-=j;
        pnt[1].front().first-=j;
        printf("%d %d %d\n",pnt[0].front().second,pnt[1].front().second,j);
        if(pnt[1].front().first==0)
            pnt[1].pop_front();
        else
            pnt[0].pop_front();
    }
    return 0;
}


#include<cstdio>
#include<list>

#define MAXX  100111

std::list<std::pair<int,int> >pnt[2];
int i,j,k,n;

int main()
{
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        scanf("%d %d",&j,&k);
        pnt[j].push_back(std::make_pair(k,i));
    }
    pnt[0].sort();
    pnt[1].sort();
    while(!pnt[0].empty() && !pnt[1].empty())
    {
        j=std::min(pnt[0].front().first,pnt[1].front().first);
        pnt[0].front().first-=j;
        pnt[1].front().first-=j;
        printf("%d %d %d\n",pnt[0].front().second,pnt[1].front().second,j);
        if(pnt[1].front().first==pnt[0].front().first)
            if(pnt[1].size()>1)
                pnt[1].pop_front();
            else
                if(pnt[0].size()>1)
                    pnt[0].pop_front();
                else
                    pnt[1].pop_front(),pnt[0].pop_front();
        else
            if(pnt[1].front().first==0)
                pnt[1].pop_front();
            else
                pnt[0].pop_front();
    }
    return 0;
}


#include<cstdio>
#include<list>

#define MAXX  100111

std::list<std::pair<int,int> >pnt[2];
int i,j,k,n;

int main()
{
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        scanf("%d %d",&j,&k);
        pnt[j].push_back(std::make_pair(k,i));
    }
    pnt[0].sort();
    pnt[1].sort();
    while(!pnt[0].empty() && !pnt[1].empty())
    {
        j=std::min(pnt[0].front().first,pnt[1].front().first);
        pnt[0].front().first-=j;
        pnt[1].front().first-=j;
        printf("%d %d %d\n",pnt[0].front().second,pnt[1].front().second,j);
        if(pnt[1].front().first==0 && pnt[0].front().first==0)
            if(pnt[1].size()>pnt[0].size())
                pnt[1].pop_front();
            else
                pnt[0].pop_front();
        else
            if(pnt[1].front().first==0)
                pnt[1].pop_front();
            else
                pnt[0].pop_front();
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <sstream>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

#define pb push_back
#define MP make_pair
#define F first
#define S second
#define ll long long
#define PII pair<int,int>

set< pair<ll,int> > A,B;
set< pair<ll,int> >::iterator it;
int n;

int main() {
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    scanf("%d",&n);
    for(int i=1;i<=n;++i) {
        ll c,s; scanf("%I64d %I64d",&c,&s);
        if(c) A.insert(MP(s,i));
        else B.insert(MP(s,i));
    }

    while( true ) {
        if(A.size() && B.size()) {
            pair<ll,int> a1 = *A.begin();
            pair<ll,int> b1 = *B.begin();
            pair<ll,int> a2;
            pair<ll,int> b2;
            it = A.end(), --it;
            a2 = (*it);
            it = B.end(), --it;
            b2 = (*it);

            if(a1.first <= b1.first) {
                printf("%d %d %I64d\n",a1.second,b2.second,a1.first);
                A.erase(a1), B.erase(b2);
                B.insert(MP((b2.first-a1.first),b2.second));
            }
            else {
                printf("%d %d %I64d\n",a2.second,b1.second,b1.first);
                A.erase(a2), B.erase(b1);
                A.insert(MP((a2.first-b1.first),a2.second));
            }
        }
        else break;
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <sstream>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

#define pb push_back
#define MP make_pair
#define F first
#define S second
#define ll long long
#define PII pair<int,int>

priority_queue<PII> A,B;
int n;

int main() {
#ifdef LOCAL
	freopen("in.txt","r",stdin);
#endif
	scanf("%d",&n);
	for(int i=1;i<=n;++i) {
		int c,s; scanf("%d%d",&c,&s);
		if(c) A.push(MP(-s,i));
		else B.push(MP(-s,i));
	}

	while( true ) {
		if(A.size() && B.size()) {
			PII a = A.top(); A.pop();
			PII b = B.top(); B.pop();
			a.first *= -1, b.first *= -1;
			if(a.first >= b.first) {
				printf("%d %d %d\n",a.second,b.second,b.first);
				A.push(MP(-(a.first-b.first),a.second));
			}
			else {
				printf("%d %d %d\n",a.second,b.second,a.first);
				B.push(MP(-(b.first-a.first),b.second));
			}
		}
		else break;
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <sstream>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

#define pb push_back
#define MP make_pair
#define F first
#define S second
#define ll long long
#define PII pair<int,int>

priority_queue<PII> A,B;
int n;

int main() {
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    scanf("%d",&n);
    for(int i=1;i<=n;++i) {
        int c,s; scanf("%d%d",&c,&s);
        if(c) A.push(MP(-s,i));
        else B.push(MP(-s,i));
    }

    while( true ) {
        if(A.size() && B.size()) {
            PII a = A.top(); A.pop();
            PII b = B.top(); B.pop();
            a.first *= -1, b.first *= -1;
            if(a.first > b.first) {
                printf("%d %d %d\n",a.second,b.second,b.first);
                A.push(MP(-(a.first-b.first),a.second));
            }
            else if(a.first < b.first) {
                printf("%d %d %d\n",a.second,b.second,a.first);
                B.push(MP(-(b.first-a.first),b.second));
            }
            else {
                if(A.size() > B.size()) {
                    printf("%d %d %d\n",a.second,b.second,a.first);
                    B.push(MP(-(b.first-a.first),b.second));
                }
                else {
                    printf("%d %d %d\n",a.second,b.second,b.first);
                    A.push(MP(-(a.first-b.first),a.second));
                }
            }
        }
        else break;
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <sstream>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

#define pb push_back
#define MP make_pair
#define F first
#define S second
#define ll long long
#define PII pair<int,int>

priority_queue< pair<ll,int> > A,B;
int n;

int main() {
#ifdef LOCAL
	freopen("in.txt","r",stdin);
#endif
	scanf("%d",&n);
	for(int i=1;i<=n;++i) {
		ll c,s; scanf("%I64d %I64d",&c,&s);
		if(c) A.push(MP(-s,i));
		else B.push(MP(-s,i));
	}

	while( true ) {
		if(A.size() && B.size()) {
			pair<ll,int> a = A.top(); A.pop();
			pair<ll,int> b = B.top(); B.pop();
			a.first *= -1, b.first *= -1;
			if(a.first >= b.first) {
				printf("%d %d %I64d\n",a.second,b.second,b.first);
				A.push(MP(-(a.first-b.first),a.second));
			}
			else {
				printf("%d %d %I64d\n",a.second,b.second,a.first);
				B.push(MP(-(b.first-a.first),b.second));
			}
		}
		else break;
	}

	return 0;
}


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

using namespace std;

typedef long long int ll;

int main()
{

    ll i,j,k,x,y,c,n;queue< pair<ll,ll> > q[10];bool p[100100];
    cin >> n ;
    for(i=1;i<=n;i++){
      cin >> j >> k ;
      if(!k) p[i]=1;
      else p[i]=0;
      q[j].push(make_pair(i,k));
    }
    c=1;
    while(!q[0].empty()&&!q[1].empty()){
       x=q[c].front().first,y=q[c].front().second;
       while(y>0||p[x]){
          j=q[1-c].front().first,k=q[1-c].front().second,i=min(k,y);
          cout<<x<<" "<<j<<" "<<i<<endl;
          p[x]=0,p[j]=0,y-=i,q[1-c].front().second-=i;if(y!=0) {q[1-c].pop();}
       }
       if(!q[c].empty())q[c].pop(); c=1-c;
    }
    if(!q[0].empty()) c=0;else c=1;
    while(!q[c].empty()){
      i=q[c].front().first;
      if(p[i])cout<<x<<" "<<i<<" 0"<<endl;
      q[c].pop();
    }
   // system("pause");
    return 0;
}


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

using namespace std;

typedef long long int ll;

int main()
{
    ll i,j,k,x,y,c,b,w,n;queue< pair<ll,ll> > q[10];bool p[100100];
    cin >> n ;
    for(i=1;i<=n;i++){
      cin >> j >> k ;
      if(!k) p[i]=1;
      else{
        p[i]=0;
        if(j) b=i;
        else w=i;
      }
      q[j].push(make_pair(i,k));
    }
    c=1;
    while(!q[0].empty()&&!q[1].empty()){
       x=q[c].front().first,y=q[c].front().second;
       if(p[x]&&c) cout<<w<<" "<<x<<" 0"<<endl;
       if(p[x]&&!c) cout<<b<<" "<<x<<" 0"<<endl;
       while(y>0){
          j=q[1-c].front().first,k=q[1-c].front().second,i=min(k,y);
          if(p[j]){
            if(c) cout<<b<<" "<<j<<" 0"<<endl;
            else cout<<w<<" "<<j<<" 0"<<endl;
            q[1-c].pop();
            continue;
          }
          cout<<x<<" "<<j<<" "<<i<<endl;
          p[x]=0,p[j]=0,y-=i,q[1-c].front().second-=i;if(y!=0) {q[1-c].pop();}
       }
       if(!q[c].empty())q[c].pop(); c=1-c;
    }
    if(!q[0].empty()) c=0;else c=1;
    if(c) x=w;else x=b;
    while(!q[c].empty()){
      i=q[c].front().first;
      if(p[i])cout<<x<<" "<<i<<" 0"<<endl;
      q[c].pop();
    }
    //system("pause");
    return 0;
}


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

using namespace std;

typedef long long int ll;

int main()
{
    ll i,j,k,x,y,c,b=-1,w=-1,n;queue< pair<ll,ll> > q[10];bool p[100100];
    cin >> n ;
    for(i=1;i<=n;i++){
      cin >> j >> k ;
      if(!k) p[i]=1;
      else{
        p[i]=0;
        if(j) b=i;
        else w=i;
      }
      q[j].push(make_pair(i,k));
    }
    c=1;if(w==-1) w=q[0].front().first,b=q[1].front().second;
    while(!q[0].empty()&&!q[1].empty()){
       x=q[c].front().first,y=q[c].front().second;
       if(p[x]&&c) cout<<w<<" "<<x<<" 0"<<endl;
       if(p[x]&&!c) cout<<b<<" "<<x<<" 0"<<endl;
       while(y>0){
          j=q[1-c].front().first,k=q[1-c].front().second,i=min(k,y);
          if(p[j]){
            if(c) cout<<b<<" "<<j<<" 0"<<endl;
            else cout<<w<<" "<<j<<" 0"<<endl;
            q[1-c].pop();
            continue;
          }
          cout<<x<<" "<<j<<" "<<i<<endl;
          p[x]=0,p[j]=0,y-=i,q[1-c].front().second-=i;if(y!=0) {q[1-c].pop();}
       }
       if(!q[c].empty())q[c].pop(); c=1-c;
    }
    if(!q[0].empty()) c=0;else c=1;
    if(c) x=w;else x=b;
    while(!q[c].empty()){
      i=q[c].front().first;
      if(p[i])cout<<x<<" "<<i<<" 0"<<endl;
      q[c].pop();
    }
    //system("pause");
    return 0;
}


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

using namespace std;

typedef long long int ll;

int main()
{

    ll i,j,k,x,y,c,n;queue< pair<ll,ll> > q[2];bool p[100100];
    cin >> n ;
    for(i=1;i<=n;i++){
      cin >> j >> k ;
      if(!k) p[i]=1;
      else p[i]=0;
      q[j].push(make_pair(i,k));
    }
    c=1;
    while(!q[0].empty()&&!q[1].empty()){
       x=q[c].front().first,y=q[c].front().second;
       while(y>0||!q[c].front().second){
          j=q[1-c].front().first,k=q[1-c].front().second,i=min(k,y);
          cout<<x<<" "<<j<<" "<<i<<endl;
          q[c].front().second=1,y-=i,q[1-c].front().second-=i;if(y!=0) {q[1-c].pop();}
       }
       if(!q[c].empty())q[c].pop(); c=1-c;
    }
    if(!q[0].empty()) c=0;else c=1;
    while(!q[c].empty()){
      i=q[c].front().first;
      if(p[i])cout<<x<<" "<<i<<" 0"<<endl;
      q[c].pop();
    }
    //system("pause");
    return 0;
}


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

using namespace std;

typedef long long int ll;

int main()
{
    ll i,j,k,c,x,y,N;queue< pair<ll,ll> > q[4];bool flag;
    cin>>N;
    for(i=1;i<=N;i++) {cin>>j>>k;q[j].push(make_pair(i,k));}
    c=1,x=q[1].front().first,y=q[1].front().second;q[1].pop();
    while(!q[0].empty()||!q[1].empty()){
      //cout<<x<<" "<<y<<endl;
      //system("pause");
      if(!y&&!q[1-c].empty()) flag=1;
      while(y>0||flag){
         j=q[1-c].front().first,k=q[1-c].front().second,i=min(k,y);
         cout<<x<<" "<<j<<" "<<i<<endl;
         y-=i,k-=i,flag=0;q[1-c].pop();
      }
      if(!q[c].empty()) x=j,y=k,c=1-c;
    }
    while(!q[1-c].empty()) {cout<<q[1-c].front().first<<" "<<x<<" 0"<<endl;q[1-c].pop();}
    //system("pause");
    return 0;
}


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

using namespace std;

typedef long long int ll;

int main()
{
    ll i,j,k,x,y,c,b=-1,w=-1,n;queue< pair<ll,ll> > q[10];bool p[100100];
    cin >> n ;
    for(i=1;i<=n;i++){
      cin >> j >> k ;
      if(!k) p[i]=1;
      else{
        p[i]=0;
        if(j) b=i;
        else w=i;
      }
      q[j].push(make_pair(i,k));
    }
    c=1;if(w==-1) {w=q[0].front().first,b=q[1].front().second,p[0]=1;}
    while(!q[0].empty()&&!q[1].empty()){
       x=q[c].front().first,y=q[c].front().second;
       if(p[x]&&c) {if(p[0]) p[w]=0;cout<<w<<" "<<x<<" 0"<<endl;p[x]=0;}
       if(p[x]&&!c) {if(p[0]) p[b]=0;cout<<b<<" "<<x<<" 0"<<endl;p[x]=0;}
       while(y>0){
          j=q[1-c].front().first,k=q[1-c].front().second,i=min(k,y);
          if(p[j]){
            if(c) {p[j]=0;cout<<b<<" "<<j<<" 0"<<endl;}
            else cout<<w<<" "<<j<<" 0"<<endl;
            q[1-c].pop();
            continue;
          }
          cout<<x<<" "<<j<<" "<<i<<endl;
          p[x]=0,p[j]=0,y-=i,q[1-c].front().second-=i;if(y!=0) {q[1-c].pop();}
       }
       if(!q[c].empty())q[c].pop(); c=1-c;
    }
    if(!q[0].empty()) c=0;else c=1;
    if(c) x=w;else x=b;
    while(!q[c].empty()){
      i=q[c].front().first;
      if(p[i])cout<<x<<" "<<i<<" 0"<<endl;
      q[c].pop();
    }
    //system("pause");
    return 0;
}


#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long int ll;
int main()
{
    ll i,j,k,c,x,y,N;queue< pair<ll,ll> > q[4];bool flag;
    cin>>N;
    for(i=1;i<=N;i++) {cin>>j>>k;q[j].push(make_pair(i,k));}
    c=1,x=q[1].front().first,y=q[1].front().second;q[1].pop();
    while(!q[0].empty()||!q[1].empty()){
      if(!y&&!q[1-c].empty()) flag=1;
      while(y>0||flag){
         j=q[1-c].front().first,k=q[1-c].front().second,i=min(k,y);
         cout<<x<<" "<<j<<" "<<i<<endl;
         y-=i,k-=i,flag=0;q[1-c].pop();
      }
      if(!q[c].empty()) x=j,y=k,c=1-c;
    }
    while(!q[1-c].empty()) {cout<<q[1-c].front().first<<" "<<x<<" 0"<<endl;q[1-c].pop();}
    return 0;
}


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

using namespace std;

typedef long long int ll;

int main()
{
    ll i,j,k,x,y,c,b,w,n;queue< pair<ll,ll> > q[10];bool p[100100];
    cin >> n ;
    for(i=1;i<=n;i++){
      cin >> j >> k ;
      if(!k) p[i]=1;
      else{
        p[i]=0;
        if(j) b=i;
        else w=i;
      }
      q[j].push(make_pair(i,k));
    }
    c=1;
    while(!q[0].empty()&&!q[1].empty()){
       x=q[c].front().first,y=q[c].front().second;
       while(y>0||p[x]){
          j=q[1-c].front().first,k=q[1-c].front().second,i=min(k,y);
          cout<<x<<" "<<j<<" "<<i<<endl;
          p[x]=0,p[j]=0,y-=i,q[1-c].front().second-=i;if(y!=0) {q[1-c].pop();}
       }
       if(!q[c].empty())q[c].pop(); c=1-c;
    }
    if(!q[0].empty()) c=0;else c=1;
    if(c) x=w;else x=b;
    while(!q[c].empty()){
      i=q[c].front().first;
      if(p[i])cout<<x<<" "<<i<<" 0"<<endl;
      q[c].pop();
    }
    //system("pause");
    return 0;
}


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

using namespace std;

typedef long long int ll;

int main()
{

    ll i,j,k,x,y,c,n;queue< pair<ll,ll> > q[10];bool p[100100];
    cin >> n ;
    for(i=1;i<=n;i++){
      cin >> j >> k ;
      if(!k) p[i]=1;
      else p[i]=0;
      q[j].push(make_pair(i,k));
    }
    c=1;
    while(!q[0].empty()&&!q[1].empty()){
       x=q[c].front().first,y=q[c].front().second;
       while(y>0||p[x]){
          j=q[1-c].front().first,k=q[1-c].front().second,i=min(k,y);
          cout<<x<<" "<<j<<" "<<i<<endl;
          p[x]=0,p[j]=0,y-=i,q[1-c].front().second-=i;if(y!=0) {q[1-c].pop();}
       }
       if(!q[c].empty())q[c].pop(); c=1-c;
    }
    if(!q[0].empty()) c=0;else c=1;
    while(!q[c].empty()){
      i=q[c].front().first;
      if(p[i]){j=i;cout<<x<<" "<<i<<" 0"<<endl;}
      q[c].pop();
    }
    c=1-c;
    while(!q[c].empty()){
       i=q[c].front().first;
       if(p[i]) cout<<j<<" "<<i<<" 0"<<endl;
       q[c].pop();
    }
    //system("pause");
    return 0;
}


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

using namespace std;

typedef long long int ll;

int main()
{
    ll i,j,k,x,y,c,b=-1,w=-1,n;queue< pair<ll,ll> > q[10];bool p[100100];
    cin >> n ;
    for(i=1;i<=n;i++){
      cin >> j >> k ;
      if(!k) p[i]=1;
      else{
        p[i]=0;
        if(j) b=i;
        else w=i;
      }
      q[j].push(make_pair(i,k));
    }
    c=1;wq.front().first=w,bq.front().second=b;
    while(!q[0].empty()&&!q[1].empty()){
       x=q[c].front().first,y=q[c].front().second;
       if(p[x]&&c) cout<<w<<" "<<x<<" 0"<<endl;
       if(p[x]&&!c) cout<<b<<" "<<x<<" 0"<<endl;
       while(y>0){
          j=q[1-c].front().first,k=q[1-c].front().second,i=min(k,y);
          if(p[j]){
            if(c) cout<<b<<" "<<j<<" 0"<<endl;
            else cout<<w<<" "<<j<<" 0"<<endl;
            q[1-c].pop();
            continue;
          }
          cout<<x<<" "<<j<<" "<<i<<endl;
          p[x]=0,p[j]=0,y-=i,q[1-c].front().second-=i;if(y!=0) {q[1-c].pop();}
       }
       if(!q[c].empty())q[c].pop(); c=1-c;
    }
    if(!q[0].empty()) c=0;else c=1;
    if(c) x=w;else x=b;
    while(!q[c].empty()){
      i=q[c].front().first;
      if(p[i])cout<<x<<" "<<i<<" 0"<<endl;
      q[c].pop();
    }
    //system("pause");
    return 0;
}


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

using namespace std;

typedef long long int ll;

int main()
{

    ll i,j,k,x,y,c,n;queue< pair<ll,ll> > q[2];bool p[100100];
    cin >> n ;
    for(i=1;i<=n;i++){
      cin >> j >> k ;
      if(!k) p[i]=1;
      else p[i]=0;
      q[j].push(make_pair(i,k));
    }
    c=1;
    while(!q[0].empty()&&!q[1].empty()){
       x=q[c].front().first,y=q[c].front().second;
       while(y>0||p[x]){
          j=q[1-c].front().first,k=q[1-c].front().second,i=min(k,y);
          cout<<x<<" "<<j<<" "<<i<<endl;
          p[x]=0,p[j]=0,y-=i,q[1-c].front().second-=i;if(y!=0) {q[1-c].pop();}
       }
       if(!q[c].empty())q[c].pop(); c=1-c;
    }
    if(!q[0].empty()) c=0;else c=1;
    while(!q[c].empty()){
      i=q[c].front().first;
      if(p[i])cout<<x<<" "<<i<<" 0"<<endl;
      q[c].pop();
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <limits.h>
using namespace std;
struct arr
{
	int v,t;
};
bool cmp(arr a,arr b)
{
	return a.v<b.v;
}
const int		MaxN=100010;
int 			i,n,v,col,p[2];
arr				s[2][MaxN];
int main()
{
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	scanf("%d",&n);
	for (i=1;i<=n;i++)
	{
		scanf("%d%d",&col,&v);
		s[col][++p[col]].v=v;
		s[col][p[col]].t=i;
	}
	sort(s[0],s[0]+p[0]-1,cmp);
	sort(s[1],s[1]+p[1]-1,cmp);
	for (i=1;i<n;i++)
	{
		int y=min(s[0][p[0]].v,s[1][p[1]].v);
		printf("%d %d %d\n",s[0][p[0]].t,s[1][p[1]].t,y);
		s[0][p[0]].v-=y;
		s[1][p[1]].v-=y;
		if (!s[0][p[0]].v) p[0]--;
		if (!s[1][p[1]].v) p[1]--;
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include<algorithm>
#include <limits.h>
using namespace std;
struct arr
{
	int v,t;
};
bool cmp(arr a,arr b)
{
	return a.v<b.v;
}
const int		MaxN=100010;
int 			i,n,v,col,p[2];
arr				s[2][MaxN];
int main()
{
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	scanf("%d",&n);
	for (i=1;i<=n;i++)
	{
		scanf("%d%d",&col,&v);
		s[col][++p[col]].v=v;
		s[col][p[col]].t=i;
	}
	sort(s[0],s[0]+p[0]-1,cmp);
	sort(s[1],s[1]+p[1]-1,cmp);
	for (i=1;i<n;i++)
	{
		int y=min(s[0][p[0]].v,s[1][p[1]].v);
		printf("%d %d %d\n",s[0][p[0]].t,s[1][p[1]].t,y);
		s[0][p[0]].v-=y;
		s[1][p[1]].v-=y;
		if (!s[0][p[0]].v) p[0]--;
		if (!s[1][p[1]].v) p[1]--;
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include<algorithm>
#include <limits.h>
using namespace std;
struct arr
{
	int v,t;
};
bool cmp(arr a,arr b)
{
	return a.v>b.v;
}
const int		MaxN=100010;
int 			i,n,v,col,p[2];
arr				s[2][MaxN];
int main()
{
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
	scanf("%d",&n);
	for (i=1;i<=n;i++)
	{
		scanf("%d%d",&col,&v);
		s[col][++p[col]].v=v;
		s[col][p[col]].t=i;
	}
	sort(s[0]+1,s[0]+p[0]+1,cmp);
	sort(s[1]+1,s[1]+p[1]+1,cmp);
	for (i=1;i<n;i++)
	{
		int y=min(s[0][p[0]].v,s[1][p[1]].v);
		printf("%d %d %d\n",s[0][p[0]].t,s[1][p[1]].t,y);
		if (y==s[0][p[0]].v)
		{
			s[1][p[1]].v-=y;
			p[0]--;
		}
		else
		{
			s[0][p[0]].v-=y;
			p[1]--;
		}
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include<algorithm>
#include <limits.h>
using namespace std;
struct arr
{
	int v,t;
};
bool cmp(arr a,arr b)
{
	return a.v>b.v;
}
const int		MaxN=100010;
int 			i,n,v,col,p[2];
arr				s[2][MaxN];
int main()
{
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	scanf("%d",&n);
	for (i=1;i<=n;i++)
	{
		scanf("%d%d",&col,&v);
		s[col][++p[col]].v=v;
		s[col][p[col]].t=i;
	}
	sort(s[0]+1,s[0]+p[0]+1,cmp);
	sort(s[1]+1,s[1]+p[1]+1,cmp);
	for (i=1;i<n;i++)
	{
		int y=min(s[0][p[0]].v,s[1][p[1]].v);
		printf("%d %d %d\n",s[0][p[0]].t,s[1][p[1]].t,y);
		if (y==s[0][p[0]].v)
		{
			s[1][p[1]].v-=y;
			p[0]--;
		}
		else
		{
			s[0][p[0]].v-=y;
			p[1]--;
		}
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include<algorithm>
#include <limits.h>
using namespace std;
struct arr
{
	int v,t;
};
bool cmp(arr a,arr b)
{
	return a.v>b.v;
}
const int		MaxN=100010;
int 			i,n,v,col,p[2];
arr				s[2][MaxN];
int main()
{
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	scanf("%d",&n);
	for (i=1;i<=n;i++)
	{
		scanf("%d%d",&col,&v);
		s[col][++p[col]].v=v;
		s[col][p[col]].t=i;
	}
	sort(s[0]+1,s[0]+p[0]+1,cmp);
	sort(s[1]+1,s[1]+p[1]+1,cmp);
	for (i=1;i<n;i++)
	{
		int y=min(s[0][p[0]].v,s[1][p[1]].v);
		printf("%d %d %d\n",s[0][p[0]].t,s[1][p[1]].t,y);
		if (p[0]&&y==s[0][p[0]].v)
		{
			s[1][p[1]].v-=y;
			p[0]--;
		}
		else
		{
			s[0][p[0]].v-=y;
			p[1]--;
		}
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include<algorithm>
#include <limits.h>
using namespace std;
struct arr
{
	int v,t;
};
bool cmp(arr a,arr b)
{
	return a.v>b.v;
}
const int		MaxN=100010;
int 			i,n,v,col,p[2];
arr				s[2][MaxN];
int main()
{
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	scanf("%d",&n);
	for (i=1;i<=n;i++)
	{
		scanf("%d%d",&col,&v);
		s[col][++p[col]].v=v;
		s[col][p[col]].t=i;
	}
	sort(s[0]+1,s[0]+p[0]+1,cmp);
	sort(s[1]+1,s[1]+p[1]+1,cmp);
	for (i=1;i<n;i++)
	{
		int y=min(s[0][p[0]].v,s[1][p[1]].v);
		printf("%d %d %d\n",s[0][p[0]].t,s[1][p[1]].t,y);
		if (p[0]>1&&y==s[0][p[0]].v)
		{
			s[1][p[1]].v-=y;
			p[0]--;
		}
		else
		{
			s[0][p[0]].v-=y;
			p[1]--;
		}
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>

using namespace std;

const int MaxN = 100224;

int n, color, weight;

set<pair<int, int> > S1, S2;
int cntb, cntr;

int main()
{
    cin.sync_with_stdio(0);
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        cin >> color >> weight;

        if (color) S1.insert(make_pair(weight, i));
        else S2.insert(make_pair(weight, i));

        if (color) cntb++;
        else cntr++;

    }

    int edges = 0;

    for (set<pair<int, int> >::iterator it1 = S1.begin(), it2 = S2.begin(); !S1.empty() && !S2.empty() ; )
    {
        int w1 = (*it1).first, w2 = (*it2).first;

        if (w1 > w2)
        {
            printf("%d %d %d\n", (*it1).second , (*it2).second, w2);

            int idx = (*it1).second;

            S2.erase(it2);
            S1.erase(it1);
            it2 = S2.begin();

            S1.insert(make_pair(w1 - w2, idx));
            it1 = S1.begin();
        }
        else if  (w2 > w1)
        {
            printf("%d %d %d\n", (*it1).second, (*it2).second, w1);

            int idx = (*it2).second;

            S2.erase(it2);
            S1.erase(it1);
            it1 = S1.begin();

            S2.insert(make_pair(w2 - w1, idx));
            it2 = S2.begin();
        }
        else
        {
            int i = (*it1).second, j = (*it2).second;

            S1.erase(it1);
            S2.erase(it2);

            if (cntr > cntb)
            {
                S1.insert(make_pair(0, i) );
                cntr--;
            }
            else {S2.insert(make_pair(0, j) ); cntb--;}

            it1 = S1.begin();
            it2 = S2.begin();
            printf("%d %d %d\n", i , j, w1);
        }
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>

using namespace std;

int n, color, weight;

set<pair<int, int> > S1, S2;

int main()
{
    cin.sync_with_stdio(0);
    cin >> n;

    int mem1, mem2; // fucking stupid purple rating contests

    for (int i = 1; i <= n; i++)
    {
        cin >> color >> weight;

        if (color) S1.insert(make_pair(weight, i));
        else S2.insert(make_pair(weight, i));

        if (color) mem1 = i;
        else mem2 = i;
    }

    int edges = 0;

    for (set<pair<int, int> >::iterator it1 = S1.begin(), it2 = S2.begin(); !S1.empty() || !S2.empty() ; )
    {
        int w1 = (*it1).first, w2 = (*it2).first;

        if (w1 > w2)
        {
            printf("%d %d %d\n", (*it1).second , (*it2).second, w2);

            int idx = (*it1).second;

            S2.erase(it2);
            S1.erase(it1);
            it2 = S2.begin();

            S1.insert(make_pair(w1 - w2, idx));
            it1 = S1.begin();
        }
        else
        {
            printf("%d %d %d\n", (*it1).second, (*it2).second, w1); // never use -> because of precedence fuckups, always use *

            int idx = (*it2).second;

            S2.erase(it2);
            S1.erase(it1);
            it1 = S1.begin();

            if (w2 - w1 > 0) S2.insert(make_pair(w2 - w1, idx));
            it2 = S2.begin();
        }

        edges++;
    }

    while (edges < n - 1)
    {
        edges++;
        printf("%d %d %d\n", mem1, mem2, 0);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>

using namespace std;

const int MaxN = 100224;

int n, color, weight;

set<pair<int, int> > S1, S2;
bool mark[MaxN];

int main()
{
    cin.sync_with_stdio(0);
    freopen("ss.txt", "r", stdin);
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        cin >> color >> weight;

        if (color) S1.insert(make_pair(weight, i));
        else S2.insert(make_pair(weight, i));

    }

    int edges = 0;

    for (set<pair<int, int> >::iterator it1 = S1.begin(), it2 = S2.begin(); !S1.empty() && !S2.empty() ; )
    {
        int w1 = (*it1).first, w2 = (*it2).first;

        if (w1 > w2)
        {
            printf("%d %d %d\n", (*it1).second , (*it2).second, w2);

            int idx = (*it1).second;

            S2.erase(it2);
            S1.erase(it1);
            it2 = S2.begin();

            S1.insert(make_pair(w1 - w2, idx));
            it1 = S1.begin();
        }
        else if  (w2 > w1)
        {
            printf("%d %d %d\n", (*it1).second, (*it2).second, w1);

            int idx = (*it2).second;

            S2.erase(it2);
            S1.erase(it1);
            it1 = S1.begin();

            S2.insert(make_pair(w2 - w1, idx));
            it2 = S2.begin();
        }
        else
        {
            int i = (*it1).second, j = (*it2).second;

            S1.erase(it1);
            S2.erase(it2);

            if (!mark[i])
            {
                S1.insert(make_pair(0, i) );
                mark[i] = true;
            }
            else {S2.insert(make_pair(0, j) ); mark[j] = true;}

            it1 = S1.begin();
            it2 = S2.begin();
            printf("%d %d 0\n", i , j);
        }
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>

using namespace std;

const int MaxN = 100224;

int n, color, weight;

set<pair<int, int> > S1, S2;
bool mark[MaxN];

int main()
{
    cin.sync_with_stdio(0);
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        cin >> color >> weight;

        if (color) S1.insert(make_pair(weight, i));
        else S2.insert(make_pair(weight, i));

    }

    int edges = 0;

    for (set<pair<int, int> >::iterator it1 = S1.begin(), it2 = S2.begin(); !S1.empty() && !S2.empty() ; )
    {
        int w1 = (*it1).first, w2 = (*it2).first;

        if (w1 > w2)
        {
            printf("%d %d %d\n", (*it1).second , (*it2).second, w2);

            int idx = (*it1).second;

            S2.erase(it2);
            S1.erase(it1);
            it2 = S2.begin();

            S1.insert(make_pair(w1 - w2, idx));
            it1 = S1.begin();
        }
        else if  (w2 > w1)
        {
            printf("%d %d %d\n", (*it1).second, (*it2).second, w1);

            int idx = (*it2).second;

            S2.erase(it2);
            S1.erase(it1);
            it1 = S1.begin();

            S2.insert(make_pair(w2 - w1, idx));
            it2 = S2.begin();
        }
        else
        {
            int i = (*it1).second, j = (*it2).second;

            S1.erase(it1);
            S2.erase(it2);

            if (!mark[i])
            {
                S1.insert(make_pair(0, i) );
                mark[i] = true;
            }
            else {S2.insert(make_pair(0, j) ); mark[j] = true;}

            it1 = S1.begin();
            it2 = S2.begin();
            printf("%d %d 0\n", i , j);
        }
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>

using namespace std;

int n, color, weight;

set<pair<int, int> > S1, S2;

int main()
{
    cin.sync_with_stdio(0);
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        cin >> color >> weight;

        if (color) S1.insert(make_pair(weight, i));
        else S2.insert(make_pair(weight, i));

    }

    int edges = 0;

    for (set<pair<int, int> >::iterator it1 = S1.begin(), it2 = S2.begin(); !S1.empty() && !S2.empty() ; )
    {
        int w1 = (*it1).first, w2 = (*it2).first;

        if (w1 > w2)
        {
            printf("%d %d %d\n", (*it1).second , (*it2).second, w2);

            int idx = (*it1).second;

            S2.erase(it2);
            S1.erase(it1);
            it2 = S2.begin();

            S1.insert(make_pair(w1 - w2, idx));
            it1 = S1.begin();
        }
        else
        {
            printf("%d %d %d\n", (*it1).second, (*it2).second, w1);

            int idx = (*it2).second;

            S2.erase(it2);
            S1.erase(it1);
            it1 = S1.begin();

            S2.insert(make_pair(w2 - w1, idx));
            it2 = S2.begin();
        }
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>

using namespace std;

const int MaxN = 100224;

int n, color, weight;

set<pair<int, int> > S1, S2;
bool mark[MaxN];

int main()
{
    cin.sync_with_stdio(0);
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        cin >> color >> weight;

        if (color) S1.insert(make_pair(weight, i));
        else S2.insert(make_pair(weight, i));

    }

    int edges = 0;

    for (set<pair<int, int> >::iterator it1 = S1.begin(), it2 = S2.begin(); !S1.empty() && !S2.empty() ; )
    {
        int w1 = (*it1).first, w2 = (*it2).first;

        if (w1 > w2)
        {
            printf("%d %d %d\n", (*it1).second , (*it2).second, w2);

            int idx = (*it1).second;

            S2.erase(it2);
            S1.erase(it1);
            it2 = S2.begin();

            S1.insert(make_pair(w1 - w2, idx));
            it1 = S1.begin();
        }
        else if  (w2 > w1)
        {
            printf("%d %d %d\n", (*it1).second, (*it2).second, w1);

            int idx = (*it2).second;

            S2.erase(it2);
            S1.erase(it1);
            it1 = S1.begin();

            S2.insert(make_pair(w2 - w1, idx));
            it2 = S2.begin();
        }
        else
        {
            int i = (*it1).second, j = (*it2).second;

            S1.erase(it1);
            S2.erase(it2);

            if (!mark[i])
            {
                S1.insert(make_pair(0, i) );
                mark[i] = true;
            }
            else {S2.insert(make_pair(0, j) ); mark[j] = true;}

            it1 = S1.begin();
            it2 = S2.begin();
            printf("%d %d %d\n", i , j, w1);
        }
    }

    return 0;
}


import bisect

n = input()
x = map(int, raw_input().split())
q = input()
x.sort()
for _ in range(q):
    m = input()
    print bisect.bisect(x, m)


import bisect
def main():
	raw_input()
	prices = [int(i) for i in raw_input().split(' ')]
	prices.sort()
	examples = int(raw_input())
	while(examples > 0):
		examples-=1
		cash = int(raw_input())
		print(bisect.bisect_right(prices,cash))
#runs the program
if __name__ == "__main__":
	main()


from bisect import *
n=input()
s=map(int, raw_input().split())
c=input()
s=sorted(s)
for i in xrange(c):
	x=input()
	print bisect_right(s, x)


n=input()
a=list(map(int,raw_input().split()))
a.sort()
q=input()
for j in range(1,q+1,1):
    m=input()
    l=0
    r=n-1
    ans=0
    if m>=a[n-1]:
        ans=n
    elif m<a[0]:
        ans=0
    else:
        while l<=r:
            mid=(l+r)/2
            if m>=a[mid] and m<a[mid+1]:
                ans=mid+1
                break
            elif a[mid]>m:
                r=mid-1
            else:
                l=mid+1
    print ans


def BinarySearch(x, n, a):
	L = 0
	R = n - 1
	result = -1
	while L <= R:
		mid = (L + R) >> 1
		if a[mid] <= x:
			result = mid
			L = mid + 1
		else:
			R = mid - 1
	if result == -1:
		return 0
	else:
		return result + 1
def Solve(n, a, m, b):
	a = sorted(a)
	answer = []
	for x in b:
		answer.append(BinarySearch(x, n, a))
	return answer
n = int(raw_input())
a = map(int, raw_input().split())
m = int(raw_input())
b = []
for i in range(m):
	b.append(int(raw_input()))
answer = Solve(n, a, m, b)
for x in answer:
	print(x)


import bisect
r=lambda:map(int,raw_input().split())

n=input()
x=sorted(r())
q=input()
for i in range(q):
    m=input()
    j=bisect.bisect(x,m)
    print j


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import time
import sys
import io
import re
import math
import itertools
import collections
import bisect
#sys.stdin=file('input.txt')
#sys.stdout=file('output.txt','w')
#10**9+7
mod=1000000007
#mod=1777777777
pi=3.141592653589
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def euclid_dis(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0

n=int(raw_input())
x=map(int,raw_input().split())
x.sort()
l=[0]*100001
R=range(100001)
chk=0
for i in R:
    while len(x):
        if x[0]<=i:
            chk+=1
            x.pop(0)
        else:
            break
    l[i]=chk
q=int(raw_input())
for i in range(q):
    m=int(raw_input())
    if m>100000:
        m=100000
    print l[m]


import bisect
#sys.stdin=file('input.txt')
#sys.stdout=file('output.txt','w')
#10**9+7
mod=1000000007
#mod=1777777777
pi=3.141592653589
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def euclid_dis(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0

n=int(raw_input())
x=map(int,raw_input().split())
x.sort()
q=int(raw_input())
for i in range(q):
    m=int(raw_input())
    print bisect.bisect_right(x,m)


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import time
import sys
import io
import re
import math
import itertools
import collections
import bisect
#sys.stdin=file('input.txt')
#sys.stdout=file('output.txt','w')
#10**9+7
mod=1000000007
#mod=1777777777
pi=3.141592653589
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def euclid_dis(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0

n=int(raw_input())
x=[int(v) for v in raw_input().split()]
x.sort()
l=[0]*100001
chk=0
for i in range(100001):
    while n:
        if x[0]<=i:
            chk+=1
            x.pop(0)
            n-=1
        else:
            break
    l[i]=chk
q=int(raw_input())
for i in range(q):
    m=int(raw_input())
    if m>100000:
        m=100000
    print l[m]


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import time
import sys
import io
import re
import math
import itertools
import collections
import bisect
#sys.stdin=file('input.txt')
#sys.stdout=file('output.txt','w')
#10**9+7
mod=1000000007
#mod=1777777777
pi=3.141592653589
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def euclid_dis(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0

n=int(raw_input())
x=map(int,raw_input().split())
x.sort()
l=[0]*100001
chk=0
for i in range(100001):
    while n:
        if x[0]<=i:
            chk+=1
            x.pop(0)
            n-=1
        else:
            break
    l[i]=chk
q=int(raw_input())
for i in range(q):
    m=int(raw_input())
    if m>100000:
        m=100000
    print l[m]


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import time
import sys
import io
import re
import math
import itertools
import collections
import bisect
#sys.stdin=file('input.txt')
#sys.stdout=file('output.txt','w')
#10**9+7
mod=1000000007
#mod=1777777777
pi=3.141592653589
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def euclid_dis(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0

n=int(raw_input())
x=map(int,raw_input().split())
x.sort()
memo1=x[n/3]
memo2=x[(2*n)/3]
#print memo
q=int(raw_input())
for i in range(q):
    ans=chk=0
    m=int(raw_input())
    if m>=memo2:
        ans=(n*2)/3
        for j in range((n*2)/3,n):
            if x[j]<=m:
                ans+=1
            else:
                break
    elif m>=memo1:
        ans=n/3
        for j in range(n/3,n):
            if x[j]<=m:
                ans+=1
            else:
                break
    else:
        for j in x:
            if j<=m:
                ans+=1
            else:
                break
    print ans


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import time
import sys
import io
import re
import math
import itertools
import collections
import bisect
#sys.stdin=file('input.txt')
#sys.stdout=file('output.txt','w')
#10**9+7
mod=1000000007
#mod=1777777777
pi=3.141592653589
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def euclid_dis(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0

n=int(raw_input())
x=map(int,raw_input().split())
x.sort()
l=[0]*100001
R=range(100001)
chk=0
for i in R:
    while len(x):
        if x[0]<=i:
            chk+=1
            x.pop(0)
        else:
            break
    l[i]=chk
q=int(raw_input())
for i in range(q):
    m=int(raw_input())
    print l[m]


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import time
import sys
import io
import re
import math
import itertools
import collections
import bisect
#sys.stdin=file('input.txt')
#sys.stdout=file('output.txt','w')
#10**9+7
mod=1000000007
#mod=1777777777
pi=3.141592653589
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def euclid_dis(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0

n=int(raw_input())
x=map(int,raw_input().split())
x.sort()
memo=x[n/2]
print memo
q=int(raw_input())
for i in range(q):
    ans=chk=0
    m=int(raw_input())
    if m>=memo:
        ans=n/2
        for j in range(n/2,n):
            if x[j]<=m:
                ans+=1
            else:
                break
    else:
        for j in x:
            if j<=m:
                ans+=1
            else:
                break
    print ans


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import time
import sys
import io
import re
import math
import itertools
import collections
import bisect
#sys.stdin=file('input.txt')
#sys.stdout=file('output.txt','w')
#10**9+7
mod=1000000007
#mod=1777777777
pi=3.141592653589
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def euclid_dis(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0

n=int(raw_input())
x=map(int,raw_input().split())
x.sort()
q=int(raw_input())
for i in range(q):
    ans=chk=0
    m=int(raw_input())
    for j in x:
        if j<=m:
            ans+=1
        else:
            break
    print ans


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import time
import sys
import io
import re
import math
import itertools
import collections
import bisect
#sys.stdin=file('input.txt')
#sys.stdout=file('output.txt','w')
#10**9+7
mod=1000000007
#mod=1777777777
pi=3.141592653589
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def euclid_dis(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0

n=int(raw_input())
x=map(int,raw_input().split())
x.sort()
memo=x[n/2]
#print memo
q=int(raw_input())
for i in range(q):
    ans=chk=0
    m=int(raw_input())
    if m>=memo:
        ans=n/2
        for j in range(n/2,n):
            if x[j]<=m:
                ans+=1
            else:
                break
    else:
        for j in x:
            if j<=m:
                ans+=1
            else:
                break
    print ans


import bisect

n=int(raw_input())
x=map(int,raw_input().split())
x.sort()
q=int(raw_input())
for i in range(q):
    m=int(raw_input())
    print bisect.bisect_right(x,m)


# import bisect

# n = raw_input()
# stores = map(int, raw_input().split())
# q = input()

# cost = [0 for _ in range(100000)]
# dp = [0 for _ in range(100000)]

# for i in stores:
# 	cost[i] += 1

# for i in xrange(1, len(dp)):
# 	dp[i] = dp[i-1] + cost[i]

# for i in xrange(q):
# 	coin = input()

# 	if coin > 100000:
# 		print dp[-1]
# 	else:
# 		print dp[coin]

import bisect
n = input()
x = sorted(map(int, raw_input().split()))
q = input()
for i in range(q):
	print bisect.bisect(x,input())


import bisect

n = raw_input()
stores = map(int, raw_input().split())
q = input()

cost = [0 for _ in xrange(100001)]
dp = [0 for _ in xrange(100001)]

for i in stores:
	cost[i] += 1

for i in xrange(1, len(dp)):
	dp[i] = dp[i-1] + cost[i]

for i in xrange(q):
	coin = input()

	if coin > 100000:
		print dp[-1]
	else:
		print dp[coin]

# import bisect
# n = input()
# x = sorted(map(int, raw_input().split()))
# q = input()
# for i in range(q):
# 	print bisect.bisect(x,input())


import bisect

n = raw_input()
stores = map(int, raw_input().split())
q = input()

cost = [0 for _ in xrange(100000)]
dp = [0 for _ in xrange(100000)]

for i in stores:
	cost[i] += 1

for i in xrange(1, len(dp)):
	dp[i] = dp[i-1] + cost[i]

for i in xrange(q):
	coin = input()

	if coin > 100000:
		print dp[-1]
	else:
		print dp[coin]

# import bisect
# n = input()
# x = sorted(map(int, raw_input().split()))
# q = input()
# for i in range(q):
# 	print bisect.bisect(x,input())


from bisect import bisect
n=input()
lst=map(int,raw_input().split())
lst.sort()
q=input()
for i in range(0,q):
    pp=input()
    print bisect(lst,pp)


import bisect
N = int(raw_input())
prices = [int(p) for p in raw_input().split()]
prices.sort()
Q = int(raw_input())
for i in range(Q):
    q = int(raw_input())
    print bisect.bisect_right(prices, q)


import bisect

n = input()
x = map(int, raw_input().split())
q = input()
x.sort()
for _ in range(q):
    m = input()
    print bisect.bisect(x, m)


# -*- coding: utf-8 -*-
import sys

def er_fen(temp,n,n_list):
    start,end = 0,n
    while True:
        flag = int((start+end)/2.0)

        if n_list[flag]>temp:
            start,end = start,flag
        else:
            start,end = flag,end

        if end-start<=1:
            if n_list[start]>temp:
                return start
            break

    return end

def some_func():
    """
    """
    n = input()
    n_list = map(int,sys.stdin.readline().split())
    cache = {}
    n_list.sort()

    m = input()
    for i in range(m):
        temp = input()
        if cache.has_key(temp):
            print cache[temp]
        else:
            data = er_fen(temp,n,n_list)
            cache[temp]=data
            print data

if __name__ == '__main__':
    some_func()


import bisect
n = input()
x = sorted(map(int, raw_input().split()))
q = input()
for i in range(q):
	print bisect.bisect(x,input())


import bisect
I=input
n=I()
a=sorted(map(int,raw_input().split()))
for _ in range(I()):
  print bisect.bisect(a,I())


I = lambda:int(raw_input())
i = lambda:map(int, raw_input().split())
n = I()
a = sorted(i())
def f(x):
	l, r, t = 0, n - 1, -1
	while l <= r:
		mid = (l + r) / 2
		if a[mid] <= x:
			t = mid
			l = mid + 1
		else:r = mid - 1
	return t + 1
for i in range(I()):
	print f(I())


from bisect import bisect_right
n=input()
a=map(int,raw_input().split())
a.sort()
for _ in range(input()):
    x=input()
    print bisect_right(a,x)


import bisect

n = int(raw_input())
x = map(int, raw_input().split())

x.sort()

q = int(raw_input())

for i in range(q):
    mi = int(raw_input())
    print bisect.bisect_right(x, mi)


def get_index(wine_price,today_money):
    left = 0
    right = len(wine_price) - 1
    while left < right:
        mid = (left + right)/2
        if wine_price[mid] <= today_money:
            left = mid + 1
        else:
            right = mid
    return right

def get_result(wine_price,today_money):
    result = get_index(wine_price,today_money)
    if result == (len(wine_price)-1) and today_money >= wine_price[-1]:
        result += 1
    return result

n = int(raw_input())
wine_price = map(int,raw_input().split())
wine_price = sorted(wine_price)

q = int(raw_input())

for i in range(q):
    temp = int(raw_input())
    print get_result(wine_price,temp)


def get_result(wine_price,today_money):

    for i in range(len(wine_price)):
        if wine_price[i] > today_money:
            return i
    return len(wine_price)

n = int(raw_input())
wine_price = map(int,raw_input().split())
wine_price = sorted(wine_price)

q = int(raw_input())

for i in range(q):
    temp = int(raw_input())
    print get_result(wine_price,temp)


def get_index(wine_price,today_money):
    left = 0
    right = len(wine_price) - 1

    while left < right:
        mid = (left + right)/2
        if wine_price[mid] <= today_money:
            left = mid + 1
        else:
            right = mid
    return right

def get_result(wine_price,today_money):

    result = get_index(wine_price,today_money)
    if result == (len(wine_price)-1) and today_money == wine_price[-1]:
        result += 1
    return result

n = int(raw_input())
wine_price = map(int,raw_input().split())
wine_price = sorted(wine_price)

q = int(raw_input())

for i in range(q):
    temp = int(raw_input())
    print get_result(wine_price,temp)


def get_index(wine_price,today_money):
    left = 0
    right = len(wine_price) - 1

    while left < right:
        mid = (left + right)/2
        if wine_price[mid] <= today_money:
            left = mid + 1
        else:
            right = mid
    return right

def get_result(wine_price,today_money):
    return get_index(wine_price,today_money)

n = int(raw_input())
wine_price = map(int,raw_input().split())
wine_price = sorted(wine_price)

q = int(raw_input())

for i in range(q):
    temp = int(raw_input())
    print get_result(wine_price,temp)


def get_result(wine_price,today_money):

    for i in range(len(wine_price)):
        if wine_price[i] > today_money:
            return i

n = int(raw_input())
wine_price = map(int,raw_input().split())
wine_price = sorted(wine_price)

q = int(raw_input())

for i in range(q):
    temp = int(raw_input())
    print get_result(wine_price,temp)


from math import pow,sqrt
drinks = input()
z = []
x = map(int,raw_input().split(' '))
numofdays = input()

for i in range(numofdays):
	z.append(input())

for i in reversed(z):
	count = 0
	for w in reversed(x):
		if i >=w:
			count+=1
	print count


from math import pow,sqrt
drinks = input()
z = []
x = map(int,raw_input().split(' '))
numofdays = input()

for i in range(numofdays):
	z.append(input())

for i in z:
	count = 0
	for w in x:
		if i >=w:
			count+=1
	print count


import bisect

drinks = input()
z = []
x = map(int,raw_input().split(' '))
x = sorted(x)
numofdays = input()

for i in range(numofdays):
	z.append(input())

def binary_search(seq, t):
    min = 0
    max = len(seq) - 1
    while True:
        if max < min:
            return -1
        m = (min + max) // 2
        if seq[m] < t:
            min = m + 1
        elif seq[m] > t:
            max = m - 1
        else:
            return m
for i in z:
	index = bisect.bisect(x,i)
	print index


from math import pow,sqrt
drinks = input()
z = []
x = map(int,raw_input().split(' '))
numofdays = input()

for i in range(numofdays):
	z.append(input())

for i in z:
	count = 0
	for w in reversed(x):
		if i >=w:
			count+=1
	print count


from math import pow,sqrt
drinks = input()
z = []
x = map(int,raw_input().split(' '))
x = sorted(x)
numofdays = input()

for i in range(numofdays):
	z.append(input())

def binary_search(seq, t):
    min = 0
    max = len(seq) - 1
    while True:
        if max < min:
            return -1
        m = (min + max) // 2
        if seq[m] < t:
            min = m + 1
        elif seq[m] > t:
            max = m - 1
        else:
            return m
for i in z:
	index = binary_search(x,i)
	print index+1


global max_count
def fastFind(n, arr, start, stop):
    if(arr[start][1] > n):
        return 0
    # if(stop >= len(arr)):
        # stop = len(arr) - 1
    if(arr[start][1] == n):
        return arr[start][0]
    if(n > arr[-1][1]):
        return arr[stop][0] + max_count
    if(arr[stop][1] == n):
        return arr[stop][0]
    if(start == stop - 1):
        return arr[stop][0]
    mid = start + stop / 2
    if(n == arr[mid][1]):
        return arr[mid][0]
    if(n > arr[mid][1]):
        return fastFind(n, arr, mid, stop)
    else:
        return fastFind(n, arr, start, mid)

def main():
    global max_count
    n = int(raw_input())
    xs = map(int, raw_input().strip().split())
    ys = sorted(xs)
    indexs = [(0,ys[0])]
    max_count = len([x for x in ys if x == ys[-1]])
    for i, y in enumerate(ys):
        if(y > ys[i-1]):
            indexs.append((i, y))

    memo =[]
    current = 0
    for i in xrange(20):
        if(current >= len(indexs)):
            memo.append(len(ys))
            continue
        if(indexs[current][1] > i):
            memo.append(indexs[current][0])
        else:
            memo.append(indexs[current][0])
            current += 1
    q = int(raw_input())
    moneys = [int(raw_input()) for _ in xrange(q)]
    for money in moneys:
        print(memo[money + 1])

main()


global max_count
def fastFind(n, arr, start, stop):
    if(stop >= len(arr)):
        stop = len(arr) - 1
    if(arr[start][1] == n):
        return arr[start][0]
    if(n > arr[-1][1]):
        return arr[stop][0] + max_count
    if(arr[stop][1] == n):
        return arr[stop][0]
    if(start == stop - 1):
        print(start, stop)
        return arr[stop][0]
    mid = start + stop / 2
    if(n == arr[mid][1]):
        return arr[mid][0]
    if(n > arr[mid][1]):
        return fastFind(n, arr, mid, stop)
    else:
        return fastFind(n, arr, start, mid)

def main():
    global max_count
    n = int(raw_input())
    xs = map(int, raw_input().strip().split())
    ys = sorted(xs)
    indexs = [(0,ys[0])]
    max_count = len([x for x in ys if x == ys[-1]])
    for i, y in enumerate(ys):
        if(y > ys[i-1]):
            indexs.append((i, y))
    q = int(raw_input())
    moneys = [int(raw_input()) for _ in xrange(q)]
    for money in moneys:
        try:
            print(fastFind(money + 1, indexs, 0, len(indexs)))
        except IndexError:
            print("...")
            print(len(ys))

main()


global max_count
def fastFind(n, arr, start, stop):
    if(stop >= len(arr)):
        stop = len(arr) - 1
    if(arr[start][1] == n):
        return arr[start][0]
    if(n > arr[-1][1]):
        return arr[stop][0] + max_count
    if(arr[stop][1] == n):
        return arr[stop][0]
    if(start == stop - 1):
        print(start, stop)
        return arr[stop][0]
    mid = start + stop / 2
    if(n == arr[mid][1]):
        return arr[mid][0]
    if(n > arr[mid][1]):
        return fastFind(n, arr, mid, stop)
    else:
        return fastFind(n, arr, start, mid)

def main():
    global max_count
    n = int(raw_input())
    xs = map(int, raw_input().strip().split())
    ys = sorted(xs)
    indexs = [(0,ys[0])]
    max_count = len([x for x in ys if x == ys[-1]])
    for i, y in enumerate(ys):
        if(y > ys[i-1]):
            indexs.append((i, y))
    q = int(raw_input())
    moneys = [int(raw_input()) for _ in xrange(q)]
    print(indexs)
    for money in moneys:
        try:
            print(fastFind(money + 1, indexs, 0, len(indexs)))
        except IndexError:
            print("...")
            print(len(ys))

main()


global max_count
def fastFind(n, arr, start, stop):
    if(stop >= len(arr)):
        stop = len(arr) - 1
    if(arr[start][1] == n):
        return arr[start][0]
    if(n > arr[-1][1]):
        return arr[stop][0] + max_count
    if(arr[stop][1] == n):
        return arr[stop][0]
    if(start == stop - 1):
        return arr[stop][0]
    mid = start + stop / 2
    if(n == arr[mid][1]):
        return arr[mid][0]
    if(n > arr[mid][1]):
        return fastFind(n, arr, mid, stop)
    else:
        return fastFind(n, arr, start, mid)

def main():
    global max_count
    n = int(raw_input())
    xs = map(int, raw_input().strip().split())
    ys = sorted(xs)
    indexs = [(0,ys[0])]
    max_count = len([x for x in ys if x == ys[-1]])
    for i, y in enumerate(ys):
        if(y > ys[i-1]):
            indexs.append((i, y))
    q = int(raw_input())
    moneys = [int(raw_input()) for _ in xrange(q)]
    for money in moneys:
        try:
            print(fastFind(money + 1, indexs, 0, len(indexs)))
        except IndexError:
            print(len(ys))

main()


global max_count
def fastFind(n, arr, start, stop):
    if(arr[start][1] > n):
        return 0
    # if(stop >= len(arr)):
        # stop = len(arr) - 1
    if(arr[start][1] == n):
        return arr[start][0]
    if(n > arr[-1][1]):
        return arr[stop][0] + max_count
    if(arr[stop][1] == n):
        return arr[stop][0]
    if(start == stop - 1):
        return arr[stop][0]
    mid = start + stop / 2
    if(n == arr[mid][1]):
        return arr[mid][0]
    if(n > arr[mid][1]):
        return fastFind(n, arr, mid, stop)
    else:
        return fastFind(n, arr, start, mid)

def main():
    global max_count
    n = int(raw_input())
    xs = map(int, raw_input().strip().split())
    ys = sorted(xs)
    indexs = [(0,ys[0])]
    max_count = len([x for x in ys if x == ys[-1]])
    for i, y in enumerate(ys):
        if(y > ys[i-1]):
            indexs.append((i, y))

    memo =[]
    current = 0
    for i in xrange(100001):
        if(current >= len(indexs)):
            memo.append(len(ys))
            continue
        if(indexs[current][1] > i):
            memo.append(indexs[current][0])
        else:
            memo.append(indexs[current][0])
            current += 1
    q = int(raw_input())
    moneys = [int(raw_input()) for _ in xrange(q)]
    for money in moneys:
        print(memo[money + 1])

main()


global max_count
def fastFind(n, arr, start, stop):
    if(arr[start][1] > n):
        return 0
    # if(stop >= len(arr)):
        # stop = len(arr) - 1
    if(arr[start][1] == n):
        return arr[start][0]
    if(n > arr[-1][1]):
        return arr[stop][0] + max_count
    if(arr[stop][1] == n):
        return arr[stop][0]
    if(start == stop - 1):
        return arr[stop][0]
    mid = start + stop / 2
    if(n == arr[mid][1]):
        return arr[mid][0]
    if(n > arr[mid][1]):
        return fastFind(n, arr, mid, stop)
    else:
        return fastFind(n, arr, start, mid)

def main():
    global max_count
    n = int(raw_input())
    xs = map(int, raw_input().strip().split())
    ys = sorted(xs)
    indexs = [(0,ys[0])]
    max_count = len([x for x in ys if x == ys[-1]])
    for i, y in enumerate(ys):
        if(y > ys[i-1]):
            indexs.append((i, y))

    memo =[]
    current = 0
    for i in xrange(100001):
        if(current >= len(indexs)):
            memo.append(len(ys))
            continue
        if(indexs[current][1] > i):
            memo.append(indexs[current][0])
        else:
            memo.append(indexs[current][0])
            current += 1
    q = int(raw_input())
    moneys = [int(raw_input()) for _ in xrange(q)]
    for money in moneys:
        try:
            print(memo[money + 1])
        except IndexError:
            print(len(ys))

main()


global max_count
def fastFind(n, arr, start, stop):
    if(arr[start][1] > n):
        return 0
    # if(stop >= len(arr)):
        # stop = len(arr) - 1
    if(arr[start][1] == n):
        return arr[start][0]
    if(n > arr[-1][1]):
        return arr[stop][0] + max_count
    if(arr[stop][1] == n):
        return arr[stop][0]
    if(start == stop - 1):
        return arr[stop][0]
    mid = start + stop / 2
    if(n == arr[mid][1]):
        return arr[mid][0]
    if(n > arr[mid][1]):
        return fastFind(n, arr, mid, stop)
    else:
        return fastFind(n, arr, start, mid)

def main():
    global max_count
    n = int(raw_input())
    xs = map(int, raw_input().strip().split())
    ys = sorted(xs)
    indexs = [(0,ys[0])]
    max_count = len([x for x in ys if x == ys[-1]])
    for i, y in enumerate(ys):
        if(y > ys[i-1]):
            indexs.append((i, y))
    q = int(raw_input())
    moneys = [int(raw_input()) for _ in xrange(q)]
    for money in moneys:
        print(fastFind(money + 1, indexs, 0, len(indexs)-1))

main()


global max_count
def fastFind(n, arr, start, stop):
    if(arr[start][1] > n):
        return 0
    if(stop >= len(arr)):
        stop = len(arr) - 1
    if(arr[start][1] == n):
        return arr[start][0]
    if(n > arr[-1][1]):
        return arr[stop][0] + max_count
    if(arr[stop][1] == n):
        return arr[stop][0]
    if(start == stop - 1):
        return arr[stop][0]
    mid = start + stop / 2
    if(n == arr[mid][1]):
        return arr[mid][0]
    if(n > arr[mid][1]):
        return fastFind(n, arr, mid, stop)
    else:
        return fastFind(n, arr, start, mid)

def main():
    global max_count
    n = int(raw_input())
    xs = map(int, raw_input().strip().split())
    ys = sorted(xs)
    indexs = [(0,ys[0])]
    max_count = len([x for x in ys if x == ys[-1]])
    for i, y in enumerate(ys):
        if(y > ys[i-1]):
            indexs.append((i, y))
    q = int(raw_input())
    moneys = [int(raw_input()) for _ in xrange(q)]
    for money in moneys:
        try:
            print(fastFind(money + 1, indexs, 0, len(indexs)))
        except IndexError:
            print(len(ys))

main()


global max_count
def fastFind(n, arr, start, stop):
    if(arr[start][1] > n):
        return 0
    # if(stop >= len(arr)):
        # stop = len(arr) - 1
    if(arr[start][1] == n):
        return arr[start][0]
    if(n > arr[-1][1]):
        return arr[stop][0] + max_count
    if(arr[stop][1] == n):
        return arr[stop][0]
    if(start == stop - 1):
        return arr[stop][0]
    mid = start + stop / 2
    if(n == arr[mid][1]):
        return arr[mid][0]
    if(n > arr[mid][1]):
        return fastFind(n, arr, mid, stop)
    else:
        return fastFind(n, arr, start, mid)

def main():
    global max_count
    n = int(raw_input())
    xs = map(int, raw_input().strip().split())
    ys = sorted(xs)
    indexs = [(0,ys[0])]
    max_count = len([x for x in ys if x == ys[-1]])
    for i, y in enumerate(ys):
        if(y > ys[i-1]):
            indexs.append((i, y))

    memo =[]
    current = 0
    for i in xrange(20):
        if(current >= len(indexs)):
            memo.append(len(ys))
            continue
        if(indexs[current][1] > i):
            memo.append(indexs[current][0])
        else:
            memo.append(indexs[current][0])
            current += 1
    q = int(raw_input())
    moneys = [int(raw_input()) for _ in xrange(q)]
    for money in moneys:
        try:
            print(memo[money + 1])
        except IndexError:
            print(len(ys))

main()


import bisect
n=int(raw_input())
l=map(int,raw_input().split())
l.sort()
for i in range(int(raw_input())):
    print bisect.bisect(l,int(raw_input()))


import bisect
n = int(raw_input())
a = sorted(map(int,raw_input().split()))
for _ in range(int(raw_input())):
    q = int(raw_input())
    if q<a[0]:
        print 0
    else:
        ans = bisect.bisect_left(a,q)
        if ans==0:
            ans += 1
        print ans


import bisect
n = int(raw_input())
a = sorted(map(int,raw_input().split()))
for _ in range(int(raw_input())):
    q = int(raw_input())
    if q<a[0]:
        print 0
    else:
        ans = bisect.bisect_right(a,q)
        print ans


N = 111111

n = int(raw_input())
x = map(int, raw_input().split())
sum = [0 for _ in xrange(N)]
for i in xrange(n):
	sum[x[i]] += 1
for i in xrange(1, N):
	sum[i] += sum[i - 1]

q = int(raw_input())
for _ in xrange(q):
	m = int(raw_input())
	print sum[m]


N = 111111

n = int(raw_input())
x = map(int, raw_input().split())
sum = [0 for _ in xrange(N)]
for i in xrange(n):
	sum[x[i]] += 1
for i in xrange(1, N):
	sum[i] += sum[i - 1]

q = int(raw_input())
for _ in xrange(q):
	m = min(int(raw_input()), N - 1)
	print sum[m]


n = int(raw_input())
x = map(int, raw_input().split())
a = [0] * 100001
for xx in x:
    a[xx] += 1
for i in xrange(100000):
    a[i+1] += a[i]
q = int(raw_input())
for i in xrange(q):
    m = int(raw_input())
    if m > 100000:
        print n
    else:
        print a[m]


n = int(raw_input())
arr = map(int, raw_input().split())
arr.sort()
Q = int(raw_input())
for sc in range(Q):
	q = int(raw_input())

	l = 0
	h = n
	while(l != h):
		mid = (l+h)//2

		if arr[mid] < q:
			l = mid+1
		else:
			h = mid
	if q < arr[0]:
		print "0"
	elif q >= arr[-1]:
		print n
	else:
		print l+1


from bisect import bisect
n = int(raw_input())
arr = map(int, raw_input().split())
arr.sort()
Q = int(raw_input())
for sc in range(Q):
	q = int(raw_input())
	i = bisect(arr, q)
	print i


import sys

def solve(budget, arr):
	"""
	he number of shops where Vasiliy will be
	able to buy a bottle of the drink on the i-th day.

	YYYY NNNN
	[lo,hi)
	"""
	lo = -1
	hi = len(arr)

	while (lo + 1 < hi):
		mid = (lo + hi) / 2
		if arr[mid] <= budget:
			lo = mid
		else:
			hi = mid
	return lo

shops = int(raw_input())

prices = [int(x) for x in raw_input().split()]

prices.sort()

days = int(raw_input())

for _ in range(days):
	print solve(int(raw_input()), prices) + 1


wczytane = []
t = []
def wczytaj():
	global wczytane
	wczytane = []
	S = raw_input()
	i = 0
	n = len(S)
	while i < n:
		a = 0
		ujemne = False
		while i < n and S[i] != ' ':
			a *= 10
			if S[i] == '-':
				ujemne = True
				i += 1
			a += int(S[i])
			i += 1
		i += 1
		if ujemne:
			wczytane.append(-a)
		else: wczytane.append(a)

def bsrch(war):
	pocz = 0
	kon = len(t) - 1
	while pocz < kon:
		sr = (pocz + kon) // 2
		if t[sr] <= war: pocz = sr + 1
		else: kon = sr
	if t[pocz] > war: return pocz
	return pocz + 1

n = input()

wczytaj()

t = wczytane
t.sort()
print t
queries = input()

for i in range(queries):
	x = input()
	print bsrch(x)


wczytane = []
t = []
def wczytaj():
	global wczytane
	wczytane = []
	S = raw_input()
	i = 0
	n = len(S)
	while i < n:
		a = 0
		ujemne = False
		while i < n and S[i] != ' ':
			a *= 10
			if S[i] == '-':
				ujemne = True
				i += 1
			a += int(S[i])
			i += 1
		i += 1
		if ujemne:
			wczytane.append(-a)
		else: wczytane.append(a)

def bsrch(war):
	pocz = 0
	kon = len(t) - 1
	while pocz < kon:
		sr = (pocz + kon) // 2
		if t[sr] <= war: pocz = sr + 1
		else: kon = sr
	if t[pocz] > war: return pocz
	return pocz + 1

n = input()

wczytaj()

t = wczytane
t.sort()

queries = input()

for i in range(queries):
	x = input()
	print bsrch(x)


import bisect
n = raw_input()
s = sorted(map(int ,raw_input().split()))
for i in xrange(int(raw_input())):
    print bisect.bisect(s,int(raw_input()))


n = input()
l = sorted(map(int, raw_input().split()))
q = input()

def numberOfEleGreater(a, ele):
    l = 0; r = len(a)-1; flag = 0
    while l<=r:
        mid = (l+r)/2
        if a[mid] <= ele:
            flag = 1;l = mid + 1
        elif a[mid] > ele:
            r = mid - 1
    return abs(r+l)/2 if flag else -1

for j in range(q):
    temp = numberOfEleGreater(l, input())
    print temp+1 if temp >= 0 else 0


def solve(n, cost, q, money):
	sortedCost = sorted(cost)
	return [binSearch(sortedCost, money[i], 0, len(sortedCost)) for i in range(q)]

def binSearch(sortedCost, amount, lower, upper):
	if upper - 1 == lower:
		if amount < sortedCost[lower] and lower == 0:
			return 0
		return upper
	mid = (lower + upper) / 2
	if amount >= sortedCost[mid]:
		return binSearch(sortedCost, amount, mid, upper)
	return binSearch(sortedCost, amount, lower, mid)

n = int(raw_input())
cost = map(int, raw_input().split())
q = int(raw_input())
money = [int(raw_input()) for i in range(q)]

for i in solve(5, [3, 10, 8, 6, 11], 4, [1, 10, 3, 11]):
	print i


def solve(n, cost, q, money):
	sortedCost = sorted(cost)
	return [binSearch(sortedCost, money[i], 0, len(sortedCost)) for i in range(q)]

def binSearch(sortedCost, amount, lower, upper):
	if upper - 1 == lower:
		if amount < sortedCost[lower] and lower == 0:
			return 0
		return upper
	mid = (lower + upper) / 2
	if amount >= sortedCost[mid]:
		return binSearch(sortedCost, amount, mid, upper)
	return binSearch(sortedCost, amount, lower, mid)

n = int(raw_input())
cost = map(int, raw_input().split())
q = int(raw_input())
money = [int(raw_input()) for i in range(q)]

for i in solve(n, cost, q, money):
	print i


n = int(raw_input().strip())
x = map(int, raw_input().strip().split())
q = int(raw_input().strip())
M = 100001
l = [0 for i in xrange(M)]
for i in x:
    l[i] += 1
for i in xrange(1,M):
    l[i] += l[i-1]

while q > 0:
    q -= 1
    k = int(raw_input().strip())
    print l[k]


n = int(raw_input().strip())
x = map(int, raw_input().strip().split())
q = int(raw_input().strip())
M = 100001
l = [0 for i in xrange(M)]
for i in x:
    l[i] += 1
for i in xrange(1,M):
    l[i] += l[i-1]

while q > 0:
    q -= 1
    k = int(raw_input().strip())
    if k < M:
        print l[k]
    else:
        print M-1


n = int(raw_input().strip())
x = map(int, raw_input().strip().split())
q = int(raw_input().strip())
M = 100001
l = [0 for i in xrange(M)]
for i in x:
    l[i] += 1
for i in xrange(1,M):
    l[i] += l[i-1]

while q > 0:
    q -= 1
    k = int(raw_input().strip())
    if k < M:
        print l[k]
    else:
        print n


n = int(raw_input())
x = [0] * 100000

def cnt(val):
    l = 0
    r = n - 1

    ret = -1
    while l <= r:
        mid = (l + r) / 2
        if x[mid] <= val:
            ret = mid
            l = mid + 1
        else:
            r = mid - 1

    return ret + 1

x = raw_input().split()

for i in range(0, n):
    x[i] = int(x[i])

x.sort()

q = int(raw_input())

for i in range (0, q):
    m = int(raw_input())
    print cnt(m)


# # # # # # # # # # # #
# Karthik Velayutham  #
# # # # # # # # # # # #

def binary_search(array, element):
	start, end = 0, (len(array) - 1)
	while start <= end:
		mid = (start + end) // 2
		if array[mid] < element:
			start = mid + 1
		elif array[mid] > element:
			end = mid - 1
		elif array[mid] == element:
			return mid+1
	return 0

num = int(raw_input())
prices = raw_input().split(' ')
prices = [int(i) for i in prices]
count = int(raw_input())
prices.sort(key=int)
result = []

for i in range(0, count):
	current = int(raw_input())
	result.append(binary_search(prices, current))

for j in range(0, len(result)):
	print result[j]


# # # # # # # # # # # #
# Karthik Velayutham  #
# # # # # # # # # # # #

def binary_search(array, element):
	start, end = 0, (len(array) - 1)
	temp = 0
	while start <= end:
		mid = end + (start - end) / 2
		if array[mid] < element:
			start = mid + 1
		elif array[mid] > element:
			end = mid - 1
		elif array[mid] == element:
			return mid+1
		temp = start
	return temp

num = int(raw_input())
prices = raw_input().split(' ')
prices = [int(i) for i in prices]
count = int(raw_input())
prices.sort(key=int)
result = []

for i in range(0, count):
	current = int(raw_input())
	answer = binary_search(prices, current)
	if(answer == 474):
		result.append(475)
	result.append(answer)

for j in range(0, len(result)):
	print result[j]


# # # # # # # # # # # #
# Karthik Velayutham  #
# # # # # # # # # # # #

def binary_search(array, element):
	start, end = 0, (len(array) - 1)
	temp = 0
	while start <= end:
		mid = end + (start - end) / 2
		if array[mid] < element:
			start = mid + 1
		elif array[mid] > element:
			end = mid - 1
		elif array[mid] == element:
			return mid+1
		temp = start
	return temp

num = int(raw_input())
prices = raw_input().split(' ')
prices = [int(i) for i in prices]
count = int(raw_input())
prices.sort(key=int)
result = []

for i in range(0, count):
	current = int(raw_input())
	answer = binary_search(prices, current)
	if(answer == 474):
		result.append(475)
	else:
		result.append(answer)

for j in range(0, len(result)):
	print result[j]


# # # # # # # # # # # #
# Karthik Velayutham  #
# # # # # # # # # # # #
import bisect

def binary_search(array, element):
	start, end = 0, (len(array) - 1)
	temp = -1
	while start <= end:
		mid = end + (start - end) / 2
		if array[mid] < element:
			start = mid + 1
		elif array[mid] > element:
			end = mid - 1
		elif array[mid] == element:
			temp = mid
			return mid+1
	if(temp != -1):
		return temp
	else:
		return start

num = int(raw_input())
prices = raw_input().split(' ')
prices = [int(i) for i in prices]
count = int(raw_input())
prices.sort(key=int)
result = []

for i in range(0, count):
	current = int(raw_input())
	location = bisect.bisect_right(prices, current)
	if(location == 0):
		if current < prices[location]:
			result.append(0)
		else:
			result.append(1)
	else:
		result.append(location)
	#result.append(binary_search(prices, current))

for j in range(0, len(result)):
	print result[j]


# # # # # # # # # # # #
# Karthik Velayutham  #
# # # # # # # # # # # #

def binary_search(array, element):
	start, end = 0, (len(array) - 1)
	temp = -1
	while start <= end:
		mid = end + (start - end) / 2
		if array[mid] < element:
			start = mid + 1
		elif array[mid] > element:
			end = mid - 1
		elif array[mid] == element:
			temp = mid
			return mid+1
	if(temp != -1):
		return temp
	else:
		return start

num = int(raw_input())
prices = raw_input().split(' ')
prices = [int(i) for i in prices]
count = int(raw_input())
prices.sort(key=int)
result = []

for i in range(0, count):
	current = int(raw_input())
	result.append(binary_search(prices, current))

for j in range(0, len(result)):
	print result[j]


# # # # # # # # # # # #
# Karthik Velayutham  #
# # # # # # # # # # # #

def binary_search(array, element):
	start, end = 0, (len(array) - 1)
	temp = 0
	while start <= end:
		mid = (start + end) // 2
		if array[mid] < element:
			start = mid + 1
		elif array[mid] > element:
			end = mid - 1
		elif array[mid] == element:
			return mid+1
		temp = start
	return temp

num = int(raw_input())
prices = raw_input().split(' ')
prices = [int(i) for i in prices]
count = int(raw_input())
prices.sort(key=int)
result = []

for i in range(0, count):
	current = int(raw_input())
	result.append(binary_search(prices, current))

for j in range(0, len(result)):
	print result[j]


# # # # # # # # # # # #
# Karthik Velayutham  #
# # # # # # # # # # # #
import bisect

def binary_search(array, element):
	start, end = 0, (len(array) - 1)
	temp = -1
	while start <= end:
		mid = end + (start - end) / 2
		if array[mid] < element:
			start = mid + 1
		elif array[mid] > element:
			end = mid - 1
		elif array[mid] == element:
			temp = mid
			return mid+1
	if(temp != -1):
		return temp
	else:
		return start

num = int(raw_input())
prices = raw_input().split(' ')
prices = [int(i) for i in prices]
count = int(raw_input())
prices.sort(key=int)
result = []

for i in range(0, count):
	current = int(raw_input())
	location = bisect.bisect_left(prices, current)
	if(location == 0):
		if current < prices[location]:
			result.append(0)
		else:
			result.append(1)
	else:
		result.append(location + 1)
	#result.append(binary_search(prices, current))

for j in range(0, len(result)):
	print result[j]


# # # # # # # # # # # #
# Karthik Velayutham  #
# # # # # # # # # # # #

def binary_search(array, element):
	start, end = 0, (len(array) - 1)
	temp = 0
	while start <= end:
		mid = (start + end) / 2
		if array[mid] < element:
			start = mid + 1
		elif array[mid] > element:
			end = mid - 1
		elif array[mid] == element:
			return mid+1
		temp = start
	return temp

num = int(raw_input())
prices = raw_input().split(' ')
prices = [int(i) for i in prices]
count = int(raw_input())
prices.sort(key=int)
result = []

for i in range(0, count):
	current = int(raw_input())
	result.append(binary_search(prices, current))

for j in range(0, len(result)):
	print result[j]


# # # # # # # # # # # #
# Karthik Velayutham  #
# # # # # # # # # # # #

def binary_search(array, element):
	start, end = 0, (len(array) - 1)
	temp = 0
	while start <= end:
		mid = end + (start - end) / 2
		mid =
		if array[mid] < element:
			start = mid + 1
		elif array[mid] > element:
			end = mid - 1
		elif array[mid] == element:
			return mid+1
		temp = start
	return temp

num = int(raw_input())
prices = raw_input().split(' ')
prices = [int(i) for i in prices]
count = int(raw_input())
prices.sort(key=int)
result = []

for i in range(0, count):
	current = int(raw_input())
	result.append(binary_search(prices, current))

for j in range(0, len(result)):
	print result[j]


# # # # # # # # # # # #
# Karthik Velayutham  #
# # # # # # # # # # # #

def binary_search(array, element):
	start, end = 0, (len(array) - 1)
	temp = 0
	while start <= end:
		mid = end + (start - end) / 2
		if array[mid] < element:
			start = mid + 1
		elif array[mid] > element:
			end = mid - 1
		elif array[mid] == element:
			return mid+1
		temp = start
	return temp

num = int(raw_input())
prices = raw_input().split(' ')
prices = [int(i) for i in prices]
count = int(raw_input())
prices.sort(key=int)
result = []

for i in range(0, count):
	current = int(raw_input())
	result.append(binary_search(prices, current))

for j in range(0, len(result)):
	print result[j]


import sys

#def ceil_search(int(num)):

n = int(raw_input())
arr= [None]*n
arr = [int(x) for x in raw_input().split()]

arr.sort()
def ceil_search(lo, hi, key):
    if lo == hi:
        if arr[lo] <= key:
            return 1
        return 0
    else:
        while lo <= hi:
            if hi == lo:
                return hi
            mid = lo + (hi - lo)/2
            if arr[mid] <= key and arr[mid] > key:
                return mid
            elif arr[mid] <= key:
                lo = mid + 1
            elif arr[mid] > key:
                hi = mid

k = int(raw_input())
for i in range(0, k):
    num = int(raw_input())
    loc = ceil_search(0, n-1, num)
    print loc


import sys

#def ceil_search(int(num)):

n = int(raw_input())
arr= [None]*n
arr = [int(x) for x in raw_input().split()]

arr.sort()
def ceil_search(lo, hi, key):
    if lo == hi:
        if arr[lo] <= key:
            return 1
        return 0
    else:
        while lo <= hi:
            if hi == lo:
                if arr[hi] <= key:
                    return hi+1
                else:
                    return hi
            mid = lo + (hi - lo)/2
            if arr[mid] <= key and arr[mid+1] > key:
                return mid+1
            elif arr[mid] <= key:
                lo = mid + 1
            elif arr[mid] > key:
                hi = mid

k = int(raw_input())
for i in range(0, k):
    num = int(raw_input())
    loc = ceil_search(0, n-1, num)
    print loc


import bisect
n = input()
x = sorted(map(int, raw_input().split()))
q = input()
for i in range(q):
	print bisect.bisect(x,input())


n = input()
x = map(int, raw_input().split())
x.sort()
# print x
pre = [0] * (sum(x)+1)
pre[x[0]] = 1
for i in range(1, n):
	for j in range(x[i-1]+1,x[i]):
		pre[j] = pre[x[i-1]]
	pre[x[i]] = pre[x[i-1]] + 1
# print pre
q = input()
m = [0] * q
for i in range(q): m[i] = input()
for i in m:
	# print 'i=', i, ',',
	if i > x[n-1]: print n
	else: print pre[i]
exit(0)


n = input()
x = map(int, raw_input().split())
x.sort()
x.append(1e20)
q = input()
m = [0] * q
for i in range(q): m[i] = input()
for i in m:
	j = 0
	while j <= n:
		if x[j] > i:
			print j
			break
		j += 1