#include <bits/stdc++.h>
using namespace std;
void fun(int *arr) {
  int k = 2, p;
  arr[0] = 1;
  arr[1] = 3;
  for (int i = 1; i < 9; i++) {
    p = k - 1;
    for (int l = 0; l < p; l++) {
      arr[k] = arr[p] + arr[l];
      k++;
    }
    arr[k] = pow(3, i + 1);
    k++;
  }
}
int main() {
  int test, num, j;
  int arr[512];
  fun(arr);
  scanf("%d", &test);
  for (int i = 0; i < test; i++) {
    scanf("%d", &num);
    j = 0;
    while (num > arr[j]) {
      j++;
    }
    printf("%d", arr[j]);
    printf("\n");
  }
}
