#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
int main() {
  int q;
  scanf("%d", &q);
  long long p = 1;
  for (int i = 0; i < 38; i++) p *= 3;
  while (q--) {
    long long n;
    scanf("%lld", &n);
    long long ans = 0;
    for (long long p2 = p, i = 38; p2; p2 /= 3, i--) {
      if (p2 <= n) ans ^= (1ll << i), n -= p2;
      if (p2 <= n) {
        ans += (1ll << i);
        n -= p2;
        break;
      }
    }
    long long out = 0;
    for (long long p2 = p, i = 38; p2; p2 /= 3, i--)
      if (ans >> i & 1) out += p2;
    printf("%lld\n", out);
  }
  exit(0);
}
