#include <bits/stdc++.h>
using namespace std;
const int inf = INT_MAX;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long pre[40];
  long long nm = 1;
  for (long long i = 0; i <= 39; i++) {
    pre[i] = nm;
    nm *= 3;
  }
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long ans = 0, i = 0;
    while (ans < n) ans += pre[i++];
    while (i >= 0) {
      if (ans - pre[i] >= n) ans -= pre[i];
      i--;
    }
    cout << ans << "\n";
  }
  return 0;
}
