#include <bits/stdc++.h>
using namespace std;
int dp[100005], n, t;
vector<int> a;
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  dp[0] = 1;
  a.push_back(1);
  for (int i = 1; i <= 10; i++) a.push_back(pow(3, i));
  for (int i = 0; i < a.size(); i++) {
    for (int j = 1e5; j >= 0; j--) {
      if (dp[j]) dp[j + a[i]] = 1;
    }
  }
  cin >> t;
  while (t--) {
    cin >> n;
    while (1) {
      if (dp[n]) {
        cout << n << endl;
        break;
      }
      n++;
    }
  }
}
