#include <bits/stdc++.h>
using namespace std;
int powerr(int x, int y) {
  int res = 1;
  while (y > 0) {
    if (y & 1) res = res * x;
    y = y >> 1;
    x = x * x;
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  set<int> nums;
  vector<int> prev;
  for (int i = 0;; ++i) {
    int x = powerr(3, i);
    nums.insert(x);
    if (x > 10000) break;
    int limit = prev.size();
    prev.push_back(x);
    for (int z = 0; z < limit; ++z) {
      int zz = x + prev[z];
      nums.insert(zz);
      prev.push_back(zz);
    }
  }
  while (q--) {
    int n;
    cin >> n;
    auto it = nums.lower_bound(n);
    cout << *it << "\n";
  }
  return 0;
}
