#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
long long int inverse(long long int i) {
  if (i == 1) return 1;
  return (MOD - ((MOD / i) * inverse(MOD % i)) % MOD + MOD) % MOD;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long pw[50];
  pw[0] = 1;
  for (int i = 1; i < 50; i++) {
    pw[i] = 3 * pw[i - 1];
  }
  long long t, n;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> n;
    long long ter[50] = {};
    long long temp = n, flag = 0;
    for (int i = 0; i < 50; i++) {
      ter[i] += temp % 3;
      temp = temp / 3;
      if (ter[i] == 2) flag = 1;
    }
    if (flag == 0)
      cout << n;
    else {
      long long p2, p0;
      for (int i = 49; i >= 0; i--) {
        if (ter[i] == 2) {
          p2 = i;
          break;
        }
      }
      long long ans;
      for (int i = p2 + 1; i < 50; i++) {
        if (ter[i] == 0) {
          ans = pw[i];
          p0 = i;
          break;
        }
      }
      long long mul = ans;
      for (int i = p0 + 1; i < 50; i++) {
        ans += ter[i] * pw[i];
      }
      cout << ans;
    }
    cout << "\n";
  }
}
