#include <bits/stdc++.h>
#define pb push_back
#define  mp make_pair
#define pp pop_back
#define pf push_front
#define fi first
#define se second
#define maxn 1000005
typedef long long ll;
using namespace std;
#define pi pair<ll,ll >
#define pii pair<ll, pair<ll,ll> >
#define mod 998244353
#define debug_iv_ac ios::sync_with_stdio(false);cin.tie(NULL);

/*
struct node{
    int d,v,i;
    node(){}

    node(int _d, int _v,int _i){
        d = _d,v = _v,i = _i;
    }

    bool operator < (node X)const{
        if(d != X.d) return d > X.d;
        //if(pos!=X.pos) return X.pos<pos;
        return v > X.v;
    }
};
*/

ll power(ll x, ll y, ll p)
{
    ll res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
                // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}

ll add(ll a, ll b)
{
    a%=mod;b%=mod;
    return (a+b)%mod;
}

ll sub(ll a,ll b)
{
    a%=mod;b%=mod;
    return (a-b+mod)%mod;
}
ll prod(ll a,ll b)
{
    a%=mod;
    b%=mod;
    return (a*b)%mod;
}

pi solve(ll a, ll b,ll p, ll g)
{
    if(a%b==0) return mp(0,1);
    pi p1 = solve(b, a%b,p,g);
    return {p1.se, p1.fi-(p1.se*(a/b))};
}
vector<pair<ll,pair<ll,pi> > > v;
int n;
bool check(int i, int j)
{
    ll x = min(v[i].fi,v[j].fi),xx = max(v[i].fi,v[j].fi);
    ll y = min(v[i].se.fi,v[j].se.fi),yy = max(v[i].se.fi,v[j].se.fi);
    ll z = min(v[i].se.se.fi,v[j].se.se.fi),zz = max(v[i].se.se.fi,v[j].se.se.fi);
    for(int l=0;l<n;l++)
    {
        if(i==l||j==l) continue;
        if(v[l].fi>=x&&v[l].fi<=xx&&v[l].se.fi>=y&&v[l].se.fi<=yy&&v[l].se.se.fi>=z&&v[l].se.se.fi<=zz) return 0;
    }
    return 1;
}
int main()
{

    debug_iv_ac
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        ll x,y,z;
        cin>>x>>y>>z;
        v.pb({x,{y,{z,i}}});
    }
    sort(v.begin(),v.end());
    if(check(n/2-1,n/2))
    {
        int l = n/2-1,r = n/2;
        while(l>=0)
        {
            cout<<v[l].se.se.se<<" "<<v[r].se.se.se<<endl;
            l--,r++;
        }

    }
    else if(check(n/2-2,n/2-1))
    {
        int l = n/2-2,r = n/2-1;
        while(l>=0)
        {
            cout<<v[l].se.se.se<<" "<<v[r].se.se.se<<endl;
            l--;r++;
        }
        cout<<v[n-2].se.se.se<<" "<<v[n-1].se.se.se<<endl;
    }
    else
    {
        int l = n/2,r = n/2+1;
        while(r<n)
        {
            cout<<v[l].se.se.se<<" "<<v[r].se.se.se<<endl;
            l--;r++;
        }
        cout<<v[0].se.se.se<<" "<<v[1].se.se.se<<endl;
    }
    return  0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define mp make_pair

const long long SuperINF = 1e18;
ll int dist;
vector<pair<ll int, pair<ll int, ll int>>> arr;
ll n;
map<ll, ll> used;

int main() {
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		ll int a, b, c;
		cin >> a >> b >> c;
		arr.push_back(mp(a, mp(b, c)));
	}

	for (ll int i = 0; i < n; i++) {
		if (used[i])
			continue;

	    dist = SuperINF;
		ll int ind = -1;

		for (ll int j = 0; j < n; j++) {
			if (i == j)
				continue;

			if (used[j])
				continue;

			ll int x1 =arr[i].first;
			ll int y1 = arr[i].second.first;
			ll int z1 = arr[i].second.second;

			ll int x2 =arr[j].first;
			ll int y2 = arr[j].second.first;
			ll int z2 = arr[j].second.second;

			ll int d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2);

			if (d < dist) {
				dist = d;
				ind = j;
			}
		}

		cout << i + 1 << " " << ind + 1 << "\n";
		used[i] = 1;
		used[ind] = 1;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define mp make_pair

const long long SuperINF = 1e16;
ll dist;
vector<pair<ll, pair<ll, ll>>> arr;
ll n;
map<ll, ll> used;

int main() {
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		ll a, b, c;
		cin >> a >> b >> c;
		arr.push_back(mp(a, mp(b, c)));
	}

	for (int i = 0; i < n; i++) {
		if (used[i])
			continue;

	    dist = SuperINF;
		ll ind = -1;

		for (int j = 0; j < n; j++) {
			if (i == j)
				continue;

			if (used[j])
				continue;

			ll x1 =arr[i].first;
			ll y1 = arr[i].second.first;
			ll z1 = arr[i].second.second;

			ll x2 =arr[j].first;
			ll y2 = arr[j].second.first;
			ll z2 = arr[j].second.second;

			ll d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2);

			if (d < dist) {
				dist = d;
				ind = j;
			}
		}

		cout << i + 1 << " " << ind + 1 << "\n";
		used[i] = 1;
		used[ind] = 1;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define mp make_pair

const long long SuperINF = 1e20;
ll dist;
vector<pair<ll, pair<ll, ll>>> arr;
ll n;
map<ll, ll> used;

int main() {
	cin >> n;

	for (int i = 0; i < n; i++) {
	    ll a, b, c;
		cin >> a >> b >> c;
		arr.push_back(mp(a, mp(b, c)));
		if (used[i])
			continue;

	    dist = SuperINF;
		ll ind = -1;

		for (int j = 0; j < n; j++) {
			if (i == j)
				continue;

			if (used[j])
				continue;

			ll x1 =arr[i].first;
			ll y1 = arr[i].second.first;
			ll z1 = arr[i].second.second;

			ll x2 =arr[j].first;
			ll y2 = arr[j].second.first;
			ll z2 = arr[j].second.second;

			ll d = pow(x2 - x1, 2) + pow(y2 - y1, 2) + pow(z2 - z1, 2);

			if (d < dist) {
				dist = d;
				ind = j;
			}
		}

		cout << i + 1 << " " << ind + 1 << "\n";
		used[i] = 1;
		used[ind] = 1;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define mp make_pair

const long long SuperINF = 1e8;
ll dist;
vector<pair<ll, pair<ll, ll>>> arr;
ll n;
map<ll, ll> used;

int main() {
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		ll a, b, c;
		cin >> a >> b >> c;
		arr.push_back(mp(a, mp(b, c)));
	}

	for (int i = 0; i < n; i++) {
		if (used[i])
			continue;

	    dist = SuperINF;
		ll ind = -1;

		for (int j = 0; j < n; j++) {
			if (i == j)
				continue;

			if (used[j])
				continue;

			ll x1 =arr[i].first;
			ll y1 = arr[i].second.first;
			ll z1 = arr[i].second.second;

			ll x2 =arr[j].first;
			ll y2 = arr[j].second.first;
			ll z2 = arr[j].second.second;

			ll d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2);

			if (d < dist) {
				dist = d;
				ind = j;
			}
		}

		cout << i + 1 << " " << ind + 1 << "\n";
		used[i] = 1;
		used[ind] = 1;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define mp make_pair

const long long SuperINF = 1e20;
ll int dist;
vector<pair<ll int, pair<ll int, ll int>>> arr;
ll n;
map<ll, ll> used;

int main() {
	cin >> n;

	for (ll int i = 0; i < n; i++) {
	    	ll int a, b, c;
		cin >> a >> b >> c;
		arr.push_back(mp(a, mp(b, c)));
		if (used[i])
			continue;

	    dist = SuperINF;
		ll int ind = -1;

		for (ll int j = 0; j < n; j++) {
			if (i == j)
				continue;

			if (used[j])
				continue;

			ll int x1 =arr[i].first;
			ll int y1 = arr[i].second.first;
			ll int z1 = arr[i].second.second;

			ll int x2 =arr[j].first;
			ll int y2 = arr[j].second.first;
			ll int z2 = arr[j].second.second;

			ll int d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2);

			if (d < dist) {
				dist = d;
				ind = j;
			}
		}

		cout << i + 1 << " " << ind + 1 << "\n";
		used[i] = 1;
		used[ind] = 1;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct Point
{
    int x,y,z,id;
    Point() {};
    Point(int _x, int _y, int _z, int _id)
    {
        x=_x;
        y=_y;
        z=_z;
        id=_id;
    }
    bool operator < (const Point &p) const
    {
        if(p.x==x)
        {
            if(p.y==y)
                return p.z<=z;
            return p.y<=y;
        }
        return p.x<=x;
    }
} arr[200004];
int main()
{
    int n,odd=0,tot,sum=0,x,y,z;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>x>>y>>z;
        arr[i]=Point(x,y,z,i+1);
    }
    //cout<<"-----------------"<<endl;
    sort(arr,arr+n);
    for(int i=0;i<n;i+=2)
    {
       // cout<<arr[i].x<<" "<<arr[i].y<<" "<<arr[i].z<<endl;
        cout<<arr[i].id<<" "<<arr[i+1].id<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
bool check[5000];
struct Point
{
    int x,y,z,id;
    Point() {};
    Point(int _x, int _y, int _z, int _id)
    {
        x=_x;
        y=_y;
        z=_z;
        id=_id;
    }
    bool operator < (const Point &p) const
    {
        if(p.x==x)
        {
            if(p.y==y)
                return p.z<z;
            return p.y<y;
        }
        return p.x<x;
    }
} arr[200004];
int main()
{
    int n,odd=0,tot,sum=0,x,y,z;
    vector<pair<int,int> >ans;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>x>>y>>z;
        arr[i]=Point(x,y,z,i+1);
    }
    sort(arr,arr+n);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.y==q.y) || (p.x==q.x && p.z==q.z) ) && check[i]==0 && check[j]==0)
            {
                ans.push_back(make_pair(p.id,q.id));
                check[i]=1;
                check[j]=1;
            }
        }
    }
    set<Point>st;
    for(int i=0; i<n; i++)
    {
        if(check[i]==0)
            st.insert(arr[i]);
    }
    set<Point>::iterator it,pt;
    int cnt=0;
    Point p,q;
    for(it=st.begin(); it!=st.end(); it++)
    {
        if(cnt==0)
            p=*it,cnt++;
        else
        {
            q=*it;
            cnt=0;
            ans.push_back(make_pair(p.id,q.id));
        }
    }
    for(int i=0; i<ans.size(); i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include<bits/stdc++.h>
using namespace std;
bool check[5000];
struct Point
{
    int x,y,z,id;
    Point() {};
    Point(int _x, int _y, int _z, int _id)
    {
        x=_x;
        y=_y;
        z=_z;
        id=_id;
    }
    bool operator < (const Point &p) const
    {
        if(p.x==x)
        {
            if(p.y==y)
                return p.z<=z;
            return p.y<=y;
        }
        return p.x<=x;
    }
} arr[200004];
int main()
{
    int n,odd=0,tot,sum=0,x,y,z;
    vector<pair<int,int> >ans;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>x>>y>>z;
        arr[i]=Point(x,y,z,i+1);
    }
    //sort(arr,arr+n);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.y==q.y) || (p.x==q.x && p.z==q.z)  || (p.y==q.y && p.z==q.z) ) && check[i]==0 && check[j]==0)
            {
                ans.push_back(make_pair(p.id,q.id));
                check[i]=1;
                check[j]=1;
            }
        }
    }
    set<Point>st;
    for(int i=0; i<n; i++)
    {
        if(check[i]==0)
            st.insert(arr[i]);
    }
    //cout<<ans.size()<<endl;
    set<Point>::iterator it,pt;
    int cnt=0;
    Point p,q;
    for(it=st.begin(); it!=st.end(); it++)
    {
        if(cnt==0)
            p=*it,cnt++;
        else
        {
            q=*it;
            cnt=0;
            ans.push_back(make_pair(p.id,q.id));
        }
    }
    for(int i=0; i<ans.size(); i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include<bits/stdc++.h>
using namespace std;
bool check[5000];
struct Point
{
    int x,y,z,id;
    Point() {};
    Point(int _x, int _y, int _z, int _id)
    {
        x=_x;
        y=_y;
        z=_z;
        id=_id;
    }
    bool operator < (const Point &p) const
    {
        if(p.x==x)
        {
            if(p.y==y)
                return p.z<=z;
            return p.y<=y;
        }
        return p.x<=x;
    }
} arr[200004];
int main()
{
    int n,odd=0,tot,sum=0,x,y,z;
    vector<pair<int,int> >ans;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>x>>y>>z;
        arr[i]=Point(x,y,z,i+1);
    }
    sort(arr,arr+n);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.y==q.y) || (p.x==q.x && p.z==q.z) ) && check[i]==0 && check[j]==0)
            {
                ans.push_back(make_pair(p.id,q.id));
                check[i]=1;
                check[j]=1;
            }
        }
    }
    set<Point>st;
    for(int i=0; i<n; i++)
    {
        if(check[i]==0)
            st.insert(arr[i]);
    }
    set<Point>::iterator it,pt;
    int cnt=0;
    Point p,q;
    for(it=st.begin(); it!=st.end(); it++)
    {
        if(cnt==0)
            p=*it,cnt++;
        else
        {
            q=*it;
            cnt=0;
            ans.push_back(make_pair(p.id,q.id));
        }
    }
    for(int i=0; i<ans.size(); i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include<bits/stdc++.h>
using namespace std;
bool check[5000];
struct Point
{
    int x,y,z,id;
    Point() {};
    Point(int _x, int _y, int _z, int _id)
    {
        x=_x;
        y=_y;
        z=_z;
        id=_id;
    }
    bool operator < (const Point &p) const
    {
        if(p.x==x)
        {
            if(p.y==y)
                return p.z<=z;
            return p.y<=y;
        }
        return p.x<=x;
    }
} arr[200004];
bool cmpx(Point p, Point q)
{
    if(p.x==q.x)
    {
        if(p.y==q.y)
            return p.z<q.z;
        return p.y<q.y;
    }
    return p.x<q.x;
}
bool cmpy(Point p, Point q)
{
    if(p.y==q.y)
    {
        if(p.x==q.x)
            return p.z<q.z;
        return p.x<q.x;
    }
    return p.y<q.y;
}
bool cmpz(Point p, Point q)
{
    if(p.z==q.z)
    {
        if(p.x==q.x)
            return p.y<q.y;
        return p.x<q.x;
    }
    return p.z<q.z;
}
int main()
{
    int n,odd=0,tot,sum=0,x,y,z;
    vector<pair<int,int> >ans;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>x>>y>>z;
        arr[i]=Point(x,y,z,i+1);
    }
    sort(arr,arr+n,cmpx);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.y==q.y) || (p.x==q.x && p.z==q.z) ) && check[p.id]==0 && check[q.id]==0)
            {
               // cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( p.x==q.x ) && check[p.id]==0 && check[q.id]==0)
            {
                // cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    sort(arr,arr+n,cmpy);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.y==q.y) || (p.y==q.y && p.z==q.z) ) && check[p.id]==0 && check[q.id]==0)
            {
                //cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if((  p.y==q.y ) && check[p.id]==0 && check[q.id]==0)
            {
                //cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    sort(arr,arr+n,cmpz);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.z==q.z) || (p.y==q.y && p.z==q.z) ) && check[p.id]==0 && check[q.id]==0)
            {
               // cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( p.z==q.z ) && check[p.id]==0 && check[q.id]==0)
            {
                // cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    Point p,q;
    sort(arr,arr+n,cmpx);
    int cnt=0;
    for(int i=0; i<n; i++)
    {
        if(check[arr[i].id]==0)
        {
            if(cnt==0)
                p=arr[i],cnt++;
            else
            {
                cnt=0;
                ans.push_back(make_pair(p.id,arr[i].id));
            }
        }
    }
    for(int i=0; i<ans.size(); i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include<bits/stdc++.h>
using namespace std;
bool check[5000];
struct Point
{
    int x,y,z,id;
    Point() {};
    Point(int _x, int _y, int _z, int _id)
    {
        x=_x;
        y=_y;
        z=_z;
        id=_id;
    }
    bool operator < (const Point &p) const
    {
        if(p.x==x)
        {
            if(p.y==y)
                return p.z<=z;
            return p.y<=y;
        }
        return p.x<=x;
    }
} arr[200004];
bool cmpx(Point p, Point q)
{
    if(p.x==q.x)
    {
        if(p.y==q.y)
            return p.z<q.z;
        return p.y<q.y;
    }
    return p.x<q.x;
}
bool cmpy(Point p, Point q)
{
    if(p.y==q.y)
    {
        if(p.x==q.x)
            return p.z<q.z;
        return p.x<q.x;
    }
    return p.y<q.y;
}
bool cmpz(Point p, Point q)
{
    if(p.z==q.z)
    {
        if(p.x==q.x)
            return p.y<q.y;
        return p.x<q.x;
    }
    return p.z<q.z;
}
int main()
{
    int n,odd=0,tot,sum=0,x,y,z;
    vector<pair<int,int> >ans;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>x>>y>>z;
        arr[i]=Point(x,y,z,i+1);
    }
    sort(arr,arr+n,cmpx);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( p.x==q.x ) && check[p.id]==0 && check[q.id]==0)
            {
                // cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    sort(arr,arr+n,cmpy);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if((  p.y==q.y ) && check[p.id]==0 && check[q.id]==0)
            {
                //cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    sort(arr,arr+n,cmpz);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( p.z==q.z ) && check[p.id]==0 && check[q.id]==0)
            {
                // cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    Point p,q;
    sort(arr,arr+n,cmpx);
    int cnt=0;
    for(int i=0; i<n; i++)
    {
        if(check[arr[i].id]==0)
        {
            if(cnt==0)
                p=arr[i],cnt++;
            else
            {
                cnt=0;
                ans.push_back(make_pair(p.id,arr[i].id));
            }
        }
    }
    for(int i=0; i<ans.size(); i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include<bits/stdc++.h>
using namespace std;
bool check[5000];
struct Point
{
    int x,y,z,id;
    Point() {};
    Point(int _x, int _y, int _z, int _id)
    {
        x=_x;
        y=_y;
        z=_z;
        id=_id;
    }
    bool operator < (const Point &p) const
    {
        if(p.x==x)
        {
            if(p.y==y)
                return p.z<=z;
            return p.y<=y;
        }
        return p.x<=x;
    }
} arr[200004];
bool cmpx(Point p, Point q)
{
    if(p.x==q.x)
    {
        if(p.y==q.y)
            return p.z<q.z;
        return p.y<q.y;
    }
    return p.x<q.x;
}
bool cmpy(Point p, Point q)
{
    if(p.y==q.y)
    {
        if(p.x==q.x)
            return p.z<q.z;
        return p.x<q.x;
    }
    return p.y<q.y;
}
bool cmpz(Point p, Point q)
{
    if(p.z==q.z)
    {
        if(p.y==q.y)
            return p.x<q.x;
        return p.y<q.y;
    }
    return p.z<q.z;
}
int main()
{
    int n,odd=0,tot,sum=0,x,y,z;
    vector<pair<int,int> >ans;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>x>>y>>z;
        arr[i]=Point(x,y,z,i+1);
    }
    sort(arr,arr+n,cmpx);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.y==q.y) || (p.x==q.x && p.z==q.z) ) && check[p.id]==0 && check[q.id]==0)
            {
               // cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    sort(arr,arr+n,cmpy);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.y==q.y) || (p.y==q.y && p.z==q.z) ) && check[p.id]==0 && check[q.id]==0)
            {
                //cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    sort(arr,arr+n,cmpz);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.z==q.z) || (p.y==q.y && p.z==q.z) ) && check[p.id]==0 && check[q.id]==0)
            {
               // cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    if(ans.size()==n/2)
    {
        for(int i=0; i<ans.size(); i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
        return 0;
    }
    //cout<<ans.size()<<endl;
    set<Point>st;
    for(int i=0; i<n; i++)
    {
        if(check[arr[i].id]==0)
            st.insert(arr[i]);
    }
    set<Point>::iterator it;
    int cnt=0;
    Point p,q;
    for(it=st.begin(); it!=st.end(); it++)
    {
        if(cnt==0)
            p=*it,cnt++;
        else
        {
            q=*it;
            cnt=0;
            ans.push_back(make_pair(p.id,q.id));
        }
    }
    for(int i=0; i<ans.size(); i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include<bits/stdc++.h>
using namespace std;
bool check[5000];
struct Point
{
    int x,y,z,id;
    Point() {};
    Point(int _x, int _y, int _z, int _id)
    {
        x=_x;
        y=_y;
        z=_z;
        id=_id;
    }
    bool operator < (const Point &p) const
    {
        if(p.x==x)
        {
            if(p.y==y)
                return p.z<=z;
            return p.y<=y;
        }
        return p.x<=x;
    }
} arr[200004];
bool cmpx(Point p, Point q)
{
    if(p.x==q.x)
    {
        if(p.y==q.y)
            return p.z<q.z;
        return p.y<q.y;
    }
    return p.x<q.x;
}
bool cmpy(Point p, Point q)
{
    if(p.y==q.y)
    {
        if(p.x==q.x)
            return p.z<q.z;
        return p.x<q.x;
    }
    return p.y<q.y;
}
bool cmpz(Point p, Point q)
{
    if(p.z==q.z)
    {
        if(p.x==q.x)
            return p.y<q.y;
        return p.x<q.x;
    }
    return p.z<q.z;
}
int main()
{
    int n,odd=0,tot,sum=0,x,y,z;
    vector<pair<int,int> >ans;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>x>>y>>z;
        arr[i]=Point(x,y,z,i+1);
    }
    sort(arr,arr+n,cmpx);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.y==q.y) || (p.x==q.x && p.z==q.z) ) && check[p.id]==0 && check[q.id]==0)
            {
               // cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    sort(arr,arr+n,cmpy);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.y==q.y) || (p.y==q.y && p.z==q.z) ) && check[p.id]==0 && check[q.id]==0)
            {
                //cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    sort(arr,arr+n,cmpz);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.z==q.z) || (p.y==q.y && p.z==q.z) ) && check[p.id]==0 && check[q.id]==0)
            {
               // cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    if(ans.size()==n/2)
    {
        for(int i=0; i<ans.size(); i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
        return 0;
    }
    Point p,q;
    sort(arr,arr+n,cmpx);
    int cnt=0;
    for(int i=0; i<n; i++)
    {
        if(check[arr[i].id]==0)
            {
                if(cnt==0)
                    p=arr[i],cnt++;
                else
                {
                    cnt=0;
                    ans.push_back(make_pair(p.id,arr[i].id));
                }
            }
    }
    for(int i=0; i<ans.size(); i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include<bits/stdc++.h>
using namespace std;
bool check[5000];
struct Point
{
    int x,y,z,id;
    Point() {};
    Point(int _x, int _y, int _z, int _id)
    {
        x=_x;
        y=_y;
        z=_z;
        id=_id;
    }
    bool operator < (const Point &p) const
    {
        if(p.x==x)
        {
            if(p.y==y)
                return p.z<=z;
            return p.y<=y;
        }
        return p.x<=x;
    }
} arr[200004];
int main()
{
    int n,odd=0,tot,sum=0,x,y,z;
    vector<pair<int,int> >ans;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>x>>y>>z;
        arr[i]=Point(x,y,z,i+1);
    }
    sort(arr,arr+n);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.y==q.y) || (p.x==q.x && p.z==q.z)  || (p.y==q.y && p.z==q.z) ) && check[i]==0 && check[j]==0)
            {
                ans.push_back(make_pair(p.id,q.id));
                check[i]=1;
                check[j]=1;
            }
        }
    }
    set<Point>st;
    for(int i=0; i<n; i++)
    {
        if(check[i]==0)
            st.insert(arr[i]);
    }
    //cout<<ans.size()<<endl;
    set<Point>::iterator it,pt;
    int cnt=0;
    Point p,q;
    for(it=st.begin(); it!=st.end(); it++)
    {
        if(cnt==0)
            p=*it,cnt++;
        else
        {
            q=*it;
            cnt=0;
            ans.push_back(make_pair(p.id,q.id));
        }
    }
    for(int i=0; i<ans.size(); i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include<bits/stdc++.h>
using namespace std;
bool check[5000];
struct Point
{
    int x,y,z,id;
    Point() {};
    Point(int _x, int _y, int _z, int _id)
    {
        x=_x;
        y=_y;
        z=_z;
        id=_id;
    }
    bool operator < (const Point &p) const
    {
        if(p.x==x)
        {
            if(p.y==y)
                return p.z<=z;
            return p.y<=y;
        }
        return p.x<=x;
    }
} arr[200004];
bool cmpx(Point p, Point q)
{
    if(p.x==q.x)
    {
        if(p.y==q.y)
            return p.z<q.z;
        return p.y<q.y;
    }
    return p.x<q.x;
}
bool cmpy(Point p, Point q)
{
    if(p.y==q.y)
    {
        if(p.x==q.x)
            return p.z<q.z;
        return p.x<q.x;
    }
    return p.y<q.y;
}
bool cmpz(Point p, Point q)
{
    if(p.z==q.z)
    {
        if(p.x==q.x)
            return p.y<q.y;
        return p.x<q.x;
    }
    return p.z<q.z;
}
int main()
{
    int n,odd=0,tot,sum=0,x,y,z;
    vector<pair<int,int> >ans;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>x>>y>>z;
        arr[i]=Point(x,y,z,i+1);
    }
    sort(arr,arr+n,cmpx);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.y==q.y) || (p.x==q.x && p.z==q.z) ) && check[p.id]==0 && check[q.id]==0)
            {
               // cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    sort(arr,arr+n,cmpy);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.y==q.y) || (p.y==q.y && p.z==q.z) ) && check[p.id]==0 && check[q.id]==0)
            {
                //cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    sort(arr,arr+n,cmpz);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.z==q.z) || (p.y==q.y && p.z==q.z) ) && check[p.id]==0 && check[q.id]==0)
            {
               // cout<<p.id<<"--"<<q.id<<endl;
                ans.push_back(make_pair(p.id,q.id));
                check[p.id]=1;
                check[q.id]=1;
            }
        }
    }
    if(ans.size()==n/2)
    {
        for(int i=0; i<ans.size(); i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
        return 0;
    }
    //cout<<ans.size()<<endl;
    set<Point>st;
    for(int i=0; i<n; i++)
    {
        if(check[arr[i].id]==0)
            st.insert(arr[i]);
    }
    set<Point>::iterator it;
    int cnt=0;
    Point p,q;
    for(it=st.begin(); it!=st.end(); it++)
    {
        if(cnt==0)
            p=*it,cnt++;
        else
        {
            q=*it;
            cnt=0;
            ans.push_back(make_pair(p.id,q.id));
        }
    }
    for(int i=0; i<ans.size(); i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include<bits/stdc++.h>
using namespace std;
bool check[5000];
struct Point
{
    int x,y,z,id;
    Point() {};
    Point(int _x, int _y, int _z, int _id)
    {
        x=_x;
        y=_y;
        z=_z;
        id=_id;
    }
    bool operator < (const Point &p) const
    {
        if(p.x==x)
        {
            if(p.y==y)
                return p.z<=z;
            return p.y<=y;
        }
        return p.x<=x;
    }
} arr[200004];
bool cmpx(Point p, Point q)
{
    if(p.x==q.x)
    {
        if(p.y==q.y)
            return p.z<q.z;
        return p.y<q.y;
    }
    return p.x<q.x;
}
bool cmpy(Point p, Point q)
{
    if(p.y==q.y)
    {
        if(p.x==q.x)
            return p.z<q.z;
        return p.x<q.x;
    }
    return p.y<q.y;
}
bool cmpz(Point p, Point q)
{
    if(p.z==q.z)
    {
        if(p.y==q.y)
            return p.x<q.x;
        return p.y<q.y;
    }
    return p.z<q.z;
}
int main()
{
    int n,odd=0,tot,sum=0,x,y,z;
    vector<pair<int,int> >ans;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>x>>y>>z;
        arr[i]=Point(x,y,z,i+1);
    }
    sort(arr,arr+n,cmpx);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.y==q.y) || (p.x==q.x && p.z==q.z) ) && check[i]==0 && check[j]==0)
            {
                ans.push_back(make_pair(p.id,q.id));
                check[i]=1;
                check[j]=1;
            }
        }
    }
    sort(arr,arr+n,cmpy);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.y==q.y) || (p.y==q.y && p.z==q.z) ) && check[i]==0 && check[j]==0)
            {
                ans.push_back(make_pair(p.id,q.id));
                check[i]=1;
                check[j]=1;
            }
        }
    }
    sort(arr,arr+n,cmpz);
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            Point p=arr[i];
            Point q=arr[j];
            if(( (p.x==q.x && p.z==q.z) || (p.y==q.y && p.z==q.z) ) && check[i]==0 && check[j]==0)
            {
                ans.push_back(make_pair(p.id,q.id));
                check[i]=1;
                check[j]=1;
            }
        }
    }
    set<Point>st;
    for(int i=0; i<n; i++)
    {
        if(check[i]==0)
            st.insert(arr[i]);
    }
    set<Point>::iterator it,pt;
    int cnt=0;
    Point p,q;
    for(it=st.begin(); it!=st.end(); it++)
    {
        if(cnt==0)
            p=*it,cnt++;
        else
        {
            q=*it;
            cnt=0;
            ans.push_back(make_pair(p.id,q.id));
        }
    }
    for(int i=0; i<ans.size(); i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
using namespace std;
typedef long long int ll;

int used[500100];

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	vector<pair<pair<int,int>,pair<int,int> > > v;
	for(int i=0;i<n;i++){
		int x,y,z; cin >> x >> y >> z;
		v.push_back(make_pair(make_pair(x,y),make_pair(z,i+1)));
	}
	sort(v.begin(),v.end());
	queue<pair<pair<int,int>,pair<int,int> > >q;
	for(int i=0;i<n;i++){
		queue<pair<pair<int,int>,pair<int,int> > >p;
		int cpx=v[i].first.first;
		while(i<n&&cpx==v[i].first.first){
			p.push(v[i]);
			i++;
		}
		i--;
		queue <pair<pair<int,int>,pair<int,int> > >r;
		while(p.size()>1){
			auto pp=p.front(); p.pop();
			auto ppp=p.front(); p.pop();
			if(pp.first.second==ppp.first.second)cout << pp.second.second << " " << ppp.second.second << endl;
			else{
				while(p.size()&&p.front().first.second==ppp.first.second){
					r.push(p.front());
					p.pop();
				}
				if(r.size()>1){
					auto rr=r.front(); r.pop();
					auto rrr=r.front(); r.pop();
					cout << rr.second.second << " " << rrr.second.second << endl;
				}
				if(r.size()){
					cout << ppp.second.second << " " << r.front().second.second << endl;
					r.pop();
					q.push(pp);
				}
				else{
					cout << pp.second.second << " " << ppp.second.second << endl;
				}
			}
		}
		stack <pair<pair<int,int>,pair<int,int> > >st;
		while(q.size()){
			st.push(q.front());
			q.pop();
		}
		while(st.size()>1){
			auto rr=st.top(); st.pop();
			auto rrr=st.top(); st.pop();
			cout << rr.second.second << " " << rrr.second.second << endl;
		}
		if(st.size()){
			q.push(st.top());
			st.pop();
		}
		if(p.size()){
			q.push(p.front());
			p.pop();
		}
		if(q.size()>1){
			auto rr=q.front(); q.pop();
			auto rrr=q.front(); q.pop();
			cout << rr.second.second << " " << rrr.second.second << endl;
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
using namespace std;
typedef long long int ll;

int used[500100];

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	vector<pair<pair<int,int>,pair<int,int> > > v;
	for(int i=0;i<n;i++){
		int x,y,z; cin >> x >> y >> z;
		v.push_back(make_pair(make_pair(x,y),make_pair(z,i+1)));
	}
	sort(v.begin(),v.end());

	queue<pair<pair<int,int>,pair<int,int> > >q;
	for(int i=0;i<n;i++){
		queue<pair<pair<int,int>,pair<int,int> > >p;
		int cpx=v[i].first.first;
		while(i<n&&cpx==v[i].first.first){
			p.push(v[i]);
			i++;
		}
		i--;
		stack <pair<pair<int,int>,pair<int,int> > >r;
		while(p.size()>1){
			auto pp=p.front(); p.pop();
			auto ppp=p.front(); p.pop();
			if(pp.first.second==ppp.first.second)cout << pp.second.second << " " << ppp.second.second << endl;
			else{
				while(p.size()&&p.front().second.first<=pp.second.first&&p.front().first.second==ppp.first.second){
					r.push(p.front());
					p.pop();
				}
				if(r.size()){
					cout << pp.second.second << " " << r.top().second.second << endl;
					r.pop();
					while(r.size()>1){
						auto rr=r.top(); r.pop();
						auto rrr=r.top(); r.pop();
						cout << rr.second.second << " " << rrr.second.second << endl;
					}
					if(r.size()){
						cout << ppp.second.second << " " << r.top().second.second << endl;
						r.pop();
					}
					else{
						q.push(ppp);
					}
				}
				else{
					cout << pp.second.second << " " << ppp.second.second << endl;
				}
			}
		}
		if(p.size()){
			q.push(p.front());
			p.pop();
		}
		if(q.size()>1){
			stack <pair<pair<int,int>,pair<int,int> > >r;
			while(q.size()){
				r.push(q.front());
				q.pop();
			}
			while(r.size()>1){
				auto rr=r.top(); r.pop();
				auto rrr=r.top(); r.pop();
				cout << rr.second.second << " " << rrr.second.second << endl;
			}
			if(r.size()){
				q.push(r.top());
				r.pop();
			}
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
typedef long long int ll;

int used[500100];

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	vector<pair<pair<int,int>,pair<int,int> >  > v;
	for(int i=0;i<n;i++){
		int x,y,z; cin >> x >> y >> z;
		v.push_back(make_pair(make_pair(x,y),make_pair(z,i+1)));
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n/2;i++){
		cout << v[i*2].second.second << " " << v[i*2+1].second.second << endl;
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
using namespace std;
typedef long long int ll;

int used[500100];

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	vector<pair<pair<int,int>,pair<int,int> > > v;
	for(int i=0;i<n;i++){
		int x,y,z; cin >> x >> y >> z;
		v.push_back(make_pair(make_pair(x,y),make_pair(z,i+1)));
	}
	sort(v.begin(),v.end());

	queue<pair<pair<int,int>,pair<int,int> > >q;
	for(int i=0;i<n;i++){
		queue<pair<pair<int,int>,pair<int,int> > >p;
		int cpx=v[i].first.first;
		while(i<n&&cpx==v[i].first.first){
			p.push(v[i]);
			i++;
		}
		i--;
		stack <pair<pair<int,int>,pair<int,int> > >r;
		while(p.size()>1){
			auto pp=p.front(); p.pop();
			auto ppp=p.front(); p.pop();
			if(pp.first.second==ppp.first.second)cout << pp.second.second << " " << ppp.second.second << endl;
			else{
				while(p.size()&&p.front().second.first<=pp.second.first&&p.front().first.first==ppp.first.second){
					r.push(p.front());
					p.pop();
				}
				if(r.size()){
					cout << pp.second.second << " " << r.top().second.second << endl;
					r.pop();
					while(r.size()>1){
						auto rr=r.top(); r.pop();
						auto rrr=r.top(); r.pop();
						cout << rr.second.second << " " << rrr.second.second << endl;
					}
					if(r.size()){
						cout << ppp.second.second << " " << r.top().second.second << endl;
						r.pop();
					}
					else{
						q.push(ppp);
					}
				}
				else{
					cout << pp.second.second << " " << ppp.second.second << endl;
				}
			}
		}
		if(p.size()){
			q.push(p.front());
			p.pop();
		}
		if(q.size()>1){
			stack <pair<pair<int,int>,pair<int,int> > >r;
			while(q.size()){
				r.push(q.front());
				q.pop();
			}
			while(r.size()>1){
				auto rr=r.top(); r.pop();
				auto rrr=r.top(); r.pop();
				cout << rr.second.second << " " << rrr.second.second << endl;
			}
			if(r.size()){
				q.push(r.top());
				r.pop();
			}
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
typedef long long int ll;

int used[500100];

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	vector<pair<pair<int,int>,pair<int,int> > > v;
	for(int i=0;i<n;i++){
		int x,y,z; cin >> x >> y >> z;
		v.push_back(make_pair(make_pair(x,y),make_pair(z,i+1)));
	}
	sort(v.begin(),v.end());

	queue<pair<pair<int,int>,pair<int,int> > >q;
	for(int i=0;i<n;i++){
		queue<pair<pair<int,int>,pair<int,int> > >p;
		int cpx=v[i].first.first;
		while(i<n&&cpx==v[i].first.first){
			p.push(v[i]);
			i++;
		}
		i--;
		while(p.size()>1){
			auto pp=p.front(); p.pop();
			auto ppp=p.front(); p.pop();
			cout << pp.second.second << " " << ppp.second.second << endl;
		}
		if(p.size()){
			if(q.size()){
				cout << q.front().second.second << " " << p.front().second.second << endl;
				q.pop();
				p.pop();
			}
			else{
				q.push(p.front());
				p.pop();
			}
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
using namespace std;
typedef long long int ll;

int used[500100];

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	vector<pair<pair<int,int>,pair<int,int> > > v;
	for(int i=0;i<n;i++){
		int x,y,z; cin >> x >> y >> z;
		v.push_back(make_pair(make_pair(x,y),make_pair(z,i+1)));
	}
	sort(v.begin(),v.end());
	queue<pair<pair<int,int>,pair<int,int> > >q;
	for(int i=0;i<n;i++){
		queue<pair<pair<int,int>,pair<int,int> > >p;
		int cpx=v[i].first.first;
		while(i<n&&cpx==v[i].first.first){
			p.push(v[i]);
			i++;
		}
		i--;
		queue <pair<pair<int,int>,pair<int,int> > >r;
		while(p.size()>1){
			auto pp=p.front(); p.pop();
			auto ppp=p.front(); p.pop();
			if(pp.first.second==ppp.first.second)cout << pp.second.second << " " << ppp.second.second << endl;
			else{
				while(p.size()&&p.front().first.second==ppp.first.second){
					r.push(p.front());
					p.pop();
				}
				while(r.size()>1){
					auto rr=r.front(); r.pop();
					auto rrr=r.front(); r.pop();
					cout << rr.second.second << " " << rrr.second.second << endl;
				}
				if(r.size()){
					cout << ppp.second.second << " " << r.front().second.second << endl;
					r.pop();
					q.push(pp);
				}
				else{
					cout << pp.second.second << " " << ppp.second.second << endl;
				}
			}
		}
		stack <pair<pair<int,int>,pair<int,int> > >st;
		while(q.size()){
			st.push(q.front());
			q.pop();
		}
		if(p.size()){
			st.push(p.front());
			p.pop();
		}
		while(st.size()>1){
			auto rr=st.top(); st.pop();
			auto rrr=st.top(); st.pop();
			cout << rr.second.second << " " << rrr.second.second << endl;
		}
		if(st.size()){
			q.push(st.top());
			st.pop();
		}
		if(q.size()>1){
			auto rr=q.front(); q.pop();
			auto rrr=q.front(); q.pop();
			cout << rr.second.second << " " << rrr.second.second << endl;
		}
	}
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
long long n, m, y=0, z = 10, a, b, c;
int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n;
    vector<pair<pair<long long , long long> , pair<long long , long long>>>v;
    for(int i=0 ; i<n ; i++){
        cin>>a>>b>>c;
        v.push_back({{a,b} , {c ,i}});
    }
    sort(v.begin() , v.end());
   while(v.size()>0){
        if(v.size() == 2)
                return cout<<v[0].second.second+1<<" "<<v[1].second.second+1 , 0;
        for(int i=0 ; i<v.size()-1 ; i++){
            bool x = false;
            for(int j = 0 ; j<v.size()-1 ; j++){
                if(j != i && j !=i+1){
                    if( v[j].first.first >= min( v[i].first.first , v[i+1].first.first) &&v[j].first.first <= max( v[i].first.first , v[i+1].first.first) &&
                        v[j].first.second >= min( v[i].first.second , v[i+1].first.second) &&v[j].first.second <= max( v[i].first.second , v[i+1].first.second) &&
                        v[j].second.first >= min( v[i].second.first , v[i+1].second.first) &&v[j].second.first <= max( v[i].second.first , v[i+1].second.first)){
                            x = true;
                            break;
                        }
                }
            }
            if(x == false){
                cout<<v[i].second.second+1<<" "<<v[i+1].second.second+1<<endl ;
               // cout<<v[i].first.first<<" "<<v[i].first.second<<" "<<v[i].second.first<<endl;
                v.erase(v.begin()+i);
                v.erase(v.begin()+i);
                break;
            }
        }
   }
    return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
long long n, m, x, y=0, z = 10, a, b, c;
int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n;
    vector<pair<pair<long long , long long> , pair<long long , long long>>>v;
    for(int i=0 ; i<n ; i++){
        cin>>a>>b>>c;
        v.push_back({{a,b} , {c ,i}});
    }
    sort(v.begin() , v.end());
   for(int i=0 ; i<n ; i+=2)
    cout<<v[i].second.second+1<<" "<<v[i+1].second.second+1<<endl;
    return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
long long n, m, y=0, z = 10, a, b, c;
int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n;
    vector<pair<pair<long long , long long> , pair<long long , long long>>>v;
    for(int i=0 ; i<n ; i++){
        cin>>a>>b>>c;
        v.push_back({{a,b} , {c ,i}});
    }
    sort(v.begin() , v.end());
    for(int i=0 ; i<v.size()-1 ; i++){
        bool x = false;
        for(int j = 0 ; j<v.size()-1 ; j++){
            if(j != i && j !=i+1){
                if( v[j].first.first >= min( v[i].first.first , v[i+1].first.first) &&v[j].first.first <= max( v[i].first.first , v[i+1].first.first) &&
                    v[j].first.second >= min( v[i].first.second , v[i+1].first.second) &&v[j].first.second <= max( v[i].first.second , v[i+1].first.second) &&
                    v[j].second.first >= min( v[i].second.first , v[i+1].second.first) &&v[j].second.first <= max( v[i].second.first , v[i+1].second.first)){
                        x = true;
                        break;
                    }
                else if(v[j].first.first < min( v[i].first.first , v[i+1].first.first) || v[j].first.first > max( v[i].first.first , v[i+1].first.first))
                    break;

            }
        }
        if(x == false){
            cout<<v[i].second.second+1<<" "<<v[i+1].second.second+1<<endl ;
            v.erase(v.begin()+i);
            v.erase(v.begin()+i);
            if(v.size()==0) break;
            i=-1;
            }
        }

    return 0;
}


def readAB():
    s = raw_input()
    i = s.index(' ')
    return int(s[:i]), int(s[i+1:])

n,k = readAB()
ii = []
for i in xrange(k + 1):
    ii.append(str(k + 1 - i))
for i in xrange(k + 1, n):
    ii.append(str(i + 1))
print ' '.join(ii)


n,k=map(int,raw_input().split())
lst=range(k+1,0,-1)+range(k+2,n+1)
print ' '.join(map(str,lst))


n,k=map(int,raw_input().split())
lst=range(k,0,-1)+range(k+1,n+1)
print ' '.join(map(str,lst))


n,k=map(int,raw_input().split())
print ' '.join(map(str,range(0,n,-1)))


n,k = map(int,raw_input().split())
for i in xrange(1,n-k):
	print i,
for i in xrange(n,n-k-1,-1):
	print i,


import sys

[n,k] = [int(x) for x in sys.stdin.readline().split()]

print ' '.join([str(x) for x in range(k+1, 0, -1)] + [str(x) for x in range(k+2, n+1)])


import sys

[n,k] = [int(x) for x in sys.stdin.readline().split()]

l1 = [str(x) for x in range(k+1, 0, -1)]
l2 = [str(x) for x in range(k+2, n+1)]
l3 = l1 + l2
print ' '.join(l3)


n, k = raw_input().split()
n = int(n)
k = int(k)
res = ""
for i in xrange(k+1, 0, -1):
	res += str(i) + " "
for i in xrange(k+2, n+1):
	res += str(i) + " "
print res


n,k=map(int,raw_input().split())
lst=[]
for i in range(1,1+n):
    lst.append(i)

lst[k-1],lst[k]=lst[k],lst[k-1]

for i in lst:
    print i,


n,k=map(int,raw_input().split())
lst=[1]
for i in range(n,n-k+1,-1):
    lst.append(i)
if k>0:

    for i in range(2,n-k+2):
        lst.append(i)
else:
    for i in range(2,n-k+1):
        lst.append(i)
for i in lst:
    print i,


n,k=map(int,raw_input().split())
lst=[]
for i in range(n,n-k,-1):
    lst.append(i)
for i in range(0,n-k):
    lst.append(i)

for i in lst:
    print i


n,k=map(int,raw_input().split())
lst=[]
for i in range(n,n-k,-1):
    lst.append(i)
for i in range(1,n-k+1):
    lst.append(i)

for i in lst:
    print i,


n,k=map(int,raw_input().split())
for i in xrange(k):
	print n-i,
for  i in xrange(n-k):
	print i+1,


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

ar = []

for i in range(k):
	ar.append(n-i)

temp = []
for i in range(1,n-k+1):
	temp.append(i)

final = ar + temp

for i in final:
	print i


inp1, inp2 = input().split(' ')
inp1 = int(inp1)
inp2 = int(inp2)

straight_list=range(inp2+1,inp1+1)
reversed_list=range(1,inp2+1)
reversed_list.reverse()

ans_list=straight_list+reversed_list

print " ".join(str(x) for x in ans_list)


n,k=map(int,raw_input().split())
for i in xrange(1,n-k):
    print i,
for i in xrange(n,n-k-1,-1):
    print i,


n, k = map(int, raw_input().split())
a, b = [], []
for i in range(k, n):
  a.append(i+1)
for i in range(k):
  b.append(i+1)
print ' '.join(map(str, a+list(reversed(b))))


n, k = map(int, raw_input().split())
i = n;
while i > n - k:
    print i
    i = i - 1
i = 1
while i <= n - k:
    print i
    i = i + 1


n,k=map(int,raw_input().split())
for i in range(n-k):
    print i + k + 1,
for j in range(k):
    print k - j,


import sys

n, k = [ int( x ) for x in sys.stdin.readline().strip().split() ]

a = [ k + 1 - i for i in range( k + 1 ) ] + range( k + 2 , n + 1 )
print ' '.join( [ str( x ) for x in a] )


import sys

n, k = [ int( x ) for x in sys.stdin.readline().strip().split() ]

a = [ k + 1 - i for i in range( k + 1 ) ] + range( k + 2 , n + 1 )
print a


l=lambda:map(int,raw_input().split())
n,k=l()
print ' '.join(map(str, range(k+1,0,-1))),
print ' '.join(map(str, range(k+2,n+1,1)))


n, k = map(int, raw_input().split())
r = map(str, range(1, n+1))
print ' '.join(r[k::-1] + r[k+1:n])


n, k = map(int, raw_input().split())
r = map(str, range(1, n+1))
print ' '.join((r[n-k-1:n+1] + r[n-k-2::-1])[:n])


'''input
5 2
'''

#~ @author = dwij28 (Abhinav Jha) ~#

n, k = map(int, raw_input().strip().split())
data = list(range(1, n+1))
data = data[k+1:][::-1] + data[:k+1]
for i in reversed(data): print i,


n,k=map(int,raw_input().split())
r=n-k
while n:print[r+1-n,n][n>r],;n-=1


n, k = map(int, raw_input().split())
print " ".join(map(str, range(k+1, 0, -1)+range(k+2, n+1)))


n,k = map(int,raw_input().split())
a = [i for i in range(1,n+1)]
print ' '.join([str(i) for i in a[k:]+(a[:k])[::-1]])


import sys

n, k = [int(i) for i in sys.stdin.readline().split()]

answer = [i for i in range(n, 0, -1)]

sorted = answer[k:]
answer = answer[:k]
sorted.sort()

for i in answer:
    sys.stdout.write( str(i) )
    sys.stdout.write( " " )
for i in sorted:
    sys.stdout.write( str(i) )
    sys.stdout.write( " " )


n, k = [int(x) for x in raw_input().split()]
res = ''
for i in range(n - k - 1): res += str(i+1) + ' '
for i in range(n, n - k - 1, -1): res += str(i) + ' '
print(res.strip())


n, k = [int(x) for x in input().split()]
res = ''
for i in range(n - k - 1): res += str(i+1) + ' '
for i in range(n, n - k - 1, -1): res += str(i) + ' '
print(res.strip())


import sys

n, k = map(int, sys.stdin.readline().split())

half = list(xrange(1, k + 2))
half.reverse()
print ' '.join(map(str, half + list(xrange(k + 2, n + 1))))


#! -*- coding: utf-8 -*-
import sys
def main(n, k):
  for i in range(k):
    print n-i,
  for i in range(1, n-k+1):
    print i,

if __name__=='__main__':
  a,b = sys.stdin.readline().strip().split(' ')
  # print a, b
  main(int(a),int(b))


#! -*- coding: utf-8 -*-
import sys
def main(n, k):
  i = 1
  while i <= n:
    # print i, n, k
    if i == k:
      print i+1, i,
      i += 2
    else:
      print i,
      i += 1

if __name__=='__main__':
  a,b = sys.stdin.readline().strip().split(' ')
  # print a, b
  main(int(a),int(b))


#! -*- coding: utf-8 -*-
import sys
def main(n, k):
  i = 1
  while i <= n:
    # print i, n, k
    if i == k:
      print i+1, i,
      i += 2
    else:
      print i,
      i += 1

if __name__=='__main__':
  a,b = sys.stdin.readline().strip().split(' ')
  print a, b
  main(int(a),int(b))


'''input
3 2
'''
y,k = map(int,raw_input().split())
n = y
ans = ""
for x in reversed(xrange(0,k)):
    ans += str(n) + " "
    n -= 1

n = 1
for x in xrange(0,y-k):
    ans += str(n) + " "
    n += 1
print ans


n, k = map(lambda x: int(x), raw_input().split())

print ' '.join(map(lambda x: str(x), range(k+1,0,-1) + range(k+2,n+1)))


n = int(input())
k = int(input())
ans ="";
for i in range(k + 1, 0, -1):
    ans += str(i) + " "
for i in range(k + 2, n + 1):
    ans += str(i) + " "
print(ans)


n, k = [ int(x) for x in input().split() ]
ans =  ([x for x in reversed(range(1, k + 2))]) +  [x for x in range(k + 2, n + 1)]
print(" ".join([ str(x) for x in ans]))


n, k = [ int(x) for x in raw_input().split() ]
ans =  ([x for x in reversed(range(1, k + 2))]) +  [x for x in range(k + 2, n + 1)]
print(" ".join([ str(x) for x in ans]))


n,k=map(int,raw_input().split())
ans =""
for i in range(k + 1, 0, -1):
    ans += str(i) + " "
for i in range(k + 2, n + 1):
    ans += str(i) + " "
print(ans)


n = int(input())
k = int(input())
ans =""
for i in range(k + 1, 0, -1):
    ans += str(i) + " "
for i in range(k + 2, n + 1):
    ans += str(i) + " "
print(ans)


n,k=map(int,raw_input().split())
r=n-k
while n:print[r+1-n,n][n>r],;n-=1


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

a = [i for i in range(k, 0, -1)]
b = [i for i in range(k+1, n+1)]

print ' '.join(map(str, a+b))


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

a = [i for i in range(k+1, 0, -1)]
b = [i for i in range(k+2, n+1)]

print ' '.join(map(str, a+b))


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

a = [0] * (n+1)

for i in range(1, n+1):
    a[i] = i

if k != 0:
    a[k], a[k+1] = a[k+1], a[k]

print ' '.join(map(str, a[1:]))


def getSequence(n,k):
    arr=[]
    for i in range(0,k):
        arr.append(n-i)
    for i in range(0,n-k):
        arr.append(i+1)
    return arr
input=raw_input()
n=int(input.split(" ")[0])
k=int(input.split(" ")[1])
print '%s' % ' '.join(map(str, getSequence(n,k)))


def getSequence(n,k):
    arr=[]
    for i in range(0,k):
        arr.append(n-i)
    for i in range(0,n-k):
        arr.append(i+1)
    return arr

input=raw_input()
n=int(input.split(" ")[0])
k=int(input.split(" ")[1])
print '%s' % ', '.join(map(str, getSequence(n,k)))


def getSequence(n,k):
    arr=[]
    for i in range(0,k):
        arr.append(n-i)
    for i in range(0,n-k):
        arr.append(i+1)
    return arr

input=raw_input()
n=int(input.split(" ")[0])
k=int(input.split(" ")[1])
print getSequence(n,k)


def getSequence(n,k):
    arr=[]
    for i in range(0,k):
        arr.append(n-i)
    for i in range(0,n-k):
        arr.append(i+1)
    return arr

input=raw_input()
n=int(input.split(" ")[0])
k=int(input.split(" ")[1])
print '%s' % ' '.join(map(str, getSequence(n,k)))


n,k = map(int,raw_input().split(' '))
for i in (range(1,n-k) + range(n - k,n + 1)[::-1]):print i,


n, k = map(int, raw_input().split())
for x in range(1, n - k) + range(n, n - k - 1,  -1):
    print x,


inp = raw_input()
sp = inp.split()
a = int(sp[0])
b = int(sp[1])
for i in range(b):
    print str(a-i),
for i in range(a-b):
    print str(i+1),


inp = raw_input()
sp = inp.split()
a = int(sp[0])
b = int(sp[1])
for i in range(a):
    print str((i+b+1)%a + 1),


n,k = map(int, raw_input().split())
if k == 0:
	for i in range(1,n+1):
		print i,
else:
	if k<=n/2:
		z = 2
		for i in xrange(2*k):
			print z,
			if i %2==0:
				z-=1
			else:
				z+=3
		for i in range((2*k) + 1,n+1):
			print i,
	else:
		for i in range(k+1,0,-1):
			print i,
		for i in range(k+2, n+1):
			print i,


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

l = range(k, -1, -1) + range(k+1, n)
l = map(lambda x: x+1, l)
print ' '.join(map(str, l))


#Codeforces 285A

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

p1 = [i for i in range(1,n-k)]
p2 = [i for i in range(n, n-k-1, -1)]

p1[len(p1):] = p2

for i in range(len(p1)):
    print str(p1[i]),


#Codeforces 285A

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

p1 = [i for i in range(1,n-k+1)]
p2 = [i for i in range(n, n-k, -1)]

p1[len(p1):] = p2

for i in range(len(p1)):
    print str(p1[i]),


n, k = map(int, raw_input().split() )
print ' '.join( map(str,  range(1, n-k)[:]+range(n, n-1-k, -1) ) )


n, k = map(int, raw_input().split() )
k += 1
for i in xrange(1, n-k+1):
    print i,
for i in xrange(n, n+1-k-1, -1):
    print i,


n, k = map(int, raw_input().split() )
if k == n-1:
    for i in xrange(n, 0, -1):
        print i,
else :
    i, j = 1, n
    while k > 0 and i!=j:
        k -= 1
        print j, i,
        j -= 1
        i += 1
    for x in xrange(i, j+1):
        print x,


n,k=map(int,raw_input().split(' '))

tab=list(xrange(1,n+1))

x=tab[:k+1][::-1]
ans=x+tab[k+1:]

for i in ans:
    print i,


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

print " ".join(map(str, sorted(range(n+1)[1:], reverse=True)))


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

lista = range(n+1)[1:]

outra = lista[:n-(k+1)] + sorted(lista[n-(k+1):], reverse=True)

print " ".join(map(str, outra))


n,k=map(int,raw_input().split())
r=n-k
while n:print[r+1-n,n][n>r],;n-=1


n, k = map(int, raw_input().split())
for i in xrange(k + 1, 0, -1):
    print i,
for i in xrange(k + 2, n + 1):
    print i,


n,k=map(int,raw_input().split())
while n-k-1:
    print n
    n-=1
while k+1:
    print n-k
    k-=1


n,k=map(int,raw_input().split())
for t in range(n,n-k,-1)+range(1,n-k+1):
    print t


///Never Give Up
///Be Grateful For What You Have
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<stdlib.h>

int main()
{
    int n,i,k;
    scanf("%d %d",&n,&k);
    if(k)
    for(i=1;i<=n;i++){
        if(i==k){
            printf("%d ",n);
            continue;
        }
        if(i==n){
            printf("%d ",k);
            continue;
        }
        printf("%d ",i);
    }
    else{
        for(i=1;i<=n;i++)
            printf("%d ",i);
    }
    return 0;
}


///Never Give Up
///Be Grateful For What You Have
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<stdlib.h>

int main()
{
    int n,i,k,j;
    scanf("%d %d",&n,&k);
    if(k)
    {
        for(i=n,j=1; j<=k; i--,j++)
            printf("%d ",i);
        for(i=1; i<=n-k; i++)
            printf("%d ",i);
    }
    else
    {
        for(i=1; i<=n; i++)
            printf("%d ",i);
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int i,j,a,k;
    scanf("%d %d",&a,&k);
    for(i=0;i<n;i++){
        if(i>=k){
            printf("%d",i-k+1);
        }
        else
            printf("%d",n-i)
    }
}


#include<stdio.h>
int main()
{
    int i,j,a,k;
    scanf("%d %d",&a,&k);
    for(i=0;i<a;i++){
        if(i>=k){
            printf("%d ",i-k+1);
        }
        else
            printf("%d ",a-i);
    }
}


#include<stdio.h>
int main(){
    int n,k,p[100000]={0,};
    scanf("%d %d",&n,&k);
    for(int i=0;i<n;i++){
        if(i>=n-k-1)
            printf("%d ",2*n-k-1-i);
        else
            printf("%d ",i+1);
    }
    return 0;
}


#include <stdio.h>

int main() {
    long int n, k, i, j;
    scanf("%ld%ld",&n,&k);
    int t[n];
    for(i=1; i<-k+n; i++)
        printf("%d ",i);
    for(i=n; i>n-k-1; i--)
        printf("%d ",i);
    return 0;
}


/*  AUTHOR:AKASH JAIN
*   USERNAME:akash19jain
*   DATE:12/10/2019
*/
// #include<algorithm>
// #include <bits/stdc++.h>
// using namespace std;

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include<ctype.h>
#define SC1(x)          scanf("%lld",&x)
#define SC2(x,y)        scanf("%lld%lld",&x,&y)
#define SC3(x,y,z)      scanf("%lld%lld%lld",&x,&y,&z)
#define SCS(x)          scanf("\n%s", x)
#define PF1(x)          printf("%lld\n",x)
#define PF2(x,y)        printf("%lld %lld\n",x,y)
#define PF3(x,y,z)      printf("%lld %lld %lld\n",x,y,z)
#define PFN                printf("\n")
#define REP(i,n)        for(long long i=0;i<(n);i++)
#define FOR(i,a,b)      for(long long i=(a);i<=(b);i++)
#define FORD(i,a,b)     for(long long i=(a);i>=(b);i--)
#define WHILE(n)        while(n--)
#define MEM(a, b)       memset(a, (b), sizeof(a))
#define ITOC(c)         ((char)(((ll)'0')+c))
#define MID(s,e)        (s+(e-s)/2)
#define SZ(a)            strlen(a)
#define MOD             1000000007
#define MAX             10000000005
#define MIN             -10000000005
#define PI              3.1415926535897932384626433832795
#define DEB(x)          printf("The value of \"%s\" is: %d\n",#x,x)
#define CASES           ll t;SC1(t);while(t--)
#define ABS(a)          ((a>0)?a:-(a))
#define SWAP(a,b)        ll z=a;a=b;b=z
#define SWAPC(a,b)        char z=a;a=b;b=z

const int INF = 1<<29;
typedef long long ll;
typedef unsigned long long ull;
#define FILEIO(name) \  freopen(name".in", "r", stdin); \   freopen(name".out", "w", stdout);
int cmp(const void * a,const void * b);
long long maxv(long long a,long long b);
long long minv(long long a,long long b);
long long gcd(long long u,long long v);
long long digits(long long n);
bool isPoweroftwo(long long n);

int main()
{
    ll n,k;
    SC2(n,k);
    if(k==0)
    {
        REP(i,n)
            printf("%lld ",i+1);
    }
    else
    {
        ll x=n-k+1;
        FORD(i,n,x)
            printf("%lld ",i);
        FOR(i,1,n-k)
            printf("%lld ",i);
    }
    return 0;
}

//qsort(arr,n,sizeof(arr[0]),cmp);
int cmp (const void * a, const void * b)
{
    if( *(ll*)a - *(ll*)b < 0 ) return -1;
    if( *(ll*)a - *(ll*)b > 0 ) return 1;
        return 0;
}
long long maxv(long long a,long long b)
{
    if(a>b) return a;
        return b;
}
long long minv(long long a,long long b)
{
    if(a<b) return a;
        return b;
}
long long gcd(long long u,long long v)
{
    if (v == 0) return u;
       return gcd(v, u%v);
}
long long digits(long long n)    //to calculate no of digits in a number
{
    return floor(log10(n))+1;
}
bool isPoweroftwo(long long x)
{
    return x && (!(x&(x-1)));
}


#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
long long cmpfunc (const void * a, const void * b) {
   return ( *(long long*)a - *(long long*)b );
}

int main(void){

long long int test,i,j,n,count,flag=0,o1=0,o2=0,b1,x,m,l,max,sum2,min,f,c,r,o,sum1,sum=0,y,a[7],b,count1=0,a2,b2;
scanf("%lld%lld",&n,&m);
if(n%m==0){
    for(i=n;i>=1;i--){
       printf("%lld ",i);
    }
}else{
    printf("1 ");
   for(i=n;i>=2;i--){
       printf("%lld ",i);
    }
}
return 0;
}


#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
long long cmpfunc (const void * a, const void * b) {
   return ( *(long long*)a - *(long long*)b );
}

int main(void){

long long int test,i,j,n,count,flag=0,o1=0,o2=0,b1,x,m,l,max,sum2,min,f,c,r,o,sum1,sum=0,y,a[7],b,count1=0,a2,b2;
scanf("%lld%lld",&n,&m);
for(i=1;i<-m+n;i++){
    printf("%lld ",i);
}
for(i=n;i>=-m+n;i--){
    printf("%lld ",i);
}
return 0;
}


#include <stdio.h>

int main()
{
    int n, k;
    scanf("%d%d", &n, &k);

    for(int i=n; i>n-k; i--) {
        printf("%d ", i);
    }

    for(int i=1; i<=n-k; i++) {
        printf("%d ", i);
    }

    return 0;
}


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

int main()
{
    int n,m,i;
    scanf("%d",&n);
    scanf("%d",&m);
    int t[n];
    for(i=0;i<n;i++)
    {
        t[i]=i+1;
    }
    for(i=0;i<n-m-1;i++)
    {
        printf("%d ",t[i]);
    }
    for(i=n-1;i>n-m-2;i--)
    {
        printf("%d ",t[i]);
    }

    return 0;
}


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

int main()
{
    int n,m,i;
    scanf("%d",&n);
    scanf("%d",&m);
    int t[n];
    for(i=0;i<n;i++)
    {
        t[i]=i+1;
    }
    for(i=0;i<n-m-1;i++)
    {
        printf("%d",t[i]);
    }
    for(i=n-1;i>n-m-2;i--)
    {
        printf("%d",t[i]);
    }

    return 0;
}


#include <stdio.h>

int main() {
	int n;

	scanf("%d", &n);
	while (n--)
		printf("%d ", n + 1);
	printf("\n");
	return 0;
}


#include <stdio.h>

int main() {
	int n, k, i;

	scanf("%d%d", &n, &k);
	for (i = 0; i < n; i++)
		printf("%d ", i < k ? n - i : i - k + 1);
	printf("\n");
	return 0;
}


#include <stdio.h>

int main() {
	static int pp[100000];
	int n, k, i, tmp;

	scanf("%d%d", &n, &k), k--;
	for (i = 0; i < n; i++)
		pp[i] = i;
	tmp = pp[k], pp[k] = pp[k + 1], pp[k + 1] = tmp;
	for (i = 0; i < n; i++)
		printf("%d ", pp[i] + 1);
	printf("\n");
	return 0;
}


// https://codeforces.com/problemset/problem/285/A

#include<stdio.h>

#define F(i, a, b) for(i = (a); i < (b); i++)
#define FD(i, a, b) for(i = (a); i > (b); i--)

int main() {

    int i, n, k;
    scanf("%d %d", &n, &k);

    FD(i, k+1, 0) {
        printf("%d ", i);
    }

    F(i, k+2,n+1) {
        printf("%d ", i);
    }
    printf("\n");
    return 0;
}


#include <stdio.h>
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	if(i==k){
		printf("%d %d ",i+1,i);
		i++;
	}
	else{
		printf("%d ",i);
	}
	}


#include <stdio.h>
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=0;i<k;i++){
			printf("%d ",n-i);
}
	for(int i=1;i<=n-k;i++){
		printf("%d ",i);
	}
	}


#include <stdio.h>
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	if(i==k){
		for(int j=i;j<=n;j++){
			printf("%d %d ",j+1,j);
			j++;
			i=j;
		}
	}
	else{
		printf("%d ",i);
	}
	}


n, k = map(int, input().split())

l = []

for i in range(k):
	l.append(n-i)

temp_list = []

for i in range(1, n-k+1):
	temp_list.append(i)

f_list = l + temp_list

print(f_list)


#include<stdio.h>

int main()
{
	int n,k,i,l=1,tem;
	scanf("%d" , &n);
	scanf("%d" , &k);
	int a[n];
	tem=n;
	for (i=1; i<=k;i++)
	{
		a[i]=tem;
		tem--;
	}
	for (i=k+1;i<=n;i++)
	{
		a[i]=l;
		l++;
	}
	for(i=1;i<=n;i++)
	printf("%d ", a[i]);
	return 0;
}


#include <bits/stdc++.h>
#define fin ios_base::sync_with_stdio(false)
#define fout cin.tie(NULL);cout.tie(NULL)
#define phb(elm) push_back(elm)
#define ppb() pop_back()
#define sclock() time(NULL)
#define eclock() time(NULL)
#define ivec(x) vector<int>x
#define svec(x) vector<string>x
#define pvec(x) vector<pair<int, int>>x
#define iset(x) set<int>x
#define sset(x) set<string>x
#define mkp(x, y) make_pair(x, y)
#define all(x) x.begin(), x.end()
#define sum(range, init) accumulate(range, init)
#define vsort(vec) sort(all(vec))
#define lb(v, e) lower_bound(all(v), e)
#define ub(v, e) upper_bound(all(v), e)
#define slb(s, e) s.lower_bound(e)
#define sub(s, e) s.upper_bound(e)
#define fill(ctr, x) memset(ctr,val,sizeof(ctr))
#define loop(i, a, b) for(int i=a;i<b;i++)
#define wf(i, a, b, step) for(int i=a;i<b;i+=step)
#define wb(i, a, b, step) for(int i=a;i>=b;i-=step)
#define iter(i, ctr) for(auto i:ctr)
#define getv(v, n) loop(i, 0, n)cin>>v[i]
#define print(x) cout<<x<<"\n"
#define printp(x, y) cout<<x<<" "<<y<<"\n"
#define pprecise(d, n) cout<<setprecision(n+1)<<d<<"\n";
#define flush cout<<flush
#define lbr cout<<"\n"
#define debug print("wtf")
#define ull unsigned long long
typedef long long int ll;
#define tlimit 1 //time_limit = 1 sec.
using namespace std;

int32_t main(){
    fin;fout;
    int n, k, num;
    cin >> n >> k;
    for(int i=0; i<n; i++){
        if(i==n-k-1)
            num=n;
        else if(i>n-k-1)
            num--;
        else
            num=i+1;
        if(i==n-1)
            print(num);
        else
            cout << num << " ";
    }
    return EXIT_SUCCESS;
}


    #include<stdio.h>
    int main()
    {
        int a[100001];
        int n,k,i;
        scanf("%d%d",&n,&k);
        for(i=1; i<=n; i++)
            a[i]=i;
        for(i=n; i>n-k; i--)
            printf("%d ",a[i]);
        for(i=1; i<n-k; i++)
            printf("%d ",a[i]);
        printf("%d\n",n-k);
        return 0;
    }


#include<stdio.h>

int main()
{
    int n, k;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n-k-1;i++)
    printf("%d ",i+1);
    for(int i=0;i<k+1;i++)
    printf("%d ",n--);
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>

int main()
{
    int n,k;
    scanf("%d %d",&n,&k);
    int a[n];
    for(int i=0;i<n;i++)
    {
        a[i]=i+1;
    }
    if(k>0 && k<n)
    {
        int p=a[n-1];
        a[n-1]=a[k-1];
        a[k-1]=p;
        for(int i=0;i<n;i++)
        {
            printf("%d ",a[i]);
        }
    }
    else
    {
        for(int i=0;i<n;i++)
        {
            printf("%d ",a[i]);
        }
    }
    return 0;
}


#include<stdio.h>
#include<stdio.h>
#include<math.h>
#include<string.h>

int main()
{
    int n,k;
    scanf("%d %d",&n,&k);
    int a[n];
    for(int i=0;i<n;i++)
    {
        a[i]=i+1;
    }
    for(int i=0;i<n-1;i++)
    {
	    for(int j=0;j<n-1-i;j++)
	    {
	    if(a[j]<a[j+1])
		    {
		    int p=a[j];
		    a[j]=a[j+1];
		    a[j+1]=p;
		    }
	    }
    }
    for(int i=0;i<n;i++)
    printf("%d ",a[i]);
    return 0;
}


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

int main()
{
    int n,k;
    scanf("%d %d",&n,&k);
    int a[n];
    for(int i=0;i<n;i++)
    {
        a[i]=i+1;
    }
    if(k>0 && k<n)
    {
        for(int i=n;i>=(n-k+1);i--)
        {
            printf("%d ",i);
        }
        for(int i=1;i<=n-k;i++)
        {
            printf("%d ",i);
        }
    }
    else
    {
        for(int i=0;i<n;i++)
        {
            printf("%d ",a[i]);
        }
    }
    return 0;
}


#include<stdio.h>

int main(){

	int n, k;

	scanf("%d %d", &n, &k);

	int z = n - k;

	for(int i = 1; i<=z-1; i++)
		printf("%d ", i);

	for(int i = n; i >= z; i--)
		printf("%d ", i);

}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeForce175
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split();
            int n = Convert.ToInt32(input[0]);
            int k = Convert.ToInt32(input[1]);
            for (int i = n; i > n - k; i--)
                Console.Write(i + " ");
            for (int i = 1; i <= n - k; i++)
                Console.Write(i +" ");
        }
    }
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mp  make_pair
#define pb  push_back
void c_p_c(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
void solve(){
    int n;
    cin>>n;
    int v[n];
    for(int i=0;i<n;i++){
        cin>>v[i];
    }
    if(n==1){
        cout<<0<<endl;
        return;
    }
    int cnt=0,c1=0;
    else if(v[n-1]>=v[n-2]){
        //cout<<"first"<<endl;
        c1+=2;
        int i=n-3;
        int p=v[n-2];
        while(i>=0 and p>=v[i]){
            c1++;
            p=v[i];
            i--;
        }
            cout<<n-c1<<'\n';
            return;
    }
    else if(v[n-1]<v[n-2]){
        cnt+=2;
    int i=n-3;
    int p=v[n-2];
    while(i>=0 and p<=v[i]){
        cnt++;
        p=v[i];
        i--;
    }
    while(i>=0 and p>=v[i]){
        cnt++;
        p=v[i];
        i--;
    }
    cout<<n-cnt<<'\n';
    return;
    }
}
int32_t main(){
    c_p_c();
    int t;
    cin>>t;
    while(t--){
        solve();
    }
	return 0;
}


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

#define F               first
#define S               second
#define int             long long int
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define mod             1000000007

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

int32_t main()
{
	c_p_c();

	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		int arr[n];
		for (int i = 0; i < n; i++) {
			cin >> arr[i];

		}
		if (n == 1) {
			cout << 0 << endl;
		}
		int ans = 0;
		else if (arr[n - 1] > arr[n - 2]) {
			ans += 2;
			int i = n - 3;
			int prev = arr[n - 2];
			while (i >= 0 and prev >= arr[i]) {
				ans++;
				prev = arr[i];
				i--;

			}
			cout << n - ans << endl;
		}
		else  {
			ans = 2;
			int i = n - 3;
			int prev = arr[n - 2];
			while (i >= 0 and prev <= arr[i]) {
				ans++;
				prev = arr[i];
				i--;
			}
			while (i >= 0 and prev >= arr[i]) {
				ans++;
				prev = arr[i];
				i--;
			}
			cout << n - ans << endl;
		}

	}
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mp  make_pair
#define pb  push_back
void c_p_c(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
// bool check(vector<int> v){
//     int m=v.size();
//     int w=0;
//     for(int i=0;i<=(v.size()/2);i++){
//         if(v[i]<=v[i+1] ){
//             continue;
//         }
//         else{

//         }
//     }
// }
void solve(){
    int n;
    cin>>n;
    vector <int> v;
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        v.pb(x);
    }

    int cnt=0,c1=0;
    if(v[n-1]>=v[n-2]){
        //cout<<"first"<<endl;
        int i=v.size()-1;
        while(v[i]>=v[i-1]){
            c1=i-1;
            i--;
        }
            cout<<i<<'\n';
            return;
    }
    cnt+=2;
    int i=n-3;
    int p=v[n-2];
    while(i>=0 and p<=v[i]){
        cnt++;
        p=v[i];
        i--;
    }
    while(i>=0 and p>=v[i]){
        cnt++;
        p=v[i];
        i--;
    }
    cout<<n-cnt<<'\n';
    return;
}
int32_t main(){
    c_p_c();
    int t;
    cin>>t;
    while(t--){
        solve();
    }
	return 0;
}


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

#define F               first
#define S               second
#define int             long long int
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define mod             1000000007

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

int32_t main()
{
	c_p_c();

	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		int arr[n];
		for (int i = 0; i < n; i++) {
			cin >> arr[i];

		}
		int ans = 0;
		if (n == 1) {
			cout << 0 << endl;
		}

		else if (arr[n - 1] > arr[n - 2]) {
			ans += 2;
			int i = n - 3;
			int prev = arr[n - 2];
			while (i >= 0 and prev >= arr[i]) {
				ans++;
				prev = arr[i];
				i--;

			}
			cout << n - ans << endl;
		}
		else {
			ans = 2;
			int i = n - 3;
			int prev = arr[n - 2];
			while (i >= 0 and prev <= arr[i]) {
				ans++;
				prev = arr[i];
				i--;
			}
			while (i >= 0 and prev >= arr[i]) {
				ans++;
				prev = arr[i];
				i--;
			}
			cout << n - ans << endl;
		}

	}
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mp  make_pair
#define pb  push_back
void c_p_c(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
void solve(){
    int n;
    cin>>n;
    int v[n];
    for(int i=0;i<n;i++){
        cin>>v[i];
    }
    int cnt=0,c1=0;
    if(n==1){
        cout<<0<<endl;
        return;
    }

    else if(v[n-1]>=v[n-2]){
        //cout<<"first"<<endl;
        c1+=2;
        int i=n-3;
        int p=v[n-2];
        while(i>=0 and p>=v[i]){
            c1++;
            p=v[i];
            i--;
        }
            cout<<n-c1<<'\n';
            return;
    }
    else if(v[n-1]<v[n-2]){
        cnt+=2;
    int i=n-3;
    int p=v[n-2];
    while(i>=0 and p<=v[i]){
        cnt++;
        p=v[i];
        i--;
    }
    while(i>=0 and p>=v[i]){
        cnt++;
        p=v[i];
        i--;
    }
    cout<<n-cnt<<'\n';
    return;
    }
}
int32_t main(){
    c_p_c();
    int t;
    cin>>t;
    while(t--){
        solve();
    }
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define mp make_pair
const int maxm = 2e5+100;
using namespace std;
const double PI = acos(-1);

vector<int> G[maxm];
int visited[maxm];

ll power(ll a,ll n,ll m)
{
	ll res = 1;
	while(n)
	{
		if(n%2)
		  res = (res*a)%m,n--;
		else
		  a = (a*a)%m,n=n/2;
	}
	return res;
}

void dfs(int n)
{
   visited[n] = 1;
   for(auto i : G[n])
   {
      if(visited[i])
	    continue;
	  dfs(i);
   }
}

bool isPrime(ll n)
{
	for(int i =2 ; i <= sqrt(n) ; i++)
	{
		if(n%i == 0)
		  return false;
	}
	return true;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll t;
	cin >> t;
	while(t--)
	{
		ll n;
		cin >> n;
		ll a[n];
		for(int i = 0; i < n ; i++)
		  cin >> a[i];
		ll b[n];
		for(int i = 0; i < n ; i++)
		   b[i] = a[n-i-1];
		if(n == 1 or n == 2)
		  cout << "0\n";
		else
		{
			int i =1,left=0,right=0;
			for(i =1 ; i < n ; )
			{
				if(b[i] > b[i-1])
				{
					while(b[i] > b[i-1] and i < n)
					  i++;
					left = 1;
				}
				else
				{
					while(b[i] <= b[i-1] and i < n)
					  i++;
					right = 1;
				}
				if(right == 1)
				  break;
			}
			cout << n-i << "\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define mp make_pair
const int maxm = 2e5+100;
using namespace std;
const double PI = acos(-1);

vector<int> G[maxm];
int visited[maxm];

ll power(ll a,ll n,ll m)
{
	ll res = 1;
	while(n)
	{
		if(n%2)
		  res = (res*a)%m,n--;
		else
		  a = (a*a)%m,n=n/2;
	}
	return res;
}

void dfs(int n)
{
   visited[n] = 1;
   for(auto i : G[n])
   {
      if(visited[i])
	    continue;
	  dfs(i);
   }
}

bool isPrime(ll n)
{
	for(int i =2 ; i <= sqrt(n) ; i++)
	{
		if(n%i == 0)
		  return false;
	}
	return true;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll t;
	cin >> t;
	while(t--)
	{
		ll n;
		cin >> n;
		ll a[n];
		for(int i = 0; i < n ; i++)
		  cin >> a[i];
		ll b[n];
		for(int i = 0; i < n ; i++)
		   b[i] = a[n-i-1];
		if(n == 1)
		  cout << "0\n";
		else
		{
			int i =1,left=0,right=0;
			for(i =1 ; i < n ; )
			{
				if(b[i] >= b[i-1])
				{
					while(b[i] >= b[i-1] and i < n)
					  i++;
					left = 1;
				}
				else
				{
					while(b[i] < b[i-1] and i < n)
					  i++;
					right = 1;
				}
				if(left == 1 and right == 1)
				  break;
				if(right == 1)
				  break;
			}
			cout << n-i << "\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define mp make_pair
const int maxm = 2e5+100;
using namespace std;
const double PI = acos(-1);

vector<int> G[maxm];
int visited[maxm];

ll power(ll a,ll n,ll m)
{
	ll res = 1;
	while(n)
	{
		if(n%2)
		  res = (res*a)%m,n--;
		else
		  a = (a*a)%m,n=n/2;
	}
	return res;
}

void dfs(int n)
{
   visited[n] = 1;
   for(auto i : G[n])
   {
      if(visited[i])
	    continue;
	  dfs(i);
   }
}

bool isPrime(ll n)
{
	for(int i =2 ; i <= sqrt(n) ; i++)
	{
		if(n%i == 0)
		  return false;
	}
	return true;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll t;
	cin >> t;
	while(t--)
	{
		ll n;
		cin >> n;
		ll a[n];
		for(int i = 0; i < n ; i++)
		  cin >> a[i];
		ll b[n];
		for(int i = 0; i < n ; i++)
		   b[i] = a[n-i-1];
		if(n == 1 or n == 2)
		  cout << "0\n";
		else
		{
			ll i =1,left=0,right=0;
			for(i =0 ; i < n ; )
			{
				if(i+1 < n)
				{
					if(b[i+1] > b[i])
					{
					   while(b[i+1] > b[i] and i < n)
					     i++;
					}
					else
					{
						while(b[i+1] <= b[i] and i < n)
						  i++;
						right = 1;
					}
					if(right == 1)
				       break;
				}

			}
			cout << n-(i+1) << "\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define mp make_pair
const int maxm = 2e5+100;
using namespace std;
const double PI = acos(-1);

vector<int> G[maxm];
int visited[maxm];

ll power(ll a,ll n,ll m)
{
	ll res = 1;
	while(n)
	{
		if(n%2)
		  res = (res*a)%m,n--;
		else
		  a = (a*a)%m,n=n/2;
	}
	return res;
}

void dfs(int n)
{
   visited[n] = 1;
   for(auto i : G[n])
   {
      if(visited[i])
	    continue;
	  dfs(i);
   }
}

bool isPrime(ll n)
{
	for(int i =2 ; i <= sqrt(n) ; i++)
	{
		if(n%i == 0)
		  return false;
	}
	return true;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll t;
	cin >> t;
	while(t--)
	{
		ll n;
		cin >> n;
		ll a[n];
		for(int i = 0; i < n ; i++)
		  cin >> a[i];
		ll b[n];
		for(int i = 0; i < n ; i++)
		   b[i] = a[n-i-1];
		if(n == 1 or n == 2)
		  cout << "0\n";
		else
		{
			int i =1,left=0,right=0;
			for(i =1 ; i < n ; )
			{
				if(b[i] > b[i-1])
				{
					while(b[i] >= b[i-1] and i < n)
					  i++;
					left = 1;
				}
				else
				{
					while(b[i] <= b[i-1] and i < n)
					  i++;
					right = 1;
				}
				if(right == 1)
				  break;
			}
			cout << n-i << "\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define mp make_pair
const int maxm = 2e5+100;
using namespace std;
const double PI = acos(-1);

vector<int> G[maxm];
int visited[maxm];

ll power(ll a,ll n,ll m)
{
	ll res = 1;
	while(n)
	{
		if(n%2)
		  res = (res*a)%m,n--;
		else
		  a = (a*a)%m,n=n/2;
	}
	return res;
}

void dfs(int n)
{
   visited[n] = 1;
   for(auto i : G[n])
   {
      if(visited[i])
	    continue;
	  dfs(i);
   }
}

bool isPrime(ll n)
{
	for(int i =2 ; i <= sqrt(n) ; i++)
	{
		if(n%i == 0)
		  return false;
	}
	return true;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll t;
	cin >> t;
	while(t--)
	{
		ll n;
		cin >> n;
		ll a[n];
		for(int i = 0; i < n ; i++)
		  cin >> a[i];
		ll b[n];
		for(int i = 0; i < n ; i++)
		   b[i] = a[n-i-1];
		if(n == 1 or n == 2)
		  cout << "0\n";
		else
		{
			ll i =1,left=0,right=0;
			for(i =0 ; i < n ; )
			{
				if(i+1 < n)
				{
					if(b[i+1] > b[i])
					{
					   while(b[i+1] > b[i] and i < n)
					     i++;
					}
					else
					{
						while(b[i+1] <= b[i] and i < n)
						  i++;
						right = 1;
					}
					if(right == 1)
				       break;
				}
				else
				  i++;
			}
			cout << n-(i+1) << "\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define mp make_pair
const int maxm = 2e5+100;
using namespace std;
const double PI = acos(-1);

vector<int> G[maxm];
int visited[maxm];

ll power(ll a,ll n,ll m)
{
	ll res = 1;
	while(n)
	{
		if(n%2)
		  res = (res*a)%m,n--;
		else
		  a = (a*a)%m,n=n/2;
	}
	return res;
}

void dfs(int n)
{
   visited[n] = 1;
   for(auto i : G[n])
   {
      if(visited[i])
	    continue;
	  dfs(i);
   }
}

bool isPrime(ll n)
{
	for(int i =2 ; i <= sqrt(n) ; i++)
	{
		if(n%i == 0)
		  return false;
	}
	return true;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll t;
	cin >> t;
	while(t--)
	{
		ll n;
		cin >> n;
		ll a[n];
		for(int i = 0; i < n ; i++)
		  cin >> a[i];
		ll b[n];
		for(int i = 0; i < n ; i++)
		   b[i] = a[n-i-1];
		if(n == 1 or n == 2)
		  cout << "0\n";
		else
		{
			ll i =1,left=0,right=0;
			for(i =0 ; i < n ; )
			{
				if(i+1 < n)
				{
					if(b[i+1] > b[i])
					{
					   while(b[i+1] > b[i] and i < n)
					     i++;
					}
					else
					{
						while(b[i+1] <= b[i] and i < n)
						  i++;
						right = 1;
					}
					if(right == 1)
				       break;
				}
			}
			if(i == n)
			  cout << "0\n";
			else
			  cout << n-(i+1) << "\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define mp make_pair
const int maxm = 2e5+100;
using namespace std;
const double PI = acos(-1);

vector<int> G[maxm];
int visited[maxm];

ll power(ll a,ll n,ll m)
{
	ll res = 1;
	while(n)
	{
		if(n%2)
		  res = (res*a)%m,n--;
		else
		  a = (a*a)%m,n=n/2;
	}
	return res;
}

void dfs(int n)
{
   visited[n] = 1;
   for(auto i : G[n])
   {
      if(visited[i])
	    continue;
	  dfs(i);
   }
}

bool isPrime(ll n)
{
	for(int i =2 ; i <= sqrt(n) ; i++)
	{
		if(n%i == 0)
		  return false;
	}
	return true;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll t;
	cin >> t;
	while(t--)
	{
		ll n;
		cin >> n;
		ll a[n];
		for(int i = 0; i < n ; i++)
		  cin >> a[i];
		ll b[n];
		for(int i = 0; i < n ; i++)
		   b[i] = a[n-i-1];
		if(n == 1 or n == 2)
		  cout << "0\n";
		else
		{
			int i =1;
			while(b[i] >= b[i-1] and i < n)
			    i++;
			while(b[i] <= b[i-1] and i < n)
				i++;
			cout << n-i << "\n";
		}
	}
	return 0;
}


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

int main() {
int con;
    long int t,ans;
    cin >> t;
    int y,s;
    long int n,i;
    while (t--) {
    	cin>>n;
    	con=0;
    	ans=0;
    	y=0,s=0;
    	long int a[n];

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

    for(long int j=0;j<n;j++)
    {
    	y=0;
    for(i=j;i<n-1;i++){

    	if(a[i]>a[i+1] && i==j)
    	{
    		y=1;
    		con+=2;
    		break;
		}
		else if(a[i]>a[i+1] && y==0 )
    	{
    		y=1;
    		con+=1;
    		continue;
		}
		if(y==1 && a[i]<a[i+1])
		{
			con++;
			break;
		}
	}
	if(con<=1)
	{
		s=1;
	ans=j;
	break;
}
	con=0;
	}
		if(s==1)
	cout<<ans<<endl;
	else
	cout<<n<<endl;
    }
}


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

int main() {
int con;
    long int t,ans;
    cin >> t;
    int y,s;
    long int n,i;
    while (t--) {
    	cin>>n;
    	con=0;
    	ans=n;
    	y=0,s=0;
    	long int a[n];

    	for(i=0;i<n;i++)
    	cin>>a[i];
    	if(n>1)
   {
 for(long int j=0;j<n-1;j++)
    {
    	y=0;
    for(i=j;i<n-1;i++){
    	if(a[i]>a[i+1] && i==j && n<=2)
    	{

    		con+=1;
    		break;
		}
		else
    	if(a[i]>a[i+1] && i==j )
    	{
    		y=1;
    		con+=2;
    		break;
		}

		else if(a[i]>a[i+1] && y==0 )
    	{
    		y=1;
    		con+=1;
    		continue;
		}
		if(y==1 && a[i]<a[i+1])
		{
			con++;
			break;
		}
	}
	if(con<=1)
	{
	ans=j;
	break;
}
	con=0;
	}
	cout<<ans<<endl;
}
else
cout<<"0"<<endl;

    }
}


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

int main() {
int con;
    long int t,ans;
    cin >> t;
    long int pos;
    long int n,i;
    while (t--) {
    	cin>>n;
    	pos=n-1;
    	long int a[n];

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

    	while(a[pos-1]>=a[pos])
    	pos--;

    	while(a[pos-1]<=a[pos])
    	pos--;

    	if(pos<0){
    		cout<<"0"<<endl;
    	}
    		else
    		cout<<pos<<endl;

}

}


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

int main() {
int con;
    long int t,ans;
    cin >> t;
    int y,s;
    long int n,i;
    while (t--) {
    	cin>>n;
    	con=0;
    	ans=n;
    	y=0,s=0;
    	long int a[n];

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

    for(long int j=0;j<n;j++)
    {
    	y=0;
    for(i=j;i<n-1;i++){

    	if(a[i]>a[i+1] && i==j)
    	{
    		y=1;
    		con+=2;
    		break;
		}
		else if(a[i]>a[i+1] && y==0 )
    	{
    		y=1;
    		con+=1;
    		continue;
		}
		if(y==1 && a[i]<a[i+1])
		{
			con++;
			break;
		}
	}
	if(con<=1)
	{
	ans=j;
	break;
}
	con=0;
	}
	cout<<ans<<endl;

    }
}


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

int main() {
int con;
    long int t,ans;
    cin >> t;
    int y,s;
    long int n,i;
    while (t--) {
    	cin>>n;
    	con=0;
    	ans=0;
    	y=0,s=0;
    	long int a[n];

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

    for(long int j=0;j<n-1;j++)
    {
    	y=0;
    for(i=j;i<n-1;i++){
    	if(a[i]>a[i+1] && i==j && n<=2)
    	{

    		con+=1;
    		break;
		}
		else
    	if(a[i]>a[i+1] && i==j )
    	{
    		y=1;
    		con+=2;
    		break;
		}

		else if(a[i]>a[i+1] && y==0 )
    	{
    		y=1;
    		con+=1;
    		continue;
		}
		if(y==1 && a[i]<a[i+1])
		{
			con++;
			break;
		}
	}
	if(con<=1)
	{
	ans=j;
	break;
}
	con=0;
	}
	cout<<ans<<endl;

    }
}


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

int main() {
int con;
    long int t,ans;
    cin >> t;
    int y,s;
    long int n,i;
    while (t--) {
    	cin>>n;
    	con=0;
    	ans=n;
    	y=0,s=0;
    	long int a[n];

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

 for(long int j=0;j<n;j++)
    {
    	y=0;
    for(i=j;i<n-1;i++){

	  if(a[i]>a[i+1] && y==0 )
    	{
    		y=1;
    		con+=1;
    		continue;
		}
		if(y==1 && a[i]<a[i+1])
		{
			con++;
			break;
		}
	}
	if(con<=1)
	{
	ans=j;
	break;
}
	con=0;
	}
	cout<<ans<<endl;
}

}


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

int main() {
int con;
    long int t,ans;
    cin >> t;
    int y,s;
    long int n,i;
    while (t--) {
    	cin>>n;
    	con=0;
    	ans=0;
    	y=0,s=0;
    	long int a[n];

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

    for(long int j=0;j<n-1;j++)
    {
    	y=0;
    for(i=j;i<n-1;i++){
    	if(a[i]>a[i+1] && i==j && n<=3)
    	{

    		con+=1;
    		break;
		}
		else
    	if(a[i]>a[i+1] && i==j )
    	{
    		y=1;
    		con+=2;
    		break;
		}

		else if(a[i]>a[i+1] && y==0 )
    	{
    		y=1;
    		con+=1;
    		continue;
		}
		if(y==1 && a[i]<a[i+1])
		{
			con++;
			break;
		}
	}
	if(con<=1)
	{
	ans=j;
	break;
}
	con=0;
	}
	cout<<ans<<endl;

    }
}


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

int main() {
int con;
    long int t,ans;
    cin >> t;
    int y,s;
    long int n,i;
    while (t--) {
    	cin>>n;
    	con=0;
    	ans=n;
    	y=0,s=0;
    	long int a[n];

    	for(i=0;i<n;i++)
    	cin>>a[i];
    	if(n>1)
   {
 for(long int j=0;j<n-1;j++)
    {
    	y=0;
    for(i=j;i<n-1;i++){
    	if(a[i]>a[i+1] && i==j && n<=2)
    	{

    		con+=1;
    		break;
		}
		else
    	if(a[i]>a[i+1] && i==j )
    	{
    		y=1;
    		con+=2;
    		break;
		}

		else if(a[i]>a[i+1] && y==0 )
    	{
    		y=1;
    		con+=1;
    		continue;
		}
		if(y==1 && a[i]<a[i+1])
		{
			con++;
			break;
		}
	}
	if(con<=1)
	{
	ans=j;
	break;
}
	con=0;
	}
	cout<<ans<<endl;
}
else
cout<<"0";

    }
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 100000000000000000
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI 3.14159265
#define pb push_back
#define LLMX LONG_LONG_MAX
#define LLMN LONG_LONG_MIN
int f =0  ;
int dp[200005] ;

signed main()
{
   int t ;
   cin >> t ;
   while(t--)
   {
      memset(dp , 0 , sizeof(dp)) ;
   vector < int > arr ;

     // arr.clear() ;
     int n ;
     cin >> n ;
     map < int , int > m ;
     vector < int > ans ;
     for(int x = 1;  x <= n ; x++)
     {
        int a ;
        cin >> a ;
        arr.pb(a) ;
     }
     if(n <= 2)
     {
        cout<<"0\n";
        continue ;
     }
     //int f  =0 ;
     f = n   ;
     //cout<<arr[n-3]<<" "<<arr[n-2]<<" "<<arr[n-1]<<"\n";
     if(arr[n-2] < arr[n-1] && arr[n-2] < arr[n-3])
     f = 0 ;
     if(f == 0)
     {
        cout<<n - 3 + 1 <<"\n";
        continue ;
     }
     f = n - 4  ;
     int rev = 0 ;
     if(arr[n-1] <= arr[n-2] && arr[n-3] >= arr[n-2])
     rev = 1 ;
     for(int x = n  -4 ; x >= 0 ; x--)
     {
        if(arr[x] > arr[x+1])
        {
           if(rev == 1)
           {
              f=  x - 1;
           }
           else
           {
           f = x +  1 ;
           break ;
           }
        }
        else
        {
           if(arr[x] < arr[x+1])
           rev = 0  ;
           f = x  - 1 ;
        }
     }
     if(f < 0)
     f = 0 ;
     cout<<f<<"\n";
   }

}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 100000000000000000
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI 3.14159265
#define pb push_back
#define LLMX LONG_LONG_MAX
#define LLMN LONG_LONG_MIN
signed main()
{
   int t ;
   cin >> t ;
   while(t--)
   {
     int n ;
     cin >> n ;
     vector < int > arr ;
     map < int , int > m ;
     vector < int > ans ;
     for(int x = 1;  x <= n ; x++)
     {
        int a ;
        cin >> a ;
        arr.pb(a) ;
     }
     if(n <= 2)
     {
        cout<<"0\n";
        continue ;
     }
     int last = -1 ;
     for(int x = 0 ; x <= n  - 3 ; x++)
     {
        if(arr[x+1] < arr[x] && arr[x+1] == arr[x+2])
        {
           //cout<<"dhf";
           int mid = arr[x+1]  ;
           int f = 0 ;
           int y = x + 2;
           while(1)
           {
              if( y == n )
              {
                 f = 1 ;
                 break ;
              }
              y++ ;
              //cout<<y<<" ";
              if(mid == arr[y])
              {
                 continue ;
              }
              else if(arr[y] > mid)
              {
                 last = x ;
                 x = y - 2 ;
                 break ;
              }
              else
              {
                 x = y - 2 ;
                 break  ;
              }
           }
        }
        else if(arr[x+1] < arr[x] && arr[x+1] < arr[x+2])
        {
           last = x ;
        }
     }
     if(last == -1)
     {
        cout<<"0\n";
     }
     else
     {
        cout<<last+1<<"\n";
        continue ;
     }
   }

}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 100000000000000000
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI 3.14159265
#define pb push_back
#define LLMX LONG_LONG_MAX
#define LLMN LONG_LONG_MIN
signed main()
{
   int t ;
   cin >> t ;
   while(t--)
   {
     int n ;
     cin >> n ;
     vector < int > arr ;
     map < int , int > m ;
     vector < int > ans ;
     for(int x = 1;  x <= n ; x++)
     {
        int a ;
        cin >> a ;
        arr.pb(a) ;
     }
     if(n <= 2)
     {
        cout<<"0\n";
        continue ;
     }
     int last = -1 ;
     for(int x = 0 ; x <= n  - 3 ; x++)
     {
        if(arr[x+1] < arr[x] && arr[x+1] < arr[x+2])
        {
           last = x ;
        }
     }
     if(last == -1)
     {
        cout<<"0\n";
     }
     else
     {
        cout<<last+1<<"\n";
        continue ;
     }
   }

}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 100000000000000000
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI 3.14159265
#define pb push_back
#define LLMX LONG_LONG_MAX
#define LLMN LONG_LONG_MIN
vector < int > arr ;
int f =0  ;
int dp[200005] ;

signed main()
{
   int t ;
   cin >> t ;
   while(t--)
   {
     // memset(dp , 0 , sizeof(dp)) ;
      arr.clear() ;
     int n ;
     cin >> n ;
     map < int , int > m ;
     vector < int > ans ;
     for(int x = 1;  x <= n ; x++)
     {
        int a ;
        cin >> a ;
        arr.pb(a) ;
     }
     if(n <= 2)
     {
        cout<<"0\n";
        continue ;
     }
     //int f  =0 ;
     f = n   ;
     //cout<<arr[n-3]<<" "<<arr[n-2]<<" "<<arr[n-1]<<"\n";
     if(arr[n-2] < arr[n-1] && arr[n-2] < arr[n-3])
     f = 0 ;
     if(f == 0)
     {
        cout<<n - 3 + 1 <<"\n";
        continue ;
     }
     f = n - 4  ;
     int rev = 0 ;
     if(arr[n-1] <= arr[n-2] && arr[n-3] >= arr[n-2])
     rev = 1 ;
     for(int x = n  -4 ; x >= 0 ; x--)
     {
        if(arr[x] > arr[x+1])
        {
           if(rev == 1)
           {
              f=  x - 1;
           }
           else
           {
           f = x +  1 ;
           break ;
           }
        }
        else
        {
           if(arr[x] < arr[x+1])
           rev = 0  ;
           f = x  - 1 ;
        }
     }
     if(f < 0)
     f = 0 ;
     cout<<f<<"\n";
   }

}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 100000000000000000
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI 3.14159265
#define pb push_back
#define LLMX LONG_LONG_MAX
#define LLMN LONG_LONG_MIN

signed main()
{
   int t ;
   cin >> t ;
   while(t--)
   {
     // memset(dp , 0 , sizeof(dp)) ;
     // arr.clear() ;
     int n ;
     cin >> n ;
     map < int , int > m ;
     vector < int > arr ;
     int f = 0 ;
     for(int x = 1;  x <= n ; x++)
     {
        int a ;
        cin >> a ;
        arr.pb(a) ;
     }
     if(n <= 2)
     {
        cout<<"0\n";
        continue ;
     }
     //int f  =0 ;
     f = 4   ;
     //cout<<arr[n-3]<<" "<<arr[n-2]<<" "<<arr[n-1]<<"\n";
     if(arr[n-2] < arr[n-1] && arr[n-2] < arr[n-3])
     f = 0 ;
     if(f == 0)
     {
        cout<<n - 3 + 1 <<"\n";
        continue ;
     }
     f = n - 4  ;
     int rev = 0 ;
     if(arr[n-1] <= arr[n-2] && arr[n-3] >= arr[n-2])
     rev = 1 ;
     for(int x = n  -4 ; x >= 0 ; x--)
     {
        if(arr[x] > arr[x+1])
        {
           if(rev == 1)
           {
              f=  x - 1;
           }
           else
           {
           f = x +  1 ;
           break ;
           }
        }
        else
        {
           if(arr[x] < arr[x+1])
           rev = 0  ;
           f = x  - 1 ;
        }
     }
     if(f < 0)
     f = 0 ;
     cout<<f<<"\n";
   }

}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 100000000000000000
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI 3.14159265
#define pb push_back
#define LLMX LONG_LONG_MAX
#define LLMN LONG_LONG_MIN
int f =0  ;
int dp[200005] ;

signed main()
{
   int t ;
   cin >> t ;
   while(t--)
   {
      for(int x = 0 ; x <= 200004 ; x++)
      dp[x] = 0 ;
   vector < int > arr ;

     // arr.clear() ;
     int n ;
     cin >> n ;
     map < int , int > m ;
     vector < int > ans ;
     for(int x = 1;  x <= n ; x++)
     {
        int a ;
        cin >> a ;
        arr.pb(a) ;
     }
     if(n <= 2)
     {
        cout<<"0\n";
        continue ;
     }
     //int f  =0 ;
     f = n   ;
     //cout<<arr[n-3]<<" "<<arr[n-2]<<" "<<arr[n-1]<<"\n";
     if(arr[n-2] < arr[n-1] && arr[n-2] < arr[n-3])
     f = 0 ;
     if(f == 0)
     {
        cout<<n - 3 + 1 <<"\n";
        continue ;
     }
     f = n - 4  ;
     int rev = 0 ;
     if(arr[n-1] <= arr[n-2] && arr[n-3] >= arr[n-2])
     rev = 1 ;
     for(int x = n  -4 ; x >= 0 ; x--)
     {
        if(arr[x] > arr[x+1])
        {
           if(rev == 1)
           {
              f=  x - 1;
           }
           else
           {
           f = x +  1 ;
           break ;
           }
        }
        else
        {
           if(arr[x] < arr[x+1])
           rev = 0  ;
           f = x  - 1 ;
        }
     }
     if(f < 0)
     f = 0 ;
     cout<<f<<"\n";
   }

}