#include <bits/stdc++.h>
using namespace std;
long long int powe(long long int x, long long int y) {
  long long int s = 1;
  while (y) {
    if (y % 2) s *= x;
    y /= 2, x *= x;
  }
  return s;
}
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, i, tn, s = 0, c = 0, k = 1, j;
    cin >> n;
    tn = n;
    vector<long long int> v;
    while (tn) v.push_back(tn % 3), tn /= 3, c++;
    reverse(v.begin(), v.end());
    for (i = 0; i < v.size(); i++) {
      if (v[i] > 1) {
        s = 3;
        for (j = i - 1; j >= 0; j--) s = min(s, v[j]);
        if (s) break;
        for (j = i - 1; j >= 0; j--)
          if (!v[j]) break;
        v[j++] = 1;
        for (; j < v.size(); j++) v[j] = 0;
        break;
      }
    }
    reverse(v.begin(), v.end());
    if (s)
      tn = powe(3, c);
    else
      for (i = 0; i < v.size(); i++) tn += k * v[i], k *= 3;
    cout << tn << endl;
  }
}
