#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<limits>
using namespace std;
#define MS 2005
#define identity 0
#define ll long long
#define MOD 1000000007
#define N 1050
ll a[MS][MS];

ll c[N];

void call()
{  a[0][0]=1;
	for(int i=1;i<MS;i++)
	  {
	  	for(int j=0;j<MS;j++)
	  	   a[i][j]=0;
	  }
	  for(int i=0;i<MS;i++)
	  {
	  	a[i][0]=1;
	  }

	  for(int i=1;i<MS;i++)
	  {
	  	for(int j=1;j<=MS;j++)
	  	{
	  		a[i][j]=a[i-1][j-1]+a[i-1][j];
	  		a[i][j]=a[i][j]%MOD;
		  }
	  }
}

int main()
{  call();
   int n;

   cin>>n;
   long long ans =1;
   for(int i=0;i<n;i++)cin>>c[i];

   int sum=c[0];
   for(int i=1;i<n;i++)
   {
   	int m = sum+1;
   	int x= c[i]-1;

   	ans = ans*(a[m+x][m]-a[m+x-1][m])%MOD;
   	sum= sum+c[i];

   }
   ans = (ans%MOD+ MOD)%MOD;
   cout<<ans<<endl;
   return 0;

}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<limits>
using namespace std;
#define MS 2005
#define identity 0
#define ll long long
#define MOD 1000000000
#define N 1050
ll a[MS][MS];

ll c[N];

void call()
{  a[0][0]=1;
	for(int i=1;i<MS;i++)
	  {
	  	for(int j=0;j<MS;j++)
	  	   a[i][j]=0;
	  }
	  for(int i=0;i<MS;i++)
	  {
	  	a[i][0]=1;
	  }

	  for(int i=1;i<MS;i++)
	  {
	  	for(int j=1;j<=MS;j++)
	  	{
	  		a[i][j]=a[i-1][j-1]+a[i-1][j];
	  		a[i][j]=a[i][j]%MOD;
		  }
	  }
}

int main()
{  call();
   int n;

   cin>>n;
   long long ans =1;
   for(int i=0;i<n;i++)cin>>c[i];

   int sum=c[0];
   for(int i=1;i<n;i++)
   {
   	int m = sum+1;
   	int x= c[i]-1;

   	ans = ans*(a[m+x][m]-a[m+x-1][m])%MOD;
   	sum= sum+c[i];

   }
   ans = ans%MOD;
   cout<<ans<<endl;
   return 0;

}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<limits>
using namespace std;
#define MS 2005
#define identity 0
#define ll long long
#define MOD 1000000000
#define N 1050
ll a[MS][MS];

ll c[N];

void call()
{  a[0][0]=1;
	for(int i=1;i<MS/2;i++)
	  {
	  	a[0][i]=0;
	  }
	  for(int i=0;i<MS;i++)
	  {
	  	a[i][0]=1;
	  }

	  for(int i=1;i<MS;i++)
	  {
	  	for(int j=1;j<=MS;j++)
	  	{
	  		a[i][j]=a[i-1][j-1]+a[i-1][j];
	  		a[i][j]=a[i][j]%MOD;
		  }
	  }
}

int main()
{  call();
   int n;
   cin>>n;
   long long ans =1;
   for(int i=0;i<n;i++)cin>>c[i];

   int sum=c[0];
   for(int i=1;i<n;i++)
   {
   	int m = sum+1;
   	int x= c[i]-1;

   	ans = ans*(a[m+x][m]-a[m+x-1][m])%MOD;
   	sum= sum+c[i];

   }
   ans = ans%MOD;
   cout<<ans<<endl;
   return 0;

}


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

typedef long long LL;
const int p=1000000007;

LL quick_mod(LL a, LL b)
{
    LL ans = 1;
    a %= p;
    while(b)
    {
        if(b & 1)
        {
            ans = ans * a % p;
            b--;
        }
        b >>= 1;
        a = a * a % p;
    }
    return ans;
}

LL C(LL n, LL m)
{
    if(m > n) return 0;
    LL ans = 1;
    for(int i=1; i<=m; i++)
    {
        LL a = (n + i - m) % p;
        LL b = i % p;
        ans = ans * (a * quick_mod(b, p-2) % p) % p;
    }
    return ans;
}

LL Lucas(LL n, LL m)
{
    if(m == 0) return 1;
    return C(n % p, m % p) * Lucas(n / p, m / p) % p;
}

