#include <bits/stdc++.h>
using namespace std;
int const MAXN = 2e6 + 9;
long long p[50];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  p[0] = 1;
  for (int i = 1; i < 39; i++) {
    long long x = p[i - 1] * 3;
    p[i] = x;
  }
  int T;
  cin >> T;
  while (T--) {
    long long n;
    cin >> n;
    long long x = 0;
    int i = 0;
    while (x < n) {
      x += p[i];
      i++;
    }
    i--;
    for (int k = i; k >= 0; k--) {
      if (x - p[k] >= n) x -= p[k];
    }
    cout << x << '\n';
  }
}
