#include <bits/stdc++.h>
using namespace std;
long long const md = 1e9 + 7;
long long binpow(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;
}
void convertToTernary(long long N, string &s) {
  if (N == 0) return;
  long long x = N % 3;
  N /= 3;
  if (x < 0) N += 1;
  convertToTernary(N, s);
  if (x < 0)
    s += to_string(x + (3 * -1));
  else
    s += to_string(x);
}
void convert(long long Decimal, string &s) {
  if (Decimal != 0) {
    convertToTernary(Decimal, s);
  } else
    s += "0";
}
void solve() {
  long long n, i, j, k;
  cin >> n;
  string s = "";
  long long Decimal = n;
  convert(Decimal, s);
  k = s.size();
  j = 0;
  for (i = 0; i < k; i++) {
    if (s[i] == '2') {
      j = 1;
      break;
    }
  }
  if (j == 0) {
    cout << n << endl;
    return;
  }
  for (i = 0; i < k; i++) {
    if (s[i] == '2') {
      j = i;
      break;
    }
  }
  long long ck = 0;
  for (i = 0; i < j; i++) {
    if (s[i] == '0') {
      ck = 1;
      break;
    }
  }
  if (ck == 0) {
    cout << binpow(3, k) << endl;
    return;
  }
  long long ans = 0, ii;
  for (i = j - 1; i >= 0; i--) {
    if (s[i] == '0') {
      ans += binpow(3, k - i - 1);
      ii = i;
      break;
    }
  }
  for (i = 0; i < ii; i++) {
    ans += (s[i] - '0') * binpow(3, k - i - 1);
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
  return 0;
}
