/****
Codeforces Round #595 (Div. 3)
*****/

/***
A

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

typedef long long ll;

int main()
{
    int q;
    cin >> q;
    while(q--)
    {
        int n;
        cin >> n;
        int a[110];
        for(int i=1;i<=n;i++)
        {
            cin >> a[i];
        }
        sort(a+1,a+1+n);
        int flag=0;
        for(int i=2;i<=n;i++)
        {
            if(a[i]-a[i-1]==1)
            {
                flag=1;
                break;
            }
        }
        if(flag){
            cout <<2<<endl;
        }
        else{
            cout << 1<<endl;
        }
    }
    return 0;
}

*/
/****
B1
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>

using namespace std;
typedef long long ll;

int main()
{
    int q;
    cin >> q;
    while(q--)
    {
        int n;
        cin >> n;
        int a[210],b[210];
        for(int i=1;i<=n;i++)
        {
            cin >> a[i];
            b[i]=0;
        }
        for(int i=1;i<=n;i++)
        {

            if(b[i]!=0)continue;
          //  cout << a[i] <<i<<endl;
            if(i == a[i]){
                    b[i]=1;
                 //   cout << b[i]<<i<<endl;
            }
            else{
                int t = a[i];
                int num =1;
                while(t!=i)
                {
                    t=a[t];
                    num++;
                }
                b[t]=num;
                b[a[i]]=num;
            }
        }
        for(int i=1;i<n;i++)
        {
           printf("%d ",b[i]);
        }
        printf("%d\n",b[n]);
    }
    return 0;
}

****/
/*
7
1
2
6
13
14
3620
10000

1
3
9
13
27
6561
19683

*/
/***
B2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>

using namespace std;
typedef long long ll;
const int N = 2e5 +10;
int a[N],b[N];
int main()
{
    int q;
    cin >> q;
    while(q--)
    {
        int n;
        cin >> n;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[i]=0;
        }
        for(int i=1;i<=n;i++)
        {
            if(b[i]!=0)continue;
            int t = a[i];
            if(t==i){
                b[i]=1;
                continue;
            }
            int num =1;
            int q[N];
            q[0]=t;
            q[1]=i;
            int cun =2;
            while(b[t]==0&&t!=i)
            {
                t = a[t];
                num++;
                q[cun]=t;
                cun++;
            }
           // cout << num << endl;
            if(num>=n)
            {
                 for(int i=0;i<cun;i++)b[q[i]]=n;break;
            }
            if(t==i)
            {
                for(int i=0;i<cun;i++)b[q[i]]=num;
            }
            else{
                for(int i=0;i<cun;i++)b[q[i]]=b[t];
            }
        }
        for(int i=1;i<n;i++){
            printf("%d ",b[i]);
        }
        printf("%d\n",b[n]);
    }
}
****/

/*
7
1
2
6
13
14
3620
10000

1
3
9
13
27
6561
19683

*/

/***
C1
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
map<ll,ll>mp;
ll a[10101000];
int main()
{
    int n;
    cin >> n;
    a[0]=1;
    a[1]=3;
    a[2]=4;
    a[3]=9;
    a[4]=10;
    a[5]=12;
    a[6]=13;
    for(int i=0;i<7;i++)mp[a[i]]=1;
    ll q=9;
    int cun =6;
    for(int i=3;i<18;i++)
    {
        q=q*3;
        cout <<q <<endl;
        int d=cun;
        cun++;
        a[cun]=q;
        for(int j=0;j<=d;j++)
        {
            ll b = (ll)q+a[j];
            if(mp[b]==1)continue;
            cun++;
            a[cun]=b;
        }
    }
    sort(a,a+cun+1);
    while(n--)
    {
        int p;
        cin >> p;
        int j = lower_bound(a,a+cun+1,p)-a;
        cout << a[j]<<endl;
    }
    return 0;
}
***/
/*
7
1
2
6
13
14
3620
10000

1
3
9
13
27
6561
19683
*/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>

using namespace std;
typedef long long ll;

int main()
{
    int n;
    cin >> n;
    while(n--)
    {
        ll p;
        cin >> p;
        ll res =1,q=3;
        while(res<p)
        {
            res+=q;
            q*=3;
        }
        while(q)
        {
            if(res-q>=p)res-=q;
            q/=3;
        }
        cout << res <<endl;
    }
    return 0;
}


/****
Codeforces Round #595 (Div. 3)
*****/

/***
A

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

typedef long long ll;

int main()
{
    int q;
    cin >> q;
    while(q--)
    {
        int n;
        cin >> n;
        int a[110];
        for(int i=1;i<=n;i++)
        {
            cin >> a[i];
        }
        sort(a+1,a+1+n);
        int flag=0;
        for(int i=2;i<=n;i++)
        {
            if(a[i]-a[i-1]==1)
            {
                flag=1;
                break;
            }
        }
        if(flag){
            cout <<2<<endl;
        }
        else{
            cout << 1<<endl;
        }
    }
    return 0;
}

*/
/****
B1
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>

using namespace std;
typedef long long ll;

int main()
{
    int q;
    cin >> q;
    while(q--)
    {
        int n;
        cin >> n;
        int a[210],b[210];
        for(int i=1;i<=n;i++)
        {
            cin >> a[i];
            b[i]=0;
        }
        for(int i=1;i<=n;i++)
        {

            if(b[i]!=0)continue;
          //  cout << a[i] <<i<<endl;
            if(i == a[i]){
                    b[i]=1;
                 //   cout << b[i]<<i<<endl;
            }
            else{
                int t = a[i];
                int num =1;
                while(t!=i)
                {
                    t=a[t];
                    num++;
                }
                b[t]=num;
                b[a[i]]=num;
            }
        }
        for(int i=1;i<n;i++)
        {
           printf("%d ",b[i]);
        }
        printf("%d\n",b[n]);
    }
    return 0;
}

****/
/*
7
1
2
6
13
14
3620
10000

1
3
9
13
27
6561
19683

*/
/***
B2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>

using namespace std;
typedef long long ll;
const int N = 2e5 +10;
int a[N],b[N];
int main()
{
    int q;
    cin >> q;
    while(q--)
    {
        int n;
        cin >> n;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[i]=0;
        }
        for(int i=1;i<=n;i++)
        {
            if(b[i]!=0)continue;
            int t = a[i];
            if(t==i){
                b[i]=1;
                continue;
            }
            int num =1;
            int q[N];
            q[0]=t;
            q[1]=i;
            int cun =2;
            while(b[t]==0&&t!=i)
            {
                t = a[t];
                num++;
                q[cun]=t;
                cun++;
            }
           // cout << num << endl;
            if(num>=n)
            {
                 for(int i=0;i<cun;i++)b[q[i]]=n;break;
            }
            if(t==i)
            {
                for(int i=0;i<cun;i++)b[q[i]]=num;
            }
            else{
                for(int i=0;i<cun;i++)b[q[i]]=b[t];
            }
        }
        for(int i=1;i<n;i++){
            printf("%d ",b[i]);
        }
        printf("%d\n",b[n]);
    }
}
****/

/*
7
1
2
6
13
14
3620
10000

1
3
9
13
27
6561
19683

*/

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
map<ll,ll>mp;
int a[101010];
int main()
{
    int n;
    cin >> n;
    a[0]=1;
    a[1]=3;
    a[2]=4;
    a[3]=9;
    a[4]=10;
    a[5]=12;
    a[6]=13;
    for(int i=0;i<7;i++)mp[a[i]]=1;
    int q=9;
    int cun =6;
    for(int i=3;i<10;i++)
    {
        q=q*3;
        int d=cun;
        for(int j=0;j<=d;j++)
        {
            int b = (ll)q*a[j];
            if(mp[b]==1||b>1000000)continue;
            cun++;
            a[cun]=b;
        }
    }
    sort(a,a+cun+1);
   /*for(int i=0;i<=cun;i++){
        printf("%d\n",a[i]);
    }*/
    while(n--)
    {
        int p;
        cin >> p;
        int j = lower_bound(a,a+cun+1,p)-a;
        cout << a[j]<<endl;
    }
    return 0;
}


/****
Codeforces Round #595 (Div. 3)
*****/

/***
A

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

typedef long long ll;

int main()
{
    int q;
    cin >> q;
    while(q--)
    {
        int n;
        cin >> n;
        int a[110];
        for(int i=1;i<=n;i++)
        {
            cin >> a[i];
        }
        sort(a+1,a+1+n);
        int flag=0;
        for(int i=2;i<=n;i++)
        {
            if(a[i]-a[i-1]==1)
            {
                flag=1;
                break;
            }
        }
        if(flag){
            cout <<2<<endl;
        }
        else{
            cout << 1<<endl;
        }
    }
    return 0;
}

*/
/****
B1
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>

using namespace std;
typedef long long ll;

int main()
{
    int q;
    cin >> q;
    while(q--)
    {
        int n;
        cin >> n;
        int a[210],b[210];
        for(int i=1;i<=n;i++)
        {
            cin >> a[i];
            b[i]=0;
        }
        for(int i=1;i<=n;i++)
        {

            if(b[i]!=0)continue;
          //  cout << a[i] <<i<<endl;
            if(i == a[i]){
                    b[i]=1;
                 //   cout << b[i]<<i<<endl;
            }
            else{
                int t = a[i];
                int num =1;
                while(t!=i)
                {
                    t=a[t];
                    num++;
                }
                b[t]=num;
                b[a[i]]=num;
            }
        }
        for(int i=1;i<n;i++)
        {
           printf("%d ",b[i]);
        }
        printf("%d\n",b[n]);
    }
    return 0;
}

****/
/*
7
1
2
6
13
14
3620
10000

1
3
9
13
27
6561
19683

*/
/***
B2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>

using namespace std;
typedef long long ll;
const int N = 2e5 +10;
int a[N],b[N];
int main()
{
    int q;
    cin >> q;
    while(q--)
    {
        int n;
        cin >> n;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[i]=0;
        }
        for(int i=1;i<=n;i++)
        {
            if(b[i]!=0)continue;
            int t = a[i];
            if(t==i){
                b[i]=1;
                continue;
            }
            int num =1;
            int q[N];
            q[0]=t;
            q[1]=i;
            int cun =2;
            while(b[t]==0&&t!=i)
            {
                t = a[t];
                num++;
                q[cun]=t;
                cun++;
            }
           // cout << num << endl;
            if(num>=n)
            {
                 for(int i=0;i<cun;i++)b[q[i]]=n;break;
            }
            if(t==i)
            {
                for(int i=0;i<cun;i++)b[q[i]]=num;
            }
            else{
                for(int i=0;i<cun;i++)b[q[i]]=b[t];
            }
        }
        for(int i=1;i<n;i++){
            printf("%d ",b[i]);
        }
        printf("%d\n",b[n]);
    }
}
****/

