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
from math import *
from sys import *
from heapq import *
from collections import defaultdict
from itertools import permutations
import os, sys
from io import IOBase, BytesIO
M=10**9+7
 
def graph_as_tree_1_root():
    n=int(input())
    dict=defaultdict(list)
    for _ in range(n-1):
        x,y=list(map(int,input().split()))
        dict[x].append(y)
        dict[y].append(x)
    tree=[[1]]
    temp=[1]
    vall=0
    seen=set([i for i in range(2,n+1)])
    lol=[]
    while len(seen)>0:
        #print(seen)
        lol=list(seen)
        tt=[]
        for x in temp:
            aa=[]
            for val in dict[x]:
                if val in seen:
                    aa+=[val]
                    seen.remove(val)
            dict[x]=aa
            tt+=aa
        tree.append(tt)
        temp=tt
        vall+=1
    for x in lol:
        dict[x]=[]
    
    print(tree,dict)
def pow(a,b):
    res=1
    while b>0:
        if b&1:
            res*=a
        a*=a
        b>>=1
    return res
def powmod(a,b,m):
    res=1
    while b>0:
        if b&1:
            res=((res*a)%m)
        a=(a*a)%m
        b//=2
    return res
def inv(a,m):
    return powmod(a,m-2,m)
def alldivisors(n) : 
    list = []  
    arr=[]
    for i in range(1, int(sqrt(n) + 1)) :
        if (n % i == 0) : 
            if (n / i == i) : 
                arr+=[i]
            else :
                arr+=[i]
                list.append(n//i)  
    arr+=list[::-1]
    return arr
def primefactorisation(n):
    potentional_p = 3
    itog_list = defaultdict(int)
    if n % 2 == 0:
        itog_list[2] = 0
    while n % 2 == 0:
        n = n // 2
        itog_list[2] += 1
    while n - 1:
        if potentional_p > (n**0.5):
            itog_list[n] += 1
            return itog_list
        while n % potentional_p == 0:
            n = n // potentional_p
            itog_list[potentional_p] += 1
        potentional_p += 2
    return itog_list
 
def meth(x):
    
    lst=[]
    
    while x>0:
        val=int(log(x,3))
        lst=[val]+lst
        x-=3**val
    if len(lst)==3 and lst[0]==lst[1]==lst[2]:
        return [lst[0]]
    return lst
 
def modi(lst):
    if len(lst)==1:
        return lst
    fin=[]
    nn=len(lst)
    temp=[]
    for i in range(nn-1):
        if lst[i+1]-lst[i]>=2:
            temp+=[lst[i]]
            fin.append(temp)
            temp=[]
        else:
            temp+=[lst[i]]
    if temp!=[]:
        fin.append(temp)
    if lst[-1]-lst[-2]>=2:
        fin.append([lst[-1]])
    else:
        fin[-1].append(lst[-1])
    return fin
 
 
def main():
    t=int(input())
   
    for _ in range(t):
        n=int(input())
        
        lst=meth(n)
        if len(lst)==len(set(lst)):
            print(n)
            continue
        res=0
        flag=0
        #print(lst)
        for lst in modi(lst)[::-1]:
            if len(lst)==1:
                res+=3**lst[0]
                continue
            nn=len(lst)
            
            fl=0
            for i in range(nn-1):
                if lst[i+1]-lst[i]==0:
                    fl=1
                    break
            if fl==1:
                res+=3**(1+lst[-1])
                flag=1
            else:
                for x in lst:
                    res+=(3**x)
            if flag==1:
                break
                

                
        print(res)
        
 
 
 
 
 
 
 
 
BUFSIZE = 8192
class FastIO(BytesIO):
    newlines = 0
 
    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None
 
    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
        return s
 
    def read(self):
        while self._fill(): pass
        return super(FastIO,self).read()
 
    def readline(self):
        while self.newlines == 0:
            s = self._fill(); self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            self.truncate(0), self.seek(0)
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s:self.buffer.write(s.encode('ascii'))
        self.read = lambda:self.buffer.read().decode('ascii')
        self.readline = lambda:self.buffer.readline().decode('ascii')
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
 
if __name__ == '__main__':
    main()
#threading.Thread(target=main).start()
 
 
 