#include<bits/stdc++.h>
using namespace std;
long long sum;
int f3(long long n)
{
    sum=1;int i=0;
    while(3*sum<=n){
        sum*=3;
        i++;
    }
    return i;
}
int main()
{
    int T,t;long long n,maxn,maxsum,ans;
    cin>>T;
    while(T--){
        cin>>n;
        ans=n;
        maxn=f3(n)+1;
        maxsum=sum*3;
        vector<long long> p(200);
        t=0;
        while(n){
            t++;
            p[f3(n)]++;
            n-=sum;
        }
        if(t>maxn)
            cout<<maxsum<<endl;
        else{
            int flag=0;
            for(int i=0;i<maxn;i++)
                if(p[i]>1)
                    flag=1;
                else if(p[i]==0)
                    flag=0;
            if(flag)
                cout<<maxsum<<endl;
            else
                cout<<ans<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
long long sum;
int f3(long long n)
{
    sum=1;int i=0;
    while(3*sum<=n){
        sum*=3;
        i++;
    }
    return i;
}
int main()
{
    int T,t;long long n,maxn,maxsum,ans;
    cin>>T;
    while(T--){
        cin>>n;
        ans=0;
        maxn=f3(n)+1;
        maxsum=sum*3;
        vector<long long> p(200);
        t=0;
        while(n){
            t++;
            p[f3(n)]++;
            n-=sum;
        }
        int flag=0;
        for(int i=0;i<maxn;i++)
            if(p[i]>1)
                flag=1;
            else if(p[i]==0&&flag){
                flag=0;
                ans=pow(3,i);
            }
            else
                ans+=p[i]*pow(3,i);
            if(flag)
                cout<<maxsum<<endl;
            else
                cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
long long sum;
int f3(long long n)
{
    sum=1;int i=0;
    while(3*sum<=n){
        sum*=3;
        i++;
    }
    return i;
}
int main()
{
    int T,t;long long n,maxn,maxsum,ans;
    cin>>T;
    while(T--){
        cin>>n;
        ans=n;
        maxn=f3(n)+1;
        maxsum=sum*3;
        vector<long long> p(200);
        t=0;
        while(n){
            t++;
            p[f3(n)]++;
            n-=sum;
        }
        int flag=0;
        for(int i=0;i<maxn;i++)
            if(p[i]>1)
                flag=1;
            else if(p[i]==0&&flag){
                flag=0;
                ans=pow(3,i);
            }
            else
                ans+=pow(3,i);
            if(flag)
                cout<<maxsum<<endl;
            else
                cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast")
//#pragma optimize("O3")

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff first
#define ss second
#define forn(i, n) for(int i=0;i<n;i++)
#define fore(i, l, r) for(int i=l;i<r;i++)
#define pi pair<int,int>
#define pl pair<ll,ll>
#define vi vector<int>
#define vpi vector<pi>
#define gcd(a,b) __gcd(a,b)
#define FASTIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define sqr(x) ((x)*(x))
#define newstr cout << "\n"
#define call(a) for(auto xxx:a)cout<<xxx<<" ";cout<<"\n";
#define popcount(a) __builtin_popcount(a)
//#define FILE
const int hashP = 239017;
const int N = 1e5+10;
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const int INF = 1e9;
const ll INF2 = 1e18;

int main() {

    FASTIO;

#ifdef FILE
    freopen("quadr.in","r",stdin);
    freopen("quadr.out","w",stdout);
#endif

    int tt;
    cin >> tt;
    while(tt--) {
        ll n;
        cin >> n;
        ll ch=1,st=0;
        while(ch*3<n) {
            ch*=3;
            st++;
        }
        ll chh=ch*3,tr=1;
        for(int i=0;i<st;i++) {
            if(ch>=n) break;
            ch+=tr;
            tr*=3;
        }
        if(ch>=n)
        cout << min(ch,chh) << "\n"; else cout << chh << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast")
//#pragma optimize("O3")

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff first
#define ss second
#define forn(i, n) for(int i=0;i<n;i++)
#define fore(i, l, r) for(int i=l;i<r;i++)
#define pi pair<int,int>
#define pl pair<ll,ll>
#define vi vector<int>
#define vpi vector<pi>
#define gcd(a,b) __gcd(a,b)
#define FASTIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define sqr(x) ((x)*(x))
#define newstr cout << "\n"
#define call(a) for(auto xxx:a)cout<<xxx<<" ";cout<<"\n";
#define popcount(a) __builtin_popcount(a)
//#define FILE
const int hashP = 239017;
const int N = 1e5+10;
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const int INF = 1e9;
const ll INF2 = 1e18;

vector <ll> step;

bool solve(int mid) {
    int nn = step.size();
    for(int i=0;i<nn;i++) {
        for(int j=0;j<nn;j++) {
            ll sum=0;
            for(int q=i;q<=j;q++) {
                sum+=step[q];
            }
            if(sum==mid) return true;
        }
    }
    return false;
}

int main() {

    FASTIO;

#ifdef FILE
    freopen("quadr.in","r",stdin);
    freopen("quadr.out","w",stdout);
#endif

    step.push_back(1);
    for(int i=0;i<=10;i++) {
        step.push_back(step[step.size()-1]*3);
    }
    int tt;
    cin >> tt;
    while(tt--) {
        ll n;
        cin >> n;
        for(int i=n;i<=n*3;i++) {
            if(solve(i)) {
                cout << i << "\n";
                break;
            }
        }
    }
  /*  int tt;
    cin >> tt;
    while(tt--) {
        ll n;
        cin >> n;
        ll ans=0;
        while(1) {
            if(n==0) break;
            ll ch=1,st=0;
            while(ch*3<=n) {
                ch*=3;
                st++;
            }
            ans+=ch;
            n=n-ch;
        }
        cout << ans << "\n";
    }*/

    return 0;
}


#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast")
//#pragma optimize("O3")

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff first
#define ss second
#define forn(i, n) for(int i=0;i<n;i++)
#define fore(i, l, r) for(int i=l;i<r;i++)
#define pi pair<int,int>
#define pl pair<ll,ll>
#define vi vector<int>
#define vpi vector<pi>
#define gcd(a,b) __gcd(a,b)
#define FASTIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define sqr(x) ((x)*(x))
#define newstr cout << "\n"
#define call(a) for(auto xxx:a)cout<<xxx<<" ";cout<<"\n";
#define popcount(a) __builtin_popcount(a)
//#define FILE
const int hashP = 239017;
const int N = 1e5+10;
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const int INF = 1e9;
const ll INF2 = 1e18;

vector <ll> step;

bool solve(int mid) {
    bool ok=0;
    while(mid>0) {
        if(mid%3==2) return false; else mid/=3;
    }
    return true;
}

int main() {

    FASTIO;

#ifdef FILE
    freopen("quadr.in","r",stdin);
    freopen("quadr.out","w",stdout);
#endif

    step.push_back(1);
    for(int i=0;i<=9;i++) {
        step.push_back(step[step.size()-1]*3);
    }
    int tt;
    cin >> tt;
    while(tt--) {
        ll n;
        cin >> n;
        sort(all(step));
        for(int i=n;i<=n*3;i++) {
            if(solve(i)) {
                cout << i << "\n";
                break;
            }
        }
    }
 /*   int tt;
    cin >> tt;
    while(tt--) {
        ll n;
        cin >> n;
        ll ans=0;
        bool s[111];
        for(int i=0;i<111;i++) s[i]=false;
        while(1) {
            if(n<=0) break;
            ll ch=1,st=0;
            while(ch*3<=n || s[st]==1) {
                ch*=3;
                st++;
            }
            s[st]=1;
            ans+=ch;
            n=n-ch;
            last=n;
        }
        cout << ans << "\n";
    }
*/
    return 0;
}


#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast")
//#pragma optimize("O3")

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff first
#define ss second
#define forn(i, n) for(int i=0;i<n;i++)
#define fore(i, l, r) for(int i=l;i<r;i++)
#define pi pair<int,int>
#define pl pair<ll,ll>
#define vi vector<int>
#define vpi vector<pi>
#define gcd(a,b) __gcd(a,b)
#define FASTIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define sqr(x) ((x)*(x))
#define newstr cout << "\n"
#define call(a) for(auto xxx:a)cout<<xxx<<" ";cout<<"\n";
#define popcount(a) __builtin_popcount(a)
//#define FILE
const int hashP = 239017;
const int N = 1e5+10;
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const int INF = 1e9;
const ll INF2 = 1e18;

vector <ll> step;

bool solve(int mid) {
    int nn = step.size();
    for(int i=0;i<nn;i++) {
        for(int j=0;j<nn;j++) {
            ll sum=0;
            for(int q=i;q<j;q++) {
                sum+=step[q];
            }
            if(sum==mid) return true;
        }
    }
    return false;
}

int main() {

    FASTIO;

#ifdef FILE
    freopen("quadr.in","r",stdin);
    freopen("quadr.out","w",stdout);
#endif

    step.push_back(1);
    for(int i=0;i<=10;i++) {
        step.push_back(step[step.size()-1]*3);
    }
    int tt;
    cin >> tt;
    while(tt--) {
        ll n;
        cin >> n;
        for(int i=n;i<=n*3;i++) {
            if(solve(i)) {
                cout << i << "\n";
                break;
            }
        }
    }

    return 0;
}


#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast")
//#pragma optimize("O3")

typedef long long ll;
typedef unsigned long long ull;

using namespace std;

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff first
#define ss second
#define forn(i, n) for(int i=0;i<n;i++)
#define fore(i, l, r) for(int i=l;i<r;i++)
#define pi pair<int,int>
#define pl pair<ll,ll>
#define vi vector<int>
#define vpi vector<pi>
#define gcd(a,b) __gcd(a,b)
#define FASTIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define sqr(x) ((x)*(x))
#define newstr cout << "\n"
#define call(a) for(auto xxx:a)cout<<xxx<<" ";cout<<"\n";
#define popcount(a) __builtin_popcount(a)
//#define FILE
const int hashP = 239017;
const int N = 1e5+10;
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const int INF = 1e9;
const ll INF2 = 1e18;

vector <ll> step;

bool solve(int mid) {
    int nn = step.size();
    for(int i=0;i<nn;i++) {
        for(int j=0;j<nn;j++) {
            ll sum=0;
            for(int q=i;q<j;q++) {
                sum+=step[q];
            }
            if(sum==mid) return true;
        }
    }
    return false;
}

int main() {

    FASTIO;

#ifdef FILE
    freopen("quadr.in","r",stdin);
    freopen("quadr.out","w",stdout);
#endif

    step.push_back(1);
    for(int i=0;i<=15;i++) {
        step.push_back(step[step.size()-1]*3);
    }
    int tt;
    cin >> tt;
    while(tt--) {
        ll n;
        cin >> n;
        for(int i=n;i<=n*3;i++) {
            if(solve(i)) {
                cout << i << "\n";
                break;
            }
        }
    }

    return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define pii pair<int,int>
#define ll long long int
#define sf(a) scanf("%d", &a)
#define forn(i,n) for(int i = 0; i < n; ++i)
const int mx = 1e6+5;
const int inf = INT_MAX;

using namespace std;
vector<ll> pre(14,0);

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int q;
	scanf("%d", &q);

	forn(i,13) pre[i+1] = pow(3,i);
	for(int i = 1; i < 14; ++i) pre[i] += pre[i-1];
	vector<ll> ans;
	for(int i = 1; i < 14; ++i){
		for(int j = i; j < 14; ++j){
			ans.pb(pre[j]-pre[i-1]);
		}
	}
	sort(ans.begin(), ans.end());
	while(q--){
		int n;
		scanf("%d", &n);
		ll res, diff = inf;
		for(int i = 0; i < ans.size(); ++i){
			if(ans[i] >= n && abs(n-ans[i]) < diff){
				res = ans[i];
				diff = abs(n-ans[i]);
			}
		}
		printf("%lld\n", res);
	}
	return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define pii pair<int,int>
#define ll long long int
#define sf(a) scanf("%d", &a)
#define forn(i,n) for(int i = 0; i < n; ++i)
const int mx = 1e6+5;
const int inf = INT_MAX;

using namespace std;
vector<ll> pre(13,0);

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int q;
	scanf("%d", &q);
	forn(i,13) pre[i] = pow(3,i);
	while(q--){
		int n;
		scanf("%d", &n);
		while(1){
			int m = n;
			for(int i = 12; i >= 0; --i){
				if(pre[i] <= m) m -= pre[i];
			}
			if(m == 0){
				printf("%d\n", n);
				break;
			}
			++n;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define pii pair<int,int>
#define ll long long int
#define sf(a) scanf("%d", &a)
#define forn(i,n) for(int i = 0; i < n; ++i)
const int mx = 1e6+5;
const int inf = 0x3f3f3f3f;

using namespace std;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int q;
	scanf("%d", &q);
	while(q--){
		ll n;
		scanf("%lld", &n);
		ll num = 0, p = 1;
		while(num < n) {
			num += p;
			p *= 3;
		}
		while(p > 0){
			if(num-p >= n) num -= p;
			p /= 3;
		}
		printf("%lld\n", num);
	}
	return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define pii pair<int,int>
#define ll long long int
#define sf(a) scanf("%d", &a)
#define forn(i,n) for(int i = 0; i < n; ++i)
const int mx = 1e6+5;
const int inf = INT_MAX;

using namespace std;
vector<ll> pre(13,0);

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int q;
	scanf("%d", &q);

	forn(i,13) pre[i] = pow(3,i);
	sort(ans.begin(), ans.end());
	while(q--){
		int n,;
		scanf("%d", &n);
		while(1){
			int m = n;
			for(int i = 12; i >= 0; --i){
				if(pre[i] >= m) m -= pre[i];
			}
			if(m == 0){
				printf("%lld\n", n);
				break;
			}
			++n;
		}
	}
	return 0;
}


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

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

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

#include <iostream>
#include<math.h>
using namespace std;
int sr(long long int n )
{  int k=0;
   while(true)
   {
       if(n/3==0)
       break;
       n=n/3;
       k++;

   }
   return k;
}

int main()
{
    int q;
    cin>>q;
    for(int i =0;i<q;i++)
    {
        long long n ;
        int k=0;

        cin>>n;
        int bla=sr(n);
        long long temp=pow(3,bla);
       // n=n-temp;
        while(true)
        {
        if(pow(3,k)>=n)
        {cout<<pow(3,k)<<endl;
        break;
        }
        if(temp>=n)

        {
            cout<<temp<<endl;
         break;
        }

        temp=temp+pow(3,k);
        k++;
        }

    }

}


#include <iostream>
#include<math.h>
using namespace std;
#define ll unsigned long long
int root(ll n)
{
    int k =0;
    while(n/3!=0)
    {
        k++;
        n=n/3;
    }
    return k;
}
int main(void)
{
   int q;
   cin>>q;
   for(int i =0;i<q;i++)
   {
      ll n;
      cin>>n;
      ll bla=n;
      ll temp=0;
      while(true)
      {
          int k=root(bla);
          if(k!=0)
          temp=temp+pow(3,k);
          //cout<<temp<<endl;
          bla=n-temp;
          //cout<<bla<<endl;
          if(bla==2)
          {
              cout<<temp+3<<endl;
              break;
          }
          if(bla==0||bla==1)
          {
              cout<<n<<endl;
              break;
          }
          if(bla>(pow(3,k)/2))
          {
             cout<<temp+pow(3,k+1)-pow(3,k)<<endl;
             break;
          }
          else
          continue;
      }
   }
}


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

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

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

#include <iostream>
#include<math.h>
using namespace std;
int sr(long long int n )
{  int k=0;
   while(true)
   {
       if(n/3==0)
       break;
       n=n/3;
       k++;

   }
   return k;
}

int main()
{
    int q;
    cin>>q;
    for(int i =0;i<q;i++)
    {
        long long n ;
        int k=0;

        cin>>n;
        int bla=sr(n);
        long long temp=pow(3,bla);
       // n=n-temp;
        while(true)
        {
        if(pow(3,k)>=n)
        {cout<<pow(3,k);
        break;
        }
        if(temp>=n)

        {
            cout<<temp;
         break;
        }

        temp=temp+pow(3,k);
        k++;
        }

    }

}


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

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

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

#include <iostream>
#include<math.h>
using namespace std;
int sr(long long int n )
{  int k=0;
    if(n==1)
    return k;
    if(n==2)
    return k+1;
    while(n/3!=0)
    {
        k++;
        n=n/3;
    }
    return k;
}
int main()
{
    int q;
    cin>>q;
    for(int i =0;i<q;i++)
    {
        long long n ;
        int k=0;
        long long temp=0;
        cin>>n;
        int bla=n;
        if(n==1)
        {
            cout<<"1"<<endl;
            continue;
        }
        if(n==2)
        {
            cout<<"3"<<endl;
            continue;
        }
        while(true)
        {
            k=sr(bla);
            int t=pow(3,k);

            bla=bla-t;
            if(bla==0)
            {
                cout<<temp<<endl;
                break;
            }
            if(bla<=(t/2))
            {
                temp=temp+t;
                continue;
            }
            else
            {
                cout<<temp+pow(3,k+1)<<endl;
                break;
            }

        }

    }

}


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

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

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

#include <iostream>
#include<math.h>
#define ll unsigned long long
using namespace std;

int root(ll n)
{   int k=0;
    if(n==1)
    return k;
    while(n/3!=0)
    {
        k++;
        n=n/3;
    }
    return k;

}

int main()
{
    int q;
    cin>>q;
    for(int i =0;i<q;i++)
    {
        ll n,bla;
        cin>>n;
        bla=n;
        ll temp=0;
        ll p=0;
        if(n==1)
        {
            cout<<n<<endl;
            continue;
        }
        while(true)
        {
            int k=root(bla);
            p=pow(3,k);
            bla=bla-p;
           // cout<<bla<<endl;
            if(bla==0)
            {cout<<n<<endl;
            break;
        }
            if(bla>(p/2))
            {
                ll t=pow(3,k+1);
                cout<<temp+t;
                break;
            }
            else
            {
                temp=temp+p;
                continue;
            }
        }
    }

    return 0;
}


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

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

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

#include <iostream>
#include<math.h>
using namespace std;
int sr(long long int n )
{  int k=0;
    if(n==1)
    return k;
    if(n==2)
    return k+1;
    while(n/3!=0)
    {
        k++;
        n=n/3;
    }
    return k;
}
int main()
{
    int q;
    cin>>q;
    for(int i =0;i<q;i++)
    {
        long long n ;
        int k=0;
        long long temp=0;
        cin>>n;
        int bla=n;
        if(n==1)
        {
            cout<<"1"<<endl;
            continue;
        }
        if(n==2)
        {
            cout<<"3"<<endl;
            continue;
        }
        while(true)
        {
            k=sr(bla);
            int t=pow(3,k);
            temp=temp+t;
            bla=bla-t;
            if(bla==0)
            {
                cout<<temp<<endl;
                break;
            }
            if(bla<=(t/2))
            {

                continue;
            }
            else
            {
                cout<<temp+pow(3,k+1)-t<<endl;
                break;
            }

        }

    }

}


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

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

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

#include <iostream>
#include<math.h>
#define ll unsigned long long
using namespace std;

int root(ll n)
{   int k=0;
    if(n==1)
    return k;
    while(n/3!=0)
    {
        k++;
        n=n/3;
    }
    return k;

}

int main()
{
    int q;
    cin>>q;
    for(int i =0;i<q;i++)
    {
        ll n,bla;
        cin>>n;
        bla=n;
        ll temp=0;
        ll p=0;
        if(n==1)
        {
            cout<<n<<endl;
            continue;
        }
        while(true)
        {
            int k=root(bla);
            p=pow(3,k);
            bla=bla-p;
           // cout<<bla<<endl;
            if(bla==0)
            {cout<<n<<endl;
            break;
        }
            if(bla>(p/2))
            {
                ll t=pow(3,k+1);
                cout<<temp+t<<endl;
                break;
            }
            else
            {
                temp=temp+p;
                continue;
            }
        }
    }

    return 0;
}


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

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

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

#include <iostream>
#include<math.h>
#define ll unsigned long long
using namespace std;

int root(ll n)
{   int k=0;
    if(n==1)
    return k;
    while(n/3!=0)
    {
        k++;
        n=n/3;
    }
    return k;

}

int main()
{
    int q;
    cin>>q;
    for(int i =0;i<q;i++)
    {
        ll n,bla;
        cin>>n;
        bla=n;
        ll temp=0;
        ll p=0;
        if(n==1)
        {
            cout<<n<<endl;
            continue;
        }
        while(true)
        {
            int k=root(bla);
            p=pow(3,k);
            bla=bla-p;
           // cout<<bla<<endl;
            if(bla==0)
            {cout<<n<<endl;
            break;
        }
            if(bla>(p/2))
            {
                cout<<temp+pow(3,k+1);
                break;
            }
            else
            {
                temp=temp+p;
                continue;
            }
        }
    }

    return 0;
}


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

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

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

#include <iostream>
#include<math.h>
using namespace std;
int sr(long long int n )
{  int k=0;
    if(n==1)
    return k;
    if(n==2)
    return k+1;
    while(n/3!=0)
    {
        k++;
        n=n/3;
    }
    return k;
}
int main()
{
    int q;
    cin>>q;
    for(int i =0;i<q;i++)
    {
        long long n ;
        int k=0;
        long long temp=0;
        cin>>n;
        int bla=n;
        if(n==1)
        {
            cout<<"1"<<endl;
            continue;
        }
        if(n==2)
        {
            cout<<"3"<<endl;
            continue;
        }
        while(true)
        {
            k=sr(bla);
            cout<<k<<endl;
            long long t=pow(3,k);
            //cout<<t<<endl;
            temp=temp+t;
            if(bla<t)
            {
                cout<<temp+pow(3,k+1)-t<<endl;
                break;
            }
            bla=bla-t;
            cout<<bla<<endl;
            if(bla==0)
            {
                cout<<temp<<endl;
                break;
            }
            else if(bla<=(t/2))
            {

                continue;
            }
            else
            {
                cout<<temp+pow(3,k+1)-t<<endl;
                break;
            }

        }

    }

}


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

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

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

#include <iostream>
#include<math.h>
using namespace std;
int sr(long long int n )
{  int k=0;
    if(n==1)
    return k;
    if(n==2)
    return k+1;
    while(n/3!=0)
    {
        k++;
        n=n/3;
    }
    return k;
}
int main()
{
    int q;
    cin>>q;
    for(int i =0;i<q;i++)
    {
        long long n ;
        int k=0;
        long long temp=0;
        cin>>n;
        int bla=n;
        if(n==1)
        {
            cout<<"1"<<endl;
            continue;
        }
        if(n==2)
        {
            cout<<"3"<<endl;
            continue;
        }
        while(true)
        {
            k=sr(bla);
            //cout<<k<<endl;
            long long t=pow(3,k);
            //cout<<t<<endl;
            temp=temp+t;
            if(bla<t)
            {
                cout<<temp+pow(3,k)-t<<endl;
                break;
            }
            bla=bla-t;
            //cout<<bla<<endl;
            if(bla==0)
            {
                cout<<temp<<endl;
                break;
            }
            else if(bla<=(t/2))
            {

                continue;
            }
            else
            {
                cout<<temp+pow(3,k+1)-t<<endl;
                break;
            }

        }

    }

}


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

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

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

#include <iostream>
#include<math.h>
using namespace std;
int sr(long long int n )
{  int k=0;
    if(n==1)
    return k;
    if(n==2)
    return k+1;
    while(n/3!=0)
    {
        k++;
        n=n/3;
    }
    return k;
}
int main()
{
    int q;
    cin>>q;
    for(int i =0;i<q;i++)
    {
        long long n ;
        int k=0;
        long long temp=0;
        cin>>n;
        int bla=n;
        if(n==1)
        {
            cout<<"1"<<endl;
            continue;
        }
        if(n==2)
        {
            cout<<"3"<<endl;
            continue;
        }
        while(true)
        {
            k=sr(bla);
            //cout<<k<<endl;
            long long t=pow(3,k);
            //cout<<t<<endl;
            temp=temp+t;
            if(bla<t)
            {
                cout<<temp+pow(3,k+1)-t<<endl;
                break;
            }
            bla=bla-t;
            //cout<<bla<<endl;
            if(bla==0)
            {
                cout<<temp<<endl;
                break;
            }
            else if(bla<=(t/2))
            {

                continue;
            }
            else
            {
                cout<<temp+pow(3,k+1)-t<<endl;
                break;
            }

        }

    }

}


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

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

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

#include <iostream>
#include<math.h>
using namespace std;
int sr(long long int n )
{  int k=0;
    if(n==1)
    return k;
    if(n==2)
    return k+1;
    while(n/3!=0)
    {
        k++;
        n=n/3;
    }
    return k;
}
int main()
{
    int q;
    cin>>q;
    for(int i =0;i<q;i++)
    {
        long long n ;
        int k=0;
        long long temp=0;
        cin>>n;
        int bla=n;
        if(n==1)
        {
            cout<<"1"<<endl;
            continue;
        }
        if(n==2)
        {
            cout<<"3"<<endl;
            continue;
        }
        while(true)
        {
            k=sr(bla);
            long long t=pow(3,k);
            temp=temp+t;
            bla=bla-t;
            if(bla==0)
            {
                cout<<temp<<endl;
                break;
            }
            if(bla<=(t/2))
            {

                continue;
            }
            else
            {
                cout<<temp+pow(3,k+1)-t<<endl;
                break;
            }

        }

    }

}


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

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

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

#include <iostream>
#include<math.h>
using namespace std;

int main()
{
    int q;
    cin>>q;
    for(int i =0;i<q;i++)
    {
        long long n ;
        int k=0;
        long long temp=0;
        cin>>n;
        while(true)
        {
          temp=temp+pow(3,k);

            if(pow(3,k)>=n)
            {
                cout<<pow(3,k)<<endl;
                break;
            }
            if(temp>=n)
            {
               cout<<temp<<endl;
               break;
            }
            k++;
        }

    }

}


#include <bits/stdc++.h>
using namespace  std;
typedef long long ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define mp make_pair
#define sz(a) a.size()
#define fi first
#define se second
#define rsz(a,n) a.resize(n)
#define test(t) ll t;cin>>t;while(t--)
#define forn(i,e) for(int i = 0; i < e; i++)
#define forsn(i,s,e) for(int i = s; i < e; i++)
#define rforn(i,s) for(int i = s; i >= 0; i--)
#define rforsn(i,s,e) for(int i = s; i >= e; i--)
#define fillv(a,k) memset(a,k,sizeof(a))
#define leadzero(a) __builtin_clz(a) //count leading zeros
#define trailzero(a) __builtin_ctz(a) //count trailing zeros
#define bitcount(a) __builtin_popcount(a) // count set bits (add ll)
#define ln cout<<"\n"
#define sp cout<<" "
#define spaceprint(a,i,s,n) {forsn(i,s,n) { cout<<a[i]; sp;}}
#define lineprint(a,i,s,n) {forsn(i,s,n) {cout<<a[i]; ln;}}
#define maxe(a) *max_element(a.begin(),a.end())
#define maxi(a) max_element(a.begin(),a.end())-a.begin()
#define mine(a) *min_element(a.begin(),a.end())
#define mini(a) min_element(a.begin(),a.end())-a.begin()
#define all(c) c.begin(),c.end()
#define trav(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element)  (find(all(container),element) != container.end())// check the presence of element
//copy(from_begin, from_end, to_begin); copy function
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<ll,ll> p64;
typedef pair<int,int> p32;
typedef pair<int,p32> p96;
typedef vector<ll> v64;
typedef vector<string> vs;
typedef vector<int> v32;
typedef vector<v32> vv32;
typedef vector<v64> vv64;
typedef vector<p32> vp32;
typedef vector<p64> vp64;
typedef vector<vp32> vvp32;
typedef map<int,int> m32;
typedef map<ll,ll> m64;
const int LIM = 1e5+5, MOD =  1e9+7;
#define sumv(v) accumulate(all(v),ll(0))
#define productv(v) accumulate(all(v), ll(1), multiplies< ll >())
ll gcd(ll a, ll b) { if(b == 0) return a; return gcd(b, a % b); }
ll fastpowMOD(ll a, ll p,ll MOD){ if(p==0) return 1; ll z = fastpowMOD(a,p/2,MOD); z = (z*z)%MOD; if(p%2) z = (z*a)%MOD; return z; }
bool seive[100005];
void SieveOfEratosthenes(ll n)
{ memset(seive, true, sizeof(seive)); for (ll p=2; p*p<=n; p++) if (seive[p] == true) for (ll i=p*p; i<=n; i += p) seive[i] = false; }
ll ans[200004]={-1};
ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}
int main()
{
    fast;
    ll q;
    cin>>q;
    while (q--)
    {
    	ll n;
    	cin>>n;
    	if (n==0)
    	{
    		cout<<1<<endl;
    		continue;
    	}
    	ll temp=n;
    	v64 v;
    	while (temp>0)
    	{
    		v.pb(temp%3);
    		temp/=3;
    	}
    	reverse(all(v));
    	ll ind=-1;
    	for (ll i=0;i<v.size();i++)
    		cout<<v[i]<<" ";
    	cout<<endl;
    	for (ll i=0;i<v.size();i++)
    	{
    		if (v[i]==2)
    		{
    			ind=i;
    			break;
    		}
    	}
    	ll ans;
    	for (ll i=0;i<v.size();i++)
    	{
    		if (v[i]==1||v[i]==2)
    		{
    			ans=i;
    			break;
    		}
    	}
    	ans=v.size()-ans;
    	if (ind==-1)
    		cout<<n<<endl;
    	else
    		cout<<power(3,ans)<<endl;
      }

}


#include <bits/stdc++.h>
using namespace  std;
typedef long long ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define mp make_pair
#define sz(a) a.size()
#define fi first
#define se second
#define rsz(a,n) a.resize(n)
#define test(t) ll t;cin>>t;while(t--)
#define forn(i,e) for(int i = 0; i < e; i++)
#define forsn(i,s,e) for(int i = s; i < e; i++)
#define rforn(i,s) for(int i = s; i >= 0; i--)
#define rforsn(i,s,e) for(int i = s; i >= e; i--)
#define fillv(a,k) memset(a,k,sizeof(a))
#define leadzero(a) __builtin_clz(a) //count leading zeros
#define trailzero(a) __builtin_ctz(a) //count trailing zeros
#define bitcount(a) __builtin_popcount(a) // count set bits (add ll)
#define ln cout<<"\n"
#define sp cout<<" "
#define spaceprint(a,i,s,n) {forsn(i,s,n) { cout<<a[i]; sp;}}
#define lineprint(a,i,s,n) {forsn(i,s,n) {cout<<a[i]; ln;}}
#define maxe(a) *max_element(a.begin(),a.end())
#define maxi(a) max_element(a.begin(),a.end())-a.begin()
#define mine(a) *min_element(a.begin(),a.end())
#define mini(a) min_element(a.begin(),a.end())-a.begin()
#define all(c) c.begin(),c.end()
#define trav(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element)  (find(all(container),element) != container.end())// check the presence of element
//copy(from_begin, from_end, to_begin); copy function
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<ll,ll> p64;
typedef pair<int,int> p32;
typedef pair<int,p32> p96;
typedef vector<ll> v64;
typedef vector<string> vs;
typedef vector<int> v32;
typedef vector<v32> vv32;
typedef vector<v64> vv64;
typedef vector<p32> vp32;
typedef vector<p64> vp64;
typedef vector<vp32> vvp32;
typedef map<int,int> m32;
typedef map<ll,ll> m64;
const int LIM = 1e5+5, MOD =  1e9+7;
#define sumv(v) accumulate(all(v),ll(0))
#define productv(v) accumulate(all(v), ll(1), multiplies< ll >())
ll gcd(ll a, ll b) { if(b == 0) return a; return gcd(b, a % b); }
ll fastpowMOD(ll a, ll p,ll MOD){ if(p==0) return 1; ll z = fastpowMOD(a,p/2,MOD); z = (z*z)%MOD; if(p%2) z = (z*a)%MOD; return z; }
bool seive[100005];
void SieveOfEratosthenes(ll n)
{ memset(seive, true, sizeof(seive)); for (ll p=2; p*p<=n; p++) if (seive[p] == true) for (ll i=p*p; i<=n; i += p) seive[i] = false; }
ll ans[200004]={-1};
ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}
int main()
{
    fast;
    ll q;
    cin>>q;
    v64 ans3;
    for (ll i=1;i<=1e6;i++)
    {
    	ll temp=i;
    	v64 v;
    	while (temp>0)
    	{
    		v.pb(temp%3);
    		temp/=3;
    	}
    	ll f=0;
    	for (ll j=0;j<v.size();j++)
    	{
    		if (v[j]==2)
    		{
    			f=1;
    			break;
    		}
    	}
    	if (f==0)
    		ans3.pb(i);
    }
    while (q--)
    {
    	ll n;
    	cin>>n;
    	// ll temp=n;
    	// v64 v;
    	// while (temp>0)
    	// {
    	// 	v.pb(temp%3);
    	// 	temp/=3;
    	// }
    	// reverse(all(v));
    	// ll f=0;
    	// // for (ll i=0;i<v.size();i++)
    	// // 	cout<<v[i]<<" ";
    	// // cout<<endl;
    	// for (ll i=0;i<v.size();i++)
    	// {
    	// 	if (v[i]==2)
    	// 	{
    	// 		f=1;
    	// 		break;
    	// 	}
    	// }
    	// if (f)
    	// {
    	// ll ans=power(3,v.size());
    	// cout<<ans<<endl;
    	// }
    	// else
    	// {
    	// 	cout<<n<<endl;
    	// }
    	ll ind=lower_bound(ans3.begin(),ans3.end(),n)-ans3.begin();
    	cout<<ans3[ind]<<endl;
      }

}


#include <bits/stdc++.h>
using namespace  std;
typedef long long ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define mp make_pair
#define sz(a) a.size()
#define fi first
#define se second
#define rsz(a,n) a.resize(n)
#define test(t) ll t;cin>>t;while(t--)
#define forn(i,e) for(int i = 0; i < e; i++)
#define forsn(i,s,e) for(int i = s; i < e; i++)
#define rforn(i,s) for(int i = s; i >= 0; i--)
#define rforsn(i,s,e) for(int i = s; i >= e; i--)
#define fillv(a,k) memset(a,k,sizeof(a))
#define leadzero(a) __builtin_clz(a) //count leading zeros
#define trailzero(a) __builtin_ctz(a) //count trailing zeros
#define bitcount(a) __builtin_popcount(a) // count set bits (add ll)
#define ln cout<<"\n"
#define sp cout<<" "
#define spaceprint(a,i,s,n) {forsn(i,s,n) { cout<<a[i]; sp;}}
#define lineprint(a,i,s,n) {forsn(i,s,n) {cout<<a[i]; ln;}}
#define maxe(a) *max_element(a.begin(),a.end())
#define maxi(a) max_element(a.begin(),a.end())-a.begin()
#define mine(a) *min_element(a.begin(),a.end())
#define mini(a) min_element(a.begin(),a.end())-a.begin()
#define all(c) c.begin(),c.end()
#define trav(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element)  (find(all(container),element) != container.end())// check the presence of element
//copy(from_begin, from_end, to_begin); copy function
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<ll,ll> p64;
typedef pair<int,int> p32;
typedef pair<int,p32> p96;
typedef vector<ll> v64;
typedef vector<string> vs;
typedef vector<int> v32;
typedef vector<v32> vv32;
typedef vector<v64> vv64;
typedef vector<p32> vp32;
typedef vector<p64> vp64;
typedef vector<vp32> vvp32;
typedef map<int,int> m32;
typedef map<ll,ll> m64;
const int LIM = 1e5+5, MOD =  1e9+7;
#define sumv(v) accumulate(all(v),ll(0))
#define productv(v) accumulate(all(v), ll(1), multiplies< ll >())
ll gcd(ll a, ll b) { if(b == 0) return a; return gcd(b, a % b); }
ll fastpowMOD(ll a, ll p,ll MOD){ if(p==0) return 1; ll z = fastpowMOD(a,p/2,MOD); z = (z*z)%MOD; if(p%2) z = (z*a)%MOD; return z; }
bool seive[100005];
void SieveOfEratosthenes(ll n)
{ memset(seive, true, sizeof(seive)); for (ll p=2; p*p<=n; p++) if (seive[p] == true) for (ll i=p*p; i<=n; i += p) seive[i] = false; }
ll ans[200004]={-1};
ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}
int main()
{
    fast;
    ll q;
    cin>>q;
    while (q--)
    {
    	ll n;
    	cin>>n;
    	if (n==0)
    	{
    		cout<<1<<endl;
    		continue;
    	}
    	ll temp=n;
    	v64 v;
    	while (temp>0)
    	{
    		v.pb(temp%3);
    		temp/=3;
    	}
    	reverse(all(v));
    	ll ind=-1;
    	// for (ll i=0;i<v.size();i++)
    	// 	cout<<v[i]<<" ";
    	// cout<<endl;
    	for (ll i=0;i<v.size();i++)
    	{
    		if (v[i]==2)
    		{
    			ind=i;
    			break;
    		}
    	}
    	ll ans;
    	for (ll i=0;i<v.size();i++)
    	{
    		if (v[i]==1||v[i]==2)
    		{
    			ans=i;
    			break;
    		}
    	}
    	ans=v.size()-ans;
    	if (ind==-1)
    		cout<<n<<endl;
    	else
    		cout<<power(3,ans)<<endl;
      }

}


#include <bits/stdc++.h>
using namespace  std;
typedef long long ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define mp make_pair
#define sz(a) a.size()
#define fi first
#define se second
#define rsz(a,n) a.resize(n)
#define test(t) ll t;cin>>t;while(t--)
#define forn(i,e) for(int i = 0; i < e; i++)
#define forsn(i,s,e) for(int i = s; i < e; i++)
#define rforn(i,s) for(int i = s; i >= 0; i--)
#define rforsn(i,s,e) for(int i = s; i >= e; i--)
#define fillv(a,k) memset(a,k,sizeof(a))
#define leadzero(a) __builtin_clz(a) //count leading zeros
#define trailzero(a) __builtin_ctz(a) //count trailing zeros
#define bitcount(a) __builtin_popcount(a) // count set bits (add ll)
#define ln cout<<"\n"
#define sp cout<<" "
#define spaceprint(a,i,s,n) {forsn(i,s,n) { cout<<a[i]; sp;}}
#define lineprint(a,i,s,n) {forsn(i,s,n) {cout<<a[i]; ln;}}
#define maxe(a) *max_element(a.begin(),a.end())
#define maxi(a) max_element(a.begin(),a.end())-a.begin()
#define mine(a) *min_element(a.begin(),a.end())
#define mini(a) min_element(a.begin(),a.end())-a.begin()
#define all(c) c.begin(),c.end()
#define trav(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element)  (find(all(container),element) != container.end())// check the presence of element
//copy(from_begin, from_end, to_begin); copy function
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<ll,ll> p64;
typedef pair<int,int> p32;
typedef pair<int,p32> p96;
typedef vector<ll> v64;
typedef vector<string> vs;
typedef vector<int> v32;
typedef vector<v32> vv32;
typedef vector<v64> vv64;
typedef vector<p32> vp32;
typedef vector<p64> vp64;
typedef vector<vp32> vvp32;
typedef map<int,int> m32;
typedef map<ll,ll> m64;
const int LIM = 1e5+5, MOD =  1e9+7;
#define sumv(v) accumulate(all(v),ll(0))
#define productv(v) accumulate(all(v), ll(1), multiplies< ll >())
ll gcd(ll a, ll b) { if(b == 0) return a; return gcd(b, a % b); }
ll fastpowMOD(ll a, ll p,ll MOD){ if(p==0) return 1; ll z = fastpowMOD(a,p/2,MOD); z = (z*z)%MOD; if(p%2) z = (z*a)%MOD; return z; }
bool seive[100005];
void SieveOfEratosthenes(ll n)
{ memset(seive, true, sizeof(seive)); for (ll p=2; p*p<=n; p++) if (seive[p] == true) for (ll i=p*p; i<=n; i += p) seive[i] = false; }
ll ans[200004]={-1};
ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}
int main()
{
    fast;
    ll q;
    cin>>q;
    v64 ans3;
    for (ll i=1;i<=n;i++)
    {
    	ll temp=n;
    	v64 v;
    	while (temp>0)
    	{
    		v.pb(temp%3);
    		temp/=3;
    	}
    	ll f=0;
    	for (ll j=0;j<v.size();j++)
    	{
    		if (v[j]==2)
    		{
    			f=1;
    			break;
    		}
    	}
    	if (f==0)
    		ans3.pb(i);
    }
    while (q--)
    {
    	ll n;
    	cin>>n;
    	// ll temp=n;
    	// v64 v;
    	// while (temp>0)
    	// {
    	// 	v.pb(temp%3);
    	// 	temp/=3;
    	// }
    	// reverse(all(v));
    	// ll f=0;
    	// // for (ll i=0;i<v.size();i++)
    	// // 	cout<<v[i]<<" ";
    	// // cout<<endl;
    	// for (ll i=0;i<v.size();i++)
    	// {
    	// 	if (v[i]==2)
    	// 	{
    	// 		f=1;
    	// 		break;
    	// 	}
    	// }
    	// if (f)
    	// {
    	// ll ans=power(3,v.size());
    	// cout<<ans<<endl;
    	// }
    	// else
    	// {
    	// 	cout<<n<<endl;
    	// }
    	ll ind=lower_bound(ans3.begin(),ans3.end(),n)-v.begin();
    	cout<<ans3[i]<<endl;
      }

}


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

int main()
{

    long long int t;
    cin>>t;
    while(t--){
    long long int n;
    cin>>n;
    long long int a=n;
    long long int arr[39]={0};
    for(int i=38;i>=0;i--){
        if(n>=pow(3,i)){
            arr[i]=1;
            n-=pow(3,i);
        }

    }
long long int sum=0;
    if(n==0)cout<<a<<endl;
    else{
        for(int i=0;i<39;i++){
            if(arr[i]==0){
               arr[i]=1;
               break;
            }
            else
                arr[i]=0;
        }

        for(int i=0;i<39;i++){
            if(arr[i]!=0)
                sum+=pow(3,i);

    }
    }
    cout<<sum<<endl;

    }
    return 0;
}


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

int main()
{

    long long int t;
    cin>>t;
    while(t--){
    long long int n;
    cin>>n;
    long long int a=n;
    long long int arr[39]={0};
    for(int i=38;i>=0;i--){
        if(n>=pow(3,i)){
            arr[i]=1;
            n-=pow(3,i);
        }

    }
long long int sum=0;
    if(n==0)cout<<a<<endl;
    else{
        for(int i=0;i<39;i++){
            if(arr[i]==0){
               arr[i]=1;
               break;
            }
            else
                arr[i]=0;
        }

        for(int i=0;i<39;i++){
            if(arr[i]!=0)
                sum+=pow(3,i);

    }
    cout<<sum<<endl;
    }

    }
    return 0;
}


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

int main()
{

    long long int t;
    cin>>t;
    while(t--){
    long long int n;
    cin>>n;
    long long int a=n;
    long long int arr[39]={0};
    for(int i=38;i>=0;i--){
        if(n>=pow(3,i)){
            arr[i]=1;
            n-=pow(3,i);
        }

    }
long long int sum=0;
int v;
    if(n==0)cout<<a<<endl;
    else{
        for(int i=0;i<39;i++){
            if(arr[i]==1){
               arr[i]=0;

            }
            else{
                arr[i]=1;
                break;
        }
        v++;
        }

        for(int i=38;i>=0;i--){
            if(arr[i]==1)
                sum+=pow(3,i);

    }
    cout<<sum<<endl;
    }

    }
    return 0;
}


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

int main()
{

    long long int t;
    cin>>t;
    while(t--){
    long long int n;
    cin>>n;
    long long int a=n;
    long long int arr[39]={0};
    for(int i=38;i>=0;i--){

        if(n>=(long long)pow(3,i)){
            arr[i]=1;
            n-=(long long)pow(3,i);
        }

    }
    // for(int i=0;i<=38;i++)  cout<<arr[i]<<endl;
long long int sum=0;
int v=0;
    if(n==0)cout<<a<<endl;
    else{
        for(int i=0;i<39;i++){
        //        cout<<arr[i]<<endl;
            if(arr[i]==1){
               arr[i]=0;

            }
            else{

                arr[i]=1;
                break;
        }
        v++;
        }
      //  for(int i=0;i<=38;i++) if(arr[i]==1) cout<<i<<endl;

        for(int i=38;i>=0;i--){
            if(arr[i]==1)
                sum=sum+pow(3,i);

    }
    cout<<sum<<endl;
    }
}
    return 0;
}


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

    long long int t;
    cin>>t;
    while(t--){
    long long int n;
    cin>>n;
    long long int a=n;
    long long int arr[39]={0};
    for(int i=38;i>=0;i--){
        if(n>pow(3,i)){
            arr[i]=1;
            n-=pow(3,i);
        }

    }
long long int sum=0;
    if(n==0)cout<<a<<endl;
    else{
        for(int i=0;i<39;i++){
            if(arr[i]==0){
               arr[i]=1;
               break;
            }
            else
                arr[i]=0;
        }

        for(int i=0;i<39;i++){
            if(arr[i]!=0)
                sum+=pow(3,i);

    }
    }
    cout<<sum<<endl;

    }
    return 0;
}


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

int main()
{

    long long int t;
    cin>>t;
    while(t--){
    long long int n;
    cin>>n;
    long long int a=n;
    long long int arr[39]={0};
    for(int i=38;i>=0;i--){
        if(n>=pow(3,i)){
            arr[i]=1;
            n-=pow(3,i);
        }
        else{
            arr[i]=0;
        }

    }
long long int sum=0;
int v;
    if(n==0)cout<<a<<endl;
    else{
        for(int i=0;i<39;i++){
            if(arr[i]==1){
               arr[i]=0;

            }
            else{
                arr[i]=1;
                break;
        }
        v++;
        }

        for(int i=38;i>=0;i--){
            if(arr[i]==1)
                sum+=pow(3,i);

    }
    cout<<sum<<endl;
    }
}
    return 0;
}


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

int main()
{

    long long int t;
    cin>>t;
    while(t--){
    long long int n;
    cin>>n;
    long long int a=n;
    long long int arr[39]={0};
    for(int i=38;i>=0;i--){
        if(n>=pow(3,i)){
            arr[i]=1;
            n-=pow(3,i);
        }
        else{
            arr[i]=0;
        }

    }
long long int sum=0;
int v=0;
    if(n==0)cout<<a<<endl;
    else{
        for(int i=0;i<39;i++){
            if(arr[i]==1){
               arr[i]=0;

            }
            else{
                arr[i]=1;
                break;
        }
        v++;
        }

        for(int i=38;i>=0;i--){
            if(arr[i]==1)
                sum+=pow(3,i);

    }
    cout<<sum<<endl;
    }
}
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define Start ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define fr first
#define sc second
#define PI 3.14159265
using namespace std;
ll Fastpow(ll a,ll b)
{
    ll Ans=1;
    while(b>0)
    {
        if(b&1)
            Ans=(Ans*a);
        a=(a*a);
        b=b>>1;
    }
    return Ans;
}

int main()
{
    Start;

    int t;
    cin>>t;
    while(t--)
    {
        ll n,num=0,Ans1=0,Ans2=1e10,num2=0;
        cin>>n;
        for(int i=0; i<15; i++)
        {
            num=Fastpow(3,i);
            if(num==n)
            {
                Ans1=num;
                break;
            }
            else if(num>n)
            {
                Ans1=num;
                int idx=1;
                if(n%3!=0)
                    idx=0;
                for(int j=idx; j<=i-1; j++)
                {
                    num2+=Fastpow(3,j);
                    if(num2>=n)
                    {
                        Ans2=num2;
                        break;
                    }

                }
                break;
            }

        }
        cout<<min(Ans1,Ans2)<<"\n";
    }

}


#include <bits/stdc++.h>
#define ll long long
#define Start ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define fr first
#define sc second
#define PI 3.14159265
using namespace std;
ll Fastpow(ll a,ll b)
{
    ll Ans=1;
    while(b>0)
    {
        if(b&1)
            Ans=(Ans*a);
        a=(a*a);
        b=b>>1;
    }
    return Ans;
}

int main()
{
    Start;

    int t;
    cin>>t;
    while(t--)
    {
        ll n,num=0,Ans=1e12;
        cin>>n;
        int x=n;

        while(1)
        {
            n=x;
            for(int i=15; i>=0; i--)
            {
                num=Fastpow(3,i);
                if(num<=n)
                {
                    while(num)
                    {
                        if(n<num)
                            num/=3;
                        else
                        {
                            n-=num;
                            num/=3;
                        }
                    }
                    break;
                }
                else Ans=min(Ans,num);
            }
            if(n==0)
            {
                Ans=x;
                break;
            }
            else
                x++;
        }
        cout<<Ans<<"\n";
    }

}


#include <bits/stdc++.h>
#define ll long long
#define Start ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define fr first
#define sc second
#define PI 3.14159265
using namespace std;
ll Fastpow(ll a,ll b)
{
    ll Ans=1;
    while(b>0)
    {
        if(b&1)
            Ans=(Ans*a);
        a=(a*a);
        b=b>>1;
    }
    return Ans;
}

int main()
{
    Start;

    int t;
    cin>>t;
    while(t--)
    {
        ll n,num=0,Ans=1e12;
        cin>>n;
        int x=n;
        for(int i=15; i>=0; i--)
        {
            num=Fastpow(3,i);

            if(num<=n)
            {
                while(num)
                {
                    if(n<num)
                        num/=3;
                    else{
                    n-=num;
                    num/=3;}
                }
                break;
            }
            else Ans=min(Ans,num);

        }
        if(n!=0)
            cout<<Ans<<"\n";
        else
            cout<<x<<"\n";
    }

}


#include <bits/stdc++.h>
#define ll long long
#define Start ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define fr first
#define sc second
#define PI 3.14159265
using namespace std;

int main()
{
    Start;

    int t;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        ll x;
        bool ok=false;
        while(!ok)
        {
            x=n;
            while(x)
            {
                if(x%3==2)
                    break;
                x/=3;

            }
            if(x==0)
                ok=true;
            else
                n++;
        }
        cout<<n<<"\n";
    }

}


// Isaunoya
#include<bits/stdc++.h>
using namespace std ;
using LL = long long ;
using uint = unsigned int ;
#define int long long
#define fir first
#define sec second
#define pb push_back
#define mp(x , y) make_pair(x , y)
template < typename T > inline void read(T & x) { x = 0 ; int f = 1 ; register char c = getchar() ;
  for( ; ! isdigit(c) ; c = getchar()) if(c == '-') f = -1 ;
  for( ; isdigit(c) ; c = getchar()) x = (x << 1) + (x << 3) + (c & 15) ;
  x *= f ;
}
template < typename T > inline void print(T x) {
  if(! x) { putchar('0') ; return ; }
  static int st[105] ;
  if(x < 0) putchar('-') , x = -x ;
  int tp = 0 ;
  while(x) st[++ tp] = x % 10 , x /= 10 ;
  while(tp) putchar(st[tp --] + '0') ;
}
template < typename T > inline void print(T x , char c) { print(x) ; putchar(c) ; }
template < typename T , typename ...Args > inline void read(T & x , Args & ...args) { read(x) ; read(args...) ; }
template < typename T > inline void sort( vector < T > & v) { sort(v.begin() , v.end()) ; return ; }
template < typename T > inline void unique( vector < T > & v) { sort(v) ; v.erase(unique(v.begin() , v.end()) , v.end()) ; }
template < typename T > inline void cmax(T & x , T y) { if(x < y) x = y ; return ; }
template < typename T > inline void cmin(T & x , T y) { if(x > y) x = y ; return ; }
const int Mod = LLONG_MAX ;
inline int QP(int x , int y) { int ans = 1 ;
  for( ; y ; y >>= 1 , x = (x * x) % Mod)
    if(y & 1) ans = (ans * x) % Mod ;
  return ans ;
}
template < typename T > inline T gcd(T x , T y) { if(y == 0) return x ; return gcd(y , x % y) ; }
template < typename T > inline T lcm(T x , T y) { return x * y / gcd(x , y) ; }
template < typename T > inline void mul(T & x , T y) { x = 1LL * x * y ; if(x >= Mod) x %= Mod ; }
template < typename T > inline void add(T & x , T y) { if((x += y) >= Mod) x -= Mod ; }
template < typename T > inline void sub(T & x , T y) { if((x -= y) < 0) x += Mod ; }

int q ;
int pw[64] ;
signed main() {
  read(q) ;
  pw[1] = 1 ;
  for(int i = 2 ; i <= 61 ; i ++) pw[i] = pw[i - 1] * 3 ;
  while(q --) {
    int n ; read(n) ;
    int ans = 0 ;
    int cnt = 0 ;
    while(ans < n) { ans += pw[++ cnt] ; }
    int cnt2 = 0 ;
    while(1) { if(ans - pw[++ cnt2] >= n) ans -= pw[cnt2] ; else break ;}
    print(ans , '\n') ;
  }
  return 0 ;
}


// Isaunoya
#include<bits/stdc++.h>
using namespace std ;
using LL = long long ;
using uint = unsigned int ;
#define int long long
#define fir first
#define sec second
#define pb push_back
#define mp(x , y) make_pair(x , y)
template < typename T > inline void read(T & x) { x = 0 ; int f = 1 ; register char c = getchar() ;
  for( ; ! isdigit(c) ; c = getchar()) if(c == '-') f = -1 ;
  for( ; isdigit(c) ; c = getchar()) x = (x << 1) + (x << 3) + (c & 15) ;
  x *= f ;
}
template < typename T > inline void print(T x) {
  if(! x) { putchar('0') ; return ; }
  static int st[105] ;
  if(x < 0) putchar('-') , x = -x ;
  int tp = 0 ;
  while(x) st[++ tp] = x % 10 , x /= 10 ;
  while(tp) putchar(st[tp --] + '0') ;
}
template < typename T > inline void print(T x , char c) { print(x) ; putchar(c) ; }
template < typename T , typename ...Args > inline void read(T & x , Args & ...args) { read(x) ; read(args...) ; }
template < typename T > inline void sort( vector < T > & v) { sort(v.begin() , v.end()) ; return ; }
template < typename T > inline void unique( vector < T > & v) { sort(v) ; v.erase(unique(v.begin() , v.end()) , v.end()) ; }
template < typename T > inline void cmax(T & x , T y) { if(x < y) x = y ; return ; }
template < typename T > inline void cmin(T & x , T y) { if(x > y) x = y ; return ; }
const int Mod = LLONG_MAX ;
inline int QP(int x , int y) { int ans = 1 ;
  for( ; y ; y >>= 1 , x = (x * x) % Mod)
    if(y & 1) ans = (ans * x) % Mod ;
  return ans ;
}
template < typename T > inline T gcd(T x , T y) { if(y == 0) return x ; return gcd(y , x % y) ; }
template < typename T > inline T lcm(T x , T y) { return x * y / gcd(x , y) ; }
template < typename T > inline void mul(T & x , T y) { x = 1LL * x * y ; if(x >= Mod) x %= Mod ; }
template < typename T > inline void add(T & x , T y) { if((x += y) >= Mod) x -= Mod ; }
template < typename T > inline void sub(T & x , T y) { if((x -= y) < 0) x += Mod ; }

int q ;
int pw[64] ;
signed main() {
  read(q) ;
  pw[1] = 1 ;
  for(int i = 2 ; i <= 61 ; i ++) pw[i] = pw[i - 1] * 3 ;
  while(q --) {
    int n ; read(n) ;
    int ans = 0 ;
    int cnt = 0 ;
    while(1) { if(ans < n) ans += pw[++ cnt] ; else break ; }
    int cnt2 = cnt ;
    while(1) { if(ans - pw[-- cnt2] >= n && cnt2 >= 1) ans -= pw[cnt2] ; else break ; }
    print(ans , '\n') ;
  }
  return 0 ;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
typedef long long LL;

bool cant[1010][1010];

bool can(int p, int n)
{
    for (int i = 1; i <= n; ++i)
    {
        if (i == p)
            continue;
        if (cant[p][i])
            return 0;
    }
    return 1;
}

int main()
{
    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);
    int n, m;
    scanf("%d%d", &n, &m);
    int i, j;
    int a, b;
    for (i = 0; i < m; ++i)
    {
        scanf("%d%d", &a, &b);
        cant[a][b] = cant[b][a] = 1;
    }
    int point;
    for (i = 1; i <= n; ++i)
    {
        if (can(i, n))
            break;
    }
    point = i;
    printf("%d\n", n - 1);
    for (i = 1; i <= n; ++i)
    {
        if (i == point)
            continue;
        else
            printf("%d %d\n", point, i);
    }
    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <utility>
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <iterator>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <numeric>
#include <limits>
#include <cmath>

using namespace std;
#define FOR(i,n) for(size_t i=0;i<(size_t)(n);i++)
#define REP(i,n,m) for(size_t i=(size_t)(n);i<(size_t)(m);i++)
#define REPR(i,n,m) for(size_t i=(size_t)(n);i>(size_t)(m);i--)
#define MF(i,val) memset(i,val,sizeof(i))
#define MP(a,b) make_pair((a),(b))
typedef  pair<int,int> ipair;
typedef long long int64;
typedef unsigned long long uint64;
template<class T> T min_val(T val,T&minVal){return minVal=min(val,minVal);}
template<class T> T max_val(T val,T&maxVal){return maxVal=max(val,maxVal);}

const int MAX_N=1001;

int edges[MAX_N][2];
int nodes[MAX_N];

int main(int argc,char** argv){
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
#endif // ONLINE_JUDGE
	int n,m;
	cin>>n>>m;
	MF(nodes,0);
	FOR(i,m){
		cin>>edges[i][0]>>edges[i][1];
		nodes[edges[i][0]-1]=1;
		nodes[edges[i][1]-1]=1;
	}
	int root;
	FOR(i,n)if(nodes[i]==0){
		root=i;
		break;
	}
	cout<<n-1<<endl;
	FOR(i,n)if(i!=root){
		printf("%d %d\n",root+1,i+1);
	}
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <utility>
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <iterator>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <numeric>
#include <limits>
#include <cmath>

using namespace std;
#define FOR(i,n) for(size_t i=0;i<(size_t)(n);i++)
#define REP(i,n,m) for(size_t i=(size_t)(n);i<(size_t)(m);i++)
#define REPR(i,n,m) for(size_t i=(size_t)(n);i>(size_t)(m);i--)
#define MF(i,val) memset(i,val,sizeof(i))
#define MP(a,b) make_pair((a),(b))
typedef  pair<int,int> ipair;
typedef long long int64;
typedef unsigned long long uint64;
template<class T> T min_val(T val,T&minVal){return minVal=min(val,minVal);}
template<class T> T max_val(T val,T&maxVal){return maxVal=max(val,maxVal);}

const int MAX_N=1001;

int edges[MAX_N][2];
int nodes[MAX_N};

int main(int argc,char** argv){
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
#endif // ONLINE_JUDGE
	int n,m;
	cin>>n>>m;
	MF(nodes,0);
	FOR(i,m){
		cin>>edges[i][0]>>edges[i][1];
		nodes[edges[i][0]-1]=1;
		nodes[edges[i][1]-1]=1;
	}
	int root;
	FOR(i,n)if(nodes[i]==0){
		root=i;
		break;
	}
	cout<<n-1<<endl;
	FOR(i,n)if(i!=root){
		printf("%d %d\n",root+!,i+1);
	}
}


#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <string>
#include <math.h>
#include <set>
#include <stack>
#include <iomanip>

#define PI 3.14159265358979323846;

using namespace std;

	stack <int> que;
	vector<vector<int>> v;
	vector<pair<int,bool>> cost;
	int n,k;

void main()
{
	//ifstream inp("input.txt");
	//cin.rdbuf(inp.rdbuf());
	int n,m;
	vector <bool> v;
	cin>>n>>m;
	v.resize(n);
	for (int i=0;i<m;i++)
	{
		int q,w;
		cin>>q>>w;
		v[q-1]=true;
		v[w-1]=true;
	}
	int j=0;
	while (v[j]!=false)
		j++;
	cout<<n-1<<endl;
	for (int i=0;i<n;i++)
	{
		if (i!=j)
			cout<<i+1<<' '<<j+1<<endl;
	}
}


#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <string>
#include <math.h>
#include <set>
#include <stack>
#include <iomanip>

#define PI 3.14159265358979323846;

using namespace std;

	stack <int> que;
	vector<vector<int>> v;
	vector<pair<int,bool>> cost;
	int n,k;

void main()
{
	//ifstream inp("input.txt");
	//cin.rdbuf(inp.rdbuf());
	int n,m;
	vector <bool> v;
	cin>>n>>m;
	v.resize(n);
	for (int i=0;i<m;i++)
	{
		int q,w;
		cin>>q>>w;
		v[q-1]=true;
		v[w-1]=true;
	}
	int j=0;
	while (v[j]!=false)
		j++;
	for (int i=0;i<n;i++)
	{
		if (i!=j)
			cout<<i+1<<' '<<j+1<<endl;
	}
}


#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <stack>
#include <list>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define Pi 3.1415926535897932384626433832795
#define sqr(x) (x)*(x)
#define forn(i,l,r) for(int i = l; i < r; ++i)
#define fn(i,l,r) for(int i = l; i <= r; ++i)
const int inf = 2147483647;
const ll llinf = 9223372036854775807LL;
/* ifstream in; in.open("input.txt");
 ofstream out; out.open("output.txt");*/
/*#ifndef ONLINE_JUDGE
	freopen("path.in", "r", stdin);
	freopen("path.out", "w", stdout);
#endif*/

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n,m;
	cin >> n >> m;
	vector <vector <int> > a(n,vector <int> (n,inf));

	int numCity=0;
	for (int i=0;i<m;i++)
	{
		int x,y;
		cin >> x>> y;
		a[x-1][y-1]=-1;
		a[y-1][x-1]=-1;
	}

	vector <bool> city(n);
	vector <pair<int,int> > ans;
	queue <pair <pair<int,int >,int > > q;
	q.push(make_pair(make_pair(0,0),0));numCity++;
	while (numCity<=n)
	{
		int i=q.front().first.first;
		int j=q.front().first.second;
		int k=q.front().second;
		q.pop();if (i!=j) ans.push_back(make_pair(i+1,j+1));
		if (k>2 || city[j]) continue;
		city[j]=1;numCity++;
		for (int x=0;x<n;x++)
			if (!city[x] && a[j][x]!=-1)
				q.push(make_pair(make_pair(j,x),k+1));
	}
	cout << ans.size() << endl;
	for (int i=0;i<ans.size();i++)
		cout << ans[i].first << " " << ans[i].second << endl;
return 0;
}


#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <stack>
#include <list>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define Pi 3.1415926535897932384626433832795
#define sqr(x) (x)*(x)
#define forn(i,l,r) for(int i = l; i < r; ++i)
#define fn(i,l,r) for(int i = l; i <= r; ++i)
const int inf = 2147483647;
const ll llinf = 9223372036854775807LL;
/* ifstream in; in.open("input.txt");
 ofstream out; out.open("output.txt");*/
/*#ifndef ONLINE_JUDGE
	freopen("path.in", "r", stdin);
	freopen("path.out", "w", stdout);
#endif*/

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n,m;
	cin >> n >> m;
	vector <vector <int> > a(n,vector <int> (n,inf));

	int numCity=0;
	for (int i=0;i<m;i++)
	{
		int x,y;
		cin >> x>> y;
		a[x-1][y-1]=-1;
		a[y-1][x-1]=-1;
	}
/*
	vector <bool> city(n);
	vector <pair<int,int> > ans;
	queue <pair <pair<int,int >,int > > q;
	q.push(make_pair(make_pair(0,0),0));numCity++;
	while (numCity<=n)
	{
		int i=q.front().first.first;
		int j=q.front().first.second;
		int k=q.front().second;
		q.pop();if (i!=j) ans.push_back(make_pair(i+1,j+1));
		if (k>2 || city[j]) continue;
		city[j]=1;numCity++;
		for (int x=0;x<n;x++)
			if (!city[x] && a[j][x]!=-1)
				q.push(make_pair(make_pair(j,x),k+1));
	}*/
	for (int i=0;i<n;i++)
	{bool fl=true;
		for (int j=0;j<n;j++)
			if (a[i][j]!=inf) { fl=false;break;}
		if (fl)
		{
			cout << n-1 << endl;
			for (int j=0;j<n;j++)
				if (i!=j)
				cout << i+1 << " " << j+1 << endl;
			return 0;
		}
	}

return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>

using namespace std;

#define all(v)         v.begin(),v.end()
#define sz(V)        ((int)((v).size()))
#define ck(a)               (a<1 || a>9)
#define lop(i,n)    for(int i=0;i<n;i++)
#define loop(i,f,l) for(int i=f;i<l;i++)
#define READ(s)   freopen(s, "r", stdin)
#define WRITE(s) freopen(s, "w", stdout)
#define INF                   1000000000
#define PI             3.141592653589793

typedef long long         ll;
typedef pair <int, int>   ii;
typedef pair<string, int> si;
typedef pair<int, string> is;
typedef pair<char, int>  chi;
typedef vector<ii>       vii;
typedef vector<int>       vi;
typedef vector<vi>       vvi;
typedef vector<string>    vs;
typedef vector<ll>       vll;
typedef set<int>         sti;
int arr[1005];
int main()
{
    int req;

    int n,m;    cin>>n>>m;
    lop(i,m)
    {
        int x,y;    cin>>x>>y;
        arr[x]=1,arr[y]=1;
    }
    cout<<n-1<<endl;4
    for(int i=1;i<=n;i++)
        if(!arr[i]) {req=i; break;}

    for(int i=1;i<=n;i++)
        if(i!=req)  cout<<i<<' '<<req<<endl;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>

using namespace std;

#define all(v)         v.begin(),v.end()
#define sz(V)        ((int)((v).size()))
#define ck(a)               (a<1 || a>9)
#define lop(i,n)    for(int i=0;i<n;i++)
#define loop(i,f,l) for(int i=f;i<l;i++)
#define READ(s)   freopen(s, "r", stdin)
#define WRITE(s) freopen(s, "w", stdout)
#define INF                   1000000000
#define PI             3.141592653589793

typedef long long         ll;
typedef pair <int, int>   ii;
typedef pair<string, int> si;
typedef pair<int, string> is;
typedef pair<char, int>  chi;
typedef vector<ii>       vii;
typedef vector<int>       vi;
typedef vector<vi>       vvi;
typedef vector<string>    vs;
typedef vector<ll>       vll;
typedef set<int>         sti;
int arr[1005];
int main()
{
    int req;

    int n,m;    cin>>n>>m;
    lop(i,m)
    {
        int x,y;    cin>>x>>y;
        arr[x]=1,arr[y]=1;
    }
    for(int i=1;i<=n;i++)
        if(!arr[i]) {req=i; break;}

    for(int i=1;i<=n;i++)
        if(i!=req)  cout<<i<<' '<<req<<endl;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>

using namespace std;

#define all(v)         v.begin(),v.end()
#define sz(V)        ((int)((v).size()))
#define ck(a)               (a<1 || a>9)
#define lop(i,n)    for(int i=0;i<n;i++)
#define loop(i,f,l) for(int i=f;i<l;i++)
#define READ(s)   freopen(s, "r", stdin)
#define WRITE(s) freopen(s, "w", stdout)
#define INF                   1000000000
#define PI             3.141592653589793

typedef long long         ll;
typedef pair <int, int>   ii;
typedef pair<string, int> si;
typedef pair<int, string> is;
typedef pair<char, int>  chi;
typedef vector<ii>       vii;
typedef vector<int>       vi;
typedef vector<vi>       vvi;
typedef vector<string>    vs;
typedef vector<ll>       vll;
typedef set<int>         sti;
int arr[1005];
int main()
{
    int req;

    int n,m;    cin>>n>>m;
    lop(i,m)
    {
        int x,y;    cin>>x>>y;
        arr[x]=1,arr[y]=1;
    }
    cout<<n-1<<endl;
    for(int i=1;i<=n;i++)
        if(!arr[i]) {req=i; break;}

    for(int i=1;i<=n;i++)
        if(i!=req)  cout<<i<<' '<<req<<endl;

    return 0;
}


# include <iostream>
# include <sstream>
# include <algorithm>
# include <vector>
# include <cstdio>
# include <time.h>
# include <fstream>
# include <cstdlib>
# include <cmath>
using namespace std;

int main()
{
    int n,m;
    vector<vector<int> > g;
    scanf("%d%d",&n,&m);
    g.resize(n+1);
    for(int i=0,t1,t2;i<m;i++){
        scanf("%d%d",&t1,&t2);
        g[t1].push_back(t2);
        g[t2].push_back(t1);
    }
    int ansV=1;
    for(int i=1;i<n;i++){
        if(!g[i].size()){ansV=i;break;}
    }
    printf("%d\n",n-1);
    for(int i=1;i<=n;i++){
        if(ansV!=i)printf("%d %d\n",ansV,i);
    }
}
/*

*/


# include <iostream>
# include <sstream>
# include <algorithm>
# include <vector>
# include <cstdio>
# include <time.h>
# include <fstream>
# include <cstdlib>
# include <cmath>
using namespace std;

int main()
{
    int n,m;
    vector<vector<int> > g;
    scanf("%d%d",&n,&m);
    g.resize(n+1);
    for(int i=0,t1,t2;i<m;i++){
        scanf("%d%d",&t1,&t2);
        g[t1].push_back(t2);
        g[t2].push_back(t1);
    }
    int ansV;
    for(int i=1;i<n;i++){
        if(!g[i].size()){ansV=i;break;}
    }
    printf("%d\n",n-1);
    for(int i=1;i<=n;i++){
        if(ansV!=i)printf("%d %d\n",ansV,i);
    }
}
/*

*/


# include <iostream>
# include <sstream>
# include <algorithm>
# include <vector>
# include <cstdio>
# include <time.h>
# include <fstream>
# include <cstdlib>
# include <cmath>
using namespace std;

int main()
{
    int n,m;
    vector<vector<int> > g;
    scanf("%d%d",&n,&m);
    g.resize(n+1);
    for(int i=0,t1,t2;i<m;i++){
        scanf("%d%d",&t1,&t2);
        g[t1].push_back(t2);
        g[t2].push_back(t1);
    }
    int ansV=1;
    for(int i=1;i<=n;i++){
        if(!g[i].size()){ansV=i;break;}
    }
    printf("%d\n",n-1);
    for(int i=1;i<=n;i++){
        if(ansV!=i)printf("%d %d\n",ansV,i);
    }
}
/*

*/# include <iostream>
# include <sstream>
# include <algorithm>
# include <vector>
# include <cstdio>
# include <time.h>
# include <fstream>
# include <cstdlib>
# include <cmath>
using namespace std;

int main()
{
    int n,m;
    vector<vector<int> > g;
    scanf("%d%d",&n,&m);
    g.resize(n+1);
    for(int i=0,t1,t2;i<m;i++){
        scanf("%d%d",&t1,&t2);
        g[t1].push_back(t2);
        g[t2].push_back(t1);
    }
    int ansV=1;
    for(int i=1;i<n;i++){
        if(!g[i].size()){ansV=i;break;}
    }
    printf("%d\n",n-1);
    for(int i=1;i<=n;i++){
        if(ansV!=i)printf("%d %d\n",ansV,i);
    }
}
/*

*/


# include <iostream>
# include <sstream>
# include <algorithm>
# include <vector>
# include <cstdio>
# include <time.h>
# include <fstream>
# include <cstdlib>
# include <cmath>
using namespace std;

int main()
{
    int n,m;
    vector<vector<int> > g;
    scanf("%d%d",&n,&m);
    g.resize(n+1);
    for(int i=0,t1,t2;i<m;i++){
        scanf("%d%d",&t1,&t2);
        g[t1].push_back(t2);
        g[t2].push_back(t1);
    }
    int ansV=1;
    for(int i=1;i<=n;i++){
        if(!g[i].size()){ansV=i;break;}
    }
    printf("%d\n",n-1);
    for(int i=1;i<=n;i++){
        if(ansV!=i)printf("%d %d\n",ansV,i);
    }
}
/*

*/


#include<iostream>
#include<vector>
#include< queue >
#include<set>

using namespace std;
#define ifor for(int
#define iv vector<int>

vector< iv > gp ;
iv visit , dist ;
int n ;

void BFS () {
	int s = n / 2 - 1 ;
	queue < int > Q ;
	Q . push( s ) ;
	visit[s] = true ;
	dist[s] = 0 ;

	int u ;    dx:
	while( ! Q.empty() ) {
		u = Q.front() , Q.pop() ;
		if( dist[u] == 2 ) goto dx ;

		ifor i= 0 ; i < n ; i++)
			if( gp [u][i] && ! visit[i]  ){
						visit[i] = true , dist[i] = dist[u] + 1 ;
						Q.push( i ) ; cout << u + 1 <<" " << i + 1 <<endl;
			}
	}

}

void outP(int i ){

	cout << n - 1 <<endl;

	ifor j = 0 ; j < n ; j++)
		if ( j != i )
			cout << i + 1<< "  " << j  + 1 <<endl;

}

void main(){

	int m ; cin >> n >> m ;

	set < int > forp ;

	int a,c ;
	while ( m-- ) {
		cin >> a >> c ;
		forp .insert( a - 1 ) ;
		forp.insert( c - 1  ) ;
	}

	ifor i = 0 ; i < n ; i++)
		if( forp.find( i ) == forp.end() ) {
			outP ( i ) ; break ;}

}

/*
void main(){

	int m ; cin >> n >> m ;

	gp = vector<iv>( n , iv( n , 1) ) ;

	visit = iv( n , 0 ) , dist = iv ( n , -1 ) ;

	int a,c ;
	while ( m-- ) {
		cin >> a >> c ;
		gp[c-1][a-1] = gp[a-1][c-1] = 0 ;

	}
	cout<< n - 1 <<endl;
	BFS() ;

}
*/


#include<iostream>
#include<vector>
#include< queue >

using namespace std;
#define ifor for(int
#define iv vector<int>

vector< iv > gp ;
iv visit , dist ;
int n ;

void BFS () {

	queue < int > Q ;
	Q . push( 0 ) ;
	visit[0] = true ;
	dist[0] = 0 ;

	int u ;    dx:
	while( ! Q.empty() ) {
		u = Q.front() , Q.pop() ;
		if( dist[u] == 2 ) goto dx ;

		ifor i= 0 ; i < n ; i++)
			if( gp [u][i] && ! visit[i]  ){
						visit[i] = true , dist[i] = dist[u] + 1 ;
						Q.push( i ) ; cout << u + 1 <<" " << i + 1 <<endl;
			}
	}

}

void main(){

	int m ; cin >> n >> m ;

	gp = vector<iv>( n , iv( n , 1) ) ;

	visit = iv( n , 0 ) , dist = iv ( n , -1 ) ;

	int a,c ;
	while ( m-- ) {
		cin >> a >> c ;
		gp[a-1][c-1] = 0 ;
	}
	cout<< n - 1 <<endl;
	BFS() ;
}


#include<iostream>
#include<vector>
#include< queue >

using namespace std;
#define ifor for(int
#define iv vector<int>

vector< iv > gp ;
iv visit , dist ;
int n ;

void BFS () {
	int s = n / 2 - 1 ;
	queue < int > Q ;
	Q . push( s ) ;
	visit[s] = true ;
	dist[s] = 0 ;

	int u ;    dx:
	while( ! Q.empty() ) {
		u = Q.front() , Q.pop() ;
		if( dist[u] == 1 ) goto dx ;

		ifor i= 0 ; i < n ; i++)
			if( gp [u][i] && ! visit[i]  ){
						visit[i] = true , dist[i] = dist[u] + 1 ;
						Q.push( i ) ; cout << u + 1 <<" " << i + 1 <<endl;
			}
	}

}

void main(){

	int m ; cin >> n >> m ;

	gp = vector<iv>( n , iv( n , 1) ) ;

	visit = iv( n , 0 ) , dist = iv ( n , -1 ) ;

	int a,c ;
	while ( m-- ) {
		cin >> a >> c ;
		gp[c-1][a-1] = gp[a-1][c-1] = 0 ;
	}
	cout<< n - 1 <<endl;
	BFS() ;
}


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

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    char* blocked = (char*) calloc(n, sizeof(char));
    int i;
    for(i = 0; i < m; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        blocked[a-1] = 1;
        blocked[b-1] = 1;
    }
    for(i = 0; i < n; i++) {
        if(blocked[i] == 0) {
            printf("%d\n", n-1);
            int j;
            for(j = 0; j < i; j++) {
                printf("%d %d\n", j+1, i+1);
            }
            for(j = i+1; j < n; j++) {
                printf("%d %d\n", i+1, j+1);
            }
            break;
        }
    }
    return 0;
}


#include<cstdio>
int main()
{
    int n,m;
    int blacklist[500][500];
    int listcount[500]={0,};

    int Allconnect;
    scanf("%d %d",&n,&m);

    for(int i=0;i<m;i++)
    {
        int temp[2];
        scanf("%d %d",&temp[0],&temp[1]);

        if(temp[0]>temp[1])
        {
            blacklist[temp[0]][listcount[temp[0]]]=temp[1];
            listcount[temp[0]]++;
        }
        else
        {
            blacklist[temp[1]][listcount[temp[1]]]=temp[0];
            listcount[temp[1]]++;
        }
    }

    for(int i=0;i<m;i++)
    {
        if(listcount[i]==0)
            Allconnect=i;
    }

    printf("%d",n-1);
    for(int i=0;i<n;i++)
    {
        if(Allconnect==i)continue;
        printf("%d %d",Allconnect,i);
    }

    return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int n,m,a[1000],b[1000],o;
	bool c[1001]={false};
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		cin>>a[i]>>b[i];
		c[a[i]]=true;
		c[b[i]]=true;
	}
	for(int i=1;i<=n;i++)
		if(!c[i])
			o=i;
	cout<<n-1<<endl;
	for(int i=1;i<=n;i++)
		if(i!=o)
			cout<<o<<" "<<i<<endl;
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n,m,a[1000],b[1000],o;
    bool c[1000]={false};
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        cin>>a[i]>>b[i];
        c[a[i]]=true;
        c[b[i]]=true;
    }
    for(int i=1;i<=n;i++)
        if(!c[i])
            o=i;
    cout<<n-1<<endl;
    for(int i=1;i<=n;i++)
        if(i!=o)
            cout<<o<<" "<<i<<endl;
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int n,m,a[1000],b[1000],o;
	bool c[1000]={false};
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		cin>>a[i]>>b[i];
		c[a[i]]=true;
		c[b[i]]=true;
	}
	for(int i=1;i<n;i++)
		if(!c[i])
			o=i;
	cout<<n-1<<endl;
	for(int i=1;i<=n;i++)
		if(i!=o)
			cout<<o<<" "<<i<<endl;
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int n,m,a[1001],b[1001],o;
	bool c[1001]={false};
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		cin>>a[i]>>b[i];
		c[a[i]]=true;
		c[b[i]]=true;
	}
	for(int i=1;i<=n;i++)
		if(!c[i])
			o=i;
	cout<<n-1<<endl;
	for(int i=1;i<=n;i++)
		if(i!=o)
			cout<<o<<" "<<i<<endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<bool> v;
