#include <bits/stdc++.h>
using namespace std;
vector<int> base3(long long n) {
  vector<int> yo;
  while (n) {
    yo.push_back(n % 3);
    n /= 3;
  }
  return yo;
}
long long top3(vector<int> yo) {
  long long ans = 0;
  for (int i = yo.size() - 1; i >= 0; i--) {
    ans = ans * 3 + yo[i];
  }
  return ans;
}
void solve() {
  long long n;
  cin >> n;
  vector<int> arr = base3(n);
  int carry = 1;
  int index = arr.size() - 1;
  while (index >= 0 && arr[index] != 2) index--;
  if (index == -1) {
    cout << n << endl;
    return;
  }
  for (int i = index; i >= 0; i--) arr[i] = 0;
  for (int i = index + 1; i < arr.size(); i++) {
    arr[i] = arr[i] + carry;
    carry = arr[i] / 3;
    arr[i] = arr[i] % 3;
    if (arr[i] > 1) {
      arr[i] = 0;
      carry++;
    }
  }
  if (carry) {
    arr.push_back(1);
  }
  cout << top3(arr) << endl;
}
int main() {
  int q;
  cin >> q;
  while (q--) solve();
}
