#include <bits/stdc++.h>
using namespace std;
vector<long long> v, tot;
void solve() {
  unsigned int pow_set_size = pow(2, v.size());
  int counter, j;
  for (counter = 0; counter < pow_set_size; counter++) {
    long long sum = 0;
    for (j = 0; j < v.size(); j++) {
      if (counter & (1 << j)) sum += v[j];
    }
    tot.push_back(sum);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  for (int i = 0; i <= 11; i++) {
    v.push_back(pow(3, i));
  }
  solve();
  int t;
  cin >> t;
  while (t--) {
    int a;
    cin >> a;
    int ind = lower_bound(tot.begin(), tot.end(), a) - tot.begin();
    cout << tot[ind] << endl;
  }
  return 0;
}
