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