#include <bits/stdc++.h>
using namespace std;
long long const mod = 1e9 + 7;
long long const inf = 1e18;
long long const mxn = 3e5 + 5;
inline void file() {}
signed main() {
  file();
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    while (1) {
      long long m = n;
      bool ok = 1;
      while (m) {
        if (m % 3 == 2) {
          ok = 0;
          break;
        }
        m /= 3;
      }
      if (ok) {
        cout << n << "\n";
        break;
      }
      n++;
    }
  }
}
