#include <bits/stdc++.h>
using namespace std;
long long int md = 1e9 + 7;
long long int __gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return __gcd(b, a % b);
}
long long int poww(long long int a, long long int b) {
  long long int res = 1;
  while (b) {
    if (b & 1) {
      res *= a;
      res %= md;
    }
    a *= a;
    a %= md;
    b >>= 1;
  }
  return res;
}
void ainp(long long int arr[], long long int n) {
  for (long long int i = 1; i <= n; i++) cin >> arr[i];
}
const long long int N = 2e5 + 5;
void solve() {
  long long int n;
  cin >> n;
  long long int t = n;
  vector<long long int> bits;
  while (n) {
    bits.push_back(n % 3);
    n /= 3;
  }
  bits.push_back(0);
  long long int cnt2 = -1;
  for (long long int i = 0; i < bits.size() - 1; i++) {
    if (bits[i] == 2) {
      cnt2 = i;
    }
  }
  if (cnt2 == -1)
    cout << t << "\n";
  else {
    for (long long int i = 0; i < cnt2; i++) {
      bits[i] = 0;
    }
    for (long long int i = cnt2; i < bits.size() - 1; i++) {
      if (bits[i] == 2) {
        bits[i] = 0;
        bits[i + 1] += 1;
      }
    }
    long long int ans = 0;
    for (long long int i = 0; i < bits.size(); i++) {
      ans += bits[i] * (long long int)pow(3, i);
    }
    cout << ans << "\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t-- > 0) {
    solve();
  }
}
