#include <bits/stdc++.h>
using namespace std;
void read(vector<long long>& v) {
  int n = v.size();
  for (int i = 0; i < n; i++) cin >> v[i];
}
void display(vector<int>& v) {
  int n = v.size();
  for (int i = 0; i < n; i++) cout << v[i] << " ";
  cout << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    unsigned long long n;
    cin >> n;
    vector<int> arr;
    unsigned long long x = n, rem;
    while (x > 0) {
      rem = x % 3;
      arr.push_back(rem);
      x = x - rem;
      x = x / 3;
    }
    int r = -1;
    for (int i = 0; i < arr.size(); i++) {
      if (arr[i] == 2) r = i;
    }
    if (r == -1)
      cout << n << "\n";
    else {
      int l = -1;
      for (int i = r + 1; i < arr.size(); i++) {
        if (arr[i] == 0) {
          l = i;
          break;
        }
      }
      if (l == -1) {
        cout << (long long)pow(3, arr.size()) << "\n";
      } else {
        long long ans = (long long)pow(3, l);
        for (int i = l + 1; i < arr.size(); i++) {
          ans = ans + arr[i] * ((long long)pow(3, i));
        }
        cout << ans << "\n";
      }
    }
  }
}
