#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  vector<int> pre;
  vector<int> dp(15);
  dp[0] = 1;
  for (int i = 1; i < 15; ++i) dp[i] = 3 * dp[i - 1];
  for (int i = 0; i < (1 << 15); ++i) {
    int value = 0;
    for (int bit = 0; bit < 15; ++bit) {
      if ((i >> bit) & 1) value += dp[bit];
    }
    pre.push_back(value);
  }
  sort(pre.begin(), pre.end());
  while (t--) {
    int n;
    cin >> n;
    auto it = lower_bound(pre.begin(), pre.end(), n);
    cout << (*it) << endl;
  }
}
