#include <bits/stdc++.h>
using namespace std;
long long cas;
long long n;
long long ans[100];
long long fac[100];
long long x;
int main() {
  scanf("%lld", &cas);
  fac[0] = 1LL;
  for (long long i = 1; i <= 38; ++i) fac[i] = fac[i - 1] * 3LL;
  while (cas--) {
    scanf("%lld", &n);
    for (long long i = 38; i >= 0; --i) {
      if (fac[i] <= n) {
        ans[i] = 1;
        n -= fac[i];
      } else {
        ans[i] = 0;
      }
    }
    if (n > 0) {
      for (long long i = 0; i <= 38; ++i) {
        if (ans[i] == 1)
          ans[i] = 0;
        else {
          ans[i] = 1;
          break;
        }
      }
    }
    long long ret = 0;
    for (long long i = 0; i <= 38; ++i)
      if (ans[i]) ret += fac[i];
    printf("%lld\n", ret);
  }
  return 0;
}
