#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  int q;
  map<unsigned long long, bool> book;
  vector<unsigned long long> v, s;
  unsigned long long sum = 0;
  for (unsigned long long i = 1; i <= 20000; i = i * 3) {
    v.push_back(i);
    book[i] = 1;
    s.push_back(i);
    sum += i;
  }
  book[sum] = 1;
  s.push_back(sum);
  int N = 9;
  for (int i = 0; i <= N; i++) {
    book[sum - v[i]] = 1;
    s.push_back(sum - v[i]);
  }
  for (int i = 0; i <= N; i++)
    for (int j = i + 1; j <= N; j++) {
      book[v[i] + v[j]] = 1;
      book[sum - v[i] - v[j]] = 1;
      s.push_back(v[i] + v[j]);
      s.push_back(sum - v[i] - v[j]);
    }
  int p = 0;
  for (int i = 0; i <= N; i++)
    for (int j = i + 1; j <= N; j++)
      for (int k = j + 1; k <= N; k++) {
        book[sum - v[i] - v[j] - v[k]] = 1;
        book[v[i] + v[j] + v[k]] = 1;
        s.push_back(sum - v[i] - v[j] - v[k]);
        s.push_back(v[i] + v[j] + v[k]);
      }
  for (int i = 0; i <= N; i++)
    for (int j = i + 1; j <= N; j++)
      for (int k = j + 1; k <= N; k++)
        for (int w = k + 1; w <= N; w++) {
          book[sum - v[i] - v[j] - v[k] - v[w]] = 1;
          book[v[i] + v[j] + v[k] + v[w]] = 1;
          s.push_back(sum - v[i] - v[j] - v[k] - v[w]);
          s.push_back(v[i] + v[j] + v[k] + v[w]);
        }
  for (int i = 0; i <= N; i++)
    for (int j = i + 1; j <= N; j++)
      for (int k = j + 1; k <= N; k++)
        for (int w = k + 1; w <= N; w++)
          for (int x = w + 1; x <= N; x++) {
            book[sum - v[i] - v[j] - v[k] - v[w] - v[x]] = 1;
            book[v[i] + v[j] + v[k] + v[w] + v[x]] = 1;
            s.push_back(sum - v[i] - v[j] - v[k] - v[w] - v[x]);
            s.push_back(v[i] + v[j] + v[k] + v[w] + v[x]);
          }
  scanf("%d", &q);
  while (q--) {
    scanf("%llu", &n);
    for (unsigned long long i = n; i <= 19683; i++)
      if (book[i]) {
        printf("%llu\n", i);
        break;
      }
  }
  return 0;
}
