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.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;

public class div3 {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int q=sc.nextInt();
		while(q-->0) {
			long n=sc.nextLong();
			String s=Long.toString(n, 3);
			boolean found=false;
			for(int i=0;i<s.length()&&!found;i++) {
				if(s.charAt(i)=='2') {
					found=true;
				}
			}
			if(!found) {
				pw.println(n);
			}else {
				boolean runOf2=false;
				for(int i=0;i<s.length();i++) {
					if(s.charAt(i)==
							'2') {
						runOf2=true;
						break;
					}else if(s.charAt(i)=='0') {
						break;
					}
				}
				if(runOf2) {
					pw.println(power(3,s.length()).toString());
						
				}else {
				char[] arr=s.toCharArray();
				
				int idx=0;
				for(int i=0;i<arr.length;i++) {
					if(arr[i]=='2') {
						idx=i;
						break;
					}
				}
				for(int i=idx;i>=0;i--) {
					if(arr[i]=='0') {
						arr[i]='1';break;
						
					}else {
						arr[i]='0';
					}
				}
				for(int i=idx;i<arr.length;i++)
					arr[i]='0';
			
				long ans=0;
				
				for(int i=0;i<arr.length;i++) {
					ans*=3;
					ans+=arr[i]-'0';
				}
				pw.println(ans);
				}
			}
		}
		pw.flush();
	}
	public static BigInteger power(long x ,int p) {// loop O(log p )
		BigInteger ans=new BigInteger("1");
		while(p>0) {
			
			if((p&1)==1) 
				ans= ans.multiply(new BigInteger(""+x));
		x=(x*x);
		p>>=1;
		}
		return ans;
	}

	static class UDS {
	int[] parent,rank,setSize;
	int n;//number of sets

	public UDS(int n) {

		this.n=n;
		parent=new int[n];
		rank=new int[n];
		setSize=new int[n];
		Arrays.fill(setSize, 1);
		for(int i=0;i<n;i++)parent[i]=i;
	}

	public  void unionSet(int i, int j) {//it can be boolean to tell me if i made the union or not
	if(!isSameSet(i, j)) {
		n--;
		int p1=findSet(i);
		int p2=findSet(j);
	if(rank[p1]<rank[p2]) {
			parent[p1]=p2;
			setSize[p2]+=setSize[p1];
		}else {
			parent[p2]=p1;
			setSize[p1]+=setSize[p2];
			if(rank[p1]==rank[p2])rank[p1]++;
		}
		
	}
	}

	public int sizeOfSet(int i) {return setSize[findSet(i)];}

	public int numDisjointSets() {return n;}

	public boolean isSameSet(int i,int j) {
		return findSet(i)==findSet(j);
	}

	public int findSet(int i) {
		return (parent[i]==i)? i : (parent[i]=findSet(parent[i]));
	}
		
	}

	

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		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 {
			String x = next();
			StringBuilder sb = new StringBuilder("0");
			double res = 0, f = 1;
			boolean dec = false, neg = false;
			int start = 0;
			if (x.charAt(0) == '-') {
				neg = true;
				start++;
			}
			for (int i = start; i < x.length(); i++) {
				if (x.charAt(i) == '.') {
					res = Long.parseLong(sb.toString());
					sb = new StringBuilder("0");
					dec = true;
				} else {
					sb.append(x.charAt(i));
					if (dec)
						f *= 10;
				}
				if (sb.length() == 18) {
					res += Long.parseLong(sb.toString()) / f;
					sb = new StringBuilder("0");
				}
			}
			res += Long.parseLong(sb.toString()) / f;
			return res * (neg ? -1 : 1);
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

	}

	public static void shuffle(int[] a) {
		int n = a.length;
		for (int i = 0; i < n; i++) {
			int r = i + (int) (Math.random() * (n - i));
			int tmp = a[i];
			a[i] = a[r];
			a[r] = tmp;
		}
	}
}
