#include <bits/stdc++.h>
using namespace std;
long long int min(long long int a, long long int b) { return (a < b) ? a : b; }
long long int max(long long int a, long long int b) { return (a > b) ? a : b; }
long long int fp(long long int a, long long int b) {
  if (b == 0) return 1;
  long long int x = fp(a, b / 2);
  x = (x * x) % 1000000007;
  if (b & 1) x = (x * a) % 1000000007;
  return x;
}
long long int factorial(long long int n) {
  long long int fact = 1;
  for (long long int i = 2; i <= n; i++) fact = fact * i;
  return fact;
}
long long int ncr(long long int n, long long int r) {
  return factorial(n) / (factorial(r) * factorial(n - r));
}
void c_p_c() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
}
signed main() {
  c_p_c();
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    vector<long long int> base3;
    long long int pos2 = -1;
    while (n > 0) {
      base3.push_back(n % 3);
      if (n % 3 == 2) pos2 = base3.size() - 1;
      n /= 3;
    }
    base3.push_back(0);
    if (pos2 != -1) {
      long long int pos0 =
          find(base3.begin() + pos2, base3.end(), 0) - base3.begin();
      base3[pos0] = 1;
      for (long long int i = pos0 - 1; i >= 0; i--) base3[i] = 0;
    }
    long long int ans = 0, power = 1;
    for (long long int i = 0; i < base3.size(); i++) {
      if (base3[i]) ans += power;
      power *= 3;
    }
    cout << ans << "\n";
  }
}