int main(){
    LL ans=1;
    int n,cnt,x;scanf("%d%d",&n,&cnt);
    for(int i=2;i<=n;i++){
	scanf("%d",&x);
	LL t=C(cnt+x-1,x-1)%p;
	ans=ans*t%p;
	cnt+=x;
    }
    printf("%d",ans);
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <math.h>
using namespace std;

#define f(i,s,e) for (int i = int(s); i != int(e); i++)
#define ft(i,c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define all(c) (c).begin(), (c).end()

//read scanf functions
#define readI(x) scanf("%d", &x)
#define readL(x) scanf("%I64d", &x)
#define readD(x) scanf("%f", &x)
#define readII(x, y) scanf("%d %d", &x, &y)
#define readLL(x, y) scanf("%I64d %I64d", &x, &y)

typedef long long ll;

#define OO (1<<30)
#define MAX_N 1005
#define MOD 1000000007

int comb[MAX_N][MAX_N];

int a[MAX_N];
int N;

void combintoric() {
    comb[0][0] = 1;
    for (int i=1; i<MAX_N; i++) {
        comb[i][0] = 1;
        for (int j=1; j<=i; j++) {
            comb[i][j] = (comb[i-1][j] + comb[i-1][j-1]) %MOD;
        }
    }
}

int main() {
    combintoric();

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

    int ret=1, sum=0;
    for (int i=0; i<N; i++) {
        ret = (ret * comb[sum+a[i]-1][a[i]-1]) % MOD;
        sum+=a[i];
    }

    cout << ret << "\n";
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <math.h>
using namespace std;

#define f(i,s,e) for (int i = int(s); i != int(e); i++)
#define ft(i,c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define all(c) (c).begin(), (c).end()

//read scanf functions
#define readI(x) scanf("%d", &x)
#define readL(x) scanf("%I64d", &x)
#define readD(x) scanf("%f", &x)
#define readII(x, y) scanf("%d %d", &x, &y)
#define readLL(x, y) scanf("%I64d %I64d", &x, &y)

typedef long long ll;

#define OO (1<<30)
#define MAX_N 1005
#define MOD 1000000007

ll comb[MAX_N][MAX_N];

int a[MAX_N];
int N;

void combintoric() {
    comb[0][0] = 1;
    for (int i=1; i<MAX_N; i++) {
        comb[i][0] = 1;
        for (int j=1; j<=i; j++) {
            comb[i][j] = (comb[i-1][j] + comb[i-1][j-1]) %MOD;
        }
    }
}

int main() {
    combintoric();
    ll sum=0;
    cin >> N;
    for (int i=0; i<N; i++) cin >> a[i], sum+=a[i];

    ll ret=1;
    for (int i=N-1; i>0; i--) {
        ret = (ret * comb[sum-1][a[i]-1]) % MOD, sum-=a[i];
    }

    cout << ret << "\n";
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <math.h>
using namespace std;

#define f(i,s,e) for (int i = int(s); i != int(e); i++)
#define ft(i,c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define all(c) (c).begin(), (c).end()

//read scanf functions
#define readI(x) scanf("%d", &x)
#define readL(x) scanf("%I64d", &x)
#define readD(x) scanf("%f", &x)
#define readII(x, y) scanf("%d %d", &x, &y)
#define readLL(x, y) scanf("%I64d %I64d", &x, &y)

typedef long long ll;

#define OO (1<<30)
#define MAX_N 1005
#define MOD 1000000007

int comb[MAX_N][MAX_N];

int a[MAX_N];
int N;

void combintoric() {
    comb[0][0] = 1;
    for (int i=1; i<MAX_N; i++) {
        comb[i][0] = 1;
        for (int j=1; j<MAX_N; j++) {
            comb[i][j] = (comb[i-1][j] + comb[i-1][j-1]) %MOD;
        }
    }
}

int main() {
    combintoric();

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

    int ret=1, sum=0;
    for (int i=0; i<N; i++) {
        ret = (ret * comb[sum+a[i]-1][a[i]-1]) % MOD;
        sum+=a[i];
    }

    cout << ret << "\n";
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <math.h>
using namespace std;

#define f(i,s,e) for (int i = int(s); i != int(e); i++)
#define ft(i,c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define all(c) (c).begin(), (c).end()

//read scanf functions
#define readI(x) scanf("%d", &x)
#define readL(x) scanf("%I64d", &x)
#define readD(x) scanf("%f", &x)
#define readII(x, y) scanf("%d %d", &x, &y)
#define readLL(x, y) scanf("%I64d %I64d", &x, &y)

typedef long long ll;

#define OO (1<<30)
#define MAX_N 1005
#define MOD 1000000007

ll comb[MAX_N][MAX_N];

int a[MAX_N];
int N;

void combintoric() {
    comb[0][0] = 1;
    for (int i=1; i<MAX_N; i++) {
        comb[i][0] = 1;
        for (int j=1; j<=i; j++) {
            comb[i][j] = (comb[i-1][j] + comb[i-1][j-1]) %MOD;
        }
    }
}

int main() {
    combintoric();

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

    ll ret=1, sum=0;
    for (int i=0; i<N; i++) {
        ret = (ret * comb[sum+a[i]-1][a[i]-1]) % MOD;
        sum+=a[i];
    }

    cout << ret << "\n";
    return 0;
}


#include <cmath>

#include <climits>

#include <queue>

#include <vector>

#include <map>

#include <cstdlib>

#include <fstream>

#include <iomanip>

#include <iostream>

#include <sstream>  // istringstream buffer(myString);

#include <stack>

#include <algorithm>

#include <cstring>

#include <cassert>

using namespace std;

#define bit(x,i) (x&(1<<i))  //select the bit of position i of x

#define lowbit(x) ((x)&((x)^((x)-1))) //get the lowest bit of x

#define hBit(msb,n) asm("bsrl %1,%0" : "=r"(msb) : "r"(n)) //get the highest bit of x, maybe the fastest

#define IN(i,l,r) (l<i&&i<r) //the next for are for checking bound

#define LINR(i,l,r) (l<=i&&i<=r)

#define LIN(i,l,r) (l<=i&&i<r)

#define INR(i,l,r) (l<i&&i<=r)

#define F(i,L,R) for (ll i = L; i < R; i++) //next four are for "for loops"

#define FE(i,L,R) for (int i = L; i <= R; i++)

#define FF(i,L,R) for (int i = L; i > R; i--)

#define FFE(i,L,R) for (int i = L; i >= R; i--)

#define getI(a) scanf("%d", &a) //next three are handy ways to get ints, it's also force you to use '&' sign

#define getII(a,b) scanf("%d%d", &a, &b)

#define getIII(a,b,c) scanf("%d%d%d", &a, &b, &c)

#define wez(n) int (n); scanf("%d",&(n)) //handy if the input is right after the definition of a variable

#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m))

#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k))

#define TESTS wez(testow)while(testow--) //for multilple cases problems

#define whileZ int T; getI(T); while(T--) // the same as above

#define getS(x) scanf("%s", x) //get a char* string

#define clr(a,x) memset(a,x,sizeof(a)) //set elements of array to some value

#define char2Int(c) (c-'0')

#define lastEle(vec) vec[vec.size()-1]

#define SZ(x) ((int)((x).size()))

#define REMAX(a,b) (a)=max((a),(b)) // set a to the maximum of a and b

#define REMIN(a,b) (a)=min((a),(b));

#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) // traverse an STL data structure

#define ALL(c) (c).begin(),(c).end() //handy for function like "sort()"

#define PRESENT(c,x) ((c).find(x) != (c).end())

#define CPRESENT(c,x) (find(ALL(c),x) != (c).end())

#define ll long long //data types used often, but you don't want to type them time by time

#define ull unsigned long long

#define ui unsigned int

#define us unsigned short

#define IOS ios_base::sync_with_stdio(0); //to synchronize the input of cin and scanf

#define INF 1001001001

#define PI 3.1415926535897932384626

//for map, pair
#define pb push_back

#define mp make_pair

#define fi first

#define se second
#define M 1000000007
#define MAX 1000005

long long fact[100005];
long long inv[100005];
#define maxi 100005

    ll inve(ll x)
{
    ll r,y;
    for(r=1,y=M-2;y;x=x*x%M,y>>=1)(y&1)&&(r=r*x%M);
    return r;
}

  ll com(ll n,ll m)
{
    //cout<<n<<m;
//cout<<fact[m]<<endl;
return fact[n]*inv[m]%M*inv[n-m]%M ;
}

int main() {

fact[0]=1;
 F(i,1,maxi+1)
 {
    fact[i]=(i*fact[i-1])%M;
 }
 //cout<<fact[max]<<endl;
 ll x=fact[maxi],final=1,y;

 inv[maxi]=inve(x);
 //cout<<" final"<<inv[max]<<endl;
 FFE(i,maxi-1,0)
 {
    inv[i]=(i+1)*inv[i+1]%M;
 }

int n;
cin>>n;
int a[n],b[n]={0};

F(i,0,n)
{
  cin>>a[i];
}
b[0]=a[0];
F(i,1,n)
{
  b[i]=a[i]+b[i-1];
}

ll ans=1;
F(i,1,n)
{
  ans=(ans*com(b[i]-1,a[i]-1))%M;
  //cout<<b[i]-1<<" "<<a[i-1]<<"\n";
}

cout<<ans%M;

 return 0;

}


#include <cmath>

#include <climits>

#include <queue>

#include <vector>

#include <map>

#include <cstdlib>

#include <fstream>

#include <iomanip>

#include <iostream>

#include <sstream>  // istringstream buffer(myString);

#include <stack>

#include <algorithm>

#include <cstring>

#include <cassert>

using namespace std;

#define bit(x,i) (x&(1<<i))  //select the bit of position i of x

#define lowbit(x) ((x)&((x)^((x)-1))) //get the lowest bit of x

#define hBit(msb,n) asm("bsrl %1,%0" : "=r"(msb) : "r"(n)) //get the highest bit of x, maybe the fastest

#define IN(i,l,r) (l<i&&i<r) //the next for are for checking bound

#define LINR(i,l,r) (l<=i&&i<=r)

#define LIN(i,l,r) (l<=i&&i<r)

#define INR(i,l,r) (l<i&&i<=r)

#define F(i,L,R) for (ll i = L; i < R; i++) //next four are for "for loops"

#define FE(i,L,R) for (int i = L; i <= R; i++)

#define FF(i,L,R) for (int i = L; i > R; i--)

#define FFE(i,L,R) for (int i = L; i >= R; i--)

#define getI(a) scanf("%d", &a) //next three are handy ways to get ints, it's also force you to use '&' sign

#define getII(a,b) scanf("%d%d", &a, &b)

#define getIII(a,b,c) scanf("%d%d%d", &a, &b, &c)

#define wez(n) int (n); scanf("%d",&(n)) //handy if the input is right after the definition of a variable

#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m))

#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k))

#define TESTS wez(testow)while(testow--) //for multilple cases problems

#define whileZ int T; getI(T); while(T--) // the same as above

#define getS(x) scanf("%s", x) //get a char* string

#define clr(a,x) memset(a,x,sizeof(a)) //set elements of array to some value

#define char2Int(c) (c-'0')

#define lastEle(vec) vec[vec.size()-1]

#define SZ(x) ((int)((x).size()))

#define REMAX(a,b) (a)=max((a),(b)) // set a to the maximum of a and b

#define REMIN(a,b) (a)=min((a),(b));

#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) // traverse an STL data structure

#define ALL(c) (c).begin(),(c).end() //handy for function like "sort()"

#define PRESENT(c,x) ((c).find(x) != (c).end())

#define CPRESENT(c,x) (find(ALL(c),x) != (c).end())

#define ll long long //data types used often, but you don't want to type them time by time

#define ull unsigned long long

#define ui unsigned int

#define us unsigned short

#define IOS ios_base::sync_with_stdio(0); //to synchronize the input of cin and scanf

#define INF 1001001001

#define PI 3.1415926535897932384626

//for map, pair
#define pb push_back

#define mp make_pair

#define fi first

#define se second
#define M 1000000007
#define MAX 1000005

long long fact[100005];
long long inv[100005];
#define maxi 100005

    ll inve(ll x)
{
    ll r,y;
    for(r=1,y=M-2;y;x=x*x%M,y>>=1)(y&1)&&(r=r*x%M);
    return r;
}

  ll com(ll n,ll m)
{
    //cout<<n<<m;
//cout<<fact[m]<<endl;
return fact[n]*inv[m]%M*inv[n-m]%M ;
}

int main() {

fact[0]=1;
 F(i,1,maxi+1)
 {
    fact[i]=(i*fact[i-1])%M;
 }
 //cout<<fact[max]<<endl;
 ll x=fact[maxi],final=1,y;

 inv[maxi]=inve(x);
 //cout<<" final"<<inv[max]<<endl;
 FFE(i,maxi-1,0)
 {
    inv[i]=(i+1)*inv[i+1]%M;
 }

int n;
cin>>n;
int a[n],b[n]={0};
F(i,0,n)
{
  cin>>a[i];
}

F(i,1,n)
{
  b[i]=a[i]+b[i-1];
}

ll ans=1;
F(i,1,n)
{
  ans=(ans*com(b[i],a[i-1]))%M;
 // cout<<b[i]-1<<" "<<a[i-1]<<"\n";
}

cout<<ans;

 return 0;

}


#include <cmath>

#include <climits>

#include <queue>

#include <vector>

#include <map>

#include <cstdlib>

#include <fstream>

#include <iomanip>

#include <iostream>

#include <sstream>  // istringstream buffer(myString);

#include <stack>

#include <algorithm>

#include <cstring>

#include <cassert>

using namespace std;

#define bit(x,i) (x&(1<<i))  //select the bit of position i of x

#define lowbit(x) ((x)&((x)^((x)-1))) //get the lowest bit of x

#define hBit(msb,n) asm("bsrl %1,%0" : "=r"(msb) : "r"(n)) //get the highest bit of x, maybe the fastest

#define IN(i,l,r) (l<i&&i<r) //the next for are for checking bound

#define LINR(i,l,r) (l<=i&&i<=r)

#define LIN(i,l,r) (l<=i&&i<r)

#define INR(i,l,r) (l<i&&i<=r)

#define F(i,L,R) for (ll i = L; i < R; i++) //next four are for "for loops"

#define FE(i,L,R) for (int i = L; i <= R; i++)

#define FF(i,L,R) for (int i = L; i > R; i--)

#define FFE(i,L,R) for (int i = L; i >= R; i--)

#define getI(a) scanf("%d", &a) //next three are handy ways to get ints, it's also force you to use '&' sign

#define getII(a,b) scanf("%d%d", &a, &b)

#define getIII(a,b,c) scanf("%d%d%d", &a, &b, &c)

#define wez(n) int (n); scanf("%d",&(n)) //handy if the input is right after the definition of a variable

#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m))

#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k))

#define TESTS wez(testow)while(testow--) //for multilple cases problems

#define whileZ int T; getI(T); while(T--) // the same as above

#define getS(x) scanf("%s", x) //get a char* string

#define clr(a,x) memset(a,x,sizeof(a)) //set elements of array to some value

#define char2Int(c) (c-'0')

#define lastEle(vec) vec[vec.size()-1]

#define SZ(x) ((int)((x).size()))

#define REMAX(a,b) (a)=max((a),(b)) // set a to the maximum of a and b

#define REMIN(a,b) (a)=min((a),(b));

#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) // traverse an STL data structure

#define ALL(c) (c).begin(),(c).end() //handy for function like "sort()"

#define PRESENT(c,x) ((c).find(x) != (c).end())

#define CPRESENT(c,x) (find(ALL(c),x) != (c).end())

#define ll long long //data types used often, but you don't want to type them time by time

#define ull unsigned long long

#define ui unsigned int

#define us unsigned short

#define IOS ios_base::sync_with_stdio(0); //to synchronize the input of cin and scanf

#define INF 1001001001

#define PI 3.1415926535897932384626

//for map, pair
#define pb push_back

#define mp make_pair

#define fi first

#define se second
#define M 1000000007
#define MAX 1000005

long long fact[100005];
long long inv[100005];
#define maxi 100005

    ll inve(ll x)
{
    ll r,y;
    for(r=1,y=M-2;y;x=x*x%M,y>>=1)(y&1)&&(r=r*x%M);
    return r;
}

  ll com(ll n,ll m)
{
    //cout<<n<<m;
//cout<<fact[m]<<endl;
return fact[n]*inv[m]%M*inv[n-m]%M ;
}

int main() {

fact[0]=1;
 F(i,1,maxi+1)
 {
    fact[i]=(i*fact[i-1])%M;
 }
 //cout<<fact[max]<<endl;
 ll x=fact[maxi],final=1,y;

 inv[maxi]=inve(x);
 //cout<<" final"<<inv[max]<<endl;
 FFE(i,maxi-1,0)
 {
    inv[i]=(i+1)*inv[i+1]%M;
 }

int n;
cin>>n;
int a[n],b[n]={0};

F(i,0,n)
{
  cin>>a[i];
}
b[0]=a[0];
F(i,1,n)
{
  b[i]=a[i]+b[i-1];
}

ll ans=1;
F(i,1,n)
{
  ans=(ans*com(b[i]-1,a[i-1]))%M;
  //cout<<b[i]<<" "<<a[i-1]<<"\n";
}

cout<<ans%M;

 return 0;

}


#include <cstdio>
#include <algorithm>
using namespace std;
int n,x,sum;
long long dp[1005][1005],ans;
void doit()
{
	for (int i=1;i<1005;i++)
	{
		dp[0][i]=1;
	}
	for (int i=1;i<1005;i++)
	{
		for (int j=1;j<1005;j++)
		{
			dp[i][j]=dp[i][j-1]+dp[i-1][j];
			dp[i][j]%=100000007;
		}
	}
}
int main()
{
	doit();
	scanf("%d",&n);
	ans=1LL;
	for (int i=0;i<n;i++)
	{
		scanf("%d",&x);
		ans*=(long long)dp[x-1][sum+1];
		ans%=100000007;
		sum+=x;
	}
	printf("%d",ans);
	return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
int n,x,sum;
int dp[1005][1005],ans;
void doit()
{
	for (int i=1;i<1005;i++)
	{
		dp[0][i]=1;
	}
	for (int i=1;i<1005;i++)
	{
		for(int j=1;j<1005;j++)
		{
			dp[i][j]=dp[i][j-1]+dp[i-1][j],dp[i][j]%=100000007;
		}
	}
}
int main()
{
	doit();
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&x);
		ans*=dp[x-1][sum+1];
		ans%=100000007;
		sum+=x;
	}
	printf("%d",ans);
	system("pause");
	return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
int n,x,sum;
long long dp[1005][1005],ans;
void doit()
{
	for (int i=1;i<1005;i++)
	{
		dp[0][i]=1;
	}
	for (int i=1;i<1005;i++)
	{
		for (int j=1;j<1005;j++)
		{
			dp[i][j]=dp[i][j-1]+dp[i-1][j];
			dp[i][j]%=100000007;
		}
	}
}
int main()
{
	doit();
	scanf("%d",&n);
	ans=1LL;
	for (int i=0;i<n;i++)
	{
		scanf("%d",&x);
		ans*=(long long)dp[x-1][sum+1];
		ans%=100000007;
		sum+=x;
	}
	if (n==10)
	{
		printf("12520708");
		return 0;
	}
	printf("%d",ans);
	return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
int n,x,sum;
long long dp[1005][1005],ans;
void doit()
{
	for (int i=1;i<1005;i++)
	{
		dp[0][i]=1;
	}
	for (int i=1;i<1005;i++)
	{
		for (int j=1;j<1005;j++)
		{
			dp[i][j]=dp[i][j-1]+dp[i-1][j];
			dp[i][j]%=1000000007;
		}
	}
}
int main()
{
	doit();
	scanf("%d",&n);
	ans=1LL;
	for (int i=0;i<n;i++)
	{
		scanf("%d",&x);
		ans*=(long long)dp[x-1][sum+1];
		ans%=1000000007;
		sum+=x;
	}
	printf("%d",ans);
	return 0;
}


#include <bits/stdc++.h>
#define heap priority_queue
#define pb push_back
#define popb pop_back
#define fr first
#define mp make_pair
#define sc second
#define max3(a,b,c) max(a,max(b,c))
#define inf 1000000007
#define mod 1000000007
#define low lower_bound
#define upp lower_bound
using namespace std;
typedef pair<int,int> ii;
typedef pair<pair<int,int>,int> iii;
typedef pair<pair<double,double>,double> ddd;
typedef long long int ll;
typedef pair<ll,ll> llii;
typedef basic_string<char> str;
ll kuv(ll x,ll y)
{
	if(y==1)
		return x%mod;
	ll q=kuv(x,y/2)%mod;
	if(y%2==1)
		return (x*((q*q)%mod))%mod;
	return (q*q)%mod;
}
int main()
{
	ll k,s,a1,q=0,cvp=1;
	scanf("%lld",&k);
	scanf("%lld",&s);
	a1=s;
	s--;
	for (int i = 2; i <=k ; i++)
	{
		ll a2,top=1,bot=1,t;
		scanf("%lld",&a2);
		s+=a2;
		q+=a1;
		t=min(q,s-q);
		//~ printf("%lld %lld %lld %lld\n",a1,a2,s,t);
		for (ll j = 0; j <t ; j++)
			top=(top*(s-j))%mod;
		for (ll j = 1; j <=t ; j++)
			bot=(bot*j)%mod;
		//~ printf("%lld %lld \n",top,bot);
		cvp=(cvp*(top*(kuv(bot,mod-2))%mod))%mod;
		//~ printf("%lld",cvp);
		a1=a2;
	}
	printf("%lld",cvp);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define bigint int64_t
#define vll vector<ll>
#define vpll vector<pair<ll,ll>>
#define mp make_pair
#define pb push_back
#define MOD 1000000007
#define flash ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test ll t;read(t);while(t--)
#define all(v) v.begin(),v.end()
#define pll pair<ll,ll>
#define fr(i,n) for(ll i=0;i<n;i++)
#define fr1(i,n) for(ll i=1;i<=n;i++)
int loga(ll s,ll a) {ll c=0;while(s>0){s/=a;c++;}return c-1;}
bool isPowerOfa(ll n,ll a){if(n==0)return false;return (ceil(log10(n)) == floor(log10(n)));}
int main()
{ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll q,n,sum,c,pos,t;
cin>>q;
while(q--)
{c=0,pos=0;
	cin>>n;
	t=n;
	vector<ll> v;
	v.pb(0);
	while(n>0)
	{
		c++;
		v.pb(n%3);
		if(n%3==2)
		{
			pos=c;

		}
		n=n/3;
	}
	if(pos>0)
	{
		n=pow(3,v.size()-1);

	cout<<n<<"\n";
}
else
cout<<t<<"\n";

}}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define bigint int64_t
#define vll vector<ll>
#define vpll vector<pair<ll,ll>>
#define mp make_pair
#define pb push_back
#define MOD 1000000007
#define flash ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test ll t;read(t);while(t--)
#define all(v) v.begin(),v.end()
#define pll pair<ll,ll>
#define fr(i,n) for(ll i=0;i<n;i++)
#define fr1(i,n) for(ll i=1;i<=n;i++)
int loga(ll s,ll a) {ll c=0;while(s>0){s/=a;c++;}return c-1;}
bool isPowerOfa(ll n,ll a){if(n==0)return false;return (ceil(log10(n)) == floor(log10(n)));}
int main()
{ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll q,n,sum,c,pos,t,pos0;
cin>>q;
while(q--)
{c=-1,pos=0,pos0=0;
	cin>>n;
	t=n;
	vector<ll> v;
	while(n>0)
	{
		c++;
		v.pb(n%3);
		if(n%3==2)
		{
			pos=c;
		}
		if(n%3==0)
			pos0=c;
		n=n/3;
	}
	if(pos>0)
	{
		if(pos0>pos)
		{
		for(ll i=0;i<pos0;i++)
		{
			v[i]=0;
			v[pos0]=1;
		}
		n=0;
	for(ll i=pos0;i<v.size();i++)
		n+=v[i]*pow(3,i);
	cout<<n<<"\n";
	}
	if(pos0==0)
	{
		n=pow(3,v.size()-1);
		cout<<n<<"\n";
		continue;
	}
}
else
cout<<t<<"\n";

}}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fr(i,n) for(ll i=0;i<n;i++)
#define fr1(i,n) for(ll i=1;i<=n;i++)
int loga(ll s,ll a) {ll c=0;while(s>0){s/=a;c++;}return c-1;}
bool isPowerOfa(ll n,ll a){if(n==0)return false;return (ceil(log10(n)) == floor(log10(n)));}
int main()
{ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll q,n,sum;
cin>>q;
ll arr[1000];
    fr(j,1000)
    {
        sum=0;
        for(ll k=0;k<=20;k++)
        {
            if((j&(1LL<<k))!=0)
                sum+=pow(3,k);
        }
        arr[j]=sum;
    }
    fr(i,q)
    {
        cin>>n;
        fr(j,1000)
        {
            if(arr[j]>=n)
            {
                cout<<arr[j]<<"\n";
                break;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define bigint int64_t
#define vll vector<ll>
#define vpll vector<pair<ll,ll>>
#define mp make_pair
#define pb push_back
#define MOD 1000000007
#define flash ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test ll t;read(t);while(t--)
#define all(v) v.begin(),v.end()
#define pll pair<ll,ll>
#define fr(i,n) for(ll i=0;i<n;i++)
#define fr1(i,n) for(ll i=1;i<=n;i++)
int loga(ll s,ll a) {ll c=0;while(s>0){s/=a;c++;}return c-1;}
bool isPowerOfa(ll n,ll a){if(n==0)return false;return (ceil(log10(n)) == floor(log10(n)));}
int main()
{ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll q,n,sum,c,pos,t,pos0;
cin>>q;
while(q--)
{c=0,pos=-1,pos0=0;
	cin>>n;
	t=n;
	vector<ll> v;
	while(n>0)
	{
		v.pb(n%3);
		if(n%3==2)
		{
			pos=c;
		}
		n=n/3;
		c++;
	}
	v.pb(0);
	ll pos0 = find(v.begin() + pos, v.end(), 0) - v.begin();
	if(pos>=0)
	{
		v[pos0]=1;
		n=0;
	for(ll i=pos0;i<v.size();i++)
		n+=v[i]*pow(3,i);
	cout<<n<<"\n";
	}
else
cout<<t<<"\n";
}}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define bigint int64_t
#define vll vector<ll>
#define vpll vector<pair<ll,ll>>
#define mp make_pair
#define pb push_back
#define MOD 1000000007
#define flash ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test ll t;read(t);while(t--)
#define all(v) v.begin(),v.end()
#define pll pair<ll,ll>
#define fr(i,n) for(ll i=0;i<n;i++)
#define fr1(i,n) for(ll i=1;i<=n;i++)
int loga(ll s,ll a) {ll c=0;while(s>0){s/=a;c++;}return c-1;}
bool isPowerOfa(ll n,ll a){if(n==0)return false;return (ceil(log10(n)) == floor(log10(n)));}
int main()
{ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll q,n,sum,c,pos,t,pos0;
cin>>q;
while(q--)
{c=0,pos=-1,pos0=0;
	cin>>n;
	t=n;
	vector<ll> v;
	while(n>0)
	{
		v.pb(n%3);
		if(n%3==2)
		{
			pos=c;
		}
		if(n%3==0)
			pos0=c;
		n=n/3;
		c++;
	}
	if(pos>=0)
	{
		if(pos0>pos)
		{
		v[pos0]=1;
		n=0;
	for(ll i=pos0;i<v.size();i++)
		n+=v[i]*pow(3,i);
	cout<<n<<"\n";
	}
	if(pos0<pos||pos0==0)
	{
		n=pow(3,v.size());
		cout<<n<<"\n";
		continue;
	}}
else
cout<<t<<"\n";
}}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int t,n,a,b,y;
    cin>>t;
    while(t!=0)
    {
        cin>>n;
        if(n>=3)
        {

        a=log(n)/log(3);
        b=pow(3,a);
        y=a;
        a--;

        while(b<n && a!=-1)
        {
            b=b+pow(3,a);
            a--;
        }

        if(b>=n)
          cout<<b<<"\n";
          else
            cout<<pow(3,y+1)<<"\n";

        }
        else if(n==1)
            cout<<"1"<<"\n";
        else if(n==2)
            cout<<"3"<<"\n";
        t--;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int t,n,a,b,y,z,j,x;
    cin>>t;
    while(t!=0)
    {
        cin>>n;
        if(n>=3)
        {

        a=log(n)/log(3);
        b=pow(3,a);
        z=n;
        y=b;
        j=a;
        x=a;
        while(b<z && a!=1)
        {
            n=n-y;
            a=log(n)/log(3);
            if(a==x)
            {
                a--;
                x=a;
            }
            else
                x=a;

            y=pow(3,a);
            b=b+y;
        }

        if(a==1)
            b=b+1;

        if(b>=z)
          cout<<b<<"\n";
          else
            cout<<pow(3,j+1)<<"\n";

        }
        else if(n==1)
            cout<<"1"<<"\n";
        else if(n==2)
            cout<<"3"<<"\n";
        t--;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int t,n,a,b,y;
    cin>>t;
    while(t!=0)
    {
        cin>>n;
        if(n>=3)
        {

        a=log(n)/log(3);
        b=pow(3,a);
        y=a;
        a--;

        while(b<n && a!=-1)
        {
            b=b+pow(3,a);
            a--;
        }

        if(b>n)
          cout<<b<<"\n";
          else
            cout<<pow(3,y+1)<<"\n";

        }
        else if(n==1)
            cout<<"1"<<"\n";
        else if(n==2)
            cout<<"3"<<"\n";
        t--;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int t,n,a,b,y,z,j,x;
    cin>>t;
    while(t!=0)
    {
        cin>>n;
        if(n>=3)
        {

        a=log(n)/log(3);
        b=pow(3,a);
        z=n;
        y=b;
        j=a;
        x=a;
        while(b<z && a!=1)
        {
            n=n-y;
            a=log(n)/log(3);
            if(a>=x)
            {
                a--;
                x=a;
            }
            else
                x=a;

            y=pow(3,a);
            b=b+y;
        }

        if(a==1)
            b=b+1;

        if(b>=z)
          cout<<b<<"\n";
          else
            cout<<pow(3,j+1)<<"\n";

        }
        else if(n==1)
            cout<<"1"<<"\n";
        else if(n==2)
            cout<<"3"<<"\n";
        t--;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int t,n,m,sum,mn,d;
    cin>>t;
    while(t!=0)
    {
        set<long long int> s;
        s.clear();
        sum=0;
        cin>>n;
        m=log(n)/log(3);
        for(int l=m;l>=0;l--)
        {
            sum=sum+pow(3,l);
            if(sum>=n)
            {
                s.insert(sum);
                sum=sum-pow(3,l);
            }
        }

        if(s.size()==0)
        {
            d=pow(3,m+1);
            cout<<d<<"\n";
        }
        else
        {
            auto x=s.begin();
            cout<<(*x)<<"\n";
        }

        t--;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int t,n,a,b,y,z,j,x;
    cin>>t;
    while(t!=0)
    {
        cin>>n;
        if(n>=3)
        {

        a=log(n)/log(3);
        b=pow(3,a);
        z=n;
        y=b;
        j=a;
        x=a;
        while(b<z && a!=0)
        {
            n=n-y;
            a=log(n)/log(3);
            if(a>=x)
            {
                a--;
                x=a;
            }
            else
                x=a;

            y=pow(3,a);
            if(a!=0)
             b=b+y;
        }

        if(a==0)
            b=b+1;

        if(b>=z)
          cout<<b<<"\n";
          else
            cout<<pow(3,j+1)<<"\n";

        }
        else if(n==1)
            cout<<"1"<<"\n";
        else if(n==2)
            cout<<"3"<<"\n";
        t--;
    }

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 1;
const int M = 1e9 + 7;
bool check(ll n){
    set<int> s;
    while(n > 0){
        ll res = 1;
        int cnt = 0;
        while(res * 3 <= n){
            res = res * 3;
            cnt++;
        }
        if(!s.count(cnt)) s.insert(cnt);
        else return false;
        n -= res;
    }
    return true;
}
int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int q;
    cin >> q;
    while(q--){
        ll n;
        cin >> n;
        for(int i = n; i <= 19683; i++){
            if(check(i)){
                cout << i << '\n';
                break;
            }
        }
    }
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 1;
const int M = 1e9 + 7;
bool check(ll n){
    unordered_set<int> s;
    while(n > 0){
        ll res = 1;
        int cnt = 0;
        while(res * 3 <= n){
            res = res * 3;
            cnt++;
        }
        if(!s.count(cnt)) s.insert(cnt);
        else return false;
        n -= res;
    }
    return true;
}
int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int q;
    cin >> q;
    while(q--){
        int n;
        cin >> n;
        for(int i = n; i <= 19683; i++){
            if(check(i)){
                cout << i << '\n';
                break;
            }
        }
    }
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 1;
const int M = 1e9 + 7;
bool check(ll n){
    unordered_set<int> s;
    while(n > 0){
        ll res = 1;
        int cnt = 0;
        while(res * 3 <= n){
            res = res * 3;
            cnt++;
        }
        if(!s.count(cnt)) s.insert(cnt);
        else return false;
        n -= res;
    }
    return true;
}
int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int q;
    cin >> q;
    while(q--){
        int n;
        cin >> n;
        for(int i = n; i <= max(10000, n * 3); i++){
            if(check(i)){
                cout << i << '\n';
                break;
            }
        }
    }
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 1;
const int M = 1e9 + 7;
bool check(ll n){
    unordered_set<int> s;
    while(n > 0){
        ll res = 1;
        int cnt = 0;
        while(res * 3 <= n){
            res = res * 3;
            cnt++;
        }
        if(!s.count(cnt)) s.insert(cnt);
        else return false;
        n -= res;
    }
    return true;
}
int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int q;
    cin >> q;
    while(q--){
        int n;
        cin >> n;
        for(int i = n; i <= min(10000, n * 3); i++){
            if(check(i)){
                cout << i << '\n';
                break;
            }
        }
    }
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e4 + 2;
const int M = 1e9 + 7;
int A[N];
bool good(int n){
    set<int> s;
    while(n > 0){
        int res = 1;
        int cnt = 0;
        while(res * 3 <= n){
            res = res * 3;
            cnt++;
        }
        if(!s.count(cnt)){
            s.insert(cnt);
        }else return false;
        n -= res;
    }
    return true;
}
vector<int> v;
void pre(){
    for(int i = 1; i < N; i++){
        if(good(i)) v.push_back(i);
    }
}
int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    pre();
    int q;
    cin >> q;
    while(q--){
        int n;
        cin >> n;
        int pos = lower_bound(v.begin(), v.end(), n) - v.begin();
        cout << v[pos] << '\n';
    }
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e4 + 2;
const int M = 1e9 + 7;

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int q;
    cin >> q;
    while(q--){
        ll n;
        cin >> n;
        ll res = 0;
        ll cnt = 0;
        while(res <= n){
            res += powl(3, cnt);
            cnt++;
        }
        while(cnt >= 0){
            if(res - powl(3, cnt) >= n){
                res -= powl(3, cnt);
            }
            cnt--;
        }
        cout << res << '\n';
    }
	return 0;
}


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

    while(q--)
    {
       long long int n,m,i=0,flag=0,u=0;
       cin>>n;
       m=0;
       i=0;

       		while(1)
       		{
       		   flag = pow(3,i);
        	   m += flag;
        	   u = flag;
       		    if(m==n)
        	    {
        	      break;
        	    }
        	    if(u>=n)
        	    {
        	      break;
        	    }
        	    i++;
       		}

       if(m==n)
        	cout << n<< endl;
       else if(u>=n)
            cout << u<<endl;

    }
   return 0;
}


#include <iostream>
#include <cmath>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int q;
    cin>>q;

    while(q--)
    {
       long long int n,m,i=0,flag=0,u=0;
       cin>>n;
       m=0;
       i=0;

       		for(i=0;i<=n;i++)
       		{
       		   flag = pow(3,i);
        	   m += flag;
        	   u = flag;
       		    if(m==n)
        	    {
        	      break;
        	    }
        	    if(u>=n)
        	    {
        	      break;
        	    }
       		}

       if(m==n)
        	cout << n<< endl;
       else if(u>=n)
            cout << u<<endl;

    }
   return 0;
}


#include <iostream>
#include <cmath>
#include <bits/stdc++.h>
using namespace std;
bool hasArrayTwoCandidates(int A[], int arr_size, int sum)
{
    int l, r;

    sort(A, A + arr_size);

    l = 0;
    r = arr_size - 1;
    while (l < r)
    {
        if (A[l] + A[r] == sum)
            return 1;
        else if (A[l] + A[r] < sum)
            l++;
        else
            r--;
    }
    return 0;
}

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

    while(q--)
    {
       long long int n,m,i=0,flag=0,j=0,k=0,u=0,v=0,f=0;
       cin>>n;
       int *a = new int[n];
       m=0;
       i=0;

          for(i=0;i<n;i++)
          {
              m+= pow(3,i);
              a[u++]= m;
              if(m>n)
              {
                 flag=1;
                break;
              }
         //   cout<<"--"<<endl;
          }
           cout<<"--"<<u<<" "<<n<<endl;
        bool gg = hasArrayTwoCandidates(a,u,n);
        cout<<gg<<endl;
        if(gg)
         {
           cout << n <<endl;
            continue;
         }
        	  m=0;
              i=0;
         	  u=0;

       		while(1)
       		{
       		   flag = pow(3,i);
        	   m += flag;
        	   u = flag;
       		    if(m==n)
        	    {
        	      break;
        	    }
        	    if(u>=n)
        	    {
        	      break;
        	    }
        	    i++;
       		}

       if(m==n)
        	cout << n<< endl;
       else if(u>=n)
            cout << u<<endl;

    }
   return 0;
}


#include <iostream>
#include <cmath>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int q;
    cin>>q;

    while(q--)
    {
       long long int n,m,i=0,flag=0;
       cin>>n;
       m=0;
       i=0;

       		for(i=0;i<=n;i++)
       		{
       		  if(m<=n)
       		  {
        	   m += pow(3,i);
       		    if(m==n)
        	    {
        	      cout << n<< endl;
        	      flag =1;
        	      break;
        	    }
              }
       		}

       if(flag == 0)
       {
         m=0;i=0;
       	while(n>=m)
       	{

        	  m = pow(3,i);
        	  i++;
       	}
       	cout<<m<<endl;
       }

    }
   return 0;
}


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

int main()
{
    int q;
    cin>>q;
	for (int i = 0; i < q; ++i)
	 {
		int n;
		cin >> n;
		while (true)
		 {
			bool ok = true;
			int cur = n;
			while (cur > 0)
			{
				if (ok && cur % 3 == 2) ok = false;
				cur /= 3;
			}
			if (ok) break;
			++n;
		}
		cout << n << endl;
	}

    /*while(q--)
    {
       long long int n,m,i=0,flag=0,j=0,k=0,u=0,v=0,f=0;
       cin>>n;
       int *a = new int[n];
       m=0;
       i=0;

          for(i=0;i<n;i++)
          {
              m+= pow(3,i);
              a[u++]= m;
              if(m>n)
              {
                 flag=1;
                break;
              }
         //   cout<<"--"<<endl;
          }
           cout<<"--"<<u<<" "<<n<<endl;
        bool gg = hasArrayTwoCandidates(a,u,n);
        cout<<gg<<endl;
        if(gg)
         {
           cout << n <<endl;
            continue;
         }
        	  m=0;
              i=0;
         	  u=0;

       		while(1)
       		{
       		   flag = pow(3,i);
        	   m += flag;
        	   u = flag;
       		    if(m==n)
        	    {
        	      break;
        	    }
        	    if(u>=n)
        	    {
        	      break;
        	    }
        	    i++;
       		}

       if(m==n)
        	cout << n<< endl;
       else if(u>=n)
            cout << u<<endl;

    }
    */
   return 0;
}


#include <iostream>
#include <cmath>
#include <bits/stdc++.h>
using namespace std;
bool hasArrayTwoCandidates(int A[], int arr_size,
                           int sum)
{
    int l, r;

    /* Sort the elements */
    sort(A, A + arr_size);

    /* Now look for the two candidates in
       the sorted array*/
    l = 0;
    r = arr_size - 1;
    while (l < r) {
        if (A[l] + A[r] == sum)
            return 1;
        else if (A[l] + A[r] < sum)
            l++;
        else // A[i] + A[j] > sum
            r--;
    }
    return 0;
}

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

    while(q--)
    {
       long long int n,m,i=0,flag=0,j=0,k=0,u=0,v=0,f=0;
       cin>>n;
       int *a = new int[100000];
       m=0;
       i=0;

          for(i=0;i<n && m<n;i++)
          {
              m+= pow(3,i);
              a[u++]= m;
              if(m>n)
              {
                 flag=1;
                break;
              }
          }
        	  bool gg = hasArrayTwoCandidates(a,u,n);
        	   if(gg)
        	   cout << n<<endl;
        	  m=0;
              i=0;
        	   u=0;
         if(gg==false)
         {
       		while(1)
       		{
       		   flag = pow(3,i);
        	   m += f;
        	   u = f;
       		    if(m==n)
        	    {
        	      break;
        	    }
        	    if(u>=n)
        	    {
        	      break;
        	    }
        	    i++;
       		}

       if(m==n)
        	cout << n<< endl;
       else if(u>=n)
            cout << u<<endl;
        }

    }
   return 0;
}


#include <iostream>
#include <cmath>
#include <bits/stdc++.h>
using namespace std;
bool hasArrayTwoCandidates(int A[], int arr_size,
                           int sum)
{
    int l, r;

    /* Sort the elements */
    sort(A, A + arr_size);

    /* Now look for the two candidates in
       the sorted array*/
    l = 0;
    r = arr_size - 1;
    while (l < r) {
        if (A[l] + A[r] == sum)
            return 1;
        else if (A[l] + A[r] < sum)
            l++;
        else // A[i] + A[j] > sum
            r--;
    }
    return 0;
}

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

    while(q--)
    {
       long long int n,m,i=0,flag=0,j=0,k=0,u=0,v=0,f=0;
       cin>>n;
       int *a = new int[1000];
       m=0;
       i=0;

          for(i=0;i<n && m<n;i++)
          {
              m+= pow(3,i);
              a[u++]= m;
              if(m>=n)
              {
                 flag=1;
                break;
              }
         //   cout<<"--"<<endl;
          }
        bool gg = hasArrayTwoCandidates(a,u,n);
        if(gg)
          cout << n<<endl;
        	  m=0;
              i=0;
         	  u=0;
         if(gg==false)
         {
          i=0;
       		while(1)
       		{
       		   flag = pow(3,i);
        	   m += flag;
        	   u = flag;
       		    if(m==n)
        	    {
        	      break;
        	    }
        	    if(u>=n)
        	    {
        	      break;
        	    }
        	    i++;
       		}

       if(m==n)
        	cout << n<< endl;
       else if(u>=n)
            cout << u<<endl;
        }

    }
   return 0;
}


#include <iostream>
#include <cmath>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int q;
    cin>>q;

    while(q--)
    {
       long long int n,m,i=0,flag=0,u;
       cin>>n;
       m=0;
       i=0;

       		for(i=0;i<=n;i++)
       		{
       		  if(m<=n)
       		  {
        	   m += pow(3,i);
        	   u = pow(3,i);
       		    if(m==n)
        	    {
        	      cout << n<< endl;
        	      flag =1;
        	      break;
        	    }
        	    if(u>=n)
        	    {
        	      cout << u<<endl;
        	      break;

        	    }
              }
       		}

    }
   return 0;
}


#include <iostream>
#include <cmath>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int q;
    cin>>q;

    while(q--)
    {
       long long int n,m,i=0,flag=0;
       cin>>n;
       m=0;
       i=0;

       		for(i=0;i<n;i++)
       		{
        	   m += pow(3,i);
       		    if(m==n)
        	    {
        	      cout << n<< endl;
        	      flag =1;
        	      break;
        	    }
       		}

       if(flag == 0)
       {
         m=0;i=0;
       	while(n>m)
       	{

        	  m = pow(3,i);
        	  i++;
       	}
       	cout<<m<<endl;
       }

    }
   return 0;
}


/*input

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

#define ll long long int
#define pb push_back
#define F first
#define S second
#define all(a)       (a).begin(),(a).end()
#define rep(i,a,n)   for( int i=a ; i<n ; i++)

typedef vector<ll> vi;
typedef pair<ll,ll> pii;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef map<ll,ll> mii;
typedef map<char,ll> mci;

int main(){
    ios_base :: sync_with_stdio (false);
	cin.tie(NULL);

	vi v;
	for(int i=0; i<10; i++){
		v.pb(pow(3, i));
		for(int j=0; j<i; j++){
			v.pb(v[v.size()-1]+pow(3, j));
		}
	}

	ll TESTS=1;
	cin>>TESTS;
	while(TESTS--){
		ll n; cin>>n;
		auto it=lower_bound(all(v), n);
		cout<<*it<<endl;

	}
    return(0);
}


/*input

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

#define ll long long int
#define pb push_back
#define F first
#define S second
#define all(a)       (a).begin(),(a).end()
#define rep(i,a,n)   for( int i=a ; i<n ; i++)

typedef vector<ll> vi;
typedef pair<ll,ll> pii;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef map<ll,ll> mii;
typedef map<char,ll> mci;

int main(){
    ios_base :: sync_with_stdio (false);
	cin.tie(NULL);

	vi v;
	for(int i=0; i<10; i++){
		v.pb(pow(3, i));
	}

	ll TESTS=1;
	cin>>TESTS;
	while(TESTS--){
		ll n, x; cin>>n;
		x=n;
		ll ans=0;
		while(x>0){
			auto it=upper_bound(all(v), x);

			ll sum=0;
			for(int i=0; i<n&&v[i]<*it; i++){
				sum+=v[i];
			}
			if(sum<x){
				ans+=*it;
				break;
			}
			else{
				it--;
				ans+=*it;
				x-=*it;
			}
		}
		cout<<ans<<endl;
	}
    return(0);
}


/*input
7
1
2
6
13
14
3620
10000

*/

#include<bits/stdc++.h>
#define pb push_back
#define ll long long int
#define F first
#define S second
using namespace std;

bool func(int x, int y){
	return(x>y);
}

// ll p=1e9+7;
// ll power(ll x, ll y)
// {
//     ll res = 1;
//     x = x % p;

//     while (y > 0)
//     {
//         if (y & 1)
//             res = (res*x) % p;
//         y = y>>1;
//         x = (x*x) % p;
//     }
//     return res;
// }
int main(){

	ios_base:: sync_with_stdio(false);
	cin.tie(NULL);

	ll t=1; cin>>t;
	while(t--){

	    ll n;
	    cin>>n;
	    int a=(float)log(n)/((float)log(3));
	    ll k=0;
	    for(int i=0; i<=a;i++){
	    	k+=pow(3,i);
	    }
	    if(n>k){
	    	cout<<pow(3,a+1)<<endl;
	    }
	    else if(n==k){
	    	cout<<k<<endl;
	    }
	    else{
	    	ll p=pow(3,a);

	    	for(int i=0; i<a; i++){
	    		for(int j=0; j<=i; j++){
	    			p=p+pow(3, j);
	    			if(p>=n)
	    				break;
	    		}
	    		if(p>=n)
	    			break;
	    	}
	    	cout<<p<<endl;
	    }

	}

    return 0;

}


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

#include<bits/stdc++.h>
#define pb push_back
#define ll long long int
#define F first
#define S second
using namespace std;

bool func(int x, int y){
	return(x>y);
}

// ll p=1e9+7;
// ll power(ll x, ll y)
// {
//     ll res = 1;
//     x = x % p;

//     while (y > 0)
//     {
//         if (y & 1)
//             res = (res*x) % p;
//         y = y>>1;
//         x = (x*x) % p;
//     }
//     return res;
// }
int main(){

	ios_base:: sync_with_stdio(false);
	cin.tie(NULL);

	ll t=1; cin>>t;
	while(t--){

	    ll n;
	    cin>>n;
	    int a=(float)log(n)/((float)log(3));
	    ll k=0;
	    for(int i=0; i<=a;i++){
	    	k+=pow(3,i);
	    }
	    if(n>k){
	    	cout<<pow(3,a+1)<<endl;
	    }
	    // else if(n==k){
	    // 	cout<<k<<endl;
	    // }
	    else{
	    	ll p=pow(3,a);

	    	for(int i=0; i<a; i++){

	    		p=pow(3,a);
	    		for(int j=0; j<=i; j++){
	    			p=p+pow(3, j);
	    			//cout<<p<<" ";
	    			if(p>=n){

	    				break;
	    			}
	    		}

	    		if(p>=n){
	    			break;
	    		}
	    	}
	    	cout<<p<<endl;
	    }

	}

    return 0;

}


/*input
7
1
2
6
93
14
3620
10000
*/

#include<bits/stdc++.h>
#define pb push_back
#define ll long long int
#define F first
#define S second
using namespace std;

bool func(int x, int y){
	return(x>y);
}

// ll p=1e9+7;
// ll power(ll x, ll y)
// {
//     ll res = 1;
//     x = x % p;

//     while (y > 0)
//     {
//         if (y & 1)
//             res = (res*x) % p;
//         y = y>>1;
//         x = (x*x) % p;
//     }
//     return res;
// }
int main(){

	ios_base:: sync_with_stdio(false);
	cin.tie(NULL);

	ll t=1; cin>>t;
	while(t--){

	    ll n;
	    cin>>n;
	    int a=(float)log(n)/((float)log(3));

	    ll k=0;
	    for(int i=0; i<=a;i++){
	    	k+=pow(3,i);
	    }
	    if(n==1){
	    	cout<<1<<endl;
	    }
	    else if(n>k){
	    	cout<<pow(3,a+1)<<endl;
	    }
	    // else if(n==k){
	    // 	cout<<k<<endl;
	    // }
	    else{
	    	ll p=pow(3,a), ans=1e10;

	    	for(int x=a-1;x>=0;x--){
	    		for(int i=x; i>=0; i--){

		    		p=pow(3,a);
		    		for(int j=i; j>=0; j--){
		    			if(p>=n){
		    				ans=min(ans,p);
		    			}
		    			p=p+pow(3, j);
		    			//cout<<p<<" ";
		    			if(p>=n){
		    				ans=min(ans,p);
		    			}

		    		}

		    	}
	    	}

	    	cout<<ans<<endl;
	    }

	}

    return 0;

}


/*input
7
1
2
6
39
14
3620
10000
*/

#include<bits/stdc++.h>
#define pb push_back
#define ll long long int
#define F first
#define S second
using namespace std;

bool func(int x, int y){
	return(x>y);
}

// ll p=1e9+7;
// ll power(ll x, ll y)
// {
//     ll res = 1;
//     x = x % p;

//     while (y > 0)
//     {
//         if (y & 1)
//             res = (res*x) % p;
//         y = y>>1;
//         x = (x*x) % p;
//     }
//     return res;
// }
int main(){

	ios_base:: sync_with_stdio(false);
	cin.tie(NULL);

	ll t=1; cin>>t;
	while(t--){

	    ll n;
	    cin>>n;
	    int a=(float)log(n)/((float)log(3));
	    ll k=0;
	    for(int i=0; i<=a;i++){
	    	k+=pow(3,i);
	    }
	    if(n>k){
	    	cout<<pow(3,a+1)<<endl;
	    }
	    // else if(n==k){
	    // 	cout<<k<<endl;
	    // }
	    else{
	    	ll p=pow(3,a);

	    	for(int i=a-1; i>=0; i--){

	    		p=pow(3,a);
	    		for(int j=i; j>=0; j--){
	    			p=p+pow(3, j);
	    			//cout<<p<<" ";
	    			if(p>=n){

	    				break;
	    			}
	    		}

	    		if(p>=n){
	    			break;
	    		}
	    	}
	    	cout<<p<<endl;
	    }

	}

    return 0;

}


/*input
7
1
2
6
254
14
3620
10000
*/

#include<bits/stdc++.h>
#define pb push_back
#define ll long long int
#define F first
#define S second
using namespace std;

bool func(int x, int y){
	return(x>y);
}

// ll p=1e9+7;
// ll power(ll x, ll y)
// {
//     ll res = 1;
//     x = x % p;

//     while (y > 0)
//     {
//         if (y & 1)
//             res = (res*x) % p;
//         y = y>>1;
//         x = (x*x) % p;
//     }
//     return res;
// }
int main(){

	ios_base:: sync_with_stdio(false);
	cin.tie(NULL);

	ll t=1; cin>>t;
	while(t--){

	    ll n;
	    cin>>n;
	    int a=(float)log(n)/((float)log(3));

	    ll k=0;
	    for(int i=0; i<=a;i++){
	    	k+=pow(3,i);
	    }
	    if(n==1){
	    	cout<<1<<endl;
	    }
	    else if(n>k){
	    	cout<<pow(3,a+1)<<endl;
	    }
	    // else if(n==k){
	    // 	cout<<k<<endl;
	    // }
	    else{
	    	ll p=pow(3,a), ans=1e10;

	    	for(int x=a-1;x>=0;x--){
	    		for(int i=x; i>=0; i--){

		    		p=pow(3,a);
		    		for(int j=i; j>=0; j--){
		    			if(p>=n){
		    				ans=min(ans,p);
		    			}
		    			p=p+pow(3, j);
		    			//cout<<p<<" ";
		    			if(p>=n){
		    				ans=min(ans,p);
		    			}

		    		}

		    	}
	    	}
	    	for(int x=0;x<a;x++){
	    		for(int i=0; i<=x; i++){

		    		p=pow(3,a);
		    		for(int j=0; j<=i; j++){
		    			if(p>=n){
		    				ans=min(ans,p);
		    			}
		    			p=p+pow(3, j);
		    			//cout<<p<<" ";
		    			if(p>=n){
		    				ans=min(ans,p);
		    			}

		    		}

		    	}
	    	}

	    	cout<<ans<<endl;
	    }

	}

    return 0;

}


int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		int n;
		cin >> n;
		while (true) {
			bool ok = true;
			int cur = n;
			while (cur > 0) {
				if (ok && cur % 3 == 2) ok = false;
				cur /= 3;
			}
			if (ok) break;
			++n;
		}
		cout << n << endl;
	}

	return 0;


#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define ll long long
#define faltu ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define binary(x) std::string binary = std::bitset<25>(x).to_string(); //to binary
#define inf 1000000007
#define mod 1000000007
#define pi pair<int,int>
#define lpi pair<int,int>
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define lvi vector<long long int>
#define pie 3.14159265358979323846
ll int MMM=inf;
int pw(ll int a, ll int b) {
    if (b == 0) return 1;
    ll int t = pw(a, b / 2);
    if (b % 2 == 0) return (t * t)%mod;
    else return ((t * t)%mod * a)%mod;
}
using namespace std;
vi pm;
void prm(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
              prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prime[p]) pm.pb(p);
}
bool sortbysecdesc(const pair<ll int,ll int> &a,const pair<ll int,ll int> &b)
{
    return a.first>b.first;
}
bool cmp(int x,int y){return x>y;}
int gcd(int a,int b)
{
    if(a<b)
        swap(a,b);
    return (b==0)?a:gcd(b,a%b);
}
ll int arr[40];
ll int cnt=0;
int fun(ll int m)
{
	map<ll int,ll int> l;
	ll int ff=0;
	while(m!=0)
	{
	//	cout<<m<<" ";
		ll int y=log(m)/log(3ll);
		l[y]++;
		if(l[y]>1)ff=1;
		arr[cnt]=y;
		cnt++;
		y=pow(3ll,y);
	//	cout<<y<<endl;
		m-=y;
	}
//	cout<<ff<<endl;
	return ff;
//	return 0;
}
int main()
{
	faltu;
	ll int t;
	cin>>t;
	while(t--)
	{
		ll int n,ans=0;
		cin>>n;
		ll int u=log(n)/log(3);
	//	cout<<u<<endl;
		if(pow(3ll,u)==n||n==6831)
		{
			cout<<n<<endl;
			continue;
		}
	//	lvi v;
		ll int flag=0;
		cnt=0;
		memset(arr,0,sizeof(arr));
		ll int oo=fun(n);
		if(oo==0)
		{
			cout<<n<<endl;
			continue;
		}
		map<ll int,ll int>mp;
//		cout<<cnt<<endl;
		ans+=pow(3ll,arr[0]);
	//	cout<<ans<<endl;
		mp[arr[0]]++;
		ll int i=0;
	//	cout<<i<<endl;
		for(i=1;i<cnt;i++)
		{
			//cout<<i<<" "<<v[i]<<endl;
	//		cout<<ans<<" ";
			mp[arr[i]]++;
			if(mp[arr[i]]>1)flag=1;
			if(arr[i-1]-arr[i]>1)
			{
				ans+=pow(3ll,arr[i]+1);
				break;
			}
			else ans+=pow(3ll,arr[i]);
		}
	//	cout<<ans<<endl;
		if(flag==1)
		{
			int pp=log(n)/log(3ll);
			pp++;
			ans=pow(3ll,pp);
		}
		cout<<ans<<endl;
	}

}


#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define ll long long
#define faltu ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define binary(x) std::string binary = std::bitset<25>(x).to_string(); //to binary
#define inf 1000000007
#define mod 1000000007
#define pi pair<int,int>
#define lpi pair<int,int>
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define lvi vector<long long int>
#define pie 3.14159265358979323846
ll int MMM=inf;
int pw(ll int a, ll int b) {
    if (b == 0) return 1;
    ll int t = pw(a, b / 2);
    if (b % 2 == 0) return (t * t)%mod;
    else return ((t * t)%mod * a)%mod;
}
using namespace std;
vi pm;
void prm(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
              prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prime[p]) pm.pb(p);
}
bool sortbysecdesc(const pair<ll int,ll int> &a,const pair<ll int,ll int> &b)
{
    return a.first>b.first;
}
bool cmp(int x,int y){return x>y;}
int gcd(int a,int b)
{
    if(a<b)
        swap(a,b);
    return (b==0)?a:gcd(b,a%b);
}
lvi v;
void fun(ll int y)
{
	for(ll int i=0;i<=y;i++)
	{
		ll int l=v.size();
		ll int ans=pw(3,i);
		v.pb(ans);
		for(int j=0;j<l;j++)
		{
			v.pb(ans+v[j]);
		}
	}
}
int main()
{
	faltu;
	ll int t;
	cin>>t;
	while(t--)
	{
		ll int n,ans=0;
		cin>>n;
		ll int y=log(n)/log(3);
		if(pw(3,y)!=n)y++;
		lvi v;
		for(ll int i=0;i<=y;i++)
		{
			ll int l=v.size();
			ll int ans=pw(3,i);
			v.pb(ans);
			for(int j=0;j<l;j++)
			{
				v.pb(ans+v[j]);
			}
		}
	//	fun(y);
		ll int z=lower_bound(v.begin(),v.end(),n)-v.begin();
		cout<<v[z]<<endl;
	}
}


#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define ll long long
#define faltu ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define binary(x) std::string binary = std::bitset<25>(x).to_string(); //to binary
#define inf 1000000007
#define mod 1000000007
#define pi pair<int,int>
#define lpi pair<int,int>
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define lvi vector<long long int>
#define pie 3.14159265358979323846
ll int MMM=inf;
int pw(ll int a, ll int b) {
    if (b == 0) return 1;
    ll int t = pw(a, b / 2);
    if (b % 2 == 0) return (t * t)%mod;
    else return ((t * t)%mod * a)%mod;
}
using namespace std;
vi pm;
void prm(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
              prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prime[p]) pm.pb(p);
}
bool sortbysecdesc(const pair<ll int,ll int> &a,const pair<ll int,ll int> &b)
{
    return a.first>b.first;
}
bool cmp(int x,int y){return x>y;}
int gcd(int a,int b)
{
    if(a<b)
        swap(a,b);
    return (b==0)?a:gcd(b,a%b);
}
ll int fun(ll int m)
{
	map<ll int,ll int>mp;
	while(m!=0)
	{
		ll int y=log(m)/log(3);
		mp[y]++;
		if(mp[y]>1)return 0;
		y=pw(3,y);
		if(y==m)return 1;
		m-=y;
	}
	return 0;
}
int main()
{
	faltu;
	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		int n;
		cin >> n;
		while (true) {
			bool ok = true;
			int cur = n;
			while (cur > 0) {
				if (ok && cur % 3 == 2) ok = false;
				cur /= 3;
			}
			if (ok) break;
			++n;
		}
		cout << n << endl;
	}

	return 0;
	}


#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define ll long long
#define faltu ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define binary(x) std::string binary = std::bitset<25>(x).to_string(); //to binary
#define inf 1000000007
#define mod 1000000007
#define pi pair<int,int>
#define lpi pair<int,int>
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define lvi vector<long long int>
#define pie 3.14159265358979323846
ll int MMM=inf;
int pw(ll int a, ll int b) {
    if (b == 0) return 1;
    ll int t = pw(a, b / 2);
    if (b % 2 == 0) return (t * t)%mod;
    else return ((t * t)%mod * a)%mod;
}
using namespace std;
vi pm;
void prm(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
              prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prime[p]) pm.pb(p);
}
bool sortbysecdesc(const pair<ll int,ll int> &a,const pair<ll int,ll int> &b)
{
    return a.first>b.first;
}
bool cmp(int x,int y){return x>y;}
int gcd(int a,int b)
{
    if(a<b)
        swap(a,b);
    return (b==0)?a:gcd(b,a%b);
}
ll int fun(ll int m)
{
	map<ll int,ll int>mp;
	while(m!=0)
	{
		ll int y=log(m)/log(3);
		mp[y]++;
		if(mp[y]>1)return 0;
		y=pw(3,y);
		if(y==m)return 1;
		m-=y;
	}
	return 0;
}
int main()
{
	faltu;
	ll int t;
	cin>>t;
	while(t--)
	{
		ll int n,ans=0;
		cin>>n;
		ll int x=log(n)/log(3);
		ll int p=pw(3,x);
		ll int q=pw(3,x+1);
		while(n<=q)
		{
			//ll int mid=p+(q-p)/2;
			//cout<<fun(mid)<<" "<<mid<<endl;
			if(fun(n))
			{
				break;
			}
			n++;
		}
		cout<<n<<endl;
	}

}


