#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long pref[40] = {0};
  long long power[40];
  power[0] = 1;
  for (int i = 1; i < 39; i++) {
    power[i] = power[i - 1] * 3;
  }
  set<long long> s;
  for (int i = 0; i < 39; i++) s.insert(power[i]);
  for (int i = 1; i <= 39; i++) {
    pref[i] = pref[i - 1] + power[i - 1];
  }
  int q;
  cin >> q;
  while (q--) {
    long long n;
    cin >> n;
    long long ans = 0;
    while (n > 0) {
      auto it = s.lower_bound(n);
      long long val = *(it);
      if (val == n) {
        ans += n;
        break;
      }
      if (val > n) it--;
      val = *(it);
      int idx;
      for (idx = 0; idx < 39; idx++) {
        if (power[idx] == val) break;
      }
      if (pref[idx + 1] < n) {
        ans += power[idx + 1];
        break;
      }
      ans += val;
      n -= val;
    }
    cout << ans << endl;
  }
  return 0;
}
