#include <bits/stdc++.h>
using namespace std;
long long power[55];
int main() {
  int q;
  cin >> q;
  long long e = 1;
  for (int i = 0; i <= 38; i++) {
    power[i] = e;
    e *= 3;
  }
  while (q--) {
    long long n;
    cin >> n;
    int a[55];
    int size = -1;
    for (int i = 38; i >= 0; i--) {
      a[i] = 0;
      if (n - power[i] >= 0) {
        n -= power[i];
        a[i] = 1;
        if (size == -1) {
          size = i;
        }
      }
    }
    if (n > 0) {
      int i = 0;
      while (a[i] == 1) {
        a[i] = 0;
        i++;
      }
      a[i] = 1;
      if (i > size) {
        size = i;
      }
    }
    long long ans = 0;
    for (int i = 0; i <= 38; i++) {
      if (a[i]) {
        ans += power[i];
      }
    }
    cout << ans << endl;
  }
}
