#include <bits/stdc++.h>
using namespace std;
char buf[(1 << 22)], *p1 = buf, *p2 = buf;
inline long long read() {
  char c =
      (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 22, stdin), p1 == p2)
           ? EOF
           : *p1++);
  long long x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 22, stdin), p1 == p2)
             ? EOF
             : *p1++);
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0',
    c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 22, stdin), p1 == p2)
             ? EOF
             : *p1++);
  return x * f;
}
const int maxn = 4005;
long long pow3[maxn] = {1}, sum[maxn] = {1};
long long ans = 0x3f3f3f3f, n, q, pow2[maxn] = {1};
bool check(long long m) {
  long long sum = 0;
  for (int i = 0; i < 40; i++) {
    sum += pow3[i] * ((m >> i) & 1);
    if (sum >= n) return true;
  }
  return false;
}
int main() {
  cin >> q;
  for (int i = 1; i < 40; i++) {
    pow3[i] = pow3[i - 1] * 3ll;
    sum[i] = sum[i - 1] + pow3[i];
  }
  for (int i = 1; i < 50; i++) pow2[i] = pow2[i - 1] * 2ll;
  while (q--) {
    cin >> n;
    long long l = 0, r = 1ll << 39;
    while (l < r) {
      long long m = (l + r) >> 1;
      if (check(m))
        r = m;
      else
        l = m + 1;
    }
    long long sum = 0;
    for (int i = 0; i < 40; i++) {
      sum += pow3[i] * ((r >> i) & 1);
    }
    printf("%lld\n", sum);
  }
  return 0;
}