int n,m,x,s,i;
cin>>n>>m;
v.resize(2*n);
for(i=1;i<=m*2;i++){cin>>x;v[x-1]=1;}
s=min_element(v.begin(),v.end())+1-v.begin();
cout<<n-1<<endl;
for(i=1;i<=n;i++){if(i==s)i++;cout<<s<<' '<<i<<endl;}
return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<bool> v;
int n,m,x,s,i;
cin>>n>>m;
v.resize(2*n);
for(i=1;i<=m*2;i++){cin>>x;v[x-1]=1;}
s=min_element(v.begin(),v.end())+1-v.begin();
cout<<n-1<<endl;
for(i=1;i<=n;i++){cout<<s<<' '<<i<<endl;if(i+1==s)i++;}
return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<bool> v;
int n,m,x,s,i;
cin>>n>>m;
v.resize(n);
for(i=1;i<=m*2;i++){cin>>x;v[x-1]=1;}
s=min_element(v.begin(),v.end())+1-v.begin();
if((v[s-1]==1||n==1)&&cout<<0<<endl)exit(0);
cout<<n-1<<endl;
for(i=1;i<=n;i++){if(i!=s)cout<<s<<' '<<i<<endl;}
return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<bool> v;
int n,m,x,s,i;
cin>>n>>m;
v.resize(2*n);
for(i=1;i<=m*2;i++){cin>>x;v[x-1]=1;}
s=min_element(v.begin(),v.end())+1-v.begin();
if((v[s-1]==1||n==1)&&cout<<0<<endl)exit(0);
cout<<n-1<<endl;
for(i=1;i<=n;i++){if(i==s)i++;cout<<s<<' '<<i<<endl;}
return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<bool> v(0);
int n,m,x,s,i;
cin>>n>>m;
v.resize(n);
for(i=1;i<=m*2;i++){cin>>x;v[x-1]=1;}
s=min_element(v.begin(),v.end())+1-v.begin();
cout<<n-1<<endl;
for(i=1;i<=n;i++){if(i!=s)cout<<s<<' '<<i<<endl;}
return 0;
}


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<stdlib.h>
#include <queue>
#include<string>
#include<cstring>
#include<sstream>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<fstream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
int GCDFast(int a,int b){   while(b)b^=a^=b^=a%=b;  return a;   }
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define PB push_back
#define PI acos(-1.0)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define LL long long
#define ALL_BITS ((1 << 31) - 1)
#define NEG_BITS(mask) (mask ^= ALL_BITS)
#define TEST_BIT(mask, i) (mask & (1 << i))
#define ON_BIT(mask, i) (mask |= (1 << i))
#define OFF_BIT(mask, i) (mask &= NEG_BITS(1 << i))

