#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    while (true) {
      bool ok = true;
      int cur = n;
      while (cur > 0) {
        if (ok && cur % 3 == 2) ok = false;
        cur /= 3;
      }
      if (ok) break;
      ++n;
    }
    cout << n << endl;
  }
}
