#include <bits/stdc++.h>
using namespace std;
int three(int n) {
  while (n != 0) {
    if (n % 3 == 2) {
      return false;
    }
    n /= 3;
  }
  return true;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    while (!three(n)) {
      n++;
    }
    cout << n << endl;
  }
}