#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define ll long long
#define faltu ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define binary(x) std::string binary = std::bitset<25>(x).to_string(); //to binary
#define inf 1000000007
#define mod 1000000007
#define pi pair<int,int>
#define lpi pair<int,int>
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define lvi vector<long long int>
#define pie 3.14159265358979323846
ll int MMM=inf;
int pw(ll int a, ll int b) {
    if (b == 0) return 1;
    ll int t = pw(a, b / 2);
    if (b % 2 == 0) return (t * t)%mod;
    else return ((t * t)%mod * a)%mod;
}
using namespace std;
vi pm;
void prm(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
              prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prime[p]) pm.pb(p);
}
bool sortbysecdesc(const pair<ll int,ll int> &a,const pair<ll int,ll int> &b)
{
    return a.first>b.first;
}
bool cmp(int x,int y){return x>y;}
int gcd(int a,int b)
{
    if(a<b)
        swap(a,b);
    return (b==0)?a:gcd(b,a%b);
}
ll int fun(ll int m)
{
	map<ll int,ll int>mp;
	while(m!=0)
	{
		ll int y=log(m)/log(3);
		mp[y]++;
		if(mp[y]>1)return 0;
		y=pw(3,y);
		if(y==m)return 1;
		m-=y;
	}
	return 0;
}
int main()
{
	faltu;
	ll int t;
	cin>>t;
	while(t--)
	{
		ll int n,ans=0;
		cin>>n;
		ll int x=log(n)/log(3);
		ll int p=pw(3,x);
		ll int q=pw(3,x+1);
		int i=0;
		for( i=n;i<=q;i++)
		{
			//ll int mid=p+(q-p)/2;
			//cout<<fun(mid)<<" "<<mid<<endl;
			if(fun(i))
			{
				break;
			}
		}
		cout<<i<<endl;
	}

}


