#include <bits/stdc++.h>
using namespace std;
inline long long add(long long a, long long b) {
  return ((a % 1000000007) + (b % 1000000007)) % 1000000007;
}
inline long long mul(long long a, long long b) {
  return ((a % 1000000007) * (b % 1000000007)) % 1000000007;
}
inline long long sub(long long a, long long b) {
  return ((a % 1000000007) - (b % 1000000007) + 1000000007) % 1000000007;
}
long long fun(long long n) { return (log(n) / log(3)); }
void solve() {
  long long n;
  cin >> n;
  set<long long> s;
  long long t = n;
  while (n > 0) {
    long long k = fun(n);
    k = pow(3, k);
    if (n > ((k * 3 - 1) >> 1)) {
      cout << t + (k * 3 - n) << "\n";
      return;
    }
    n -= k;
  }
  cout << t << "\n";
  return;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  cin >> t;
  for (long long a = 0; a < t; a++) {
    solve();
  }
}
