#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long q;
  cin >> q;
  vector<long long> v(10);
  for (long long i = 0; i <= 8; i++) {
    v[i] = pow(3, i);
  }
  vector<long long> p(9);
  for (long long i = 0; i < 9; i++) p[i] = i;
  set<long long> s;
  long long sum = 0;
  do {
    sum = 0;
    for (long long i = 0; i <= 8; i++) {
      sum += v[p[i]];
      s.insert(sum);
    }
  } while (next_permutation(p.begin(), p.end()));
  while (q--) {
    long long n;
    cin >> n;
    if (n > 9841) {
      cout << "19683\n";
      continue;
    }
    long long p1 = *(s.upper_bound(n));
    long long p2 = *(--s.upper_bound(n));
    if (p2 >= n)
      cout << p2 << '\n';
    else
      cout << p1 << '\n';
  }
  return 0;
}