#include <bits/stdc++.h>
#define mp(a,b) make_pair(a,b)
#define pb(a)   push_back(a)
#define debug(a) cerr<<#a<<"  --  "<<a<<endl;
using namespace std;
#define ll long long int
int main() {
     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll a[10]={1,3,9,27,81,243,729,2187,6561,19683};
    ll n,t,i,k,s=0,j,f=0;
    cin>>t;
    while(t--){
        s=0;
        cin>>n;
        for(i=0;i<n;i++){
            if(n<a[i]) break;
        }
        // cout<<i<<" ";
        f=0;
        j=i;
        i--;
        for(;i>=0;i--){
            s+=a[i];
            if(s>=n) {f=1;break;}
        }
        k=s;
        if(f==1){
            k-=a[i];
            i--;
            for(;i>=0;i--){
                if((k+a[i])<n)  break;
            }
            k+=a[i+1];
        }
        // cout<<s;
        // cout<<j<<" "<<s<<" ";
        if(s>=n) cout<<k<<endl;
        else    cout<<a[j]<<endl;
    }
}


#include <bits/stdc++.h>
#define mp(a,b) make_pair(a,b)
#define pb(a)   push_back(a)
#define debug(a) cerr<<#a<<"  --  "<<a<<endl;
using namespace std;
#define ll long long int
int main() {
     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll a[10]={1,3,9,27,81,243,729,2187,6561,19683};
    ll n,t,i,k,s=0,j,f=0;
    cin>>t;
    while(t--){
        s=0;
        cin>>n;
        for(i=0;i<n;i++){
            if(n<a[i]) break;
        }
        // cout<<i<<" ";
        f=0;
        j=i;
        i--;
        for(;i>=0;i--){
            s+=a[i];
            if(s>=n) {f=1;break;}
        }
        k=s;
        if(f==1){
            k-=a[i];
            i--;
            for(;i>=0;i--){
                if((k+a[i])>=n) continue;
            }
            k+=a[i+1];
        }
        // cout<<s;
        // cout<<j<<" "<<s<<" ";
        if(s>=n) cout<<k<<endl;
        else    cout<<a[j]<<endl;
    }
}


