#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> mark(40, 0);
    long long mult = 1, i = 0;
    while (mult < n) {
      i++;
      mult *= 3;
    }
    for (; i >= 0; i--) {
      long long val = (n / mult) % 3;
      if (val > 1) {
        break;
      }
      if (val) {
        mark[i] = 1;
      }
      mult /= 3;
    }
    if (i != -1) {
      i++;
      mult *= 3;
      while (mark[i]) {
        i++;
        mult *= 3;
      }
      mark[i] = 1;
      n = 0;
      for (; i < 40; i++) {
        if (mark[i]) n += mult;
        mult *= 3;
      }
    }
    cout << n << '\n';
  }
}
