#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  long long pre_subset = LONG_MAX;
  long long ans;
  for (long long mask = 0; mask < (1 << 25); ++mask) {
    long long curr_subset = 0;
    long long ans;
    for (int i = 0; i < 29; ++i) {
      if (mask & (1 << i)) {
        curr_subset += pow(3, i);
        if (curr_subset >= n) {
          cout << curr_subset << "\n";
          return;
        }
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
