#include <bits/stdc++.h>
using namespace std;
bool cheak(long long n) {
  while (n > 0) {
    long long rem = n % 3;
    if (rem > 1) return 0;
    n /= 3;
  }
  return 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  long long n, i, j, l = 0, sum = 0;
  string str, st;
  cin >> t;
  while (t--) {
    long long dp[13], stor[13];
    long long cnt = 0, count = 0, p = 0, k = 0;
    cin >> n;
    for (i = n;; i++) {
      if (cheak(i)) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}
