#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const float EPS = 1e-9;
const long long MOD = 1e9 + 7;
int main() {
  int q = 0;
  cin >> q;
  while (q--) {
    long long n = 0;
    cin >> n;
    vector<long long> arr;
    while (n > 0) {
      arr.push_back(n % 3);
      n /= 3;
    }
    arr.push_back(0);
    long long cur = 0, curt = 1;
    for (int i = 0; i < arr.size(); i++) {
      if (arr[i] > 1) {
        cur = 0;
        arr[i] = -1;
        arr[i + 1]++;
      } else if (arr[i] > 0) {
        cur += curt;
      }
      curt *= 3;
    }
    cout << cur << endl;
  }
  return 0;
}
