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
import java.io.*;
import java.util.*;
public class Main {
    static PrintWriter pw;
    static HashMap<Integer, Boolean> visit;
    static boolean[][] arr;
    static ArrayList<Pair> list1;
    static ArrayList<Pair> list2;
    //	static void dfs(int i, long c) {
//		visit[i]=true;
//		max=Math.max(c, max);
//		for(Pair p: adj[i]) {
//			if(!visit[p.x-1]) {
//				dfs(p.x-1, c+1l*p.y);
//			}
//		}
//	}
//	static Queue<Integer> q;
//	static void bfs(int i) {
//		visit[i].x=true;
//		visit[i].n=0;
//		q.add(i);
//		while(!q.isEmpty()){
//			int u = q.poll();
//			for(int v : adj[u]){
//				if(!visit[v].x){
//					q.add(v);
//					visit[v].x = true;
//					visit[v].n=visit[u].n+1;
//				}
//			}
//		}
//	}
//	static void tpsort(int i) {
//		visit[i]=true;
//		for(int x: adj[i]) {
//			if(!visit[x-1]) {
//				tpsort(x-1);
//			}
//		}
//		sticks.add(i+1);
//	}
    static int max;
    static int[] dx= {1,-1,0,0};
    static int[] dy= {0,0,1,-1};
    static boolean valid(int i, int j) {
        return i<arr.length && i>=0 && j<arr.length && j>=0;
    }
    //	static void dfs1(int i, int j) {
//		visit[i][j]=true;
//		for(int k=0; k<4; k++) {
//			if(valid(i+dx[k],j+dy[k]) && !visit[i+dx[k]][j+dy[k]] && (arr[i+dx[k]][j+dy[k]])) {
//				list1.add(new Pair(i+dx[k],j+dy[k]));
//				dfs1(i+dx[k],j+dy[k]);
//			}
//		}
//	}
    static int ans;
    static int[] first;
    static int[] sec;
    static void dfs(int[] arr, int i, int c) {
        if(visit.containsKey(i)) {
            if(!visit.get(i)) {
                visit.replace(i, true);
                first[i]=c;
            }else {
                sec[i]=c-first[i];
                visit.remove(i);
            }
        }
        if(!visit.isEmpty() && visit.containsKey(arr[i]-1)) {
            dfs(arr, arr[i]-1, c+1);
        }
    }
    static long ceildiv(long x,long y) {
        return x%y==0? x/y: x/y+1;
    }
    public static long gcf(long x, long y) {
        while(x%y!=0 && y%x!=0) {
            if(x>y)
                x%=y;
            else
                y%=x;
        }
        return x>y? y:x;
    }
    static long Pow(long a, int e){
        long res = 1;
        while (e > 0) {
            if ((e & 1) == 1)
                res = (res * a);
            a = (a * a) ;
            e >>= 1;
        }
        return res;
    }
    public static void main(String[] args) throws Exception {
        Scanner sc= new Scanner(System.in);
        pw = new PrintWriter(System.out);
        int t=sc.nextInt();
        while(t-->0) {
            long x=sc.nextLong();
            long temp=x;
            LinkedList<Character> link=new LinkedList<Character>();
            while(x>0){
                link.addFirst((char)(x%3+(int)('0')));
                x/=3;
            }
            int[] arr=new int[link.size()+1];
            int n=arr.length;
            for(int i=1; i<n; i++){
                arr[i]=(int)link.getFirst()-(int)'0';
                link.removeFirst();
            }
            int two=-1;
            for(int i=0; i<n; i++){
                if(arr[n-1-i]==2){
                    two=i;
                }
            }
            if(two==-1){
                pw.println(temp);
            }else{
                int idx=-1;
                for(int i=two+1; i<n; i++){
                    if(arr[n-1-i]==0) {
                        idx = n - 1 - i;
                        break;
                    }
                }
                arr[idx]=1;
                for(int i=idx+1; i<n; i++){
                    arr[i]=0;
                }
                long ans=0l;
                for(int i=0; i<n; i++){
                    ans+=arr[n-1-i]*1l*Pow(3, i);
                }
                pw.println(ans);
            }
        }
        pw.close();
    }
    static class Scanner {
        StringTokenizer st;
        BufferedReader br;
        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public Scanner(FileReader r) {
            br = new BufferedReader(r);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        public int[] nextArr(int n) throws IOException {
            int[] arr = new int[n];
            for (int i = 0; i < arr.length; i++)
                arr[i] = nextInt();
            return arr;
        }
        public Integer[] nextsort(int n) throws IOException{
            Integer[] arr=new Integer[n];
            for(int i=0; i<n; i++)
                arr[i]=nextInt();
            return arr;
        }
        public boolean ready() throws IOException {
            return br.ready();
        }
    }
    static class Pair implements Comparable<Pair>{
        int x;
        int y;
        public Pair(int a, int b) {
            this.x=a;
            this.y=b;
        }
        public int hashCode() {
            return (this.x+""+this.y).hashCode();
        }
        public int compareTo(Pair other) {
            if(this.x!=other.x)
                return this.x-other.x;
            else {
                return this.y-other.y;
            }
        }
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (this.getClass() != obj.getClass()) {
                return false;
            }
            Pair p=(Pair)obj;
            return this.x==p.x && this.y==p.y;
        }
    }
    static class visit{
        boolean x;
        int n;
        public visit(){
            this.x=false;
            this.n=-1;
        }
        public int compareTo(visit other) {
            return this.n-other.n;
        }
    }
    static class Tuple implements Comparable<Tuple>{
        int x;
        int y;
        boolean d;
        public Tuple(int a, int b, boolean t) {
            this.x=a;
            this.y=b;
            this.d=t;
        }
        public int compareTo(Tuple other) {
            if(this.d && !other.d) {
                return 1;
            }else if(!this.d && other.d) {
                return -1;
            }else {
                return this.y-other.y;
            }
        }
    }
}
