#include <bits/stdc++.h>
using namespace std;
constexpr long long base = 3;
long long solve(long long n) {
  long long ans = 0;
  long long i = 0;
  bool reset = false;
  do {
    int mod = n % base;
    if (mod == 0 && reset) {
      ans = pow(base, i);
      reset = false;
    } else if (mod == 1 && !reset)
      ans += pow(base, i);
    else if (mod == 2 && !reset) {
      ans = 0;
      reset = true;
    }
    n /= base;
    ++i;
  } while (n > 0);
  if (reset) ans = pow(base, i);
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    cout << solve(n) << endl;
  }
  return 0;
}
