#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
clock_t time_p = clock();
void rtime() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
long long power(long long a, long long b, long long m = mod) {
  long long ans = 1;
  a = a % m;
  while (b > 0) {
    if (b & 1) ans = (1ll * a * ans) % m;
    b >>= 1;
    a = (1ll * a * a) % m;
  }
  return ans;
}
long long rr[] = {0, 1, 1, 1, 0, -1, -1, -1};
long long cc[] = {1, 1, 0, -1, -1, -1, 0, 1};
const long long N = 100050;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long t = 1;
  cin >> t;
  while (t--) {
    long long pow_p[40], p = 1;
    for (long long i = 0; i < 39; i++) {
      pow_p[i] = p;
      p *= 3;
    }
    long long n, m, x = 0;
    cin >> n;
    m = n;
    for (long long i = 38; i >= 0; i--) {
      if (m >= pow_p[i]) {
        m -= pow_p[i];
        x += pow(2ll, i);
      }
    }
    if (m == 0) {
      cout << n << '\n';
    } else {
      x += 1;
      long long ans = 0;
      for (long long i = 39; i >= 0; i--) {
        if ((1ll << i) & x) {
          ans += pow_p[i];
        }
      }
      cout << ans << '\n';
    }
  }
  rtime();
  return 0;
}
