The only difference between easy and hard versions is the maximum value of n.

You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.

The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).

For example:

  * 30 is a good number: 30 = 3^3 + 3^1, 
  * 1 is a good number: 1 = 3^0, 
  * 12 is a good number: 12 = 3^2 + 3^1, 
  * but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0), 
  * 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid), 
  * 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid). 



Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.

For the given positive integer n find such smallest m (n ≤ m) that m is a good number.

You have to answer q independent queries.

Input

The first line of the input contains one integer q (1 ≤ q ≤ 500) — the number of queries. Then q queries follow.

The only line of the query contains one integer n (1 ≤ n ≤ 10^4).

Output

For each query, print such smallest integer m (where n ≤ m) that m is a good number.

Example

Input


7
1
2
6
13
14
3620
10000


Output


1
3
9
13
27
6561
19683
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//package solution;

/**
 *
 * @author USER
 */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.text.*;
public class Solution{     
    static class InputReader { 
        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;
        public InputReader(InputStream st) {
            this.stream = st;
        } 
        public int read() {
            if (snumChars == -1)
                throw new InputMismatchException();
            if (curChar >= snumChars) {
                curChar = 0;
                try {
                    snumChars = stream.read(buf);
                } 
                catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (snumChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }
        public int ni() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        } 
        public long nl() {
            int c = read();
            while (isSpaceChar(c)) {
            c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        } 
        public int[] nia(int n) {
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = ni();
            }
            return a;
        } 
        public String rs() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                    c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }
        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        } 
        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
 
        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        } 
    }
    public static class Key {

        private final int x;
        private final int y;

        public Key(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Key)) return false;
            Key key = (Key) o;
            return x == key.x && y == key.y;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }

    }
    
    static PrintWriter w = new PrintWriter(System.out);
    static long mod=998244353L,mod1=1000000007;
    static int co[][];
    static void dsort(){
        Arrays.parallelSort(co,(o1,o2)->o1[0]!=o2[0]?o1[0]-o2[0]:o1[1]-o2[1]);
    }
     static long  power(long x, long y) { 
        long temp=0; 
        if( y == 0) 
            return 1; 
        temp = power(x, y/2); 
        if (y%2 == 0) 
            return (temp*temp)%mod1; 
        else
            return (((x*temp)%mod1)*temp)%mod1; 
    } 
    static long modInverse(long a, long m){
        long num=m;
        long x=1;
        long power=a%mod1;
        while(num>0){
              if(num%2==1){
                  x=(x*power)%mod1;
            }
            num>>=1;
            power=(power*power)%mod1;         
        }
        return x;
    }
    static long ncrWithoutMod(int n,int r){
        if(n<r)return 0L;
        long ans=fact[n]/(fact[r]*fact[n-r]);
        return ans;
    }
    static long fact[]=new long[1000005];
    public static void findFact(){
        fact[0]=1;
        for(int i=1;i<=1000000;i++){
            fact[i]=(i*fact[i-1])%mod1;
        }
    } 
    
   
    public static void main(String [] args){
        InputReader sdf=new InputReader(System.in);
        Scanner sc=new Scanner(System.in);
                  int t=sc.nextInt();
            next: while(t-->0){
               long n=sc.nextLong();
               String s=Long.toString(n, 3);
               s="0"+s;
               char[]ch=s.toCharArray();
               in: for(int i=1;i<ch.length;i++){
                   if(ch[i]=='2'){
                       for(int k=i;k<ch.length;k++){
                           ch[k]='0';
                       }
                       for(int k=i-1;k>=0;k--){
                           if(ch[k]=='0'){
                               ch[k]='1';
                               break in;
                           }
                           else ch[k]='0';
                       }
                   }
               }
               s=ch.toString();
               String ss="";
               for(int i=0;i<ch.length;i++){
                   ss=ss+ch[i];
               }
               n=(long)Long.parseLong(ss,3);
               System.out.println(n);
            }
        w.close();
    
}
}
                
        
//        
//        
//        
//      ///RKC 
//
//    static long ncr(long n, long r) 
//    {    if(r==0)return 1;
//            else {
//                long ans=n%M;
//                ans*=ncr(n-1,r-1)%M;
//                ans%=M;
//                ans*=inverse(r)%M;
//                ans%=M;return ans;
//                        
//    }
//    
//    }
//    static long fast_exp(long x,long n){
//        long ans=1;
//        while(n>0){
//            if((n&1)!=0){
//                ans*=x;
//                ans%=M;
//            }
//            n=n>>1;
//            x=(x*x)%M;
//        }
//        return ans;
//    }
//static long inverse(long x){return fast_exp(x,M-2);}
//    
//      
//        ///RKC
//
//        
//        
//        
//        static long bigpowmod(long a,long n,long m){
//            a=a%m;
//            long res=1;
//            while(n>0){
//                if((n&1)!=0){
//                    res=(res*a);
//                    res=res%m;
//                }
//                a=(a*a)%m;
//                n=n>>1;
// 
//            }
//            
//            return res;
//        }
//        
//        static long printNcR(long n, long r) { 
//  
//        // p holds the value of n*(n-1)*(n-2)...,  
//        // k holds the value of r*(r-1)...  
//        long p = 1, k = 1; 
//  
//        // C(n, r) == C(n, n-r),  
//        // choosing the smaller value  
//        if (n - r < r) { 
//            r = n - r; 
//        } 
//  
//        if (r != 0) { 
//            while (r > 0) { 
//                p *= n; 
//                k *= r; 
//  
//                // gcd of p, k  
//                long m = __gcd(p, k); 
//  
//                // dividing by gcd, to simplify product  
//                // division by their gcd saves from the overflow  
//                p /= m; 
//                k /= m; 
//  
//                n--; 
//                r--; 
//            } 
//  
//            // k should be simplified to 1  
//            // as C(n, r) is a natural number  
//            // (denominator should be 1 ) .  
//        } else { 
//            p = 1; 
//        } 
//  
//        // if our approach is correct p = ans and k =1  
//       return p;
//    } 
//  
//    static long __gcd(long n1, long n2) { 
//        long gcd = 1; 
//  
//        for (int i = 1; i <= n1 && i <= n2; ++i) { 
//            // Checks if i is factor of both integers 
//            if (n1 % i == 0 && n2 % i == 0) { 
//                gcd = i; 
//            } 
//        } 
//        return gcd; 
//    } 
//        static long power(int x,int y) 
//        { 
//            long temp; 
//            if( y == 0) 
//                return 1; 
//                temp = power(x, y/2); 
//                if (y%2 == 0) 
//                     return temp*temp; 
//                    else
//                    return x*temp*temp; 
//} 
//   
//
//
//
//static class Reader 
//    { 
//        final private int BUFFER_SIZE = 1 << 16; 
//        private DataInputStream din; 
//        private byte[] buffer; 
//        private int bufferPointer, bytesRead; 
//  
//        public Reader() 
//        { 
//            din = new DataInputStream(System.in); 
//            buffer = new byte[BUFFER_SIZE]; 
//            bufferPointer = bytesRead = 0; 
//        } 
//  
//        public Reader(String file_name) throws IOException 
//        { 
//            din = new DataInputStream(new FileInputStream(file_name)); 
//            buffer = new byte[BUFFER_SIZE]; 
//            bufferPointer = bytesRead = 0; 
//        } 
//  
//        public String readLine() throws IOException 
//        { 
//            byte[] buf = new byte[64]; // line length 
//            int cnt = 0, c; 
//            while ((c = read()) != -1) 
//            { 
//                if (c == '\n') 
//                    break; 
//                buf[cnt++] = (byte) c; 
//            } 
//            return new String(buf, 0, cnt); 
//        } 
//  
//        public int nextInt() throws IOException 
//        { 
//            int ret = 0; 
//            byte c = read(); 
//            while (c <= ' ') 
//                c = read(); 
//            boolean neg = (c == '-'); 
//            if (neg) 
//                c = read(); 
//            do
//            { 
//                ret = ret * 10 + c - '0'; 
//            }  while ((c = read()) >= '0' && c <= '9'); 
//  
//            if (neg) 
//                return -ret; 
//            return ret; 
//        } 
//  
//        public long nextLong() throws IOException 
//        { 
//            long ret = 0; 
//            byte c = read(); 
//            while (c <= ' ') 
//                c = read(); 
//            boolean neg = (c == '-'); 
//            if (neg) 
//                c = read(); 
//            do { 
//                ret = ret * 10 + c - '0'; 
//            } 
//            while ((c = read()) >= '0' && c <= '9'); 
//            if (neg) 
//                return -ret; 
//            return ret; 
//        } 
//  
//        public double nextDouble() throws IOException 
//        { 
//            double ret = 0, div = 1; 
//            byte c = read(); 
//            while (c <= ' ') 
//                c = read(); 
//            boolean neg = (c == '-'); 
//            if (neg) 
//                c = read(); 
//  
//            do { 
//                ret = ret * 10 + c - '0'; 
//            } 
//            while ((c = read()) >= '0' && c <= '9'); 
//  
//            if (c == '.') 
//            { 
//                while ((c = read()) >= '0' && c <= '9') 
//                { 
//                    ret += (c - '0') / (div *= 10); 
//                } 
//            } 
//  
//            if (neg) 
//                return -ret; 
//            return ret; 
//        } 
//  
//        private void fillBuffer() throws IOException 
//        { 
//            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); 
//            if (bytesRead == -1) 
//                buffer[0] = -1; 
//        } 
//  
//        private byte read() throws IOException 
//        { 
//            if (bufferPointer == bytesRead) 
//                fillBuffer(); 
//            return buffer[bufferPointer++]; 
//        } 
//  
//        public void close() throws IOException 
//        { 
//            if (din == null) 
//                return; 
//            din.close(); 
//        } 
//    } 
//  
//    
//}
//