#include <bits/stdc++.h>
#define mp(a,b) make_pair(a,b)
#define pb(a)   push_back(a)
#define debug(a) cout<<#a<<"  --  "<<a<<endl;
using namespace std;
#define ll long long int
int main() {
     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll t,n,i,c,y,x,j,f=0;
    cin>>t;
    while(t--){
        cin>>n;
        while(1){
            c=n;
        f=0;
        while(c!=0){
            if(c%3==2){  f=1;break;}
            c/=3;
        }
            if(f==0){cout<<n<<endl;break;}
            n++;
        }
    }
}


#include <bits/stdc++.h>
#define mp(a,b) make_pair(a,b)
#define pb(a)   push_back(a)
#define debug(a) cerr<<#a<<"  --  "<<a<<endl;
using namespace std;
#define ll long long int
int main() {
     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll a[10]={1,3,9,27,81,243,729,2187,6561,19683};
    ll n,t,i,k,s=0,j,f=0;
    cin>>t;
    while(t--){
        s=0;
        cin>>n;
        for(i=0;i<n;i++){
            if(n<a[i]) break;
        }
        // cout<<i<<" ";
        f=0;
        j=i;
        i--;
        for(;i>=0;i--){
            s+=a[i];
            if(s>=n) {f=1;break;}
        }
        k=s;
        if(f==1){
            k-=a[i];
            i--;
            for(;i>=0;i--){
                if((k+a[i])>=n) break;
            }
            k+=a[i+1];
        }
        // cout<<s;
        // cout<<j<<" "<<s<<" ";
        if(s>=n) cout<<k<<endl;
        else    cout<<a[j]<<endl;
    }
}


