#include <bits/stdc++.h>
using namespace std;
long long ans(long long n) {
  long long i;
  map<long long, long long> mp;
  long long x = 1;
  mp[x] = 0;
  for (i = 1; i < 40; i++) {
    x *= 3;
    mp[x] = 0;
  }
  long long N = n;
  for (auto iter = mp.rbegin(); iter != mp.rend(); iter++) {
    while (N >= iter->first) {
      iter->second++;
      N -= iter->first;
    }
  }
  for (auto iter = mp.rbegin(); iter != mp.rend(); iter++) {
    if (iter->second == 2) {
      auto iter2 = iter;
      while (iter2->second != 0) iter2--;
      iter2->second = 1;
      for (auto iter3 = ++iter2; iter3 != mp.rend(); iter3++) iter3->second = 0;
      iter->second = 0;
    }
  }
  long long ans = 0;
  for (auto i : mp) {
    ans += i.second * i.first;
  }
  return ans;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long i, j;
  long long q;
  cin >> q;
  while (q--) {
    long long n;
    cin >> n;
    cout << ans(n) << "\n";
  }
  return 0;
}
