#include <bits/stdc++.h>
using namespace std;
long long int pow1(long long int n, long long int p) {
  if (p == 0) return 1;
  long long int x = pow1(n, p / 2);
  x = (x * x) % 1000000007;
  if (p % 2 == 0)
    return x;
  else
    return (x * n) % 1000000007;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int q;
  cin >> q;
  for (long long int i1 = 0; i1 < q; i1++) {
    long long int n;
    cin >> n;
    long long int n1 = n;
    vector<long long int> v;
    long long int f1 = 0;
    long long int ind = -1;
    long long int i = 0;
    while (n > 0) {
      if (n % 3 == 2) {
        ind = i;
        f1 = 1;
      }
      v.push_back(n % 3);
      n = n / 3;
      i++;
    }
    if (ind == -1) {
      cout << n1 << "\n";
      continue;
    }
    long long int f = -1;
    for (long long int k = ind + 1; k < i; k++) {
      if (v[k] == 0) {
        v[k] = 1;
        v[ind]--;
        f = k;
        break;
      }
    }
    long long int ans = 0;
    if (f == -1) {
      long long int ans = 1;
      for (long long int j = 0; j < i; j++) ans = ans * 3;
      cout << ans << "\n";
      continue;
    } else {
      for (long long int j = i - 1; j >= 0; j--) {
        if (j >= f) {
          if (v[j] == 0)
            ans = ans * 3;
          else
            ans = ans * 3 + 1;
        } else
          ans = ans * 3;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