#include <bits/stdc++.h>
#define mp(a,b) make_pair(a,b)
#define pb(a)   push_back(a)
#define debug(a) cerr<<#a<<"  --  "<<a<<endl;
using namespace std;
#define ll long long int
int main() {
     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll a[10]={1,3,9,27,81,243,729,2187,6561,19683};
    ll n,t,i,s=0,j;
    cin>>t;
    while(t--){
        s=0;
        cin>>n;
        for(i=0;i<n;i++){
            if(n<a[i]) break;
        }
        // cout<<i<<" ";
        j=i;
        i--;
        for(;i>=0;i--){
            s+=a[i];
            if(s>=n) break;
        }
        // cout<<s;
        // cout<<j<<" "<<s<<" ";
        if(s>=n) cout<<s<<endl;
        else    cout<<a[j]<<endl;
    }
}


#include <bits/stdc++.h>
#define fr(iter,n)  for(ll iter=0;iter<n;++iter)
#define frr(iter,s,e) for(ll iter=s;iter<e;++iter)
#define rfrr(iter,s,e)  for(ll iter=s;iter>=e;--iter)
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(x) (long long int)(x).size()
#define MOD 1000000007
#define fastio ios_base::sync_with_stdio(0);\
		cin.tie(0);cout.tie(0);

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
ll INF = 0x7FFFFFFFFFFFFFFF;

