#include <bits/stdc++.h>
using namespace std;
vector<int> b;
vector<int> good;
bool vis[1024];
int find(int n) {
  int cnt = 0;
  int r = 0;
  while (n) {
    int a = n % 2;
    r += a * (int)pow(3, cnt);
    cnt++;
    n /= 2;
  }
  return r;
}
int bs(int v) {
  int i = 0, j = good.size();
  while (i < j) {
    int m = (i + j) >> 1;
    if (v <= good[m]) j = m - 1;
    if (v > good[m]) i = m + 1;
  }
  return i;
}
int main() {
  for (int i = 1; i <= 1024; i++) {
    good.push_back(find(i));
  }
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    int k = 0;
    while (good[k] < n) {
      k++;
    }
    cout << good[k] << '\n';
  }
  return 0;
}
