#include <bits/stdc++.h>
using namespace std;
long long md = (1e9) * 1e9 + 7;
long long MOD = 1e9 + 7;
int INF = 1e9 + 7;
long long binpow(long long a, long long n) {
  if (n == 0) return 1;
  if (n % 2 == 1)
    return (binpow(a, n - 1) * a) % MOD;
  else {
    int b = binpow(a, n / 2) % MOD;
    return (b * b) % MOD;
  }
}
pair<int, long long> rev(pair<long long, int> x) {
  return make_pair(x.second, x.first);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  long long p[45], pref[45];
  p[0] = 1;
  pref[0] = 1;
  for (int i = 1; i < 41; i++) {
    p[i] = 3 * p[i - 1];
    pref[i] = pref[i - 1] + p[i];
  }
  while (q--) {
    long long n, ans;
    cin >> n;
    if (n == 1) {
      cout << 1 << endl;
      continue;
    }
    for (int i = 0; i < 41; i++) {
      if (p[i] == n) {
        ans = n;
        break;
      } else if (p[i] > n) {
        if (pref[i - 1] < n)
          ans = p[i];
        else {
          ans = pref[i - 1];
          for (int j = i - 1; j >= 0; j--)
            if (ans - p[j] >= n) {
              ans -= p[j];
            }
        }
        break;
      }
    }
    cout << ans << endl;
  }
}
