#include <bits/stdc++.h>
using namespace std;
unsigned long long dp[39] = {0};
unsigned long long sum[39] = {0};
void compute() {
  dp[0] = 1ULL;
  sum[0] = 1ULL;
  for (int i = 1; i <= 38; i++) {
    dp[i] = 3ULL * dp[i - 1];
    sum[i] = sum[i - 1] + dp[i];
  }
}
unsigned long long solve(unsigned long long x) {
  unsigned long long ans = 0;
  for (int i = 38; i >= 0; i--) {
    if (ans >= x) return ans;
    if (i) {
      if (ans + sum[i - 1] >= x)
        continue;
      else
        ans += dp[i];
    } else
      ans++;
  }
  return ans;
}
int main() {
  compute();
  int t;
  cin >> t;
  while (t--) {
    unsigned long long n;
    cin >> n;
    cout << solve(n) << endl;
  }
}
