#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<int> v = {1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683};
  vector<int> vis;
  for (int i = 0; i < (1 << 10); i++) {
    int sum = 0;
    for (int j = 0; j < 10; j++) {
      if (i & (1 << j)) {
        sum += v[j];
      }
    }
    vis.push_back(sum);
  }
  sort(vis.begin(), vis.end());
  int q;
  cin >> q;
  while (q--) {
    int x;
    cin >> x;
    cout << *lower_bound(vis.begin(), vis.end(), x) << endl;
  }
  return 0;
}
