#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int a[maxn];
int b[maxn];
int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    long long n;
    scanf("%lld", &n);
    long long num = 0;
    long long sum = 0;
    long long y = 1;
    while (n) {
      long long x = n % 3;
      n /= 3;
      x += num;
      num = 0;
      if (x >= 2) {
        x = 0;
        num = 1;
        sum = 0;
      }
      sum += y * x;
      y *= 3;
    }
    if (num > 0) sum = y * num;
    printf("%lld\n", sum);
  }
}
