#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
int sum;
set<int> s;
set<int>::iterator it;
void combinationUtil(int arr[], int n, int r, int index, int data[], int i);
void printCombination(int arr[], int n, int r) {
  int data[r];
  combinationUtil(arr, n, r, 0, data, 0);
}
void combinationUtil(int arr[], int n, int r, int index, int data[], int i) {
  sum = 0;
  if (index == r) {
    for (int j = 0; j < r; j++) {
      sum += data[j];
    }
    s.insert(sum);
    return;
  }
  if (i >= n) return;
  data[index] = arr[i];
  combinationUtil(arr, n, r, index + 1, data, i + 1);
  combinationUtil(arr, n, r, index, data, i + 1);
}
int main() {
  int tc, n, i, idx, temp, f, c, j;
  cin >> tc;
  while (tc--) {
    int arr[] = {1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683};
    int n = sizeof(arr) / sizeof(arr[0]);
    for (i = 1; i <= 9; i++) {
      printCombination(arr, n, i);
    }
    cin >> n;
    for (it = s.begin(); it != s.end(); it++) {
      if (*it >= n) {
        cout << *it << endl;
        break;
      }
    }
  }
  return 0;
}
