#include <bits/stdc++.h>
using namespace std;
int main() {
  long long p[50];
  p[0] = 1;
  long long cumsum[50];
  cumsum[0] = 1;
  for (long long i = 1; i <= 38; i++) {
    p[i] = p[i - 1] * 3;
    cumsum[i] = cumsum[i - 1] + p[i];
  }
  long long t;
  scanf("%lld", &t);
  while (t--) {
    long long n;
    scanf("%lld", &n);
    long long ans = 0;
    long long x = n;
    for (long long i = 38; i >= 1; i--) {
      if (cumsum[i - 1] >= x) continue;
      x = x - p[i];
      ans = ans + p[i];
    }
    if (x > 0) {
      ans++;
    }
    cout << ans << "\n";
  }
}
