#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
template <class T>
void Swap(T& a, T& b) {
  a ^= b;
  b ^= a;
  a ^= b;
}
template <class type>
void show(type name) {
  for (auto& nm : name) cerr << nm << ' ';
  cerr << '\n';
}
using lng = long long;
using ivec = vector<int>;
const int mxn = 10;
int check(ivec& nums, int n) {
  int ans = 1e9;
  for (int i = 0; i < (1 << (int)nums.size()); ++i) {
    int cnt = 0;
    for (int j = 0; j < (int)nums.size(); ++j) {
      if ((i & (1 << j)) > 0) cnt += ceil(pow(3, nums[j]));
    }
    if (cnt >= n) ans = min(ans, cnt);
  }
  return ans;
}
int main() {
  int q, n;
  cin >> q;
  while (q--) {
    cin >> n;
    ivec nums(min(n, mxn) + 1);
    iota(begin(nums), end(nums), 0);
    cout << check(nums, n) << '\n';
  }
}
