#include <bits/stdc++.h>
using namespace std;
long long int arr[13];
long long int allsum[13];
vector<long long int> vec;
long long int power(long long int a, long long int p) {
  long long int res = 1;
  while (p) {
    if (p % 2) {
      res *= a;
      p--;
    } else if (p % 2 == 0) {
      a *= a;
      p /= 2;
    }
  }
  return res;
}
void precalculate() {
  for (int i = 0; i <= 12; i++) {
    long long int pw = power(3, i);
    arr[i] = pw;
  }
}
void sum(long long int arr[], int curr_index, long long int allsum[],
         int allsum_index) {
  if (curr_index == 12) {
    long long int ss = 0;
    for (int i = 0; i < allsum_index; i++) {
      ss += allsum[i];
    }
    vec.push_back(ss);
    return;
  }
  allsum[allsum_index] = arr[curr_index];
  sum(arr, curr_index + 1, allsum, allsum_index + 1);
  sum(arr, curr_index + 1, allsum, allsum_index);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  precalculate();
  sum(arr, 0, allsum, 0);
  sort(vec.begin(), vec.end());
  int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    int index = lower_bound(vec.begin(), vec.end(), n) - vec.begin();
    cout << vec[index] << '\n';
  }
}