/*
7
1
2
6
13
14
3620
10000

1
3
9
13
27
6561
19683

*/

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
map<ll,ll>mp;
int a[101010];
int main()
{
    int n;
    cin >> n;
    a[0]=1;
    a[1]=3;
    a[2]=4;
    a[3]=9;
    a[4]=10;
    a[5]=12;
    a[6]=13;
    for(int i=0;i<7;i++)mp[a[i]]=1;
    int q=9;
    int cun =6;
    for(int i=3;i<11;i++)
    {
        q=q*3;
        int d=cun;
        for(int j=0;j<=d;j++)
        {
            int b = (ll)q+a[j];
            if(mp[b]==1||b>1000000)continue;
            cun++;
            a[cun]=b;
        }
    }
    sort(a,a+cun+1);
   /*for(int i=0;i<=cun;i++){
        printf("%d\n",a[i]);
    }*/
    while(n--)
    {
        int p;
        cin >> p;
        int j = lower_bound(a,a+cun+1,p)-a;
        cout << a[j]<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int q;
    cin >> q;
    while (q--)
    {
        long long n;
        cin >> n;
        long long pow3 = 1;
        int N = n;
        for (int i = 1;; i++)
        {
            pow3 *= 3;
            if (pow3 == N || pow3 + 1 == N)
            {
                cout << n;
                break;
            }
            else if (pow3 > N)
            {
                N = N - (pow3 / 3);
                pow3 = 1;
            }
        }
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int q;
    cin >> q;
    while (q--)
    {
        long long n;
        cin >> n;
        long long pow3 = 1;
        long long N = n;
        for (int i = 1;; i++)
        {
            pow3 *= 3;
            if (pow3 == N || pow3 + 1 == N)
            {
                cout << n;
                break;
            }
            else if (pow3 > N)
            {
                N = N - (pow3 / 3);
                pow3 = 1;
            }
        }
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int pows[10];
int getMaxPowerOf3(int n)
{
    int val;
    for (int i = 0; i < 10; i++)
    {
        if (pows[i] == n)
        {
            return i;
        }
        else if (pows[i] > n)
        {
            return i - 1;
        }
    }
}
int main()
{
    pows[0] = 1;
    for (int i = 1; i < 10; i++)
    {
        pows[i] = 3 * pows[i - 1];
    }
    int q;
    cin >> q;
    while (q--)
    {
        int n;
        cin >> n;
        int leftNo = n;
        int maxPowN = getMaxPowerOf3(leftNo);
        int prevPow = -1;
        int thres = -1;
        int usedPows[10];
        for (int i = 0; i < 10; i++)
        {
            usedPows[i] = 0;
        }
        while (leftNo != 0)
        {
            int maxPow = getMaxPowerOf3(leftNo);
            // cout << "maxPow :" << maxPow << endl;
            if (usedPows[maxPow] == 1)
            {
                for (int i = maxPow + 1; i < 10; i++)
                {
                    if (usedPows[i] == 0)
                    {
                        // cout << "i & pow[i] " << i << " " << pows[i] << " " << leftNo << endl;
                        if (i > maxPowN)
                        {
                            thres = pows[i];
                        }
                        else
                        {
                            thres = pows[i];
                            for (int j = i + 1; j <= maxPowN; j++)
                            {
                                if (usedPows[j])
                                {
                                    thres += pows[j];
                                    break;
                                }
                            }
                        }
                        break;
                    }
                }
                if (thres > -1)
                {
                    break;
                }
            }
            leftNo = leftNo - pows[maxPow];
            // cout << "leftNo is:::-" << leftNo << endl;
            usedPows[maxPow] = 1;
        }
        if (leftNo == 0)
        {
            cout << n;
        }
        else
        {
            cout << thres;
        }
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int pows[10];
int getMaxPowerOf3(int n)
{
    int val;
    for (int i = 0; i < 10; i++)
    {
        if (pows[i] == n)
        {
            return i;
        }
        else if (pows[i] > n)
        {
            return i - 1;
        }
    }
}
int main()
{
    pows[0] = 1;
    for (int i = 1; i < 10; i++)
    {
        pows[i] = 3 * pows[i - 1];
    }
    int q;
    cin >> q;
    while (q--)
    {
        int n;
        cin >> n;
        int leftNo = n;
        int prevPow = -1;
        while (leftNo != 0)
        {
            int maxPow = getMaxPowerOf3(leftNo);
            // cout << "maxPow :" << maxPow << endl;
            if (maxPow == prevPow)
            {
                break;
            }
            leftNo = leftNo - pows[maxPow];
            // cout << leftNo << endl;
            prevPow = maxPow;
        }
        if (leftNo == 0)
        {
            cout << n;
        }
        else
        {
            cout << pows[getMaxPowerOf3(n) + 1];
        }
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int pows[10];
int getMaxPowerOf3(int n)
{
    int val;
    for (int i = 0; i < 10; i++)
    {
        if (pows[i] == n)
        {
            return i;
        }
        else if (pows[i] > n)
        {
            return i - 1;
        }
    }
}
int main()
{
    pows[0] = 1;
    for (int i = 1; i < 10; i++)
    {
        pows[i] = 3 * pows[i - 1];
    }
    int q;
    cin >> q;
    while (q--)
    {
        int n;
        cin >> n;
        int leftNo = n;
        int maxPowN = getMaxPowerOf3(leftNo);
        int prevPow = -1;
        int thres = -1;
        int usedPows[10];
        for (int i = 0; i < 10; i++)
        {
            usedPows[i] = 0;
        }
        while (leftNo != 0)
        {
            int maxPow = getMaxPowerOf3(leftNo);
            // cout << "maxPow :" << maxPow << endl;
            if (usedPows[maxPow] == 1)
            {
                for (int i = maxPow + 1; i < 10; i++)
                {
                    if (usedPows[i] == 0)
                    {
                        // cout << "i & pow[i] " << i << " " << pows[i] << " " << leftNo << endl;
                        if (i > maxPowN)
                        {
                            thres = pows[i];
                        }
                        else
                        {
                            thres = pows[i];
                            for (int j = i + 1; j <= maxPowN; j++)
                            {
                                if (usedPows[j])
                                {
                                    thres += pows[j];
                                }
                                // cout << "leftNo is:::-" << leftNo << endl;
                            }
                        }
                        break;
                    }
                }
                if (thres > -1)
                {
                    break;
                }
            }
            leftNo = leftNo - pows[maxPow];
            // cout << "leftNo is:::-" << leftNo << endl;
            usedPows[maxPow] = 1;
        }
        if (leftNo == 0)
        {
            cout << n;
        }
        else
        {
            cout << thres;
        }
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int pows[10];
int getMaxPowerOf3(int n)
{
    int val;
    for (int i = 0; i < 10; i++)
    {
        if (pows[i] == n)
        {
            return i;
        }
        else if (pows[i] > n)
        {
            return i - 1;
        }
    }
}
int main()
{
    pows[0] = 1;
    for (int i = 1; i < 10; i++)
    {
        pows[i] = 3 * pows[i - 1];
    }
    int q;
    cin >> q;
    while (q--)
    {
        int n;
        cin >> n;
        int leftNo = n;
        int maxPowN = getMaxPowerOf3(leftNo);
        int prevPow = -1;
        int thres = 0;
        int usedPows[10];
        for (int i = 0; i < 10; i++)
        {
            usedPows[i] = 0;
        }
        while (leftNo != 0)
        {
            int maxPow = getMaxPowerOf3(leftNo);
            // cout << "maxPow :" << maxPow << endl;
            if (usedPows[maxPow] == 1)
            {
                for (int i = maxPow + 1; i < 10; i++)
                {
                    if (usedPows[i] == 0)
                    {
                        // cout << "i & pow[i] " << i << " " << pows[i] << " " << leftNo << endl;
                        if (i > maxPowN)
                        {
                            cout << pows[i];
                        }
                        else
                        {
                            cout << (n + pows[i] - (leftNo + pows[maxPow]));
                        }
                        thres = pows[i] - (leftNo + pows[maxPow]);
                        break;
                    }
                }
                if (thres)
                {
                    break;
                }
            }
            leftNo = leftNo - pows[maxPow];
            // cout << "leftNo is:::-" << leftNo << endl;
            usedPows[maxPow] = 1;
        }
        if (leftNo == 0)
        {
            cout << n;
        }
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int q;
    cin >> q;
    while (q--)
    {
        long long n;
        cin >> n;
        long long pow3 = 1;
        long long N = n;
        for (int i = 1;; i++)
        {
            if (pow3 == N)
            {
                cout << n;
                break;
            }
            else if (pow3 + 1 == N)
            {
                cout << n + 1;
                break;
            }
            else if (pow3 > N)
            {
                N = N - (pow3 / 3);
                pow3 = 1;
            }
            pow3 *= 3;
        }
        cout << endl;
    }
    return 0;
}


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

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ll long long
#define rep(i,j,n) for(int i=j; i<n; i++)
#define INF 1000000000000000000 // If you use it, make sure not to put in "int" type variable ;(, use long long int
void optimizeIO() { ios_base::sync_with_stdio(false); cin.tie(NULL); }
const ll mod=1000000007;

bool f(int n)
{
	while(n) { if(n%3==2) return false; n/=3; }
	return true;
}

int main()
{
    optimizeIO();
    int nT; cin>>nT;
	while(nT--)
	{
		int n; cin>>n;
		while(1)
		{
			if(f(n)) { cout<<n<<'\n'; break; }
			else n++;
		}
	}
	return 0;
}


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

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ll long long
#define rep(i,j,n) for(int i=j; i<n; i++)
#define INF 1000000000000000000 // If you use it, make sure not to put in "int" type variable ;(, use long long int
void optimizeIO() { ios_base::sync_with_stdio(false); cin.tie(NULL); }
const ll mod=1000000007;

inline bool f(int n)
{
	while(n) { if(n%3==2) return false; n/=3; }
	return true;
}

int main()
{
    optimizeIO();
    int nT; cin>>nT;
	while(nT--)
	{
		int n; cin>>n;
		while(1)
		{
			if(f(n)) { cout<<n<<'\n'; break; }
			else n++;
		}
	}
	return 0;
}


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

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ll long long
#define rep(i,j,n) for(int i=j; i<n; i++)
#define INF 1000000000000000000 // If you use it, make sure not to put in "int" type variable ;(, use long long int
void optimizeIO() { ios_base::sync_with_stdio(false); cin.tie(NULL); }
const ll mod=1000000007;

inline bool f(int n)
{
	vector<int>v;
	while(n) { v.pb(n%3); n/=3; }
	n=v.size();
	rep(i,0,n) if(v[i]!=0 && v[i]!=1) return false;
	return true;
}

int main()
{
    optimizeIO();
    int nT; cin>>nT;
	while(nT--)
	{
		int n; cin>>n;
		while(1)
		{
			if(f(n)) { cout<<n<<'\n'; break; }
			else n++;
		}
	}
	return 0;
}


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

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ll long long
#define rep(i,j,n) for(int i=j; i<n; i++)
#define INF 1000000000000000000 // If you use it, make sure not to put in "int" type variable ;(, use long long int
void optimizeIO() { ios_base::sync_with_stdio(false); cin.tie(NULL); }
const ll mod=1000000007;

int main()
{
    optimizeIO();
    int nT; cin>>nT;
	while(nT--)
	{
		ll n; cin>>n;
		ll sn=n, plus=0;
		while(1)
		{
			bool ans=true;
			n=sn+plus;
			while(n) { if(n%3==2){ans=false; break; } n/=3; }
			if(ans) break;
			else plus++;
		}
		cout<<sn+plus<<'\n';
	}
	return 0;
}


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

long long sumList[12] = { 0 };

int judge(int n) {
    if (n == 1) {
        return 1;
    }
    if (n == 2) {
        return 3;
    }

    int sqInt = 10;
    while (sumList[sqInt - 1] >= n) {
        sqInt--;
    }
    int answer = pow(3, sqInt);
    while (answer < n) {
        sqInt--;
        if (answer + pow(3, sqInt) > n && (sqInt == 1 ||(sqInt > 0 && (answer + sumList[sqInt - 1] > n)))) {
            continue;
        }
        else {
            answer += (int)pow(3, sqInt);
        }
        if (sqInt == 0) {
            if (answer < n) {
                answer += 1;
            }
            break;
        }
    }
    return answer;
}

int main() {
    for (int i = 0; i <= 11; i++) {
        if (i > 0) {
            sumList[i] = sumList[i - 1] + pow(3, i);
        }
        else {
            sumList[i] = pow(3, i);
        }
    }

    ios::sync_with_stdio(false);
    int caseCount = 0;
    cin >> caseCount;

    for (int i = 0; i < caseCount; i++) {
        int n;
        cin >> n;
        int answer = judge(n);
        cout << answer << endl;
    }
    return 0;
}


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

long long sumList[23] = {0};

int judge(int n) {
    if (n == 1) {
        return 1;
    }
    if (n == 2) {
        return 3;
    }

    int sqInt = 9;
    while (sumList[sqInt - 1] >= n) {
        sqInt--;
    }
    int answer = pow(3, sqInt);
    while (answer < n) {
        sqInt--;
        if (answer + pow(3, sqInt) > n) {
            continue;
        } else {
            answer += (int) pow(3, sqInt);
        }
        if (sqInt == 0) {
            if (answer < n) {
                answer += answer + 1;
            }
            break;
        }
    }
    return answer;
}

int main() {
    for (int i = 0; i <= 22; i++) {
        if (i > 0) {
            sumList[i] = sumList[i - 1] + pow(3, i);
        } else {
            sumList[i] = pow(3, i);
        }
    }

    ios::sync_with_stdio(false);
    int caseCount = 0;
    cin >> caseCount;

    for (int i = 0; i < caseCount; i++) {
        int n;
        cin >> n;
        int answer = judge(n);
        cout << answer << endl;
    }

    return 0;
}


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

long long sumList[12] = { 0 };

int judge(int n) {
    if (n == 1) {
        return 1;
    }
    if (n == 2) {
        return 3;
    }

    int sqInt = 10;
    while (sumList[sqInt - 1] >= n) {
        sqInt--;
    }
    int answer = pow(3, sqInt);
    while (answer < n) {
        sqInt--;
        if (answer + pow(3, sqInt) > n || (sqInt > 0 && (answer + sumList[sqInt - 1] > n))) {
            continue;
        }
        else {
            answer += (int)pow(3, sqInt);
        }
        if (sqInt == 0) {
            if (answer < n) {
                answer += 1;
            }
            break;
        }
    }
    return answer;
}

int main() {
    for (int i = 0; i <= 11; i++) {
        if (i > 0) {
            sumList[i] = sumList[i - 1] + pow(3, i);
        }
        else {
            sumList[i] = pow(3, i);
        }
    }

    ios::sync_with_stdio(false);
    int caseCount = 0;
    cin >> caseCount;

    for (int i = 0; i < caseCount; i++) {
        int n;
        cin >> n;
        int answer = judge(n);
        cout << answer << endl;
    }
    return 0;
}


#include <iostream>
#include <cmath>

using namespace std;

long long sumList[23] = {0};

int judge(int n) {
    if (n == 1) {
        return 1;
    }
    if (n == 2) {
        return 3;
    }

    int sqInt = 8;
    while (sumList[sqInt - 1] >= n) {
        sqInt--;
    }
    int answer = pow(3, sqInt);
    while (answer < n) {
        sqInt--;
        if (answer + pow(3, sqInt) > n) {
            continue;
        } else {
            answer += (int) pow(3, sqInt);
        }
        if (sqInt == 0) {
            if (answer < n) {
                answer += answer + 1;
            }
            break;
        }
    }
    return answer;
}

int main() {
    for (int i = 0; i <= 22; i++) {
        if (i > 0) {
            sumList[i] = sumList[i - 1] + pow(3, i);
        } else {
            sumList[i] = pow(3, i);
        }
    }

    ios::sync_with_stdio(false);
    int caseCount = 0;
    cin >> caseCount;

    for (int i = 0; i < caseCount; i++) {
        int n;
        cin >> n;
        int answer = judge(n);
        cout << answer << endl;
    }
    return 0;
}


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

long long sumList[12] = { 0 };

int judge(int n) {
    if (n == 1) {
        return 1;
    }
    if (n == 2) {
        return 3;
    }

    int sqInt = 10;
    while (sumList[sqInt - 1] >= n) {
        sqInt--;
    }
    int answer = pow(3, sqInt);
    while (answer < n) {
        sqInt--;
        if (answer + pow(3, sqInt) > n && answer + sumList[sqInt - 1] > n) {
            continue;
        }
        else {
            answer += (int)pow(3, sqInt);
        }
        if (sqInt == 0) {
            if (answer < n) {
                answer += 1;
            }
            break;
        }
    }
    return answer;
}

int main() {
    for (int i = 0; i <= 11; i++) {
        if (i > 0) {
            sumList[i] = sumList[i - 1] + pow(3, i);
        }
        else {
            sumList[i] = pow(3, i);
        }
    }

    ios::sync_with_stdio(false);
    int caseCount = 0;
    cin >> caseCount;

    for (int i = 0; i < caseCount; i++) {
        int n;
        cin >> n;
        int answer = judge(n);
        cout << answer << endl;
    }
    return 0;
}


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

long long sumList[12] = { 0 };

int judge(int n) {
    if (n == 1) {
        return 1;
    }
    if (n == 2) {
        return 3;
    }

    int sqInt = 10;
    while (sumList[sqInt - 1] >= n) {
        sqInt--;
    }
    int answer = pow(3, sqInt);
    while (answer < n) {
        sqInt--;

        if (answer + pow(3, sqInt) <= n || (sqInt > 0 && (answer + sumList[sqInt - 1] < n))) {
            answer += (int)pow(3, sqInt);
        } else {
            continue;
        }
        if (sqInt == 0) {
            if (answer < n) {
                answer += 1;
            }
            break;
        }
    }
    return answer;
}

int main() {
    for (int i = 0; i <= 11; i++) {
        if (i > 0) {
            sumList[i] = sumList[i - 1] + pow(3, i);
        }
        else {
            sumList[i] = pow(3, i);
        }
    }

    ios::sync_with_stdio(false);
    int caseCount = 0;
    cin >> caseCount;

    for (int i = 0; i < caseCount; i++) {
        int n;
        cin >> n;
        int answer = judge(n);
        cout << answer << endl;
    }
    return 0;
}


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

long long sumList[12] = {0};

int judge(int n) {
    if (n == 1) {
        return 1;
    }
    if (n == 2) {
        return 3;
    }

    int sqInt = 10;
    while (sumList[sqInt - 1] >= n) {
        sqInt--;
    }
    int answer = pow(3, sqInt);
    while (answer < n) {
        sqInt--;
        if (answer + pow(3, sqInt) > n) {
            continue;
        } else {
            answer += (int) pow(3, sqInt);
        }
        if (sqInt == 0) {
            if (answer < n) {
                answer += answer + 1;
            }
            break;
        }
    }
    return answer;
}

int main() {
    for (int i = 0; i <= 11; i++) {
        if (i > 0) {
            sumList[i] = sumList[i - 1] + pow(3, i);
        } else {
            sumList[i] = pow(3, i);
        }
    }

    ios::sync_with_stdio(false);
    int caseCount = 0;
    cin >> caseCount;

    for (int i = 0; i < caseCount; i++) {
        int n;
        cin >> n;
        int answer = judge(n);
        cout << answer << endl;
    }
//    cout << judge(1) << endl;
//    cout << judge(2) << endl;
//    cout << judge(6) << endl;
//    cout << judge(13) << endl;
//    cout << judge(14) << endl;
//    cout << judge(3620) << endl;
//    cout << judge(6563) << endl;

    return 0;
}


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

long long sumList[12] = {0};

int judge(int n) {
    if (n == 1) {
        return 1;
    }
    if (n == 2) {
        return 3;
    }

    int sqInt = 10;
    while (sumList[sqInt - 1] >= n) {
        sqInt--;
    }
    int answer = pow(3, sqInt);
    while (answer < n) {
        sqInt--;
        if (answer + pow(3, sqInt) > n) {
            continue;
        } else {
            answer += (int) pow(3, sqInt);
        }
        if (sqInt == 0) {
            if (answer < n) {
                answer += answer + 1;
            }
            break;
        }
    }
    return answer;
}

int main() {
    for (int i = 0; i <= 11; i++) {
        if (i > 0) {
            sumList[i] = sumList[i - 1] + pow(3, i);
        } else {
            sumList[i] = pow(3, i);
        }
    }

    ios::sync_with_stdio(false);
    int caseCount = 0;
    cin >> caseCount;

    for (int i = 0; i < caseCount; i++) {
        int n;
        cin >> n;
        int answer = judge(n);
        cout << answer << endl;
    }
    return 0;
}


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

long long sumList[12] = { 0 };

int judge(int n) {
    if (n == 1) {
        return 1;
    }
    if (n == 2) {
        return 3;
    }

    int sqInt = 10;
    while (sumList[sqInt - 1] >= n) {
        sqInt--;
    }
    int answer = pow(3, sqInt);
    while (answer < n) {
        sqInt--;
        if (answer + pow(3, sqInt) > n || answer + sumList[sqInt - 1] > n) {
            continue;
        }
        else {
            answer += (int)pow(3, sqInt);
        }
        if (sqInt == 0) {
            if (answer < n) {
                answer += 1;
            }
            break;
        }
    }
    return answer;
}

int main() {
    for (int i = 0; i <= 11; i++) {
        if (i > 0) {
            sumList[i] = sumList[i - 1] + pow(3, i);
        }
        else {
            sumList[i] = pow(3, i);
        }
    }

    ios::sync_with_stdio(false);
    int caseCount = 0;
    cin >> caseCount;

    for (int i = 0; i < caseCount; i++) {
        int n;
        cin >> n;
        int answer = judge(n);
        cout << answer << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
using namespace std;
const int maxx = 19683;
int result;
int fresult;
int n;
void rec(int ind)
{
    result += pow(3,ind);
    if(result >= n)
    {
        fresult = min(result,fresult);
        return;
    }
    for(int i = ind + 1;i < 10;i++)
    {
        rec(i);
    }
    result -= pow(3,ind);
}

int main()
{
	std::ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    #endif
   int q;
   cin>>q;
   while(q--)
   {
       fresult = maxx;
       cin>>n;
       for(int i = 0;i < 10;i++)
       {
           result = 0;
           rec(i);
       }
       cout<<fresult<<endl;

   }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
using namespace std;
const int maxx = 19683;
int result;
int fresult;
int n;
void rec(int ind)
{
    result += pow(3,ind);
    if(result >= n)
    {
        fresult = min(result,fresult);
        return;
    }
    for(int i = ind + 1;i < 9;i++)
    {
        rec(i);
        result -= pow(3,ind);
    }

}

int main()
{
	std::ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    #endif
   int q;
   cin>>q;
   while(q--)
   {
       fresult = maxx;
       cin>>n;
       for(int i = 0;i < 9;i++)
       {
           result = 0;
           rec(i);
       }
       cout<<fresult<<endl;

   }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
using namespace std;
const int maxx = 19683;
int result = 0;
int fresult = maxx;
int n;
void rec(int ind)
{
    result += pow(3,ind);
   // cout<<result<<endl;
    if(result >= n)
    {
        fresult = min(result,fresult);
        return;
    }
    for(int i = ind + 1;i < 10;i++)
    {

        rec(i);

    }
}

int main()
{
	std::ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    #endif
   int q;
   cin>>q;
   while(q--)
   {
       result = 0;
       fresult = 19683;
       cin>>n;
       for(int i = 0;i <= 9;i++)
       {
           result = 0;
           rec(i);
       }
       cout<<fresult<<endl;

   }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
using namespace std;
const int maxx = 19683;
int result;
int fresult;
int n;
void rec(int ind)
{
    if(result >= n)
    {
        fresult = min(result,fresult);
    }
    result += pow(3,ind);
   // cout<<result<<" "<<ind<<endl;
    for(int i = ind + 1;i < 10;i++)
    {
        rec(i);
    }
    result -= pow(3,ind);

}

int main()
{
	std::ios::sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    #endif
   int q;
   cin>>q;
   while(q--)
   {
       fresult = maxx;
       cin>>n;
       for(int i = 0;i < 10;i++)
       {
           result =0;
           rec(i);
       }
       cout<<fresult<<endl;
   }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
using namespace std;
const int maxx = 19683;
int result = 0;
int fresult = maxx;
int n;
void rec(int ind)
{
    result += pow(3,ind);
    if(result >= n)
    {
        fresult = min(result,fresult);
        return;
    }
    for(int i = ind + 1;i < 11;i++)
    {
        rec(i);
    }
}

int main()
{
	std::ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    #endif
   int q;
   cin>>q;
   while(q--)
   {
       fresult = maxx;
       cin>>n;
       for(int i = 0;i < 11;i++)
       {
           result = 0;
           rec(i);
       }
       cout<<fresult<<endl;

   }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
using namespace std;
const int maxx = 19683;
int result;
int fresult;
int n;
void rec(int ind)
{
    result += pow(3,ind);
    if(result >= n)
    {
        fresult = min(result,fresult);
        return;
    }
    for(int i = ind + 1;i < 10;i++)
    {
        return rec(i);

    }
    result -= pow(3,ind);

}

int main()
{
	std::ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    #endif
   int q;
   cin>>q;
   while(q--)
   {
       fresult = maxx;
       cin>>n;
       for(int i = 0;i < 10;i++)
       {
           result = 0;
           rec(i);
       }
       cout<<fresult<<endl;

   }
    return 0;
}


// IIITDMJ

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

#define PI acos(-1)
#define MAX_LL numeric_limits<ll> :: max();
#define nitro std::ios_base::sync_with_stdio (false); cin.tie(NULL)
#define pb push_back
#define popb pop_back
#define mp make_pair
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define repl(i,a,b) for(ll i=a;i<b;i++)
#define repv(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)
#define test int t; cin>>t;while(t--)
#define F first
#define S second
#define all(a) a.begin(),a.end()
#define last(a) a.size()-1
#define has(c,x) (c.find(x)!=c.end())
#define in(a) cin >> a
#define out(a) cout << a
#define nl cout << endl

typedef stringstream sstr;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<double,double> pdd;
typedef pair<long,long> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;

ll pwr(ll base, ll p, ll mod){ll ans = 1; while(p) { if(p&1) ans=(ans*base)%mod; base=(base*base)%mod; p/=2; } return ans;}
ll gcd(ll a, ll b) {  return b == 0 ? a : gcd(b,a%b); }
ll lcm(ll a, ll b) {  return a*(b/gcd(a,b)); }
bool cmp(int a, int b) { return a>b; }

const int dx[8]={0,1, 0,-1,-1,1,-1, 1};
const int dy[8]={1,0,-1, 0,-1,1, 1,-1};
const int dsx[4]={0,1, 0,-1};
const int dsy[4]={1,0,-1, 0};
const int mod = 1e9+7;

#define db(...) ZZ(#__VA_ARGS__, __VA_ARGS__)

template <class T, class S> void outvp(vector<pair<T,S>> &v){
    for(pair<T,S> x: v) cout << x.first << " " << x.second << endl;
}
template <typename T> void outv(vector<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outs(set<T> &v){
    for(T x: v) cout << x << " "; cout << endl;
}
template <typename T> void outm(map<T, int> &v, int n=1){
    for(T &x: v) cout << (n==1 ? x.first : x.second) << " "; cout << endl;
}
template <typename T> void readv(vector<T> &v, ll n){
    repl(i,0,n){ T x; in(x); v.pb(x); }
}

#define db(...) ZZ(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void ZZ(const char* name, Arg1&& arg1){std::cerr << name << " = " << arg1 << endl;}
template <typename Arg1, typename... Args>void ZZ(const char* names, Arg1&& arg1, Args&&... args)
{
  const char* comma = strchr(names + 1, ',');
  std::cerr.write(names, comma - names) << " = " << arg1;
  ZZ(comma, args...);
}

int multiply(int x, int res[], int res_size) {

// Initialize carry
int carry = 0;

// One by one multiply n with
// individual digits of res[]
for (int i = 0; i < res_size; i++) {
    int prod = res[i] * x + carry;

    // Store last digit of
    // 'prod' in res[]
    res[i] = prod % 10;

    // Put rest in carry
    carry = prod / 10;
}

// Put carry in res and
// increase result size
while (carry) {
    res[res_size] = carry % 10;
    carry = carry / 10;
    res_size++;
}
return res_size;
}

// This function finds
// power of a number x
void power(int x, int n)
{

//printing value "1" for power = 0
if(n == 0 ){
    cout<<"1";
    return;
}

int res[10000];
int res_size = 0;
int temp = x;

// Initialize result
while (temp != 0) {
    res[res_size++] = temp % 10;
    temp = temp / 10;
}

// Multiply x n times
// (x^n = x*x*x....n times)
for (int i = 2; i <= n; i++)
    res_size = multiply(x, res, res_size);

// cout << x << "^" << n << " = ";
for (int i = res_size - 1; i >= 0; i--)
    cout << res[i];
}

int main () {
    nitro;

    // #ifdef DBG
        // freopen("in.txt","r",stdin);
    // #endif

    test{
      vi a;
      ll n; cin>>n;
      ll temp = n;

      while(n > 0){
        a.pb(n%3);
        n /= 3;
      }

      int z=-1;
      bool f = false;
      repb(i,a.size()-1,0){
        if((!f)&&(a[i] == 0)) z = i;

        if(a[i]==2){
          f = true;
          break;
        }

        if(f&&z==-1) break;
      }

      if(f&&z>0){
        repb(i,z-1,0)
          a[i] = 0;
      }

      if(f&&z==-1){
        power(3,a.size());
      }
      else{
        if (f) a[z] = 1;
        ll ans = a[0];
        ll num = 3;
        rep(i,1,a.size()){
          if(a[i])
            ans += num;
          num *= 3;
        }
        cout << ans;
      }
      nl;
    }

  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <string.h>
#include <stack>
#include <queue>
#include <unordered_map>
#include <string>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const long long MAXN = 202222;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9+9;
using namespace std;

ll n, a[MAXN];
int main(){
    scanf("%lld", &n);
    for(int i=0;i<n;i++) scanf("%lld", a+i);

    int L = 0, R = 0, conse = 1, maxlen=1;
    for(int i=1;i<n;i++){
        if(a[i] == a[i-1]){
            conse += 1;
        }
        else{
            if(conse > maxlen){
                maxlen = conse;
                L = i-maxlen;
                R = i-1;
            }
            conse = 1;
        }
    }
    if(conse > maxlen){
        R = n-1;
        L = n-conse;
    }
    //cout<<"L="<<L<<" R="<<R<<endl;
    printf("%lld\n", n-(R-L+1));
    for(int i=L-1; i>=0;i--)
        printf("%d %d %d\n", 1, i+1, i+2);
    for(int i=R+1;i<n;i++)
        printf("%d %d %d\n", 1, i, i+1);
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <string.h>
#include <stack>
#include <queue>
#include <unordered_map>
#include <string>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const long long MAXN = 202222;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9+9;
using namespace std;

ll n, a[MAXN];
int main(){
    scanf("%lld", &n);
    for(int i=0;i<n;i++) scanf("%lld", a+i);

    int L = 0, R = 0, conse = 1, maxlen=1;
    for(int i=1;i<n;i++){
        if(a[i] == a[i-1]){
            conse += 1;
        }
        else{
            if(conse > maxlen){
                maxlen = conse;
                L = i-maxlen;
                R = i-1;
            }
            conse = 1;
        }
    }
    if(conse > maxlen){
        R = n-1;
        L = n-conse;
    }
    //cout<<"L="<<L<<" R="<<R<<endl;
    printf("%lld\n", n-(R-L+1));
    for(int i=L-1; i>=0;i--){
        printf("%d %d %d\n", a[i]<a[i+1]?1:2, i+1, i+2);
        a[i] = a[i+1];
    }
    for(int i=R+1;i<n;i++){
        printf("%d %d %d\n", a[i]<a[i-1]?1:2, i+1, i);
        a[i] = a[i-1];
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <string.h>
#include <stack>
#include <queue>
#include <unordered_map>
#include <string>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const long long MAXN = 202222;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9+9;
using namespace std;

ll n, a[MAXN];
int main(){
    scanf("%lld", &n);
    for(int i=0;i<n;i++) scanf("%lld", a+i);

    int L = 0, R = 0, conse = 1, maxlen=1;
    for(int i=1;i<n;i++){
        if(a[i] == a[i-1]){
            conse += 1;
        }
        else{
            if(conse > maxlen){
                maxlen = conse;
                L = i-maxlen;
                R = i-1;
            }
            conse = 1;
        }
    }
    if(conse > maxlen){
        R = n-1;
        L = n-conse;
    }
    //cout<<"L="<<L<<" R="<<R<<endl;
    printf("%lld\n", n-(R-L+1));
    for(int i=L-1; i>=0;i--)
        printf("%d %d %d\n", a[i]<a[i+1]?1:2, i+1, i+2);
    for(int i=R+1;i<n;i++)
        printf("%d %d %d\n", a[i]<a[i-1]?1:2, i+1, i);
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <string.h>
#include <stack>
#include <queue>
#include <unordered_map>
#include <string>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const long long MAXN = 202222;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9+9;
using namespace std;

ll n, a[MAXN];
int main(){
    scanf("%lld", &n);
    for(int i=0;i<n;i++) scanf("%lld", a+i);

    int L = 0, R = 0, conse = 1, maxlen=1;
    for(int i=1;i<n;i++){
        if(a[i] == a[i-1]){
            conse += 1;
        }
        else{
            if(conse > maxlen){
                maxlen = conse;
                L = i-maxlen;
                R = i-1;
            }
            conse = 1;
        }
    }
    if(conse > maxlen){
        R = n-1;
        L = n-conse;
    }

    int cnt = 0;
    for(int i=0;i<n;i++)
        if(a[i] == a[R])
            cnt++;

    //cout<<"L="<<L<<" R="<<R<<endl;
    printf("%lld\n", n-cnt);
    for(int i=L-1; i>=0;i--){
        if(a[i] == a[R]) continue;
        printf("%d %d %d\n", a[i]<a[i+1]?1:2, i+1, i+2);
        a[i] = a[i+1];
    }
    for(int i=R+1;i<n;i++){
        if(a[i] == a[R]) continue;
        printf("%d %d %d\n", a[i]<a[i-1]?1:2, i+1, i);
        a[i] = a[i-1];
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <string.h>
#include <stack>
#include <queue>
#include <unordered_map>
#include <string>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const long long MAXN = 202222;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9+9;
using namespace std;

ll n, a[MAXN];
int main(){
    scanf("%lld", &n);
    for(int i=0;i<n;i++) scanf("%lld", a+i);

    int L = 0, R = 0, conse = 1, maxlen=1;
    for(int i=1;i<n;i++){
        if(a[i] == a[i-1]){
            conse += 1;
        }
        else{
            if(conse > maxlen){
                maxlen = conse;
                L = i-maxlen;
                R = i-1;
            }
            conse = 1;
        }
    }
    if(conse > maxlen){
        R = n-1;
        L = n-conse;
    }
    //cout<<"L="<<L<<" R="<<R<<endl;
    printf("%lld\n", n-(R-L+1));
    for(int i=L-1; i>=0;i--)
        printf("%d %d %d\n", a[i]<a[i+1]?1:2, i+1, i+2);
    for(int i=R+1;i<n;i++)
        printf("%d %d %d\n", a[i]<a[i-1]?1:2, i, i+1);
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <string.h>
#include <stack>
#include <queue>
#include <unordered_map>
#include <string>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const long long MAXN = 202222;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9+9;
using namespace std;

ll n, a[MAXN];
int main(){
    scanf("%lld", &n);
    map<ll, int> cnt;
    for(int i=0;i<n;i++) {
        scanf("%lld", a+i);
        cnt[a[i]]++;
    }

    int num=0, maxcnt=0;
    for(int i=0;i<n;i++){
        if(cnt[a[i]] > maxcnt){
            maxcnt = cnt[a[i]];
            num = a[i];
        }
    }

    queue<int> q;
    bool vis[MAXN] = {false};
    for(int i=0;i<n;i++){
        if(a[i] == num){
            vis[i] = true;
            q.push(i);
        }
    }

    printf("%lld\n", n-maxcnt);
    while(!q.empty()){
        int idx = q.front(); q.pop();
        if(idx-1>=0){
            if(a[idx-1] != num){
                printf("%d %d %d\n", a[idx-1]<num?1:2,idx ,idx+1);
                a[idx-1] = num;
                q.push(idx-1);
            }
        }

        if(idx+1 < n){
            if(a[idx+1] != num){
                printf("%d %d %d\n", a[idx+1]<num?1:2, idx+2, idx+1);
                a[idx+1] = num;
                q.push(idx+1);
            }
        }
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <string.h>
#include <stack>
#include <queue>
#include <unordered_map>
#include <string>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const long long MAXN = 202222;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9+9;
using namespace std;

ll n, a[MAXN];
int main(){
    scanf("%lld", &n);
    for(int i=0;i<n;i++) scanf("%lld", a+i);

    int L = 0, R = 0, conse = 1, maxlen=1;
    for(int i=1;i<n;i++){
        if(a[i] == a[i-1]){
            conse += 1;
        }
        else{
            if(conse > maxlen){
                maxlen = conse;
                L = i-maxlen;
                R = i-1;
            }
            conse = 1;
        }
    }
    if(conse > maxlen){
        R = n-1;
        L = n-conse;
    }
    //cout<<"L="<<L<<" R="<<R<<endl;
    printf("%lld\n", n-(R-L+1));
    for(int i=L-1; i>=0;i--)
        printf("%d %d %d\n", a[i]<a[i-1]?1:2, i+1, i+2);
    for(int i=R+1;i<n;i++)
        printf("%d %d %d\n", a[i]<a[i-1]?1:2, i, i+1);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{

    int n,a,i,j,st,fn,cnt=1,mx=-535,val,ans=0;
    vector<int>v,x,y,z;

    cin>>n;
    for(i=0; i<n; i++)
    {
        scanf("%d",&a);
        v.push_back(a);
        if(i>0)
        {
            if(v[i]==v[i-1])
                cnt++;

            else
            {
                if(cnt>mx)
                {
                    mx=cnt;
                    fn=i-1;
                    val=v[i-1];
                    cnt=1;
                }
            }
        }
    }
    //cout<<cnt<<" "<<mx<<endl;
    if(cnt>mx)
    {
        mx=cnt;
        val=v[n-1];
        fn=n-1;
    }
    st=(fn-mx)+1;

    //cout<<val<<" "<<st<<" "<<fn<<endl;

    if(st!=0)
    {
        for(i=st-1; i>=0; i--)
        {
            if(v[i]<val)
            {
                x.push_back(1);
                y.push_back(i+1);
                z.push_back(i+2);

                ans++;
            }
            if(v[i]>val)
            {
                x.push_back(2);
                y.push_back(i+1);
                z.push_back(i+2);

                ans++;
            }
        }
    }

    if(fn!=n-1)
    {
        for(i=fn+1;i<n;i++)
        {
            if(v[i]<val)
            {
                x.push_back(1);
                y.push_back(i+1);
                z.push_back(i);

                ans++;
            }
            if(v[i]>val)
            {
                x.push_back(2);
                y.push_back(i+1);
                z.push_back(i);

                ans++;
            }
        }
    }

    cout<<ans<<endl;

    for(i=0;i<ans;i++)
    {
        printf("%d %d %d\n",x[i],y[i],z[i]);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{

    int n,a,i,j,st,fn,cnt=1,mx=-535,val,ans=0;
    vector<int>v,x,y,z;

    cin>>n;
    for(i=0; i<n; i++)
    {
        scanf("%d",&a);
        v.push_back(a);
        if(i>0)
        {
            if(v[i]==v[i-1])
                cnt++;

            else
            {
                if(cnt>mx)
                {
                    mx=cnt;
                    fn=i;
                    val=v[i];
                    cnt=1;
                }
            }
        }
    }

    if(cnt>mx)
    {
        mx=cnt;
        val=v[n-1];
        fn=n-1;
    }
    st=(fn-mx)+1;

    //cout<<val<<" "<<st<<" "<<fn<<endl;

    if(st!=0)
    {
        for(i=st-1; i>=0; i--)
        {
            if(v[i]<val)
            {
                x.push_back(1);
                y.push_back(i+1);
                z.push_back(i+2);

                ans++;
            }
            if(v[i]>val)
            {
                x.push_back(0);
                y.push_back(i+1);
                z.push_back(i+2);

                ans++;
            }
        }
    }

    if(fn!=n-1)
    {
        for(i=fn+1;i<n;i++)
        {
            if(v[i]<val)
            {
                x.push_back(2);
                y.push_back(i);
                z.push_back(i+1);

                ans++;
            }
            if(v[i]>val)
            {
                x.push_back(2);
                y.push_back(i);
                z.push_back(i+1);

                ans++;
            }
        }
    }

    cout<<ans<<endl;

    for(i=0;i<ans;i++)
    {
        printf("%d %d %d\n",x[i],y[i],z[i]);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{

    int n,a,i,j,st,fn,cnt=1,mx=-535,val,ans=0;
    vector<int>v,x,y,z;

    cin>>n;
    for(i=0; i<n; i++)
    {
        scanf("%d",&a);
        v.push_back(a);
        if(i>0)
        {
            if(v[i]==v[i-1])
                cnt++;

            else
            {
                if(cnt>mx)
                {
                    mx=cnt;
                    fn=i;
                    val=v[i];
                    cnt=1;
                }
            }
        }
    }

    if(cnt>mx)
    {
        mx=cnt;
        val=v[n-1];
        fn=n-1;
    }
    st=(fn-mx)+1;

    //cout<<val<<" "<<st<<" "<<fn<<endl;

    if(st!=0)
    {
        for(i=st-1; i>=0; i--)
        {
            if(v[i]<val)
            {
                x.push_back(1);
                y.push_back(i+1);
                z.push_back(i+2);

                ans++;
            }
            if(v[i]>val)
            {
                x.push_back(2);
                y.push_back(i+1);
                z.push_back(i+2);

                ans++;
            }
        }
    }

    if(fn!=n-1)
    {
        for(i=fn+1;i<n;i++)
        {
            if(v[i]<val)
            {
                x.push_back(1);
                y.push_back(i);
                z.push_back(i+1);

                ans++;
            }
            if(v[i]>val)
            {
                x.push_back(2);
                y.push_back(i);
                z.push_back(i+1);

                ans++;
            }
        }
    }

    cout<<ans<<endl;

    for(i=0;i<ans;i++)
    {
        printf("%d %d %d\n",x[i],y[i],z[i]);
    }

    return 0;
}


//equalize them all-correct solution

#include<bits/stdc++.h>
using namespace std;
int main()
{

    int n,a,i,j,k,mx=-535,val,pos,ans=0;
    vector<int>v,x,y,z;
    map<int,int>mp;

    cin>>n;

    for(i=0;i<n;i++)
    {
        scanf("%d",&a);
        v.push_back(a);
        mp[a]++;
        if(mp[a]>mx)
        {
            mx=mp[a];
            val=a;
            pos=i;

        }
    }
    cout<<val<<" "<<pos<<endl;
    if(pos!=0)
    {
        for(i=pos-1;i>=0;i--)
        {
            if(v[i]<val)
            {
                x.push_back(1);
                y.push_back(i+1);
                z.push_back(i+2);
                ans++;
            }

            if(v[i]>val)
            {
                x.push_back(2);
                y.push_back(i+1);
                z.push_back(i+2);
                ans++;
            }

        }
    }

    if(pos!=n-1)
    {
        for(i=pos+1;i<=n-1;i++)
        {
            if(v[i]<val)
            {
                x.push_back(1);
                y.push_back(i+1);
                z.push_back(i);
                ans++;
            }
            if(v[i]>val)
            {
                x.push_back(2);
                y.push_back(i+1);
                z.push_back(i);
                ans++;
            }
        }
    }

    cout<<ans<<endl;

    for(i=0;i<ans;i++)
    {
        printf("%d %d %d\n",x[i],y[i],z[i]);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{

    int n,a,i,j,st,fn,cnt=1,mx=-535,val,ans=0;
    vector<int>v,x,y,z;

    cin>>n;
    for(i=0; i<n; i++)
    {
        scanf("%d",&a);
        v.push_back(a);
        if(i>0)
        {
            if(v[i]==v[i-1])
                cnt++;

            else
            {
                if(cnt>mx)
                {
                    mx=cnt;
                    fn=i-1;
                    val=v[i-1];
                    cnt=1;
                }
            }
        }
    }
    //cout<<cnt<<" "<<mx<<endl;
    if(cnt>mx)
    {
        mx=cnt;
        val=v[n-1];
        fn=n-1;
    }
    st=(fn-mx)+1;

    //cout<<val<<" "<<st<<" "<<fn<<endl;

    if(st!=0)
    {
        for(i=st-1; i>=0; i--)
        {
            if(v[i]<val)
            {
                x.push_back(1);
                y.push_back(i+1);
                z.push_back(i+2);

                ans++;
            }
            if(v[i]>val)
            {
                x.push_back(2);
                y.push_back(i+2);
                z.push_back(i+1);

                ans++;
            }
        }
    }

    if(fn!=n-1)
    {
        for(i=fn+1;i<n;i++)
        {
            if(v[i]<val)
            {
                x.push_back(1);
                y.push_back(i);
                z.push_back(i+1);

                ans++;
            }
            if(v[i]>val)
            {
                x.push_back(2);
                y.push_back(i+1);
                z.push_back(i);

                ans++;
            }
        }
    }

    cout<<ans<<endl;

    for(i=0;i<ans;i++)
    {
        printf("%d %d %d\n",x[i],y[i],z[i]);
    }

    return 0;
}


//equalize them all-correct solution

#include<bits/stdc++.h>
using namespace std;
int main()
{

    int n,a,i,j,k,mx=-535,val,pos,ans=0;
    vector<int>v,x,y,z;
    map<int,int>mp;

    cin>>n;

    for(i=0;i<n;i++)
    {
        scanf("%d",&a);
        v.push_back(a);
        mp[a]++;
        if(mp[a]>mx)
        {
            mx=mp[a];
            val=a;
            pos=i;

        }
    }
    //cout<<val<<" "<<pos<<endl;
    if(pos!=0)
    {
        for(i=pos-1;i>=0;i--)
        {
            if(v[i]<val)
            {
                x.push_back(1);
                y.push_back(i+1);
                z.push_back(i+2);
                ans++;
            }

            if(v[i]>val)
            {
                x.push_back(2);
                y.push_back(i+1);
                z.push_back(i+2);
                ans++;
            }

        }
    }

    if(pos!=n-1)
    {
        for(i=pos+1;i<=n-1;i++)
        {
            if(v[i]<val)
            {
                x.push_back(1);
                y.push_back(i+1);
                z.push_back(i);
                ans++;
            }
            if(v[i]>val)
            {
                x.push_back(2);
                y.push_back(i+1);
                z.push_back(i);
                ans++;
            }
        }
    }

    cout<<ans<<endl;

    for(i=0;i<ans;i++)
    {
        printf("%d %d %d\n",x[i],y[i],z[i]);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{

    int n,a,i,j,k,mx=-535,val,pos,ans=0;
    vector<int>v,x,y,z;
    map<int,int>mp;

    cin>>n;

    for(i=0;i<n;i++)
    {
        scanf("%d",&a);
        v.push_back(a);
        mp[a]++;
        if(mp[a]>mx)
        {
            mx=mp[a];
            val=a;
            pos=i;

        }
    }
    //cout<<val<<" "<<pos<<endl;
    if(pos!=0)
    {
        for(i=pos-1;i>=0;i--)
        {
            if(v[i]<val)
            {
                x.push_back(1);
                y.push_back(i+1);
                z.push_back(i+2);
                ans++;
            }

            if(v[i]>val)
            {
                x.push_back(2);
                y.push_back(i+1);
                z.push_back(i+2);
                ans++;
            }

        }
    }

    if(pos!=n-1)
    {
        for(i=pos+1;i<=n-1;i++)
        {
            if(v[i]<val)
            {
                x.push_back(1);
                y.push_back(i+1);
                z.push_back(i);
                ans++;
            }
            if(v[i]>val)
            {
                x.push_back(2);
                y.push_back(i+1);
                z.push_back(i);
                ans++;
            }
        }
    }

    cout<<ans<<endl;

    for(i=0;i<ans;i++)
    {
        printf("%d %d %d\n",x[i],y[i],z[i]);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{

    int n,a,i,j,st,fn,cnt=1,mx=-535,val,ans=0;
    vector<int>v,x,y,z;

    cin>>n;
    for(i=0; i<n; i++)
    {
        scanf("%d",&a);
        v.push_back(a);
        if(i>0)
        {
            if(v[i]==v[i-1])
                cnt++;

            else
            {
                if(cnt>mx)
                {
                    mx=cnt;
                    fn=i;
                    val=v[i];
                    cnt=1;
                }
            }
        }
    }

    if(cnt>mx)
    {
        mx=cnt;
        val=v[n-1];
        fn=n-1;
    }
    st=(fn-mx)+1;

    //cout<<val<<" "<<st<<" "<<fn<<endl;

    if(st!=0)
    {
        for(i=0; i<st; i++)
        {
            if(v[i]<val)
            {
                x.push_back(1);
                y.push_back(i+1);
                z.push_back(i+2);

                ans++;
            }
            if(v[i]>val)
            {
                x.push_back(0);
                y.push_back(i+1);
                z.push_back(i+2);

                ans++;
            }
        }
    }

    if(fn!=n-1)
    {
        for(i=fn+1;i<n;i++)
        {
            if(v[i]<val)
            {
                x.push_back(2);
                y.push_back(i);
                z.push_back(i+1);

                ans++;
            }
            if(v[i]>val)
            {
                x.push_back(2);
                y.push_back(i);
                z.push_back(i+1);

                ans++;
            }
        }
    }

    cout<<ans<<endl;

    for(i=0;i<ans;i++)
    {
        printf("%d %d %d\n",x[i],y[i],z[i]);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{

    int n,a,i,j,st,fn,cnt=1,mx=-535,val,ans=0;
    vector<int>v,x,y,z;

    cin>>n;
    for(i=0; i<n; i++)
    {
        scanf("%d",&a);
        v.push_back(a);
        if(i>0)
        {
            if(v[i]==v[i-1])
                cnt++;

            else
            {
                if(cnt>mx)
                {
                    mx=cnt;
                    fn=i;
                    val=v[i];
                    cnt=1;
                }
            }
        }
    }

    if(cnt>mx)
    {
        mx=cnt;
        val=v[n-1];
        fn=n-1;
    }
    st=(fn-mx)+1;

    //cout<<val<<" "<<st<<" "<<fn<<endl;

    if(st!=0)
    {
        for(i=st-1; i>=0; i--)
        {
            if(v[i]<val)
            {
                x.push_back(1);
                y.push_back(i+1);
                z.push_back(i+2);

                ans++;
            }
            if(v[i]>val)
            {
                x.push_back(2);
                y.push_back(i+2);
                z.push_back(i+1);

                ans++;
            }
        }
    }

    if(fn!=n-1)
    {
        for(i=fn+1;i<n;i++)
        {
            if(v[i]<val)
            {
                x.push_back(1);
                y.push_back(i);
                z.push_back(i+1);

                ans++;
            }
            if(v[i]>val)
            {
                x.push_back(2);
                y.push_back(i+1);
                z.push_back(i);

                ans++;
            }
        }
    }

    cout<<ans<<endl;

    for(i=0;i<ans;i++)
    {
        printf("%d %d %d\n",x[i],y[i],z[i]);
    }

    return 0;
}


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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long  int n,a[200005],i,hash[200005]={0},max=-1,ultimatemax,count=0,j=1;
  cin>>n;
  for(i=1;i<=n;i++)
  {
      cin>>a[i];
      hash[a[i]]++;
      if(hash[a[i]]>max)
      {
          max=hash[a[i]];
          ultimatemax=a[i];
      }
  }
  //cout<<max<<endl;
  cout<<n-max<<endl;
  while(a[j]!=ultimatemax)
  j++;
  for(i=j-1;i>=1;i--)
  {
      if(a[i]<ultimatemax)
      cout<<"1"<<" "<<i<<" "<<i+1<<endl;
      else
      cout<<"2"<<" "<<i<<" "<<i+1<<endl;
    a[i]=ultimatemax;
  }
  for(i=j;i<=n-1;i++)
  {
      if(a[i]==ultimatemax&&a[i+1]!=ultimatemax)
        {
if (a[i+1] < ultimatemax)
          cout << "1"<< " " << i+ 1<< " " << i << endl;
        else
          cout << "2"<< " " << i +1<< " " << i << endl;
        a[i+1] = ultimatemax;
        }
  }
return 0;
}


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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
   int n,a[200005],i,hash[200005],max=-1,ultimatemax,count=0;
  cin>>n;
  for(i=1;i<=n;i++)
  {
      cin>>a[i];
      hash[a[i]]++;
      if(hash[a[i]]>max)
      {
          max=hash[a[i]];
          ultimatemax=a[i];
      }
  }
  cout<<n-max<<endl;
for(i=1;i<=n;i++)
{
    if(a[i]==ultimatemax)
    continue;
   if(i-1>=1&&a[i-1]==ultimatemax)
   {
       if(a[i]<ultimatemax)

         cout<<"1"<<" "<<i<<" "<<i-1<<endl;
       else if(a[i]>ultimatemax)
        cout<<"2"<<" "<<i<<" "<<i-1<<endl;
       a[i]=ultimatemax;
       count++;
   }
  else if(i+1<=n&&a[i+1]==ultimatemax)
   {
       if(a[i]<ultimatemax)
    cout<<"1"<<" "<<i<<" "<<i+1<<endl;
          else if (a[i] > ultimatemax)
           cout<<"2"<<" "<<i<<" "<<i+1<<endl;
            a[i]=ultimatemax;
            count++;
   }
}
for(i=1;i<=n;i++)
{
    if(a[i]==ultimatemax)
    continue;
   if(i-1>=1&&a[i-1]==ultimatemax)
   {
       if(a[i]<ultimatemax)

         cout<<"1"<<" "<<i<<" "<<i-1<<endl;
       else if(a[i]>ultimatemax)
        cout<<"2"<<" "<<i<<" "<<i-1<<endl;
       a[i]=ultimatemax;
       count++;
   }
  else if(i+1<=n&&a[i+1]==ultimatemax)
   {
       if(a[i]<ultimatemax)
    cout<<"1"<<" "<<i<<" "<<i+1<<endl;
          else if (a[i] > ultimatemax)
           cout<<"2"<<" "<<i<<" "<<i+1<<endl;
            a[i]=ultimatemax;
            count++;
   }
}

return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n,a[200001],i,hash[200001],f=1,visited[200001]={0},max=-1,ultimatemax,count=0;
  map<pair<long long int,long long int>,long long int>m;
  map<pair<long long int,long long int>,long long int>::iterator it;
  cin>>n;
  for(i=1;i<=n;i++)
  {
      cin>>a[i];
      hash[a[i]]++;
      if(hash[a[i]]>max)
      {
          max=hash[a[i]];
          ultimatemax=a[i];
      }
  }
for(i=1;i<=n;i++)
{
    if(a[i]==ultimatemax)
    continue;
   if(i-1>=1&&a[i-1]==ultimatemax)
   {
       if(a[i]<ultimatemax)
            m[{1,i}]=i-1;
       else if(a[i]>ultimatemax)
       m[{2,i}]=i-1;
       a[i]=ultimatemax;
       count++;
   }
  else if(i+1<=n&&a[i+1]==ultimatemax)
   {
       if(a[i]<ultimatemax)
          m[{1,i}]=i+1;
          else if (a[i] > ultimatemax)
            m[{2, i}] = i+1;
            a[i]=ultimatemax;
            count++;
   }
}
for (i = 1; i <= n; i++) {
  if (a[i] == ultimatemax)
    continue;
  if (i - 1 >= 1 && a[i - 1] == ultimatemax) {
    if (a[i] < ultimatemax)
      m[{1, i}] = i - 1;
    else if (a[i] > ultimatemax)
      m[{2, i}] = i - 1;
    a[i] = ultimatemax;
    count++;
  } else if (i + 1 <= n && a[i + 1] == ultimatemax) {
    if (a[i] < ultimatemax)
      m[{1, i}] = i + 1;
    else if (a[i] > ultimatemax)
      m[{2, i}] = i + 1;
    a[i] = ultimatemax;
    count++;
  }
}
cout<<count<<endl;
for(it=m.begin();it!=m.end();it++)
{
cout<<(it->first).first<<" "<<(it->first).second<<" "<<it->second<<endl;
}

return 0;
}


#include <bits/stdc++.h>
using namespace std;
struct triad
{
    long long int f,s,t;
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n,a[200001],i,hash[200001],f=1,visited[200001]={0},max=-1,ultimatemax,count=0;
  vector<triad>v;
  cin>>n;
  for(i=1;i<=n;i++)
  {
      cin>>a[i];
      hash[a[i]]++;
      if(hash[a[i]]>max)
      {
          max=hash[a[i]];
          ultimatemax=a[i];
      }
  }
for(i=1;i<=n;i++)
{
    if(a[i]==ultimatemax)
    continue;
   if(i-1>=1&&a[i-1]==ultimatemax)
   {
       if(a[i]<ultimatemax)

        v.push_back({(long long int)1,i,i-1});
       else if(a[i]>ultimatemax)
       v.push_back({(long long int)2,i,i-1});
       a[i]=ultimatemax;
       count++;
   }
  else if(i+1<=n&&a[i+1]==ultimatemax)
   {
       if(a[i]<ultimatemax)
       v.push_back({(long long int)1,i,i+1});
          else if (a[i] > ultimatemax)
            v.push_back({(long long int)2,i,i+1});
            a[i]=ultimatemax;
            count++;
   }
}
for (i = 1; i <= n; i++) {
  if (a[i] == ultimatemax)
    continue;
  if (i - 1 >= 1 && a[i - 1] == ultimatemax) {
    if (a[i] < ultimatemax)

      v.push_back({(long long int)1, i, i - 1});
    else if (a[i] > ultimatemax)
      v.push_back({(long long int)2, i, i - 1});
    a[i] = ultimatemax;
    count++;
  } else if (i + 1 <= n && a[i + 1] == ultimatemax) {
    if (a[i] < ultimatemax)
      v.push_back({(long long int)1, i, i + 1});
    else if (a[i] > ultimatemax)
      v.push_back({(long long int)2, i, i + 1});
    a[i] = ultimatemax;
    count++;
  }
}
cout<<count<<endl;
for(i=0;i<v.size();i++)
{
cout<<v[i].f<<" "<<v[i].s<<" "<<v[i].t<<endl;
}

return 0;
}


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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long  int n,a[200005],i,hash[200005],max=-1,ultimatemax,count=0,j=1;
  cin>>n;
  for(i=1;i<=n;i++)
  {
      cin>>a[i];
      hash[a[i]]++;
      if(hash[a[i]]>max)
      {
          max=hash[a[i]];
          ultimatemax=a[i];
      }
  }
  cout<<n-max<<endl;
  while(a[j]!=ultimatemax)
  j++;
  for(i=j-1;i>=1;i--)
  {
      if(a[i]<ultimatemax)
      cout<<"1"<<" "<<i<<" "<<i+1<<endl;
      else
      cout<<"2"<<" "<<i<<" "<<i+1<<endl;
    a[i]=ultimatemax;
  }
  for(i=j;i<=n-1;i++)
  {
      if(a[i]==ultimatemax&&a[i+1]!=ultimatemax)
        {
if (a[i+1] < ultimatemax)
          cout << "1"<< " " << i+ 1<< " " << i << endl;
        else
          cout << "2"<< " " << i +1<< " " << i << endl;
        a[i+1] = ultimatemax;
        }
  }
return 0;
}


#include <bits/stdc++.h>
using namespace std;
struct triad
{
    long long int f,s,t;
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n,a[200001],i,hash[200001],f=1,visited[200001]={0},max=-1,ultimatemax,count=0;
  vector<triad>v;
  cin>>n;
  for(i=1;i<=n;i++)
  {
      cin>>a[i];
      hash[a[i]]++;
      if(hash[a[i]]>max)
      {
          max=hash[a[i]];
          ultimatemax=a[i];
      }
  }
  cout<<n-max<<endl;
for(i=1;i<=n;i++)
{
    if(a[i]==ultimatemax)
    continue;
   if(i-1>=1&&a[i-1]==ultimatemax)
   {
       if(a[i]<ultimatemax)

         cout<<"1"<<" "<<i<<" "<<i-1<<endl;
       else if(a[i]>ultimatemax)
        cout<<"2"<<" "<<i<<" "<<i-1<<endl;
       a[i]=ultimatemax;
       count++;
   }
  else if(i+1<=n&&a[i+1]==ultimatemax)
   {
       if(a[i]<ultimatemax)
    cout<<"1"<<" "<<i<<" "<<i+1<<endl;
          else if (a[i] > ultimatemax)
           cout<<"2"<<" "<<i<<" "<<i+1<<endl;
            a[i]=ultimatemax;
            count++;
   }
}
for(i=1;i<=n;i++)
{
    if(a[i]==ultimatemax)
    continue;
   if(i-1>=1&&a[i-1]==ultimatemax)
   {
       if(a[i]<ultimatemax)

         cout<<"1"<<" "<<i<<" "<<i-1<<endl;
       else if(a[i]>ultimatemax)
        cout<<"2"<<" "<<i<<" "<<i-1<<endl;
       a[i]=ultimatemax;
       count++;
   }
  else if(i+1<=n&&a[i+1]==ultimatemax)
   {
       if(a[i]<ultimatemax)
    cout<<"1"<<" "<<i<<" "<<i+1<<endl;
          else if (a[i] > ultimatemax)
           cout<<"2"<<" "<<i<<" "<<i+1<<endl;
            a[i]=ultimatemax;
            count++;
   }
}

return 0;
}


#include <bits/stdc++.h>
using namespace std;
struct triad
{
    long long int f,s,t;
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n,a[200001],i,hash[200001]={0},max=-1,ultimatemax,count=0;
  vector<triad>v;
  cin>>n;
  for(i=1;i<=n;i++)
  {
      cin>>a[i];
      hash[a[i]]++;
      if(hash[a[i]]>max)
      {
          max=hash[a[i]];
          ultimatemax=a[i];
      }
  }
for(i=1;i<=n;i++)
{
    if(a[i]==ultimatemax)
    continue;
   if(i-1>=1&&a[i-1]==ultimatemax)
   {
       if(a[i]<ultimatemax)

        v.push_back({(long long int)1,i,i-1});
       else if(a[i]>ultimatemax)
       v.push_back({(long long int)2,i,i-1});
       a[i]=ultimatemax;
       count++;
   }
  else if(i+1<=n&&a[i+1]==ultimatemax)
   {
       if(a[i]<ultimatemax)
       v.push_back({(long long int)1,i,i+1});
          else if (a[i] > ultimatemax)
            v.push_back({(long long int)2,i,i+1});
            a[i]=ultimatemax;
            count++;
   }
}
for (i = 1; i <= n; i++) {
  if (a[i] == ultimatemax)
    continue;
  if (i - 1 >= 1 && a[i - 1] == ultimatemax) {
    if (a[i] < ultimatemax)

      v.push_back({(long long int)1, i, i - 1});
    else if (a[i] > ultimatemax)
      v.push_back({(long long int)2, i, i - 1});
    a[i] = ultimatemax;
    count++;
  } else if (i + 1 <= n && a[i + 1] == ultimatemax) {
    if (a[i] < ultimatemax)
      v.push_back({(long long int)1, i, i + 1});
    else if (a[i] > ultimatemax)
      v.push_back({(long long int)2, i, i + 1});
    a[i] = ultimatemax;
    count++;
  }
}
cout<<count<<endl;
for(i=0;i<v.size();i++)
{
cout<<v[i].f<<" "<<v[i].s<<" "<<v[i].t<<endl;
}

return 0;
}


#include <bits/stdc++.h>
using namespace std;
struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n,a[200001],i,hash[200001],f=1,visited[200001]={0},max=-1,ultimatemax,count=0;
  unordered_map<pair<long long int,long long int>,long long int,hash_pair>m;
  unordered_map<pair<long long int,long long int>,long long int,hash_pair>::iterator it;
  cin>>n;
  for(i=1;i<=n;i++)
  {
      cin>>a[i];
      hash[a[i]]++;
      if(hash[a[i]]>max)
      {
          max=hash[a[i]];
          ultimatemax=a[i];
      }
  }
for(i=1;i<=n;i++)
{
    if(a[i]==ultimatemax)
    continue;
   if(i-1>=1&&a[i-1]==ultimatemax)
   {
       if(a[i]<ultimatemax)
            m[{1,i-1}]=i;
       else if(a[i]>ultimatemax)
       m[{2,i-1}]=i;
       a[i]=ultimatemax;
       count++;
   }
  else if(i+1<=n&&a[i+1]==ultimatemax)
   {
       if(a[i]<ultimatemax)
          m[{1,i}]=i+1;
          else if (a[i] > ultimatemax)
            m[{2, i}] = i+1;
            a[i]=ultimatemax;
            count++;
   }
}
for (i = 1; i <= n; i++) {
  if (a[i] == ultimatemax)
    continue;
  if (i - 1 >= 1 && a[i - 1] == ultimatemax) {
    if (a[i] < ultimatemax)
      m[{1, i-1}] = i;
    else if (a[i] > ultimatemax)
      m[{2, i-1}] = i;
    a[i] = ultimatemax;
    count++;
  } else if (i + 1 <= n && a[i + 1] == ultimatemax) {
    if (a[i] < ultimatemax)
      m[{1, i}] = i + 1;
    else if (a[i] > ultimatemax)
      m[{2, i}] = i + 1;
    a[i] = ultimatemax;
    count++;
  }
}
cout<<count<<endl;
for(it=m.begin();it!=m.end();it++)
{
cout<<(it->first).first<<" "<<(it->first).second<<" "<<it->second<<endl;
}

return 0;
}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define sll            set<ll int>
#define msll        multiset<ll int>
#define io    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

priority_queue<ll,vi,greater<ll> > pq;
ll cnt=0;
vector<pair<ll,ll> > vv;
int main()
{
    io
    ll i,j,n,a[200005];
    cin>>n;
    for(i=1;i<=n;i++){
        cin>>a[i];
    }
    ll maxx = *max_element(a+1,a+n+1);
    a[0]=a[n+1]=maxx;
    for(i=1;i<=n;i++){
        if(a[i]==maxx) pq.push(i);
    }
    while(!pq.empty()){
        ll tmp = pq.top();
        pq.pop();
        if(a[tmp-1]!=maxx){
            cnt++;
            vv.pb({tmp-1,tmp});
            pq.push(tmp-1);
            a[tmp-1]=maxx;
        }
        if(a[tmp+1]!=maxx){
            cnt++;
            vv.pb({tmp+1,tmp});
            pq.push(tmp+1);
            a[tmp+1]=maxx;
        }

    }
    cout<<cnt<<"\n";
    for(auto it:vv){
        cout<<"1 "<<it.F<<" "<<it.S<<"\n";
    }

    return 0;
}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define sll            set<ll int>
#define msll        multiset<ll int>
#define io    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

vi v1,v2;
map<ll,ll> fr;

int main()
{
    io
    ll i,j,n,a[200005];
    cin>>n;
    for(i=1;i<=n;i++){
        cin>>a[i];
        fr[a[i]]++;
    }
    ll mee=0,maxx;
    for(auto it:fr){
        if(it.S>mee){
            mee=it.S; maxx=it.F;
        }
    }
    ll c1=0,c2=0;
    a[0]=a[n+1]=maxx;
    for(i=0;i<=n+1;i++){
        if(a[i]==maxx) v1.pb(i);
    }
    for(i=0;i<v1.size()-1;i++){
        c1+=(v1[i+1]-v1[i]-1);
    }

    cout<<c1<<"\n";
        for(i=1;i<v1.size()-1;i++){
            for(j=v1[i];j+1<v1[i+1];j++){
                cout<<"1 "<<j+1<<" "<<j<<"\n";
            }
        }
        for(j=v1[1];j-1>v1[0];j--){
            cout<<"1 "<<j-1<<" "<<j<<"\n";
        }
        return 0;

    return 0;
}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define sll            set<ll int>
#define msll        multiset<ll int>
#define io    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

vi v1,v2;
map<ll,ll> fr;

int main()
{
    io
    ll i,j,n,a[200005];
    cin>>n;
    for(i=1;i<=n;i++){
        cin>>a[i];
        fr[a[i]]++;
    }
    ll mee=0,maxx;
    for(auto it:fr){
        if(it.S>mee){
            mee=it.S; maxx=it.F;
        }
    }
    ll c1=0,c2=0;
    a[0]=a[n+1]=maxx;
    for(i=0;i<=n+1;i++){
        if(a[i]==maxx) v1.pb(i);
    }
    for(i=0;i<v1.size()-1;i++){
        c1+=(v1[i+1]-v1[i]-1);
    }

    cout<<c1<<"\n";
        for(i=1;i<v1.size()-1;i++){
            for(j=v1[i];j+1<v1[i+1];j++){
                if(j+1<j) cout<<"1 "<<j+1<<" "<<j<<"\n";
                else cout<<"2 "<<j+1<<" "<<j<<"\n";
            }
        }
        for(j=v1[1];j-1>v1[0];j--){
            if(j-1<j) cout<<"1 "<<j-1<<" "<<j<<"\n";
            else cout<<"2 "<<j-1<<" "<<j<<"\n";
        }
        return 0;

    return 0;
}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define sll            set<ll int>
#define msll        multiset<ll int>
#define io    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

vi v1,v2;
map<ll,ll> fr;

int main()
{
    io
    ll i,j,n,a[200005];
    cin>>n;
    for(i=1;i<=n;i++){
        cin>>a[i];
        fr[a[i]]++;
    }
    ll mee=0,maxx;
    for(auto it:fr){
        if(it.S>mee){
            mee=it.S; maxx=it.F;
        }
    }
    ll c1=0,c2=0;
    a[0]=a[n+1]=maxx;
    for(i=0;i<=n+1;i++){
        if(a[i]==maxx) v1.pb(i);
    }
    for(i=0;i<v1.size()-1;i++){
        c1+=(v1[i+1]-v1[i]-1);
    }

    cout<<c1<<"\n";
        for(i=1;i<v1.size()-1;i++){
            for(j=v1[i];j+1<v1[i+1];j++){
                if(a[j+1]<maxx){
                    cout<<"1 "<<j+1<<" "<<j<<"\n";
                }
                else cout<<"2 "<<j+1<<" "<<j<<"\n";
            }
        }
        for(j=v1[1];j-1>v1[0];j--){
            if(a[j-1]<maxx) cout<<"1 "<<j-1<<" "<<j<<"\n";
            else cout<<"2 "<<j-1<<" "<<j<<"\n";
        }
        return 0;

    return 0;
}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define sll            set<ll int>
#define msll        multiset<ll int>
#define io    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

vi v1,v2;

int main()
{
    io
    ll i,j,n,a[200005];
    cin>>n;
    for(i=1;i<=n;i++){
        cin>>a[i];
    }
    ll minn= (*min_element(a+1,a+n+1));
    ll maxx= (*max_element(a+1,a+n+1));

    ll c1=0,c2=0;
    a[0]=a[n+1]=maxx;
    for(i=0;i<=n+1;i++){
        if(a[i]==maxx) v1.pb(i);
    }
    for(i=0;i<v1.size()-1;i++){
        c1+=(v1[i+1]-v1[i]-1);
    }

    a[0]=a[n+1]=minn;
    for(i=0;i<=n+1;i++){
        if(a[i]==minn) v2.pb(i);
    }
    for(i=0;i<v2.size()-1;i++){
        c2+=(v2[i+1]-v2[i]-1);
    }

    if(c1<=c2){
        cout<<c1<<"\n";
        for(i=1;i<v1.size()-1;i++){
            for(j=v1[i];j+1<v1[i+1];j++){
                cout<<"1 "<<j+1<<" "<<j<<"\n";
            }
        }
        for(j=v1[1];j-1>v1[0];j--){
            cout<<"1 "<<j-1<<" "<<j<<"\n";
        }
        return 0;
    }
    else{
        cout<<c2<<"\n";
        for(i=1;i<v2.size()-1;i++){
            for(j=v2[i];j+1<v2[i+1];j++){
                cout<<"2 "<<j+1<<" "<<j<<"\n";
            }
        }
        for(j=v2[1];j-1>v2[0];j--){
            cout<<"2 "<<j-1<<" "<<j<<"\n";
        }
        return 0;

    }

    return 0;
}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define sll            set<ll int>
#define msll        multiset<ll int>
#define io    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

priority_queue<ll,vi,greater<ll> > pq;
priority_queue<ll> pq2;
ll cnt=0,cnt2=0;
vector<pair<ll,ll> > vv,vv2;
ll a[200005],b[200005];
int main()
{
    io
    ll i,j,n;
    cin>>n;
    for(i=1;i<=n;i++){
        cin>>a[i];
        b[i]=a[i];
    }
    ll maxx = *max_element(a+1,a+n+1);
    ll minn = *min_element(a+1,a+n+1);

    a[0]=a[n+1]=maxx;
    for(i=1;i<=n;i++){
        if(a[i]==maxx) pq.push(i);
    }
    while(!pq.empty()){
        ll tmp = pq.top();
        pq.pop();
        if(a[tmp-1]!=maxx){
            cnt++;
            vv.pb({tmp-1,tmp});
            pq.push(tmp-1);
            a[tmp-1]=maxx;
        }
        if(a[tmp+1]!=maxx){
            cnt++;
            vv.pb({tmp+1,tmp});
            pq.push(tmp+1);
            a[tmp+1]=maxx;
        }

    }
    // cout<<cnt<<"\n";
    for(i=1;i<=n;i++){
        a[i]=b[i];
    }

    // other half
    a[0]=a[n+1]=minn;
    for(i=1;i<=n;i++){
        if(a[i]==minn) pq2.push(i);
    }
    while(!pq2.empty()){
        ll tmp = pq2.top();
        pq2.pop();
        if(a[tmp-1]!=minn){
            cnt2++;
            vv2.pb({tmp-1,tmp});
            pq2.push(tmp-1);
            a[tmp-1]=minn;
        }
        if(a[tmp+1]!=minn){
            cnt2++;
            vv2.pb({tmp+1,tmp});
            pq2.push(tmp+1);
            a[tmp+1]=minn;
        }

    }

    if(cnt<=cnt2){
        cout<<cnt<<"\n";
        for(auto it:vv){
            cout<<"1 "<<it.F<<" "<<it.S<<"\n";
        }
        return 0;
    }
    else{
        cout<<cnt2<<"\n";
        for(auto it:vv2){
            cout<<"2 "<<it.F<<" "<<it.S<<"\n";
        }
        return 0;
    }

    return 0;
}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define sll            set<ll int>
#define msll        multiset<ll int>
#define io    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

priority_queue<ll,vi,greater<ll> > pq,pq2;
// priority_queue<ll> pq2;
ll cnt=0,cnt2=0;
vector<pair<ll,ll> > vv,vv2;
ll a[200005],b[200005];
int main()
{
    io
    ll i,j,n;
    cin>>n;
    for(i=1;i<=n;i++){
        cin>>a[i];
        b[i]=a[i];
    }
    ll maxx = *max_element(a+1,a+n+1);
    ll minn = *min_element(a+1,a+n+1);

    a[0]=a[n+1]=maxx;
    for(i=1;i<=n;i++){
        if(a[i]==maxx) pq.push(i);
    }
    while(!pq.empty()){
        ll tmp = pq.top();
        pq.pop();
        if(a[tmp-1]!=maxx){
            cnt++;
            vv.pb({tmp-1,tmp});
            pq.push(tmp-1);
            a[tmp-1]=maxx;
        }
        if(a[tmp+1]!=maxx){
            cnt++;
            vv.pb({tmp+1,tmp});
            pq.push(tmp+1);
            a[tmp+1]=maxx;
        }

    }
    // cout<<cnt<<"\n";
    for(i=1;i<=n;i++){
        a[i]=b[i];
    }

    // other half
    a[0]=a[n+1]=minn;
    for(i=1;i<=n;i++){
        if(a[i]==minn) pq2.push(i);
    }
    while(!pq2.empty()){
        ll tmp = pq2.top();
        pq2.pop();
        if(a[tmp-1]!=minn){
            cnt2++;
            vv2.pb({tmp-1,tmp});
            pq2.push(tmp-1);
            a[tmp-1]=minn;
        }
        if(a[tmp+1]!=minn){
            cnt2++;
            vv2.pb({tmp+1,tmp});
            pq2.push(tmp+1);
            a[tmp+1]=minn;
        }

    }

    if(cnt<=cnt2){
        cout<<cnt<<"\n";
        for(auto it:vv){
            cout<<"1 "<<it.F<<" "<<it.S<<"\n";
        }
        return 0;
    }
    else{
        cout<<cnt2<<"\n";
        for(auto it:vv2){
            cout<<"2 "<<it.F<<" "<<it.S<<"\n";
        }
        return 0;
    }

    return 0;
}


#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define sll            set<ll int>
#define msll        multiset<ll int>
#define io    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

priority_queue<ll,vi,greater<ll> > pq;
priority_queue<ll> pq2;
ll cnt=0,cnt2=0;
vector<pair<ll,ll> > vv,vv2;
ll a[200005],b[200005];
int main()
{
    io
    ll i,j,n;
    cin>>n;
    for(i=1;i<=n;i++){
        cin>>a[i];
        b[i]=a[i];
    }
    ll maxx = *max_element(a+1,a+n+1);
    ll minn = *min_element(a+1,a+n+1);

    a[0]=a[n+1]=maxx;
    for(i=1;i<=n;i++){
        if(a[i]==maxx) pq.push(i);
    }
    while(!pq.empty()){
        ll tmp = pq.top();
        pq.pop();
        if(a[tmp-1]!=maxx){
            cnt++;
            vv.pb({tmp-1,tmp});
            pq.push(tmp-1);
            a[tmp-1]=maxx;
        }
        if(a[tmp+1]!=maxx){
            cnt++;
            vv.pb({tmp+1,tmp});
            pq.push(tmp+1);
            a[tmp+1]=maxx;
        }

    }
    // cout<<cnt<<"\n";
    for(i=1;i<=n;i++){
        a[i]=b[i];
    }

    // other half
    a[0]=a[n+1]=minn;
    for(i=1;i<=n;i++){
        if(a[i]==minn) pq2.push(i);
    }
    while(!pq2.empty()){
        ll tmp = pq2.top();
        pq2.pop();
        if(a[tmp-1]!=minn){
            cnt2++;
            vv2.pb({tmp-1,tmp});
            pq2.push(tmp-1);
            a[tmp-1]=minn;
        }
        if(a[tmp+1]!=minn){
            cnt2++;
            vv2.pb({tmp+1,tmp});
            pq2.push(tmp+1);
            a[tmp+1]=maxx;
        }

    }

    if(cnt<=cnt2){
        cout<<cnt<<"\n";
        for(auto it:vv){
            cout<<"1 "<<it.F<<" "<<it.S<<"\n";
        }
        return 0;
    }
    else{
        cout<<cnt2<<"\n";
        for(auto it:vv2){
            cout<<"2 "<<it.F<<" "<<it.S<<"\n";
        }
        return 0;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define debug1(x) cout<<#x<<" "<<x<<endl;
#define debug2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
#define pii pair<ll,ll>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define mp make_pair
#define ll long long int
#define Q queue<pair<int,int>>
struct data{
  int op,p,q;

};

int main()
{
int n;
cin>>n;
int i;
int a[500005];
int b[500005];
vector <int> hash(500005,0);
for(i=0;i<200000;i++)
	hash[i]=0;
for(int i=0;i<n;i++)
{
	cin>>a[i];
	//b[a[i]]=i;
	hash[a[i]]++;
}
int x;
int max=0;
int pos;
for(i=0;i<n;i++)
{
	if(max<hash[a[i]])
	{
		max=hash[a[i]];
		pos=i;
		x=a[i];
	}
}
  printf("%d\n",n-hash[x]);
  for(i=pos+1;i<n;i++)
  {
    //debug1(i);
    //cout<<endl;
    if(x>a[i])
    {
      printf("1 %d %d\n",i+1,i);
    }
    else if(x < a[i])
    {
      printf("2 %d %d\n",i+1,i);
    }
  }
  for(i=pos-1;i>=0;i--)
  {
    if(x>a[i])
    {
      printf("1 %d %d\n",i+1,i+2);
    }
    else if(x < a[i])
    {
      printf("1 %d %d\n",i+1,i+2);
    }
  }
  //cout<<v1.size()<<endl;
  //for(i=0;i<v1.size();i++)
    //printf("%d %d %d\n",v1[i].op,v1[i].p+1,v1[i].q+1);

}


#include<bits/stdc++.h>
using namespace std;
#define debug1(x) cout<<#x<<" "<<x<<endl;
#define debug2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
#define pii pair<ll,ll>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define mp make_pair
#define ll long long int
#define Q queue<pair<int,int>>
struct data{
  int op,p,q;

};

int main()
{
  int n;
  cin>>n;
  int i;
 int a[200005];
  int b[200005];
  for(int i=0;i<n;i++)
    {
        cin>>a[i];
        b[a[i]]=i;
    }
  int hash[200005];
  for(i=0;i<200005;i++)
    hash[i]=0;
  for(int i=0;i<n;i++)
  {
    hash[a[i]]++;
  }
  int max=0,j=0;
  for(i=0;i<200004;i++)
    if(hash[i]>max)
      j=i;
  int p=b[i];
  int x=j;
  vector <struct data> v1;
  //debug2(p,x);
  for(i=p-1;i>=0;i--)
  {
    if(x>a[i])
    {
      v1.push_back({1,i,i+1});
    }
    else if(x < a[i])
    {
      v1.push_back({2,i,i+1});
    }
  }
  for(i=p;i<n;i++)
  {
    if(x>a[i])
    {
      v1.push_back({1,i,i+1});
    }
    else if(x < a[i])
    {
      v1.push_back({2,i,i+1});
    }
  }

  for(i=0;i<v1.size();i++)
    printf("%d %d %d\n",v1[i].op,v1[i].p+1,v1[i].q+1);

}


#include<bits/stdc++.h>
using namespace std;
#define debug1(x) cout<<#x<<" "<<x<<endl;
#define debug2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
#define pii pair<ll,ll>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define mp make_pair
#define ll long long int
#define Q queue<pair<int,int>>
struct data{
  int op,p,q;

};

int main()
{
int n;
cin>>n;
int i;
int a[200005];
int b[200005];
int hash[200005];
for(i=0;i<200005;i++)
	hash[i]=0;
for(int i=0;i<n;i++)
{
	cin>>a[i];
	hash[a[i]]++;
}
int max=0;
int pos;
for(i=0;i<n;i++)
{
	if(max<hash[a[i]])
	{
		max=a[i];
		pos=i;
	}
}
  vector <struct data> v1;
  int x=max;

  //debug3(pos,x,n);
  printf("%d\n",n-hash[max]);
  for(i=pos+1;i<n;i++)
  {
    //debug1(i);
    //cout<<endl;
    if(x>a[i])
    {
      printf("1 %d %d\n",i+1,i);
    }
    else if(x < a[i])
    {
      printf("2 %d %d\n",i+1,i);
    }
  }
  for(i=pos-1;i>=0;i--)
  {
    if(x>a[i])
    {
      printf("1 %d %d\n",i+1,i+2);
    }
    else if(x < a[i])
    {
      printf("2 %d %d\n",i+1,i+2);
    }
  }
  //cout<<v1.size()<<endl;
  //for(i=0;i<v1.size();i++)
    //printf("%d %d %d\n",v1[i].op,v1[i].p+1,v1[i].q+1);

}


#include<bits/stdc++.h>
using namespace std;
#define debug1(x) cout<<#x<<" "<<x<<endl;
#define debug2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
#define pii pair<ll,ll>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define mp make_pair
#define ll long long int
#define Q queue<pair<int,int>>
struct data{
  int op,p,q;

};

int main()
{
int n;
cin>>n;
int i;
int a[500005];
int b[500005];
vector <int> hash(500005,0);
for(i=0;i<500005;i++)
	hash[i]=0;
for(int i=0;i<n;i++)
{
	cin>>a[i];
	hash[a[i]]++;
}
int max=0;
int pos;
for(i=0;i<n;i++)
{
	if(max<hash[a[i]])
	{
		max=a[i];
		pos=i;
	}
}
  vector <struct data> v1;
  int x=max;

  //debug3(pos,x,n);
  printf("%d\n",n-hash[max]);
  for(i=pos+1;i<n;i++)
  {
    //debug1(i);
    //cout<<endl;
    if(x>a[i])
    {
      printf("1 %d %d\n",i+1,i);
    }
    else if(x < a[i])
    {
      printf("2 %d %d\n",i+1,i);
    }
  }
  for(i=pos-1;i>=0;i--)
  {
    if(x>a[i])
    {
      printf("1 %d %d\n",i+1,i+2);
    }
    else if(x < a[i])
    {
      printf("1 %d %d\n",i+1,i+2);
    }
  }
  //cout<<v1.size()<<endl;
  //for(i=0;i<v1.size();i++)
    //printf("%d %d %d\n",v1[i].op,v1[i].p+1,v1[i].q+1);

}


#include<bits/stdc++.h>
using namespace std;
#define debug1(x) cout<<#x<<" "<<x<<endl;
#define debug2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
#define pii pair<ll,ll>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define mp make_pair
#define ll long long int
#define Q queue<pair<int,int>>
struct data{
  int op,p,q;

};

int main()
{
int n;
cin>>n;
int i;
int a[200005];
int b[200005];
int hash[200005];
for(i=0;i<200005;i++)
	hash[i]=0;
for(int i=0;i<n;i++)
{
	cin>>a[i];
	hash[a[i]]++;
}
int max=0;
int pos;
for(i=0;i<n;i++)
{
	if(max<hash[a[i]])
	{
		max=a[i];
		pos=i;
	}
}
  vector <struct data> v1;
  int x=max;

  //debug3(pos,x,n);
  printf("%d\n",n-hash[max]);
  for(i=pos+1;i<n;i++)
  {
    //debug1(i);
    //cout<<endl;
    if(x>a[i])
    {
      printf("1 %d %d\n",i+1,i);
    }
    else if(x < a[i])
    {
      printf("2 %d %d\n",i+1,i);
    }
  }
  for(i=pos-1;i>=0;i--)
  {
    if(x>a[i])
    {
      printf("1 %d %d\n",i+1,i+2);
    }
    else if(x < a[i])
    {
      printf("1 %d %d\n",i+1,i+2);
    }
  }
  //cout<<v1.size()<<endl;
  //for(i=0;i<v1.size();i++)
    //printf("%d %d %d\n",v1[i].op,v1[i].p+1,v1[i].q+1);

}


#include<bits/stdc++.h>
using namespace std;
#define debug1(x) cout<<#x<<" "<<x<<endl;
#define debug2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
#define pii pair<ll,ll>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define mp make_pair
#define ll long long int
#define Q queue<pair<int,int>>
struct data{
  int op,p,q;

};

int main()
{
int n;
cin>>n;
int i;
int a[500005];
int b[500005];
int hash[500005];
for(i=0;i<500005;i++)
	hash[i]=0;
for(int i=0;i<n;i++)
{
	cin>>a[i];
	hash[a[i]]++;
}
int max=0;
int pos;
for(i=0;i<n;i++)
{
	if(max<hash[a[i]])
	{
		max=a[i];
		pos=i;
	}
}
  vector <struct data> v1;
  int x=max;

  //debug3(pos,x,n);
  printf("%d\n",n-hash[max]);
  for(i=pos+1;i<n;i++)
  {
    //debug1(i);
    //cout<<endl;
    if(x>a[i])
    {
      printf("1 %d %d\n",i+1,i);
    }
    else if(x < a[i])
    {
      printf("2 %d %d\n",i+1,i);
    }
  }
  for(i=pos-1;i>=0;i--)
  {
    if(x>a[i])
    {
      printf("1 %d %d\n",i+1,i+2);
    }
    else if(x < a[i])
    {
      printf("1 %d %d\n",i+1,i+2);
    }
  }
  //cout<<v1.size()<<endl;
  //for(i=0;i<v1.size();i++)
    //printf("%d %d %d\n",v1[i].op,v1[i].p+1,v1[i].q+1);

}