// UP, RIGHT, DOWN, LEFT, UPPER-RIGHT, LOWER-RIGHT, LOWER-LEFT, UPPER-LEFT
int dx[8] = {-1, 0, 1, 0, -1, 1,  1, -1};
int dy[8] = { 0, 1, 0,-1,  1, 1, -1, -1};
#define MOD 1000000007
#define MX 100010
#define pii pair<int,int>
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}//gcd
int GCD(int b, int a)
{
	if(a == 0)	return b;
	return GCD(a, b%a);
}
long long getLcm(long long a,long long b){
	long long c=a/gcd(a,b);
	return b*c;
}
/*
//---------coinChange--DP faster----
long long table[30000+1];
long long coinChangecount(int m, int n )
{
memset(table, 0, sizeof(table));
table[0] = 1;

for(int i=0; i<m; i++)
for(int j=a[i]; j<=n; j++)
table[j] += table[j-a[i]];

return table[n];
}
//----coinChange- using recursion+dp------------
long long dp[6][30005];
long long coin_Change(int coinNumber, int reminder){//how many ways to count this coin

if(dp[coinNumber][reminder])
return dp[coinNumber][reminder];

if(reminder == 0){
dp[coinNumber][reminder] = 1;return dp[coinNumber][reminder];
}
if(coinNumber <0){
dp[coinNumber][reminder] = 0;return dp[coinNumber][reminder];
}
if(reminder<0){
dp[coinNumber][reminder] = 0;return dp[coinNumber][reminder];
}
if(coinNumber<=0 && reminder>0){
dp[coinNumber][reminder] = 0;return dp[coinNumber][reminder];
}

dp[coinNumber][reminder] = coin_Change(coinNumber, reminder - a[coinNumber-1])+ coin_Change(coinNumber-1, reminder);
return dp[coinNumber][reminder];
}
-------------------------------------------
//----------------- LDS+ LIS N^2----------------
for(i = n - 1; i >= 0; i--) {
for( j = i + 1; j < n; j++) {
if(a[i] > a[j] && ( lds[i] < (lds[j]+1)))
lds[i] = lds[j]+1;
}
}
for(i = n - 1; i >= 0; i--) {
for( j = i + 1; j < n; j++) {
if(a[i] < a[j] && ( lis[i] < (lis[j]+1))){
lis[i] = lis[j]+1;
}
}
}
vector<int>List; // LIS list
for(i = n-1; i >=0 && max; i--){
if(max == lis[i]){
List.push_back(a[i]);
max--;
}
}
//----binary search---------------------
int binarySearchLIS(int key,int size){
int low =0,high = size-1,mid,ret=100005,index=-1;
while(low<=high){
mid = (low+high)/2;
if(lis[mid] == key)
return -1;//already found
else if(lis[mid] > key){
if(ret>lis[mid]){
ret = lis[mid];
index = mid;
}
high=mid-1;//high index decreased as the number is already greater than mid
}
else if(lis[mid] < key){
low=mid+1; //key is bigger than the mid number so low index increased
}
}
return index;
}
//----------palindrome-------------
int isPalindrome(string str){
int i,n=str.size(),j,mid=str.size()/2;
for(i=0,j=n-1;i<mid;i++,j--)
if(str[i] != str[j])
return 0;
if(i==mid)
return 1;
return 0;
}
//---------------------binary search with extra condition----------------
pair<int,int>P[15];
int binary_search( int first, int last,int key)
{
while (last >= first)
{
// calculate the midpoint for roughly equal partition
int imid = (first + last)/2;
if(P[imid].first <= key && P[imid].second>= key)
return imid;

else if (P[imid].second < key)
first = imid + 1;
else
last = imid - 1;
}

return -1;// key was not found
}
//--------nlogn list------LIS-------------
int GetCeilIndex(int a[], int T[], int l, int r, int key) {
int m;

while( r - l > 1 ) {
m = l + (r - l)/2;
if( a[T[m]] >= key )
r = m;
else
l = m;
}

return r;
}
int LongestIncreasingSubsequence( int size) {
// Add boundary case, when array size is zero
// Depend on smart pointers

int *tailIndices = new int[size];
int *prevIndices = new int[size];
int len,j,i;

memset(tailIndices, 0, sizeof(tailIndices[0])*size);
memset(prevIndices, 0xFF, sizeof(prevIndices[0])*size);

tailIndices[0] = 0;
prevIndices[0] = -1;
len = 1; // it will always point to empty location
for( int i = 1; i < size; i++ ) {
if( a[i] < a[tailIndices[0]] ) {
// new smallest value
tailIndices[0] = i;
} else if( a[i] > a[tailIndices[len-1]] ) {
// a[i] wants to extend largest subsequence
prevIndices[i] = tailIndices[len-1];
tailIndices[len++] = i;
} else {
// a[i] wants to be a potential condidate of future subsequence
// It will replace ceil value in tailIndices
int pos = GetCeilIndex(a, tailIndices, -1, len-1, a[i]);

prevIndices[i] = tailIndices[pos-1];
tailIndices[pos] = i;
}
}
j=0;
for(i = tailIndices[len-1],j=0; i >= 0; i = prevIndices[i],j++ ){
lisCal[j] = a[i];
}
len = j;

delete[] tailIndices;
delete[] prevIndices;

return len;
}
---------Flood Fill-------------------
#define pii pair<int,int>
//int fx[]={1,-1,0,0,1,1,-1,-1};
//int fy[]={0,0,1,-1,1,-1,1,-1};
int fx[] = {1,-1,0,0}; //not in diagonal
int fy[] = {0,0,1,-1};
int cell[105][105];
int d[105][105],vis[105][105];
int row,col;
bool check(int tx,int ty)
{
if (tx>=0 && tx<row && ty>=0 && ty<col)
return true;
return false;
}
int bfs(int sx,int sy)
{
vis[sx][sy]=1;
int sum = 1;
queue<pii>q;
q.push(pii(sx,sy));
while(!q.empty())
{
pii top=q.front(); q.pop();
for(int k=0;k<4;k++)
{
int tx=top.first+fx[k];
int ty=top.second+fy[k];
if((check(tx,ty)) && (cell[tx][ty]==cell[sx][sy]|| cell[tx][ty]==1) && vis[tx][ty]==0)//
{
vis[tx][ty]=1;
sum++;
//d[tx][ty]=d[top.first][top.second]+1;
q.push(pii(tx,ty));
}
}
}
return sum;
}
*/

