#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 2e5 + 111, N2 = 1e2 + 11;
long long pow3[39];
int main() {
  int t;
  cin >> t;
  pow3[0] = 1;
  for (int i = 1; i <= 38; i++) pow3[i] = pow3[i - 1] * 3;
  while (t--) {
    long long n;
    scanf("%lld", &n);
    long long ans = 0;
    for (int i = 38; i >= 0; i--)
      if ((pow3[i] - 1) / 2 < n) n -= pow3[i], ans += pow3[i];
    printf("%lld\n", ans);
  }
}
