#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7, inv2 = (MOD + 1) / 2;
double eps = 1e-6;
int INF = 1e9;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int read() {
  int v = 0, f = 1;
  char c = getchar();
  while (c < 48 || 57 < c) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (48 <= c && c <= 57) v = (v << 3) + v + v + c - 48, c = getchar();
  return v * f;
}
int isok(int x) {
  while (x) {
    int t = x % 3;
    if (t == 2) return 0;
    x /= 3;
  }
  return 1;
}
int main() {
  int cas = read();
  while (cas--) {
    int n = read();
    int i = n;
    while (!isok(i)) i++;
    printf("%d\n", i);
  }
  return 0;
}
