#include <bits/stdc++.h>
using namespace std;
const int N = 100010, M = 40;
long long p[M];
int s[M];
int main() {
  p[0] = 1;
  for (int i = 1; i < M; i++) p[i] = p[i - 1] * 3;
  int t;
  cin >> t;
  while (t--) {
    memset(s, 0, sizeof s);
    long long n;
    cin >> n;
    for (int i = M - 1; i >= 0; i--)
      if (n / p[i]) {
        s[i] = n / p[i];
        n %= p[i];
      }
    bool flag = false;
    for (int i = M - 1; i >= 0; i--) {
      if (s[i] >= 2) {
        int j;
        for (j = i; s[j] != 0; j++) s[j] = 0;
        s[j] = 1;
        flag = true;
        for (int k = j - 1; k >= 0; k--) s[k] = 0;
      }
      if (flag) break;
    }
    long long ans = 0;
    for (int i = 0; i < M; i++) ans += p[i] * s[i];
    cout << ans << endl;
  }
  return 0;
}
