#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int tern[64];
int idx = 0;
bool isTernary(long long n) {
  int i = 0;
  while (n > 0) {
    tern[i] = n % 3;
    n = n / 3;
    i++;
  }
  idx = i - 1;
  for (int j = i - 1; j >= 0; j--)
    if (tern[j] == 2) {
      return false;
    }
  return true;
}
int main() {
  ios_base::sync_with_stdio(), cout.tie(NULL), cin.tie(NULL);
  ;
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    memset(tern, 0, sizeof(tern));
    if (isTernary(n)) {
      cout << n << "\n";
      continue;
    } else {
      for (int i = n; i <= 100000; i++) {
        if (isTernary(i)) {
          cout << i << "\n";
          break;
        }
      }
    }
  }
  return 0;
}
