#include <bits/stdc++.h>
using namespace std;
int wrapper(int n) {
  while (n) {
    int a = n % 3;
    n = n / 3;
    if (a == 2) return 0;
  }
  return 1;
}
void solve() {
  int n;
  cin >> n;
  while (wrapper(n) == 0) n++;
  cout << n << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
