#include <bits/stdc++.h>
using namespace std;
bool pr[17000007 + 3];
int p[17000007 / 2], pz = 0;
void sieve() {
  int lim = sqrt(17000007);
  p[pz++] = 2;
  for (int i = 3; i < 17000007; i += 2) {
    if (!pr[i]) {
      p[pz++] = i;
      if (i <= lim) {
        for (int j = i * i; j < 17000007; j += 2 * i) pr[j] = 1;
      }
    }
  }
}
vector<pair<int, int> > factors;
void factorize(int n) {
  factors.clear();
  for (int i = 0; i < pz && p[i] * p[i] <= n; i++) {
    if (n % p[i] == 0) {
      int cnt = 0;
      while (n % p[i] == 0) n /= p[i], cnt++;
      factors.push_back({p[i], cnt});
    }
  }
  if (n > 1) factors.push_back({n, 1});
}
long long pp[1000], sp[1000];
long long good[17000007];
long long mx;
int cnt1 = 0;
void solve(int pos, long long sum) {
  if (pos == 0) {
    mx = max(mx, sum);
    cnt1++;
    ;
    return;
  }
  solve(pos - 1, sum);
  solve(pos - 1, sum + pp[pos]);
}
vector<long long> v1, v2;
int main() {
  pp[0] = 1;
  sp[0] = 1;
  for (int i = int(1); i < int(40); i++) {
    pp[i] = pp[i - 1] * 3;
    sp[i] = sp[i - 1] + pp[i];
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    long long n;
    scanf("%lld", &n);
    long long sum = 0;
    int i = 39;
    while (i >= 0 and n > 0) {
      if (!i) {
        sum += pp[i];
        n -= pp[i];
        break;
      }
      if (sp[i - 1] < n) {
        sum += pp[i];
        n -= pp[i];
      }
      i--;
    }
    printf("%lld\n", sum);
  }
  return 0;
}
