#include <bits/stdc++.h>
using namespace std;
long long T;
long long n;
long long Pref[20020];
vector<long long> A;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> T;
  long long Sum = 0;
  long long i = 0;
  while (Sum <= 1e18) {
    Sum += (powl(3, i));
    A.push_back(powl(3, i));
    i++;
  }
  Pref[0] = A[0];
  for (long long i = 1; i < A.size(); i++) Pref[i] += Pref[i - 1] + A[i];
  long long m = A.size();
  while (T--) {
    cin >> n;
    auto Index = lower_bound(Pref, Pref + m, n) - Pref;
    long long Sum = Pref[Index];
    Index--;
    while (Index >= 0) {
      if (Sum - A[Index] >= n) Sum -= A[Index];
      Index--;
    }
    cout << Sum << endl;
  }
}