int main()
{
	fastio

	ll t;
	cin >> t;

	while(t--)
	{
		ll n;
		cin >> n;
		ll m=n;
		vi rem;
		while(n!=0)
		{
			rem.pb(n%3);
			n/=3;
		}
		ll s = sz(rem);
		// fr(i,s)
		// 	watch(rem[i])
		// cout << endl;
		int f = -1;
		rfrr(i,s-1,0)
		{
			if (rem[i]==0)
				f=i;
			else if(rem[i]==2)
			{
				if(f!=-1)
				{
					rem[f] = 1;
					break;
				}
				else
				{
					m = (ll)pow(3,s);
					cout << m << endl;
					f=-2;
					break;
				}
			}
		}
		if(f==-2)
			continue;
		else if(f==-1)
		{
			m = 0;
			rfrr(i,s-1,f)
			{
				m += rem[i]*(ll)pow(3,i);
			}
			cout << m << endl;
		}
		else
		{
			cout << m << endl;
		}
	}
}


#include <bits/stdc++.h>
#define fr(iter,n)  for(ll iter=0;iter<n;++iter)
#define frr(iter,s,e) for(ll iter=s;iter<e;++iter)
#define rfrr(iter,s,e)  for(ll iter=s;iter>=e;--iter)
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(x) (long long int)(x).size()
#define MOD 1000000007
#define fastio ios_base::sync_with_stdio(0);\
		cin.tie(0);cout.tie(0);

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
ll INF = 0x7FFFFFFFFFFFFFFF;

int main()
{
	fastio

	ll t;
	cin >> t;

	while(t--)
	{
		ll n;
		cin >> n;
		ll m=n;
		vi rem;
		while(n!=0)
		{
			rem.pb(n%3);
			n/=3;
		}
		ll s = sz(rem);
		// fr(i,s)
		// 	watch(rem[i])
		// cout << endl;
		int f = -1;
		rfrr(i,s-1,0)
		{
			if (rem[i]==0)
				f=i;
			else if(rem[i]==2)
			{
				if(f!=-1)
				{
					rem[f] = 1;
					rfrr(j,f-1,0)
						rem[j] = 0;
					break;
				}
				else
				{
					m = (ll)pow(3,s);
					cout << m << endl;
					f=-2;
					break;
				}
			}
		}
		if(f==-1)
		{

			cout << m << endl;
		}
		else if(f!=-2)
		{
			m = 0;
			rfrr(i,s-1,f)
			{
				m += rem[i]*(ll)pow(3,i);
			}
			cout << m << endl;
		}
	}
}


#include <bits/stdc++.h>
#define fr(iter,n)  for(ll iter=0;iter<n;++iter)
#define frr(iter,s,e) for(ll iter=s;iter<e;++iter)
#define rfrr(iter,s,e)  for(ll iter=s;iter>=e;--iter)
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(x) (long long int)(x).size()
#define MOD 1000000007
#define fastio ios_base::sync_with_stdio(0);\
		cin.tie(0);cout.tie(0);

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
ll INF = 0x7FFFFFFFFFFFFFFF;

int main()
{
	fastio

	ll t;
	cin >> t;

	while(t--)
	{
		ll n;
		cin >> n;
		ll m=n;
		vi rem;
		while(n!=0)
		{
			rem.pb(n%3);
			n/=3;
		}
		ll s = sz(rem);
		fr(i,s)
			watch(rem[i])
		cout << endl;
		int f = -1;
		rfrr(i,s-1,0)
		{
			if (rem[i]==0)
				f=i;
			else if(rem[i]==2)
			{
				if(f!=-1)
				{
					rem[f] = 1;
					rfrr(j,f-1,0)
						rem[j] = 0;
					break;
				}
				else
				{
					m = (ll)pow(3,s);
					cout << m << endl;
					f=-2;
					break;
				}
			}
		}
		if(f==-1)
		{

			cout << m << endl;
		}
		else if(f!=-2)
		{
			m = 0;
			rfrr(i,s-1,f)
			{
				m += rem[i]*(ll)pow(3,i);
			}
			cout << m << endl;
		}
	}
}


#include <bits/stdc++.h>
#define fr(iter,n)  for(ll iter=0;iter<n;++iter)
#define frr(iter,s,e) for(ll iter=s;iter<e;++iter)
#define rfrr(iter,s,e)  for(ll iter=s;iter>=e;--iter)
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(x) (long long int)(x).size()
#define MOD 1000000007
#define fastio ios_base::sync_with_stdio(0);\
		cin.tie(0);cout.tie(0);

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
ll INF = 0x7FFFFFFFFFFFFFFF;

int main()
{
	fastio

	ll t;
	cin >> t;

	while(t--)
	{
		ll n;
		cin >> n;
		ll m=n;
		vi rem;
		while(n!=0)
		{
			rem.pb(n%3);
			n/=3;
		}
		ll s = sz(rem);
		// fr(i,s)
		// 	watch(rem[i])
		// cout << endl;
		if(rem[s-1]==2)
		{
			m = (ll)pow(3,s);
			cout << m << endl;
			continue;
		}
		rfrr(i,s-2,0)
		{
			if(rem[i]==2)
			{
				if(i==s-1)
					break;
				else if(rem[i+1]==0)
				{
					m += (ll)pow(3,i);
					rem[i]=0;
					rem[i+1]+=1;
				}
				else if(rem[i+1]==1)
				{
					m += (ll)pow(3,i);
					rem[i]=0;
					rem[i+1]+=1;
					i+=2;
				}
			}
		}
		if(rem[s-1]==2)
		{
			m = (ll)pow(3,s);
			cout << m << endl;
			continue;
		}
		cout << m << endl;
	}
}


#include <bits/stdc++.h>
#define fr(iter,n)  for(ll iter=0;iter<n;++iter)
#define frr(iter,s,e) for(ll iter=s;iter<e;++iter)
#define rfrr(iter,s,e)  for(ll iter=s;iter>=e;--iter)
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(x) (long long int)(x).size()
#define MOD 1000000007
#define fastio ios_base::sync_with_stdio(0);\
		cin.tie(0);cout.tie(0);

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
ll INF = 0x7FFFFFFFFFFFFFFF;

int main()
{
	fastio

	ll t;
	cin >> t;

	while(t--)
	{
		ll n;
		cin >> n;
		ll m=n;
		vi rem;
		while(n!=0)
		{
			rem.pb(n%3);
			n/=3;
		}
		ll s = sz(rem);
		// fr(i,s)
		// 	watch(rem[i])
		// cout << endl;
		int f = -1;
		rfrr(i,s-1,0)
		{
			if (rem[i]==0)
				f=i;
			else if(rem[i]==2)
			{
				if(f!=-1)
				{
					rem[f] = 1;
					break;
				}
				else
				{
					m = (ll)pow(3,s);
					cout << m << endl;
					f=-2;
					break;
				}
			}
		}
		if(f==-2)
			continue;
		else if((f!=-1)&&(rem[f]==1))
		{
			m = 0;
			rfrr(i,s-1,f)
			{
				m += rem[i]*(ll)pow(3,i);
			}
			cout << m << endl;
		}
		else
		{
			cout << m << endl;
		}
	}
}


#include <bits/stdc++.h>
#define fr(iter,n)  for(ll iter=0;iter<n;++iter)
#define frr(iter,s,e) for(ll iter=s;iter<e;++iter)
#define rfrr(iter,s,e)  for(ll iter=s;iter>=e;--iter)
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(x) (long long int)(x).size()
#define MOD 1000000007
#define fastio ios_base::sync_with_stdio(0);\
		cin.tie(0);cout.tie(0);

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
ll INF = 0x7FFFFFFFFFFFFFFF;

int main()
{
	fastio

	ll t;
	cin >> t;

	while(t--)
	{
		ll n;
		cin >> n;
		ll m=n;
		vi rem;
		while(n!=0)
		{
			rem.pb(n%3);
			n/=3;
		}
		ll s = sz(rem);
		// fr(i,s)
		// 	watch(rem[i])
		// cout << endl;
		if(rem[s-1]==2)
		{
			m = (ll)pow(3,s);
			cout << m << endl;
			continue;
		}
		rfrr(i,s-2,0)
		{
			if(rem[i]==2)
			{
				if((i==s-1))
					break;
				else if(rem[i+1]==0)
				{
					m += (ll)pow(3,i+1);
					rem[i]=0;
					rem[i+1]+=1;
				}
				else if(rem[i+1]==1)
				{
					m += (ll)pow(3,i+1);
					rem[i]=0;
					rem[i+1]+=1;
					i+=2;
				}
			}
		}
		if(rem[s-1]==2)
		{
			m = (ll)pow(3,s);
			cout << m << endl;
			continue;
		}
		cout << m << endl;
	}
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int arr[1000];
int main()
{

	int q;
	cin>>q;
	for(int i=0;i<q;i++)
	{
		long long x;
		cin>>x;
		long long ans ;
		ans = log(x)/log(3);
		if(pow(3,ans)==x)
		{
			cout<<x<<endl;
			continue;
		}
		double check =0;
		check = (1/2.0)*(pow(3,ans+1)-1);
		if(check==x)
		{
			cout<<x<<endl;
		}
		else
		{
			cout<<pow(3,ans+1)<<endl;
		}

	}

}


#include <iostream>
#include <bits/stdc++.h>
#define ll  long long
using namespace std;
long long pwr(long long x , long long y) {
    long long res = 1;
    for (int i=1; i<=y; i++)
        res *=x;
    return res;
}
int main()
{

    int q;
    cin >> q;
    while (q--) {
        long long n;
        cin >> n;
        string val;
        long long rep = n;
        while (rep > 0) {
            val += (char)(rep%3 + '0');
            rep/=3;
        }
        val += '0';
        int id = -1;
        for (int i=0; i<val.size()-1; i++) {
            if (val[i] == '2') {
                id = i;
                val[i+1] += 1;
            }
            if (val[i] == '3') {
                val[i] = '0';
                val[i+1] += 1;
            }
        }
        long long ans = 0;
        for (long long i=id+1; i<val.size(); i++) {
            if (val[i] == '1') ans += pwr(3 , i);
        }
        cout <<ans<<endl;
    }

}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int arr[1000];
int main()
{

	int q;
	cin>>q;
	for(int i=0;i<q;i++)
	{
		double x;
		cin>>x;
		long long ans ;
		ans = log(x)/log(3);
		long long check =0;
		check = (1/2.0)*(pow(3,ans+1)-1);
		if(check==x)
		{
			cout<<x<<endl;
		}
		else
		{
			cout<<pow(3,ans+1)<<endl;
		}

	}

}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int arr[1000];
int main()
{

	int q;
	cin>>q;
	for(int i=0;i<q;i++)
	{
		double x;
		cin>>x;
		long long ans ;
		ans = log(x)/log(3);
		if(pow(3,ans)==x)
		{
			cout<<x<<endl;
			continue;
		}
		long long check =0;
		check = (1/2.0)*(pow(3,ans+1)-1);
		if(check==x)
		{
			cout<<x<<endl;
		}
		else
		{
			cout<<pow(3,ans+1)<<endl;
		}

	}

}


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

	int q;
	cin>>q;
	for(int i=0;i<q;i++)
	{
		long long x;
		cin>>x;
		long long ans ;
		ans = log(x)/log(3);
		if(pow(3,ans)==x)
		{
			cout<<x<<endl;
			continue;
		}
		double check =0;
		check = (1/2.0)*(pow(3,ans+1)-1);
		if(check==x)
		{
			cout<<x<<endl;
		}
		else
		{
			cout<<pow(3,ans+1)<<endl;
		}

	}

}