//long long dp[105][30005];
long long minimium = 1000005;
int minCoin=0;

/* prime generate
#define N 1000000
bool prime_number[N];
long long prime_generator(long long n)
{
long long i = 0 , x = 0,j = 0,limit;

prime_number[0] = false;
prime_number[1] = false;
for(i = 2 ; i<= n ; i++)
prime_number[i] = true;
limit = (long long)sqrt((double)n);
for(x = 2 ; x <=  limit;x++)
{
if(prime_number[x] == true)
{
for(i = 4 ; i <= n ;i++)
if(prime_number[i] == true & i != x)
if(i % x == 0)
prime_number[i] = false;
}
}
x = 0;
for(i = 2 ; i<= n ; i++)
if(prime_number[i])
x++;//number of primes
return x;
}*/

//int a[200005];

int visit[100005];
vector<int>tree[100005];
vector<int>problem_road[100005];
vector<int>result;

int dfs(int current_vertex,int source_vertex){
	int ret = 0;
	for(int i = 0 ;i <tree[current_vertex].size();i++){
		int new_index = tree[current_vertex][i]; //x =  new index

		if( new_index != source_vertex){

			int y = dfs(new_index,current_vertex); // vertex = previous/source index

			if(y == 0 && problem_road[current_vertex][i] == 1)
			result.push_back(new_index);

		ret+=max(problem_road[current_vertex][i],y);
		}
	}
	return ret;
}
int main()
{
	long long i,m,x,testCase=0,j,k,y,flag,temp;
	long long n;
	long long  sum,max,number,min,p,q,reminder,index;
	string str,S,str1,str2;
	long long l,r,s,sk,t;

	while(cin>>n>>m){
		memset(visit,0,sizeof visit);
		while(m--){
			cin>>x>>y;
			visit[x] = 1;
			visit[y] = 1;
		}
		for(i=1;i<=n;i++){
			if(visit[i] != 1){
				x = i;
			break;
			}
		}
		if(n == 1 ){
		cout<<0<<"\n";
		continue;
		}
		cout<<n-1<<"\n";
		for(i=1;i<=n;i++){
		if(x!=i)
			cout<<x<<" "<<i<<"\n";
		}
	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<stdlib.h>
#include <queue>
#include<string>
#include<cstring>
#include<sstream>
#include<list>
#include<math.h>
#include<map>
#include<set>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<fstream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
int GCDFast(int a,int b){   while(b)b^=a^=b^=a%=b;  return a;   }
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define PB push_back
#define PI acos(-1.0)
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define LL long long
#define ALL_BITS ((1 << 31) - 1)
#define NEG_BITS(mask) (mask ^= ALL_BITS)
#define TEST_BIT(mask, i) (mask & (1 << i))
#define ON_BIT(mask, i) (mask |= (1 << i))
#define OFF_BIT(mask, i) (mask &= NEG_BITS(1 << i))

// UP, RIGHT, DOWN, LEFT, UPPER-RIGHT, LOWER-RIGHT, LOWER-LEFT, UPPER-LEFT
int dx[8] = {-1, 0, 1, 0, -1, 1,  1, -1};
int dy[8] = { 0, 1, 0,-1,  1, 1, -1, -1};
#define MOD 1000000007
#define MX 100010
#define pii pair<int,int>
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}//gcd
int GCD(int b, int a)
{
	if(a == 0)	return b;
	return GCD(a, b%a);
}
long long getLcm(long long a,long long b){
	long long c=a/gcd(a,b);
	return b*c;
}
/*
//---------coinChange--DP faster----
long long table[30000+1];
long long coinChangecount(int m, int n )
{
memset(table, 0, sizeof(table));
table[0] = 1;

for(int i=0; i<m; i++)
for(int j=a[i]; j<=n; j++)
table[j] += table[j-a[i]];

return table[n];
}
//----coinChange- using recursion+dp------------
long long dp[6][30005];
long long coin_Change(int coinNumber, int reminder){//how many ways to count this coin

if(dp[coinNumber][reminder])
return dp[coinNumber][reminder];

if(reminder == 0){
dp[coinNumber][reminder] = 1;return dp[coinNumber][reminder];
}
if(coinNumber <0){
dp[coinNumber][reminder] = 0;return dp[coinNumber][reminder];
}
if(reminder<0){
dp[coinNumber][reminder] = 0;return dp[coinNumber][reminder];
}
if(coinNumber<=0 && reminder>0){
dp[coinNumber][reminder] = 0;return dp[coinNumber][reminder];
}

dp[coinNumber][reminder] = coin_Change(coinNumber, reminder - a[coinNumber-1])+ coin_Change(coinNumber-1, reminder);
return dp[coinNumber][reminder];
}
-------------------------------------------
//----------------- LDS+ LIS N^2----------------
for(i = n - 1; i >= 0; i--) {
for( j = i + 1; j < n; j++) {
if(a[i] > a[j] && ( lds[i] < (lds[j]+1)))
lds[i] = lds[j]+1;
}
}
for(i = n - 1; i >= 0; i--) {
for( j = i + 1; j < n; j++) {
if(a[i] < a[j] && ( lis[i] < (lis[j]+1))){
lis[i] = lis[j]+1;
}
}
}
vector<int>List; // LIS list
for(i = n-1; i >=0 && max; i--){
if(max == lis[i]){
List.push_back(a[i]);
max--;
}
}
//----binary search---------------------
int binarySearchLIS(int key,int size){
int low =0,high = size-1,mid,ret=100005,index=-1;
while(low<=high){
mid = (low+high)/2;
if(lis[mid] == key)
return -1;//already found
else if(lis[mid] > key){
if(ret>lis[mid]){
ret = lis[mid];
index = mid;
}
high=mid-1;//high index decreased as the number is already greater than mid
}
else if(lis[mid] < key){
low=mid+1; //key is bigger than the mid number so low index increased
}
}
return index;
}
//----------palindrome-------------
int isPalindrome(string str){
int i,n=str.size(),j,mid=str.size()/2;
for(i=0,j=n-1;i<mid;i++,j--)
if(str[i] != str[j])
return 0;
if(i==mid)
return 1;
return 0;
}
//---------------------binary search with extra condition----------------
pair<int,int>P[15];
int binary_search( int first, int last,int key)
{
while (last >= first)
{
// calculate the midpoint for roughly equal partition
int imid = (first + last)/2;
if(P[imid].first <= key && P[imid].second>= key)
return imid;

else if (P[imid].second < key)
first = imid + 1;
else
last = imid - 1;
}

return -1;// key was not found
}
//--------nlogn list------LIS-------------
int GetCeilIndex(int a[], int T[], int l, int r, int key) {
int m;

while( r - l > 1 ) {
m = l + (r - l)/2;
if( a[T[m]] >= key )
r = m;
else
l = m;
}

return r;
}
int LongestIncreasingSubsequence( int size) {
// Add boundary case, when array size is zero
// Depend on smart pointers

int *tailIndices = new int[size];
int *prevIndices = new int[size];
int len,j,i;

memset(tailIndices, 0, sizeof(tailIndices[0])*size);
memset(prevIndices, 0xFF, sizeof(prevIndices[0])*size);

tailIndices[0] = 0;
prevIndices[0] = -1;
len = 1; // it will always point to empty location
for( int i = 1; i < size; i++ ) {
if( a[i] < a[tailIndices[0]] ) {
// new smallest value
tailIndices[0] = i;
} else if( a[i] > a[tailIndices[len-1]] ) {
// a[i] wants to extend largest subsequence
prevIndices[i] = tailIndices[len-1];
tailIndices[len++] = i;
} else {
// a[i] wants to be a potential condidate of future subsequence
// It will replace ceil value in tailIndices
int pos = GetCeilIndex(a, tailIndices, -1, len-1, a[i]);

prevIndices[i] = tailIndices[pos-1];
tailIndices[pos] = i;
}
}
j=0;
for(i = tailIndices[len-1],j=0; i >= 0; i = prevIndices[i],j++ ){
lisCal[j] = a[i];
}
len = j;

delete[] tailIndices;
delete[] prevIndices;

return len;
}
---------Flood Fill-------------------
#define pii pair<int,int>
//int fx[]={1,-1,0,0,1,1,-1,-1};
//int fy[]={0,0,1,-1,1,-1,1,-1};
int fx[] = {1,-1,0,0}; //not in diagonal
int fy[] = {0,0,1,-1};
int cell[105][105];
int d[105][105],vis[105][105];
int row,col;
bool check(int tx,int ty)
{
if (tx>=0 && tx<row && ty>=0 && ty<col)
return true;
return false;
}
int bfs(int sx,int sy)
{
vis[sx][sy]=1;
int sum = 1;
queue<pii>q;
q.push(pii(sx,sy));
while(!q.empty())
{
pii top=q.front(); q.pop();
for(int k=0;k<4;k++)
{
int tx=top.first+fx[k];
int ty=top.second+fy[k];
if((check(tx,ty)) && (cell[tx][ty]==cell[sx][sy]|| cell[tx][ty]==1) && vis[tx][ty]==0)//
{
vis[tx][ty]=1;
sum++;
//d[tx][ty]=d[top.first][top.second]+1;
q.push(pii(tx,ty));
}
}
}
return sum;
}
*/

//long long dp[105][30005];
long long minimium = 1000005;
int minCoin=0;

/* prime generate
#define N 1000000
bool prime_number[N];
long long prime_generator(long long n)
{
long long i = 0 , x = 0,j = 0,limit;

prime_number[0] = false;
prime_number[1] = false;
for(i = 2 ; i<= n ; i++)
prime_number[i] = true;
limit = (long long)sqrt((double)n);
for(x = 2 ; x <=  limit;x++)
{
if(prime_number[x] == true)
{
for(i = 4 ; i <= n ;i++)
if(prime_number[i] == true & i != x)
if(i % x == 0)
prime_number[i] = false;
}
}
x = 0;
for(i = 2 ; i<= n ; i++)
if(prime_number[i])
x++;//number of primes
return x;
}*/

//int a[200005];

int visit[100005];
vector<int>tree[100005];
vector<int>problem_road[100005];
vector<int>result;

int dfs(int current_vertex,int source_vertex){
	int ret = 0;
	for(int i = 0 ;i <tree[current_vertex].size();i++){
		int new_index = tree[current_vertex][i]; //x =  new index

		if( new_index != source_vertex){

			int y = dfs(new_index,current_vertex); // vertex = previous/source index

			if(y == 0 && problem_road[current_vertex][i] == 1)
			result.push_back(new_index);

		ret+=max(problem_road[current_vertex][i],y);
		}
	}
	return ret;
}
int main()
{
	long long i,m,x,testCase=0,j,k,y,flag,temp;
	long long n;
	long long  sum,max,number,min,p,q,reminder,index;
	string str,S,str1,str2;
	long long l,r,s,sk,t;

	while(cin>>n>>m){
		memset(visit,0,sizeof visit);
		while(m--){
			cin>>x>>y;
			visit[x] = 1;
			visit[y] = 1;
		}
		for(i=1;i<=n;i++){
			if(visit[i] != 1){
				x = i;
			break;
			}
		}
		if(n == 1){
		cout<<1<<"\n";
		cout<<"1 1"<<"\n";
		continue;
		}
		cout<<n-1<<"\n";
		for(i=1;i<=n;i++){
		if(x!=i)
			cout<<x<<" "<<i<<"\n";
		}
	}
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
  int n,m,a,j=1;
  bool s[1005];
  memset(s,true,sizeof(s));
  cin>>n>>m;
  for(int i=0;i<m*2;i++)
  {
       cin>>a;
       s[a]=false;
  }
  while(!s[j]) j++;
  cout<<n-1<<endl;
 for(int i=1;i<=n;i++)
 {
     if(i==j) continue;
     else cout<<j<<' '<<i<<endl;
 }
 return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
  int n,m,a,b;
  bool s[1005];
   memset(s,true,sizeof(s));
  cin>>n>>m;
  for(int i=0;i<m;i++)
  {
      cin>>a>>b;
      if(s[a]==true)  s[a]=false;
      if(s[b]==true)  s[b]=false;
  }
  int j=1;
  for(j=1;j<1005;j++)
  {
      if(s[j]==true) break;
  }
  cout<<n-1<<endl;
 for(int i=1;i<=n;i++)
 {
     if(i==j) continue;
     else cout<<j<<' '<<i<<endl;
 }
 return 0;

}


#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    int n, m, c1, c2;
    std::vector<int> vec;
    std::cin >> n >> m;
    for (int i = 0; i != m; ++i)
    {
        std::cin >> c1 >> c2;
        vec.push_back(c1);
        vec.push_back(c2);
    }
    int ii;
    for (ii = 1; ii <= n; ++ii)
    {
        if (vec.end() == std::find(vec.begin(), vec.end(), ii))
            break;
    }
    std::cout << n - 1 << std::endl;
    for (int j = 1; j <= n; ++j)
    {
        if (ii != j)
        {
            std::cout << ii << " " << j << std::endl;
        }
    }
    return 0;
}


