#include <bits/stdc++.h>
using namespace std;
bool solve(int n) {
  while (n) {
    int t = n % 3;
    if (t >= 2) return false;
    n /= 3;
  }
  return true;
}
int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    int n;
    scanf("%d", &n);
    while (n) {
      if (solve(n)) {
        printf("%d\n", n);
        break;
      }
      n++;
    }
  }
  return 0;
}
