#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n, i, j, pre, flag, x;
  cin >> t;
  for (i = 0; i < t; i++) {
    cin >> n;
    while (true) {
      flag = 1;
      x = n;
      while (x > 0) {
        if (flag == 1 && x % 3 == 2) {
          flag = 0;
        }
        x = x / 3;
      }
      if (flag == 1) {
        break;
      }
      n++;
    }
    cout << n << endl;
  }
}
