#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e9 + 9;
int main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  unsigned int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long sum = 1, mul = 1;
    while (sum < n) {
      mul = mul * 3;
      sum += mul;
    }
    while (mul != 0) {
      if (sum - mul >= n) {
        sum = sum - mul;
      }
      mul = mul / 3;
    }
    cout << sum << '\n';
  }
  return 0;
}
