#include <bits/stdc++.h>
using namespace std;
const int maxn = 51;
long long ans[maxn], a[maxn];
long long poww(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a;
    b /= 2;
    a = a * a;
  }
  return res;
}
void init() {
  ans[0] = 1;
  a[0] = 1;
  for (long long i = 1; i <= 38; i++) {
    a[i] = poww(3, i);
  }
  for (long long i = 1; i <= 38; i++) {
    ans[i] = ans[i - 1] + a[i];
  }
}
int main() {
  ios::sync_with_stdio(false);
  init();
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long m = 0;
    for (long long i = 38; i >= 1; i--) {
      if (ans[i - 1] >= n) continue;
      n -= a[i];
      m += a[i];
    }
    if (n > 0) {
      m += 1;
    }
    cout << m << endl;
  }
}
