#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    while (true) {
      int tmp = n;
      bool f = true;
      while (tmp) {
        if (tmp % 3 == 2) {
          f = false;
          break;
        }
        tmp /= 3;
      }
      if (!f)
        n++;
      else
        break;
    }
    cout << n << endl;
  }
  return 0;
}
