#include <bits/stdc++.h>
using namespace std;
void quicken() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
vector<int> v;
int solve(int n) {
  int l = 0, r = 512, mid, ans = n;
  while (l <= r) {
    mid = (l + r) / 2;
    if (v[mid] >= n)
      ans = v[mid], r = mid - 1;
    else
      l = mid + 1;
  }
  return ans;
}
int main() {
  quicken();
  pow(3, 9);
  int arr[10];
  for (int i = 0; i < 10; i++) arr[i] = pow(3, i);
  for (int i = 0; i < 10; i++) v.push_back(arr[i]);
  for (int i = 0; i < 10; i++)
    for (int j = i + 1; j < 10; j++) v.push_back(arr[i] + arr[j]);
  for (int i = 0; i < 10; i++)
    for (int j = i + 1; j < 10; j++)
      for (int k = j + 1; k < 10; k++) v.push_back(arr[i] + arr[j] + arr[k]);
  for (int i = 0; i < 10; i++)
    for (int j = i + 1; j < 10; j++)
      for (int k = j + 1; k < 10; k++)
        for (int l = k + 1; l < 10; l++)
          v.push_back(arr[i] + arr[j] + arr[k] + arr[l]);
  for (int i = 0; i < 10; i++)
    for (int j = i + 1; j < 10; j++)
      for (int k = j + 1; k < 10; k++)
        for (int l = k + 1; l < 10; l++)
          for (int m = l + 1; m < 10; m++)
            v.push_back(arr[i] + arr[j] + arr[k] + arr[l] + arr[m]);
  for (int i = 0; i < 10; i++)
    for (int j = i + 1; j < 10; j++)
      for (int k = j + 1; k < 10; k++)
        for (int l = k + 1; l < 10; l++)
          for (int m = l + 1; m < 10; m++)
            for (int n = m + 1; n < 10; n++)
              v.push_back(arr[i] + arr[j] + arr[k] + arr[l] + arr[m] + arr[n]);
  for (int i = 0; i < 10; i++)
    for (int j = i + 1; j < 10; j++)
      for (int k = j + 1; k < 10; k++)
        for (int l = k + 1; l < 10; l++)
          for (int m = l + 1; m < 10; m++)
            for (int n = m + 1; n < 10; n++)
              for (int o = n + 1; o < 10; o++)
                v.push_back(arr[i] + arr[j] + arr[k] + arr[l] + arr[m] +
                            arr[n] + arr[o]);
  for (int i = 0; i < 10; i++)
    for (int j = i + 1; j < 10; j++)
      for (int k = j + 1; k < 10; k++)
        for (int l = k + 1; l < 10; l++)
          for (int m = l + 1; m < 10; m++)
            for (int n = m + 1; n < 10; n++)
              for (int o = n + 1; o < 10; o++)
                for (int p = o + 1; p < 10; p++)
                  v.push_back(arr[i] + arr[j] + arr[k] + arr[l] + arr[m] +
                              arr[n] + arr[o] + arr[p]);
  for (int i = 0; i < 10; i++)
    for (int j = i + 1; j < 10; j++)
      for (int k = j + 1; k < 10; k++)
        for (int l = k + 1; l < 10; l++)
          for (int m = l + 1; m < 10; m++)
            for (int n = m + 1; n < 10; n++)
              for (int o = n + 1; o < 10; o++)
                for (int p = o + 1; p < 10; p++)
                  for (int q = p + 1; q < 10; q++)
                    v.push_back(arr[i] + arr[j] + arr[k] + arr[l] + arr[m] +
                                arr[n] + arr[o] + arr[p] + arr[q]);
  sort(v.begin(), v.end());
  int q, n;
  cin >> q;
  while (q--) {
    cin >> n;
    cout << solve(n) << '\n';
  }
  return 0;
}
