#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  vector<long long> pows(40);
  vector<long long> pref(40);
  pows[0] = 1, pref[0] = 1;
  for (int i = 1; i < 40; ++i) {
    pows[i] = pows[i - 1] * 3;
    pref[i] = pref[i - 1] + pows[i];
  }
  int q;
  cin >> q;
  for (int qr = 0; qr < q; ++qr) {
    long long n;
    cin >> n;
    long long res = 0;
    for (int i = 39; i >= 0; --i) {
      if (i > 0 && res + pref[i - 1] < n || i == 0 && res < n) res += pows[i];
    }
    cout << res << '\n';
  }
}
