#include <bits/stdc++.h>
using namespace std;
int main() {
  int q, n, i, j, k;
  cin >> q;
  for (i = 0; i < q; i++) {
    vector<short int> v;
    cin >> n;
    while (n) {
      v.push_back(n % 3);
      n = n / 3;
    }
    for (j = v.size() - 1; j >= 0; j--) {
      if (v[j] == 2) {
        v[j] = 0;
        for (k = j + 1; k < v.size(); k++) {
          if (v[k] == 1)
            v[k] = 0;
          else if (v[k] == 0) {
            v[k] = 1;
            break;
          } else if (v[k] == 2)
            break;
        }
        for (k = j - 1; k >= 0; k--) v[k] = 0;
      }
    }
    if (v[v.size() - 1] == 2 || v[v.size() - 1] == 0) {
      v[v.size() - 1] = 0;
      v.push_back(1);
    }
    int ans = 0;
    for (j = 0; j < v.size(); j++) {
      ans += v[j] * pow(3, j);
    }
    cout << ans << endl;
  }
  return 0;
}
