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
//  package Jobeel;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
import java.util.Map.*;

public class codeforces {
	static int [] size,arr;
	static int inv;
	static HashSet<StringBuilder> set_1;
	static TreeSet<Long> set; 
	static void solve(int index , int mask) {
		if(index==arr.length) {
			long sum=0;
			for(int i=0;i<arr.length;i++) {
				if((mask & 1<<i)!=0) sum+=arr[i];
				
			}
			set.add(sum);
			return;
		}
		solve(index+1,mask | 1<<index);
		solve(index+1,mask);
	}
	public static int find(int i) {
		if(arr[i]==i) return i;
		else {
			int result=find(arr[i]);
			arr[i]=result;
			return result;
		}
	}
public static void union(int u,int v) {
		int root_u=find(u);
		int root_v=find(v);
		if (root_u==root_v) return;
		else if(size[root_u]>size[root_v]) {
			size[root_u]+=size[root_v];
			arr[root_v]=root_u;
		}else  {
			size[root_v]+=size[root_u];
			arr[root_u]=root_v;
		}
	}
    static int search(int x,pair [] arr) {
    	int low=0,high=arr.length-1,ans=-1;
    	while(low<=high) {
    		int mid=(low+high)>>1;
    	    if(arr[mid].x>=x) {ans=mid; high=mid-1;}
    	    else low=mid+1;
    	}
    	return ans;
    }
   static  long gcd(long a, long b) 
    { 
        if (b == 0) 
            return a; 
        return gcd(b, a % b);  
          
    } 
   public static long largestDivisor(long n){
	   long divisor = 0;
	   long  w=n>>1;
	   for (int i = 1; i <= w; i++)
	       if (n % i == 0) 
	           divisor = i;

	   return divisor; 
	}
   static void display(char [] ch) {
	   for(int i=0;i<ch.length;i++) pw.print(ch[i]);
   }
       public static void main(String [] args) throws IOException, InterruptedException {
	    InputReader sc=new InputReader(System.in);
	    arr=new int [10];
	    arr[0]=1;
	    for(int i=1;i<10;i++)arr[i]=3*arr[i-1];
	    //pw.println(Arrays.toString(arr));
	    set=new TreeSet<>();
	    int t=sc.nextInt();
    	solve(0, 0);
	    while(t-->0) {
	    	int x=sc.nextInt();
	    	pw.println(set.ceiling((long)x));
	    }
	//    pw.println(set);
	     pw.flush();
		pw.close();
	}
	static class pair implements Comparable<pair>{
		String  name; int x,y ;
		
		
		public pair (int x,int y) {
			this.x=x; this.y=y;
		}
		public int compareTo(pair o) {
			return o.x-x;
		}
		public String toString() {
			return x+" "+y;
		}
	}

	static 	PrintWriter pw=new PrintWriter(System.out);
	public static StringBuilder toString(int [] arr,int l,int r) {
		StringBuilder sb=new StringBuilder();
		for(int i=l;i<=r;i++) sb.append(arr[i]+" ");
		return sb;
	}
	public static StringBuilder toString(long [] arr,int l,int r) {
		StringBuilder sb=new StringBuilder();
		for(int i=l;i<=r;i++) sb.append(arr[i]+" ");
		return sb;
	}
	public static StringBuilder toString(char [] arr,int l,int r) {
		StringBuilder sb=new StringBuilder();
		for(int i=l;i<=r;i++) sb.append(arr[i]);
		return sb;
	}
	static class InputReader  {
		 
		private InputStream in;
		private byte[] buffer = new byte[1024];
		private int curbuf;
		private int lenbuf;
 
		public InputReader(InputStream in) {
			this.in = in;
			this.curbuf = this.lenbuf = 0;
		}
 
		public boolean hasNextByte() {
			if (curbuf >= lenbuf) {
				curbuf = 0;
				try {
					lenbuf = in.read(buffer);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return false;
			}
			return true;
		}
 
		private int readByte() {
			if (hasNextByte())
				return buffer[curbuf++];
			else
				return -1;
		}
 
		private boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}
 
		private void skip() {
			while (hasNextByte() && isSpaceChar(buffer[curbuf]))
				curbuf++;
		}
 
		public boolean hasNext() {
			skip();
			return hasNextByte();
		}
 
		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (!isSpaceChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}
 
		public int nextInt() {
			if (!hasNext())
				throw new NoSuchElementException();
			int c = readByte();
			while (isSpaceChar(c))
				c = readByte();
			boolean minus = false;
			if (c == '-') {
				minus = true;
				c = readByte();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res = res * 10 + c - '0';
				c = readByte();
			} while (!isSpaceChar(c));
			return (minus) ? -res : res;
		}
 
		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			int c = readByte();
			while (isSpaceChar(c))
				c = readByte();
			boolean minus = false;
			if (c == '-') {
				minus = true;
				c = readByte();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res = res * 10 + c - '0';
				c = readByte();
			} while (!isSpaceChar(c));
			return (minus) ? -res : res;
		}
 
		public double nextDouble() {
			return Double.parseDouble(next());
		}
 
		public int[] nextIntArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}
 
		public long[] nextLongArray(int n) {
			long[] a = new long[n];
			for (int i = 0; i < n; i++)
				a[i] = nextLong();
			return a;
		}
 
		public char[][] nextCharMap(int n, int m) {
			char[][] map = new char[n][m];
			for (int i = 0; i < n; i++)
				map[i] = next().toCharArray();
			return map;
		}
	}

	}