#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int a[12];
vector<int> b;
void dfs(int t, int s) {
  if (t == 10) return;
  for (int i = t; i < 10; i++) {
    b.push_back(s + a[i]);
    dfs(i + 1, s + a[i]);
  }
}
void init() {
  a[0] = 1;
  a[1] = 3;
  for (int i = 2; i < 10; i++) {
    a[i] = a[i - 1] * 3;
  }
  dfs(0, 0);
  sort(b.begin(), b.end());
}
int main() {
  init();
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    int l = b.size();
    for (int i = 0; i < l; i++) {
      if (b[i] >= n) {
        printf("%d\n", b[i]);
        break;
      }
    }
  }
  return 0;
}