#include <iostream>
#include <vector>

int main()
{
    int n, m, c1, c2;
    std::vector<int> vec;
    std::cin >> n >> m;
    for (int i = 0; i != m; ++i)
    {
        std::cin >> c1 >> c2;
        vec.push_back(c1);
        vec.push_back(c2);
    }
    int ii;
    for (ii = 1; ii <= n; ++ii)
    {
        if (vec.end() == std::find(vec.begin(), vec.end(), ii))
            break;
    }
    for (int j = 1; j <= n; ++j)
    {
        if (ii != j)
        {
            std::cout << ii << " " << j << std::endl;
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    int n, m, c1, c2;
    std::vector<int> vec;
    std::cin >> n >> m;
    for (int i = 0; i != m; ++i)
    {
        std::cin >> c1 >> c2;
        vec.push_back(c1);
        vec.push_back(c2);
    }
    int ii;
    for (ii = 1; ii <= n; ++ii)
    {
        if (vec.end() == std::find(vec.begin(), vec.end(), ii))
            break;
    }
    for (int j = 1; j <= n; ++j)
    {
        if (ii != j)
        {
            std::cout << ii << " " << j << std::endl;
        }
    }
    return 0;
}


#include <iostream>
#include <vector>

int main()
{
    int n, m, c1, c2;
    std::vector<int> vec;
    std::cin >> n >> m;
    for (int i = 0; i != m; ++i)
    {
        std::cin >> c1 >> c2;
        vec.push_back(c1);
        vec.push_back(c2);
    }
    int i;
    for (i = 1; i <= n; ++i)
    {
        if (vec.end() == std::find(vec.begin(), vec.end(), i))
            break;
    }
    for (int j = 1; j <= n; ++j)
    {
        if (i != j)
        {
            std::cout << i << " " << j << std::endl;
        }
    }
    return 0;
}