#include <bits/stdc++.h>
using namespace std;
void FindGoodNum();
unsigned int bases[] = {1,   2,   4,   8,    16,   32,  64,
                        128, 256, 512, 1024, 2048, 4096};
unsigned int getGNum(unsigned int b) {
  static int i;
  static unsigned int res;
  res = 0;
  for (i = 0; i < 13; i++) {
    if (b & bases[i]) {
      res += pow(3, i);
    }
  }
  return res;
}
unsigned int n;
int main() {
  unsigned int q;
  cin >> q;
  while (q--) {
    cin >> n;
    FindGoodNum();
  }
}
void FindGoodNum() {
  unsigned int b = 1;
  unsigned int gNum = 1;
  for (; gNum < n; b++) {
    gNum = getGNum(b);
  }
  cout << gNum << "\n";
}