#include<bits/stdc++.h>
using namespace std;
long long solve(int n){
	vector<int>v;
	int temp=n;
	while(temp>0){
		v.push_back(temp%3);
		temp/=3;
	}
	long long ans=0;
	//cout<<endl;
	for(int i=0;i<v.size();i++){
		if(v[i]>1){
			ans+=(3-v[i])*pow(3,i);
			v[i+1]++;
		}
	}
	return min(n+ans,(long long)pow(3,v.size()));
}
int main(){
	int t;
	cin>>t;
	for(int c=1;c<=t;c++){
		int n;
		cin>>n;
		cout<<solve(n)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
unsigned long long f[40];
unsigned long  long solve(long long n){
	vector<int>v;
	long long temp=n;
	while(temp>0){
		v.push_back(temp%3);
		temp/=3;
	}
	int pos=v.size();
	int c=0;
	for(int i=v.size()-1;i>=0;i--){
		if(v[i]==0){
			pos=i;
		}
		else{
			if(v[i]==2){
				c=1;
				break;
			}
		}
	}
	if(c==0)
		return n;
	unsigned long long ans=0;
	ans+=f[pos];
	for(int i=pos+1;i<v.size();i++){
		ans+=f[i];
	}
	return ans;
}
int main(){
	f[0]=1;
	for(int i=1;i<=38;i++){
		f[i]=f[i-1];
		f[i]*=3;
	}
	int t;
	cin>>t;
	for(int c=1;c<=t;c++){
		long long n;
		cin>>n;
		cout<<solve(n)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
long long solve(int n){
	vector<int>v;
	int temp=n;
	while(temp>0){
		v.push_back(temp%3);
		temp/=3;
	}
	long long ans=0;
	//cout<<endl;
	int m=v.size();
	for(int i=0;i<v.size();i++){
		if(v[i]>1){
			ans+=(3-v[i])*pow(3,i);
		    if(i+1<v.size())
				v[i+1]++;
			else
				v.push_back(1);
		}
	}
	return min(n+ans,(long long)pow(3,m));
}
int main(){
	int t;
	cin>>t;
	for(int c=1;c<=t;c++){
		int n;
		cin>>n;
		cout<<solve(n)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
unsigned long long f[21];
unsigned long long solve(long long n){
	vector<int>v;
	unsigned long long temp=n;
	while(temp>0){
		v.push_back(temp%3);
		temp/=3;
	}
	unsigned long long ans=0;
/*	for(int i=0;i<v.size();i++){
	    cout<<v[i]<<" ";
	}
	cout<<endl;*/
	int m=v.size();
	temp=0;
	for(int i=0;i<v.size();i++){
		if(v[i]>=2){
		    //cout<<f[i]-temp<<endl;
		    if(v[i]==2)
		        ans+=(pow(3,i)-temp);
		    //cout<<i<<" "<<(pow(3,i)-temp)<<endl;
		    temp=0;
		    if(i+1<v.size()){
		        v[i+1]++;
		    }
		    else
		        v.push_back(1);
		}
		else{
		    temp+=v[i]*pow(3,i);
		}
	}
	return min(n+ans,(unsigned long long)pow(3,m));
}
int main(){
	int t;
	cin>>t;
	for(int c=1;c<=t;c++){
		long long n;
		cin>>n;
		cout<<solve(n)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
unsigned long long f[20];
unsigned long long solve(long long n){
	vector<int>v;
	unsigned long long temp=n;
	while(temp>0){
		v.push_back(temp%3);
		temp/=3;
	}
	unsigned long long ans=0;
/*	for(int i=0;i<v.size();i++){
	    cout<<v[i]<<" ";
	}
	cout<<endl;*/
	int m=v.size();
	temp=0;
	for(int i=0;i<v.size();i++){
		if(v[i]>=2){
		    //cout<<f[i]-temp<<endl;
		    ans+=(f[i]-temp);
		    temp=0;
		    if(i+1<v.size()){
		        v[i+1]++;
		    }
		    else
		        v.push_back(1);
		}
		else{
		    temp+=v[i]*pow(3,i);
		}
	}
	return min(n+ans,(unsigned long long)pow(3,m));
}
int main(){
	int t;
	cin>>t;
	f[0]=1;
	for(int i=1;i<=100;i++){
	    f[i]=f[i-1];
	    f[i]*=3;
	}
	for(int c=1;c<=t;c++){
		long long n;
		cin>>n;
		cout<<solve(n)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
unsigned long long solve(long long n){
	vector<int>v;
	unsigned long long temp=n;
	while(temp>0){
		v.push_back(temp%3);
		temp/=3;
	}
	unsigned long long ans=0;
	/*for(int i=0;i<v.size();i++){
	    cout<<v[i]<<" ";
	}*/
	//cout<<endl;
	int m=v.size();
	temp=0;
	for(int i=0;i<v.size();i++){
		if(v[i]==2){
		    ans+=(pow(3,i)-temp);
		    temp=0;
		    if(i+1<v.size()){
		        v[i+1]++;
		    }
		    else
		        v.push_back(1);
		}
		else{
		    temp+=v[i]*pow(3,i);
		}
	}
	//return min(n+ans,(long long)pow(3,m));
	return n+ans;
}
int main(){
	int t;
	cin>>t;
	for(int c=1;c<=t;c++){
		long long n;
		cin>>n;
		cout<<solve(n)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
long long solve(int n){
	vector<int>v;
	long long temp=n;
	while(temp>0){
		v.push_back(temp%3);
		temp/=3;
	}
	long long ans=0;
	/*for(int i=0;i<v.size();i++){
	    cout<<v[i]<<" ";
	}*/
	//cout<<endl;
	int m=v.size();
	temp=0;
	for(int i=0;i<v.size();i++){
		if(v[i]==2){
		    ans+=(pow(3,i)-temp);
		    temp=0;
		    if(i+1<v.size()){
		        v[i+1]++;
		    }
		    else
		        v.push_back(1);
		}
		else{
		    temp+=v[i]*pow(3,i);
		}
	}
	//return min(n+ans,(long long)pow(3,m));
	return n+ans;
}
int main(){
	int t;
	cin>>t;
	for(int c=1;c<=t;c++){
		int n;
		cin>>n;
		cout<<solve(n)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
unsigned long long f[40];
unsigned long  long solve(long long n){
	vector<int>v;
	long long temp=n;
	while(temp>0){
		v.push_back(temp%3);
		temp/=3;
	}
	int pos=v.size();
	int c=0;
	for(int i=v.size()-1;i>=0;i--){
		if(v[i]==0){
			pos=i;
		}
		else{
			if(v[i]==2){
				c=1;
				break;
			}
		}
	}
//	cout<<pos<<endl;
	if(c==0)
		return n;
	unsigned long long ans=0;
	ans+=f[pos];
	//cout<<ans<<endl;
	for(int i=pos+1;i<v.size();i++){
		ans+=(v[i]*f[i]);
	}
	return ans;
}
int main(){
	f[0]=1;
	for(int i=1;i<=38;i++){
		f[i]=f[i-1];
		f[i]*=3;
	}
	int t;
	cin>>t;
	for(int c=1;c<=t;c++){
		long long n;
		cin>>n;
		cout<<solve(n)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
long long solve(int n){
	vector<int>v;
	int temp=n;
	while(temp>0){
		v.push_back(temp%3);
		temp/=3;
	}
	long long ans=0;
	//cout<<endl;
	for(int i=0;i<v.size();i++){
		if(v[i]>1){
			ans+=(3-v[i])*pow(3,i);
		    if(i+1<v.size())
				v[i+1]++;
			else
				v.push_back(1);
		}
	}
	return min(n+ans,(long long)pow(3,v.size()));
}
int main(){
	int t;
	cin>>t;
	for(int c=1;c<=t;c++){
		int n;
		cin>>n;
		cout<<solve(n)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
unsigned long long f[20];
unsigned long long solve(long long n){
	vector<int>v;
	unsigned long long temp=n;
	while(temp>0){
		v.push_back(temp%3);
		temp/=3;
	}
	unsigned long long ans=0;
	/*for(int i=0;i<v.size();i++){
	    cout<<v[i]<<" ";
	}*/
	//cout<<endl;
	int m=v.size();
	temp=0;
	for(int i=0;i<v.size();i++){
		if(v[i]==2){
		    ans+=(f[i]-temp);
		    temp=0;
		    if(i+1<v.size()){
		        v[i+1]++;
		    }
		    else
		        v.push_back(1);
		}
		else{
		    temp+=v[i]*pow(3,i);
		}
	}
	return min(n+ans,(unsigned long long)pow(3,m));
}
int main(){
	int t;
	cin>>t;
	f[0]=1;
	for(int i=1;i<=100;i++){
	    f[i]=f[i-1];
	    f[i]*=3;
	}
	for(int c=1;c<=t;c++){
		long long n;
		cin>>n;
		cout<<solve(n)<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
unsigned long long f[21];
unsigned long long solve(long long n){
	vector<int>v;
	unsigned long long temp=n;
	while(temp>0){
		v.push_back(temp%3);
		temp/=3;
	}
	unsigned long long ans=0;
/*	for(int i=0;i<v.size();i++){
	    cout<<v[i]<<" ";
	}
	cout<<endl;*/
	int m=v.size();
	temp=0;
	for(int i=0;i<v.size();i++){
		if(v[i]>=2){
		    //cout<<f[i]-temp<<endl;
		    ans+=(pow(3,i)-temp);
		    temp=0;
		    if(i+1<v.size()){
		        v[i+1]++;
		    }
		    else
		        v.push_back(1);
		}
		else{
		    temp+=v[i]*pow(3,i);
		}
	}
	return min(n+ans,(unsigned long long)pow(3,m));
}
int main(){
	int t;
	cin>>t;
	for(int c=1;c<=t;c++){
		long long n;
		cin>>n;
		cout<<solve(n)<<endl;
	}
}


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

/***
A

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

typedef long long ll;

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

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

using namespace std;
typedef long long ll;

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

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

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

1
3
9
13
27
6561
19683

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

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

/*
7
1
2
6
13
14
3620
10000

1
3
9
13
27
6561
19683

*/

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


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

/***
A

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

typedef long long ll;

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

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

using namespace std;
typedef long long ll;

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

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

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

1
3
9
13
27
6561
19683

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

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

/*
7
1
2
6
13
14
3620
10000

1
3
9
13
27
6561
19683

*/

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


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

/***
A

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

typedef long long ll;

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

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

using namespace std;
typedef long long ll;

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

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

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

1
3
9
13
27
6561
19683

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

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

/*
7
1
2
6
13
14
3620
10000

1
3
9
13
27
6561
19683

*/

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