#include <bits/stdc++.h>
using namespace std;
bool f(int n) {
  while (n > 0) {
    int tmp = n % 3;
    if (tmp > 1) return false;
    n /= 3;
  }
  return true;
}
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    int x = pow(10, 6);
    for (int i = n; i <= x; i++) {
      if (f(i)) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}
