#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("avx,avx2,fma")
using namespace std;
long long power(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a);
    }
    a = (a * a);
    b >>= 1;
  }
  return res;
}
long long fermat_inv(long long y) { return power(y, 998244353 - 2); }
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
void solve() {
  long long n;
  cin >> n;
  long long nn = n;
  vector<long long> ans;
  long long f = -1;
  long long c = 0;
  while (n) {
    if (n % 3 == 2) {
      f = c;
    }
    ans.push_back(n % 3);
    c++;
    n /= 3;
  }
  if (f == -1) {
    cout << nn << "\n";
  } else {
    ans.push_back(0);
    long long z;
    for (long long i = f + 1; i < ans.size(); i++) {
      if (ans[i] == 0) {
        z = i;
        break;
      }
    }
    ans[z] = 1;
    for (long long i = z - 1; i >= 0; i--) {
      ans[i] = 0;
    }
    long long res = 0, pp = 1;
    for (long long i = 0; i < ans.size(); i++) {
      res += (ans[i] * pp);
      pp *= 3LL;
    }
    cout << res << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
