#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    long long n;
    cin >> n;
    long long sum = 0;
    while (n > 0) {
      unsigned long long p3 = 1;
      long long sum2 = 0;
      while (sum2 < n) {
        sum2 += p3;
        p3 *= 3;
      }
      p3 /= 3;
      sum += p3;
      n -= p3;
    }
    cout << sum << "\n";
  }
  return 0;
}
