#include <bits/stdc++.h>
using namespace std;
unsigned long long n, q, v[55];
unsigned long long p[40];
int main() {
  p[0] = 1;
  for (int i = 1; i <= 39; i++) {
    p[i] = 3 * p[i - 1];
  }
  cin >> q;
  for (int z = 1; z <= q; z++) {
    cin >> n;
    unsigned long long cop = n, mx = 0;
    int poz1 = -1, poz2 = -1, nr = 1;
    while (cop) {
      mx = max(mx, cop % 3);
      v[nr] = cop % 3;
      cop /= 3;
      nr++;
    }
    if (mx <= 1)
      cout << n << "\n";
    else {
      v[nr + 1] = 0;
      for (int i = nr; i >= 1; i--)
        if (v[i] == 2) {
          poz1 = i;
          break;
        }
      for (int i = poz1; i <= nr + 1; i++)
        if (v[i] == 0) {
          poz2 = i;
          break;
        }
      while (n) {
        n++;
        cop = n, mx = 0;
        while (cop) {
          mx = max(mx, cop % 3);
          cop /= 3;
        }
        if (mx <= 1) {
          cout << n << "\n";
          break;
        }
      }
    }
  }
  return 0;
}
