#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " , ";
  __f(comma + 1, args...);
}
inline long long int gcd(long long int a, long long int b) {
  a = abs(a), b = abs(b);
  while (b) a = a % b, swap(a, b);
  return a;
}
inline long long int power(long long int a, long long int p) {
  long long int res = 1, x = a;
  while (p) {
    if (p & 1) res = (res * x);
    x = (x * x), p >>= 1;
  }
  return res;
}
inline long long int big_mod(long long int a, long long int p,
                             long long int m) {
  long long int res = 1 % m, x = a % m;
  while (p) {
    if (p & 1) res = (res * x) % m;
    x = (x * x) % m, p >>= 1;
  }
  return res;
}
const long long int INF = 1e15;
const int nax = 1e4 + 5;
vector<long long int> three;
long long int n, ans;
int sz;
void solve(int idx, long long int sum) {
  if (idx >= sz) {
    if (sum >= n) {
      ans = min(ans, sum);
    }
    return;
  }
  solve(idx + 1, sum + three[idx]);
  solve(idx + 1, sum);
}
void pre_cal() {
  for (long long int i = 0;; i++) {
    long long int x = power(3, i);
    if (x > nax) {
      three.push_back(x);
      break;
    }
    three.push_back(x);
  }
}
int main() {
  pre_cal();
  sz = three.size();
  int q;
  cin >> q;
  while (q--) {
    cin >> n;
    ans = INF;
    solve(0, 0);
    cout << ans << endl;
  }
  return 0;
}
