#include <bits/stdc++.h>
using namespace std;
long long int pows[51];
vector<int> base_3(long long int n) {
  vector<int> f(51);
  int r = 0;
  while (n > 0) {
    f[r++] = (int)n % 3;
    n /= 3;
  }
  return f;
}
int main() {
  int q;
  cin >> q;
  pows[1] = 3;
  pows[0] = 1;
  for (int i = 1; i <= 50; i++) {
    pows[i] = pows[i - 1] * 3;
  }
  while (q--) {
    long long int n;
    cin >> n;
    vector<int> v = base_3(n);
    int ind = -1;
    for (int j = 50; j >= 0; j--) {
      if (v[j] == 2 && ind == -1) {
        ind = j;
      }
    }
    if (ind == -1) {
      cout << n << "\n";
    } else {
      int r = ind + 1;
      while (r < 50 && v[r] == 1) {
        r++;
      }
      v[r] = 1;
      for (int i = 0; i < r; i++) {
        v[i] = 0;
      }
      long long int ans = 0;
      for (int j = 0; j < 51; j++) {
        ans += v[j] * pows[j];
      }
      cout << ans << "\n";
    }
  }
  return 0;
}
