#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n;
  cin >> n;
  vector<int> a;
  while (n) {
    a.push_back(n % 3);
    n = n / 3;
  }
  a.push_back(0);
  bool big = false;
  for (int i = a.size() - 1; i >= 0; i--) {
    if (big)
      a[i] = 0;
    else if (a[i] == 2) {
      int k = i;
      while (a[k] != 0) {
        a[k] = 0;
        k++;
      }
      a[k] = 1;
      a[i] = 0;
      big = true;
    }
  }
  n = 0;
  long long int p = 1;
  for (int i = 0; i < a.size(); i++) {
    if (a[i] == 1) n += p;
    p *= 3;
  }
  cout << n << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
