#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int INF = 1034567890;
const long long LL_INF = 1234567890123456789ll;
const double PI = acos(-1);
const long double ERR = 1E-10;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int cnt = 1;
    int place{};
    int ph;
    for (auto i{0}; i < 9; i++) {
      if (n >= cnt) {
        place = cnt;
      }
      cnt *= 3;
    }
    place *= 3;
    ph = place;
    int res = 0;
    if (n > place / 2) {
      res = place;
    } else {
      place /= 3;
      res += place;
      n -= place;
      while (n && place) {
        place /= 3;
        if (n > place / 2) {
          n -= place;
          res += place;
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}
