#include <bits/stdc++.h>
using namespace std;
int num[20];
int top = 0;
void f(int x) {
  while (x != 0) {
    num[top++] = (x % 3);
    x /= 3;
  }
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    memset(num, 0, sizeof(num));
    int n;
    cin >> n;
    top = 0;
    f(n);
    for (int i = 0; i < 18; i++) {
      if (num[i] == 2) {
        num[i] += 1;
        for (int j = 0; j < i; j++) num[j] = 0;
      }
      num[i + 1] += num[i] / 3;
      num[i] %= 3;
    }
    long long int ans = 0;
    for (int i = 0; i < 19; i++) {
      ans += num[i] * pow(3, i);
    }
    cout << ans << endl;
  }
  return 0;
}
