#include <bits/stdc++.h>
using namespace std;
const long long N = 50;
long long dp[N];
void solve() {
  long long n;
  cin >> n;
  long long count = 0;
  while (n > 0) {
    long long quotient = n / 3;
    long long rem = n % 3;
    n = n / 3;
    dp[count] = rem;
    count++;
  }
  for (long long i = 0; i < N; i++) {
    if (dp[i] == 0 || dp[i] == 1)
      continue;
    else {
      dp[i] = -1;
      dp[i + 1] += 1;
    }
  }
  long long finalAns = 0;
  for (long long i = N - 1; i >= 0; i--) {
    if (dp[i] == 0) continue;
    if (dp[i] == -1)
      break;
    else {
      long long coeff = dp[i];
      finalAns += pow(3, i);
    }
  }
  cout << finalAns << endl;
  for (long long i = 0; i < N; i++) dp[i] = 0;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
