#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int q;
  cin >> q;
  while (q--) {
    long long int n;
    cin >> n;
    long long int x = 0;
    long long int on = n;
    long long int n_ori = n;
    while (on > 0) {
      x++;
      on /= 3;
    }
    x--;
    vector<long long int> v;
    long long int flag1 = 0;
    for (long long int i = x; i >= 0; i--) {
      if (n >= (long long int)pow(3, i)) {
        v.push_back(1);
        n -= (long long int)pow(3, i);
      } else {
        v.push_back(0);
      }
      if (n == 0) {
        cout << n_ori << endl;
        flag1 = 1;
        break;
      }
    }
    if (flag1 == 1) {
      continue;
    }
    long long int flag = 0;
    for (long long int i = v.size() - 1; i >= 0; i--) {
      if (v[i] == 0) {
        v[i] = 1;
        for (long long int j = i + 1; j < v.size(); j++) {
          v[j] = 0;
        }
        flag = 1;
        break;
      }
    }
    if (flag == 0) {
      cout << (long long int)pow(3, x + 1) << endl;
      continue;
    }
    long long int ans = 0;
    for (long long int i = 0; i < v.size(); i++) {
      if (v[i] == 1) {
        ans += pow(3, v.size() - 1 - i);
      }
    }
    cout << ans << endl;
  }
}
