#include <bits/stdc++.h>
using namespace std;
int Read() {
  int out = 0, flag = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') flag = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    out = out * 10 + c - '0';
    c = getchar();
  }
  return flag * out;
}
const int maxn = 1e5 + 7;
bool find(int n) {
  while (n) {
    if (n % 3 >= 2) return 0;
    n /= 3;
  }
  return 1;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = n;; i++) {
      if (find(i)) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}