class MySort {
//    public void selectionSort(int[]a,int left,int right){
//        //left index from 0 and right= last index
//        for(int i=left;i<=right;i++){
//            int min_index=i;
//            for(int j=i+1;j<=right;j++){
//                if(a[min_index]>a[j])min_index=j;
//            }
//            int temp=a[min_index];
//            a[min_index]=a[i];
//            a[i]=temp;
//        }
//    }
//    
//    public void bubbleSort(int a[],int  left,int right){
//        //left index from 0 and right= last index
//        
//        for(int i=left;i<=right;i++){
//            for(int j=left;j<right;j++){
//                if(a[j+1]<a[j]){
//                    int temp=a[j+1];
//                    a[j+1]=a[j];
//                    a[j]=temp;
//                }
//            }
//        }
//    }
//    
//    
//    public void insertionSort(int a[],int left,int right){
//        //left index from 0 and right= last index
//        for(int i=left+1;i<=right;i++){
//            int temp=a[i];
//            int j=i-1;
//            for(;j>=0&&temp<a[j];j--){
//                a[j+1]=a[j];
//            }
//            a[j+1]=temp;
//        }
//    }
//    
//    public void quickSort(int a[],int left,int right){
//        //left index from 0 and right= last index
//        if(left<right){
//            int pi_index=partition(a,left,right);
//            
//            quickSort(a,pi_index+1,right);//for left side of pivot
//            quickSort(a,left,pi_index-1);//for right side of pivot
//            
//        }
//        
//    }
//    private int partition(int a[],int left,int right){
//            //last element is used as pivot
//            int pivot=a[right];
//            int i=left-1;
//            for(int j=left;j<right;j++){
//                if(pivot>a[j]){
//                    i++;
//                    int temp=a[i];
//                    a[i]=a[j];
//                    a[j]=temp;
//                }
//            }
//            int temp=a[i+1];
//            a[i+1]=a[right];
//            a[right]=temp;
//            return i+1;
//    }
//    
//    
    public void mergeSort(int []a,int left,int right){
        if(left<right){
            int m=(left+right)/2;
            mergeSort(a, left, m);
            mergeSort(a, m+1, right);
            merging(a,left,m,right);
            
        }
        
        
    }
    private void merging(int[]a,int left,int m,int right){
        int n1=m-left+1;
        int n2=right-(m+1)+1;
        int []L=new int[n1];
        int []R=new int[n2];
        
        for(int i=0;i<n1;i++){L[i]=a[left+i];}
        for(int i=0;i<n2;i++){R[i]=a[m+1+i];}
        
        int i=0;int j=0;
        int k=left;
        while(i<n1&&j<n2){
            if(L[i]<=R[j]){
                a[k]=L[i];
                i++;
            }
            else {
                a[k]=R[j];
                j++;
            }
            k++;
        }
        
        while(i<n1){
            a[k]=L[i];
            i++;
            k++;
        }
        while(j<n2){
            a[k]=R[j];
            j++;
            k++;
        }
    }
    
}

                
