#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9;
const long long inf64 = 1e18;
const long long MOD = inf + 7;
void solve() {
  long long n;
  cin >> n;
  long long ans = 0;
  long long b[40];
  b[0] = 1;
  for (long long i = 1; i <= 39; i++) b[i] = b[i - 1] * (long long)3;
  long long pre[40] = {0};
  pre[0] = b[0];
  for (long long i = 1; i < 40; i++) pre[i] = pre[i - 1] + b[i];
  long long k1 = -1;
  for (long long i = 0; i < 40; i++) {
    if (b[i] > n) {
      k1 = i;
      break;
    }
    if (b[i] == n) {
      cout << b[i] << "\n";
      return;
    }
  }
  if (pre[k1 - 1] < n) {
    cout << b[k1] << "\n";
    return;
  }
  if (pre[k1 - 1] == n) {
    cout << n << "\n";
    return;
  }
  vector<long long> v;
  long long m = n;
  while (n > 0) {
    v.push_back(n % 3);
    n = n / 3;
  }
  long long two = -1;
  for (long long i = 0; i < v.size(); i++) {
    if (v[i] == 2) two = i;
  }
  if (two == -1) {
    cout << m << "\n";
    return;
  }
  long long g;
  v.push_back(0);
  for (long long i = two + 1; i < v.size(); i++) {
    if (v[i] == 0) {
      g = i;
      break;
    }
  }
  for (long long i = 0; i < g; i++) v[i] = 0;
  v[g] = 1;
  ans = 0;
  for (long long i = 0; i < v.size(); i++) ans = ans + (v[i] * pow(3LL, i));
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) solve();
}
