#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    int m = n;
    vector<int> v;
    vector<int> A;
    int p = 3;
    while (m > 0) {
      int r = m % p;
      v.push_back(r);
      A.push_back(r);
      m = m / p;
    }
    v.push_back(0);
    A.push_back(0);
    int s = v.size();
    int key = 1;
    for (int i = 0; i < s - 1; i++) {
      key *= 3;
      if (i == 0) {
        key = key / 3;
      }
      if (v[i] == 2) {
        n += key;
        v[i] = 0;
        v[i + 1] += 1;
      } else if (v[i] == 3) {
        v[i + 1] += 1;
        v[i] = 0;
      }
    }
    int flag = 0;
    for (int i = s - 1; i >= 0; i--) {
      if (A[i] == 0) {
        if (v[i] == 1) {
          flag = i;
          break;
        }
      }
    }
    key = pow(3, s - 1);
    if (v[s - 1] == 1) {
      cout << key << "\n";
    } else {
      int ans = 0;
      for (int i = s - 2; i >= 0; i--) {
        key = key / 3;
        if (v[i] == 1) {
          if (A[i] == 0) {
            ans += key;
            i = 0;
          } else {
            ans += key;
          }
        }
      }
      cout << ans << "\n";
    }
  }
  return 0;
}
