#include <bits/stdc++.h>
using namespace std;
void Fastio() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int main() {
  Fastio();
  vector<long long> v;
  long long tmp = 1;
  v.push_back(1);
  while (tmp <= 1e18 + 1) {
    tmp *= 3;
    v.push_back(tmp);
  }
  int t;
  cin >> t;
  while (t--) {
    long long n, sum = 0, pos = 0;
    cin >> n;
    for (int i = 0; i < v.size(); i++) {
      if (sum >= n) break;
      sum += v[i];
      pos = i;
    }
    for (int i = pos; i >= 0; i--) {
      if (sum - v[i] >= n) sum -= v[i];
    }
    cout << sum << '\n';
  }
  return 0;
}
