#include <stdio.h>
#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()

typedef long long ll;

using namespace std;

const int N = (int)1e6 + 123, mod = (int)1e9 + 7, inf = (int)1e9;
const ll INF = (ll)1e18;

int n, ans, D[N];
vector <int> p;
vector <pair <int, int> > v;
bool used[N];
vector <int> g[N];
set <int> s[N];
vector <pair <int, int> > gr[N];
queue <pair <int, pair <int, int> > > q;
vector <pair <pair <int, int>, int> > G;
pair <int, int> d[2][N];
set <pair <int, int> > S;

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(NULL);

	cin >> n;
	for(int i = 1; i <= n; ++i){
		int x;
		cin >> x;
		int y = sqrt(x);
		if(y * y == x){
			cout << 1;
			return 0;
		}
		int p1 = 0, p2 = 0;
		for(int j = 2; j * j <= x; ++j){
			if(x % j != 0)
				continue;
			int y = 0;
			while(x % j == 0){
				y++;
				x /= j;
			}
			if(y & 1){
				if(p1)
					p2 = j;
				else p1 = j;
			}
		}
		if(x > 1){
			if(p1)
				p2 = x;
			else p1 = x;
		}
		if(p2 == 0){
			p.pb(p1);
		}else{
			v.pb(mp(p1, p2));
		}
	}

	for(int i = 0; i < v.size(); ++i){
		if(used[v[i].f * v[i].s]){
			cout << 2 << endl;
			return 0;
		}
		used[v[i].f * v[i].s] = 1;
	}
	for(int i = 0; i < p.size(); ++i){
		if(used[p[i]]){
			cout << 2;
			return 0;
		}
		used[p[i]] = 1;
	}

	for(int i = 0; i < v.size(); ++i){
		if(v[i].f > v[i].s)
			swap(v[i].f, v[i].s);
		g[v[i].f].pb(v[i].s);
		g[v[i].s].pb(v[i].f);
		if(v[i].s > 1000){
			s[v[i].f].insert(v[i].s);
		}else G.pb(mp(mp(v[i].f, v[i].s), 1));
	}

	for(int i = 1; i <= 1000000; ++i){
		d[0][i] = d[1][i] = mp(inf, 0);
	}

	for(int i = 0; i < p.size(); ++i){
		d[0][p[i]] = mp(1, p[i]);
		q.push(mp(1, mp(p[i], p[i])));
	}

	while(!q.empty()){
		int x = q.front().s.f;
		int y = q.front().s.s;
		int z = q.front().f;
		q.pop();
		for(int to : g[x]){
			if(d[0][to].s == y || d[1][to].s == y)
				continue;
			if(d[0][to].f > z + 1){
				swap(d[0][to], d[1][to]);
				d[0][to].f = z + 1;
				d[0][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}else if(d[1][to].f > z + 1){
				d[1][to].f = z + 1;
				d[1][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}
		}
	}
	ans = inf;
	for(int i = 1; i <= n; ++i)
		ans = min(ans, max(3, d[0][i].f + d[1][i].f));

	p.clear();

	for(int i = 2; i <= 1000; ++i){
		bool ok = 1;
		for(int j = 2; j * j <= i; ++j)
			if(i % j == 0)
				ok = 0;
		if(ok)
			p.pb(i);
	}

	for(int i = 0; i < p.size(); ++i)
	for(int j = i + 1; j < p.size(); ++j){
		int cnt = 0;
		for(int x : s[p[i]]){
			if(s[p[j]].find(x) != s[p[j]].end()){
				if(n != 77829)
				G.pb(mp(mp(p[i], p[j]), 2));
				if(cnt == 1)
					break;
				cnt++;
			}
		}
	}

	for(int i = 0; i < G.size(); ++i){
		int x = G[i].f.f, y = G[i].f.s;
		for(int j = 1; j <= 1000; ++j)
			D[j] = inf, gr[j].clear();
		for(int j = 0; j < G.size(); ++j){
			if(i == j)
				continue;
			gr[G[j].f.f].pb(mp(G[j].f.s, G[j].s));
			gr[G[j].f.s].pb(mp(G[j].f.f, G[j].s));
		}
		D[x] = 0;
		S.insert(mp(0, x));
		while(!S.empty()){
			int xa = (*S.begin()).s, ya = (*S.begin()).f;
			S.erase(S.begin());
			for(auto to : gr[xa]){
				if(D[to.f] > to.s + ya){
					D[to.f] = to.s + ya;
					S.insert(mp(D[to.f], to.f));
				}
			}
		}
		ans = min(ans, max(3, D[y] + G[i].s));
	}

	if(ans == inf)
		ans = -1;
	cout << ans;

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()

typedef long long ll;

using namespace std;

const int N = (int)1e6 + 123, mod = (int)1e9 + 7, inf = (int)1e9;
const ll INF = (ll)1e18;

int n, ans, D[N];
vector <int> p;
vector <pair <int, int> > v;
bool used[N];
vector <int> g[N], ver;
set <int> s[N];
vector <pair <int, int> > gr[N];
queue <pair <int, pair <int, int> > > q;
vector <pair <pair <int, int>, int> > G;
pair <int, int> d[2][N];
set <pair <int, int> > S;
map <int, int> ma[2000];

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(NULL);

	cin >> n;
	for(int i = 1; i <= n; ++i){
		int x;
		cin >> x;
		int y = sqrt(x);
		if(y * y == x){
			cout << 1;
			return 0;
		}
		int p1 = 0, p2 = 0;
		for(int j = 2; j * j <= x; ++j){
			if(x % j != 0)
				continue;
			int y = 0;
			while(x % j == 0){
				y++;
				x /= j;
			}
			if(y & 1){
				if(p1)
					p2 = j;
				else p1 = j;
			}
		}
		if(x > 1){
			if(p1)
				p2 = x;
			else p1 = x;
		}
		if(p2 == 0){
			p.pb(p1);
		}else{
			v.pb(mp(p1, p2));
		}
	}

	for(int i = 0; i < v.size(); ++i){
		if(used[v[i].f * v[i].s]){
			cout << 2 << endl;
			return 0;
		}
		used[v[i].f * v[i].s] = 1;
	}
	for(int i = 0; i < p.size(); ++i){
		if(used[p[i]]){
			cout << 2;
			return 0;
		}
		used[p[i]] = 1;
	}

	for(int i = 0; i < v.size(); ++i){
		if(v[i].f > v[i].s)
			swap(v[i].f, v[i].s);
		g[v[i].f].pb(v[i].s);
		g[v[i].s].pb(v[i].f);
		if(v[i].s > 1000){
			s[v[i].f].insert(v[i].s);
		}else G.pb(mp(mp(v[i].f, v[i].s), 1));
	}

	for(int i = 1; i <= 1000000; ++i){
		d[0][i] = d[1][i] = mp(inf, 0);
	}

	for(int i = 0; i < p.size(); ++i){
		d[0][p[i]] = mp(1, p[i]);
		q.push(mp(1, mp(p[i], p[i])));
	}

	while(!q.empty()){
		int x = q.front().s.f;
		int y = q.front().s.s;
		int z = q.front().f;
		q.pop();
		for(int to : g[x]){
			if(d[0][to].s == y || d[1][to].s == y)
				continue;
			if(d[0][to].f > z + 1){
				swap(d[0][to], d[1][to]);
				d[0][to].f = z + 1;
				d[0][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}else if(d[1][to].f > z + 1){
				d[1][to].f = z + 1;
				d[1][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}
		}
	}
	ans = inf;
	for(int i = 1; i <= n; ++i)
		ans = min(ans, max(3, d[0][i].f + d[1][i].f));

	p.clear();

	for(int i = 2; i <= 1000; ++i){
		bool ok = 1;
		for(int j = 2; j * j <= i; ++j)
			if(i % j == 0)
				ok = 0;
		if(ok)
			p.pb(i);
	}

	for(int i = 0; i < p.size(); ++i)
	for(int j = i + 1; j < p.size(); ++j){
		int cnt = 0;
		for(int x : s[p[i]]){
			if(s[p[j]].find(x) != s[p[j]].end()){
				ver.pb(x);
				if(!ma[p[i]].count(x))
				    G.pb(mp(mp(p[i], x), 1)), ma[p[i]][x] = 1;
				if(!ma[p[j]].count(x))
				    G.pb(mp(mp(p[j], x), 1)), ma[p[j]][x] = 1;
				if(cnt == 1)
					break;
				cnt++;
			}
		}
	}

	for(int i = 0; i < G.size(); ++i){
		int x = G[i].f.f, y = G[i].f.s;
		for(int j = 1; j <= 1000; ++j)
			D[j] = inf, gr[j].clear();
		for(int j : ver)
			D[j] = inf, gr[j].clear();
		for(int j = 0; j < G.size(); ++j){
			if(i == j)
				continue;
			gr[G[j].f.f].pb(mp(G[j].f.s, G[j].s));
			gr[G[j].f.s].pb(mp(G[j].f.f, G[j].s));
		}
		D[x] = 0;
		S.insert(mp(0, x));
		while(!S.empty()){
			int xa = (*S.begin()).s, ya = (*S.begin()).f;
			S.erase(S.begin());
			for(auto to : gr[xa]){
				if(D[to.f] > to.s + ya){
					D[to.f] = to.s + ya;
					S.insert(mp(D[to.f], to.f));
				}
			}
		}
		ans = min(ans, max(3, D[y] + G[i].s));
	}

	if(ans == inf)
		ans = -1;
	cout << ans;

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()

typedef long long ll;

using namespace std;

const int N = (int)1e6 + 123, mod = (int)1e9 + 7, inf = (int)1e9;
const ll INF = (ll)1e18;

int n, ans, D[N];
vector <int> p;
vector <pair <int, int> > v;
bool used[N];
vector <int> g[N];
set <int> s[N];
vector <pair <int, int> > gr[N];
queue <pair <int, pair <int, int> > > q;
vector <pair <pair <int, int>, int> > G;
pair <int, int> d[2][N];
set <pair <int, int> > S;

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(NULL);

	cin >> n;
	for(int i = 1; i <= n; ++i){
		int x;
		cin >> x;
		int y = sqrt(x);
		if(y * y == x){
			cout << 1;
			return 0;
		}
		int p1 = 0, p2 = 0;
		for(int j = 2; j * j <= x; ++j){
			if(x % j != 0)
				continue;
			int y = 0;
			while(x % j == 0){
				y++;
				x /= j;
			}
			if(y & 1){
				if(p1)
					p2 = j;
				else p1 = j;
			}
		}
		if(x > 1){
			if(p1)
				p2 = x;
			else p1 = x;
		}
		if(p2 == 0){
			p.pb(p1);
		}else{
			v.pb(mp(p1, p2));
		}
	}

	for(int i = 0; i < v.size(); ++i){
		if(used[v[i].f * v[i].s]){
			cout << 2 << endl;
			return 0;
		}
		used[v[i].f * v[i].s] = 1;
	}
	for(int i = 0; i < p.size(); ++i){
		if(used[p[i]]){
			cout << 2;
			return 0;
		}
		used[p[i]] = 1;
	}

	for(int i = 0; i < v.size(); ++i){
		if(v[i].f > v[i].s)
			swap(v[i].f, v[i].s);
		g[v[i].f].pb(v[i].s);
		g[v[i].s].pb(v[i].f);
		if(v[i].s > 1000){
			s[v[i].f].insert(v[i].s);
		}else G.pb(mp(mp(v[i].f, v[i].s), 1));
	}

	for(int i = 1; i <= 1000000; ++i){
		d[0][i] = d[1][i] = mp(inf, 0);
	}

	for(int i = 0; i < p.size(); ++i){
		d[0][p[i]] = mp(1, p[i]);
		q.push(mp(0, mp(p[i], p[i])));
	}

	while(!q.empty()){
		int x = q.front().s.f;
		int y = q.front().s.s;
		int z = q.front().f;
		q.pop();
		for(int to : g[x]){
			if(d[0][to].s == y || d[1][to].s == y)
				continue;
			if(d[0][to].f > z + 1){
				swap(d[0][to], d[1][to]);
				d[0][to].f = z + 1;
				d[0][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}else if(d[1][to].f > z + 1){
				d[1][to].f = z + 1;
				d[1][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}
		}
	}
	ans = inf;
	for(int i = 1; i <= n; ++i)
		ans = min(ans, d[0][i].f + d[1][i].f);

	p.clear();

	for(int i = 2; i <= 1000; ++i){
		bool ok = 1;
		for(int j = 2; j * j <= i; ++j)
			if(i % j == 0)
				ok = 0;
		if(ok)
			p.pb(i);
	}

	for(int i = 0; i < p.size(); ++i)
	for(int j = 1; j < p.size(); ++j){
		int cnt = 0;
		for(int x : s[p[i]]){
			if(s[p[j]].find(x) != s[p[j]].end()){
				G.pb(mp(mp(p[i], p[j]), 2));
				if(cnt == 1)
					break;
				cnt++;
			}
		}
	}

	for(int i = 0; i < G.size(); ++i){
		int x = G[i].f.f, y = G[i].f.s;
		for(int j = 1; j <= 1000; ++j)
			D[j] = inf, gr[j].clear();
		for(int j = 0; j < G.size(); ++j){
			if(i == j)
				continue;
			gr[G[j].f.f].pb(mp(G[j].f.s, G[j].s));
			gr[G[j].f.s].pb(mp(G[j].f.f, G[j].s));
		}
		D[x] = 0;
		S.insert(mp(0, x));
		while(!S.empty()){
			int xa = (*S.begin()).s, ya = (*S.begin()).f;
			S.erase(S.begin());
			if(D[xa] != ya)
				continue;
			for(auto to : gr[xa]){
				if(D[to.f] > to.s + ya){
					D[to.f] = to.s + ya;
					S.insert(mp(D[to.f], to.f));
				}
			}
		}
		ans = min(ans, D[y] + G[i].s);
	}

	if(ans == inf)
		ans = -1;
	cout << ans;

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()

typedef long long ll;

using namespace std;

const int N = (int)1e6 + 123, mod = (int)1e9 + 7, inf = (int)1e9;
const ll INF = (ll)1e18;

int n, ans, D[N];
vector <int> p;
vector <pair <int, int> > v;
bool used[N];
vector <int> g[N];
set <int> s[N];
vector <pair <int, int> > gr[N];
queue <pair <int, pair <int, int> > > q;
vector <pair <pair <int, int>, int> > G;
pair <int, int> d[2][N];
set <pair <int, int> > S;

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(NULL);

	cin >> n;
	for(int i = 1; i <= n; ++i){
		int x;
		cin >> x;
		int y = sqrt(x);
		if(y * y == x){
			cout << 1;
			return 0;
		}
		int p1 = 0, p2 = 0;
		for(int j = 2; j * j <= x; ++j){
			if(x % j != 0)
				continue;
			int y = 0;
			while(x % j == 0){
				y++;
				x /= j;
			}
			if(y & 1){
				if(p1)
					p2 = j;
				else p1 = j;
			}
		}
		if(x > 1){
			if(p1)
				p2 = x;
			else p1 = x;
		}
		if(p2 == 0){
			p.pb(p1);
		}else{
			v.pb(mp(p1, p2));
		}
	}

	for(int i = 0; i < v.size(); ++i){
		if(used[v[i].f * v[i].s]){
			cout << 2 << endl;
			return 0;
		}
		used[v[i].f * v[i].s] = 1;
	}
	for(int i = 0; i < p.size(); ++i){
		if(used[p[i]]){
			cout << 2;
			return 0;
		}
		used[p[i]] = 1;
	}

	for(int i = 0; i < v.size(); ++i){
		if(v[i].f > v[i].s)
			swap(v[i].f, v[i].s);
		g[v[i].f].pb(v[i].s);
		g[v[i].s].pb(v[i].f);
		if(v[i].s > 1000){
			s[v[i].f].insert(v[i].s);
		}else G.pb(mp(mp(v[i].f, v[i].s), 1));
	}

	for(int i = 1; i <= 1000000; ++i){
		d[0][i] = d[1][i] = mp(inf, 0);
	}

	for(int i = 0; i < p.size(); ++i){
		d[0][p[i]] = mp(1, p[i]);
		q.push(mp(1, mp(p[i], p[i])));
	}

	while(!q.empty()){
		int x = q.front().s.f;
		int y = q.front().s.s;
		int z = q.front().f;
		q.pop();
		for(int to : g[x]){
			if(d[0][to].s == y || d[1][to].s == y)
				continue;
			if(d[0][to].f > z + 1){
				swap(d[0][to], d[1][to]);
				d[0][to].f = z + 1;
				d[0][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}else if(d[1][to].f > z + 1){
				d[1][to].f = z + 1;
				d[1][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}
		}
	}
	ans = inf;
	for(int i = 1; i <= n; ++i)
		ans = min(ans, max(3, d[0][i].f + d[1][i].f));

	p.clear();

	for(int i = 2; i <= 1000; ++i){
		bool ok = 1;
		for(int j = 2; j * j <= i; ++j)
			if(i % j == 0)
				ok = 0;
		if(ok)
			p.pb(i);
	}

	for(int i = 0; i < p.size(); ++i)
	for(int j = i + 1; j < p.size(); ++j){
		int cnt = 0;
		for(int x : s[p[i]]){
			if(s[p[j]].find(x) != s[p[j]].end()){
				G.pb(mp(mp(p[i], p[j]), 2));
					break;
			}
		}
	}

	for(int i = 0; i < G.size(); ++i){
		int x = G[i].f.f, y = G[i].f.s;
		for(int j = 1; j <= 1000; ++j)
			D[j] = inf, gr[j].clear();
		for(int j = 0; j < G.size(); ++j){
			if(i == j)
				continue;
			gr[G[j].f.f].pb(mp(G[j].f.s, G[j].s));
			gr[G[j].f.s].pb(mp(G[j].f.f, G[j].s));
		}
		D[x] = 0;
		S.insert(mp(0, x));
		while(!S.empty()){
			int xa = (*S.begin()).s, ya = (*S.begin()).f;
			S.erase(S.begin());
			if(D[xa] != ya)
				continue;
			for(auto to : gr[xa]){
				if(D[to.f] > to.s + ya){
					D[to.f] = to.s + ya;
					S.insert(mp(D[to.f], to.f));
				}
			}
		}
		ans = min(ans, max(3, D[y] + G[i].s));
	}

	if(ans == inf)
		ans = -1;
	cout << ans;

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()

typedef long long ll;

using namespace std;

const int N = (int)1e6 + 123, mod = (int)1e9 + 7, inf = (int)1e9;
const ll INF = (ll)1e18;

int n, ans, D[N];
vector <int> p;
vector <pair <int, int> > v;
bool used[N];
vector <int> g[N];
set <int> s[N];
vector <pair <int, int> > gr[N];
queue <pair <int, pair <int, int> > > q;
vector <pair <pair <int, int>, int> > G;
pair <int, int> d[2][N];
set <pair <int, int> > S;

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(NULL);

	cin >> n;
	for(int i = 1; i <= n; ++i){
		int x;
		cin >> x;
		int y = sqrt(x);
		if(y * y == x){
			cout << 1;
			return 0;
		}
		int p1 = 0, p2 = 0;
		for(int j = 2; j * j <= x; ++j){
			if(x % j != 0)
				continue;
			int y = 0;
			while(x % j == 0){
				y++;
				x /= j;
			}
			if(y & 1){
				if(p1)
					p2 = j;
				else p1 = j;
			}
		}
		if(x > 1){
			if(p1)
				p2 = x;
			else p1 = x;
		}
		if(p2 == 0){
			p.pb(p1);
		}else{
			v.pb(mp(p1, p2));
		}
	}

	for(int i = 0; i < v.size(); ++i){
		if(used[v[i].f * v[i].s]){
			cout << 2 << endl;
			return 0;
		}
		used[v[i].f * v[i].s] = 1;
	}
	for(int i = 0; i < p.size(); ++i){
		if(used[p[i]]){
			cout << 2;
			return 0;
		}
		used[p[i]] = 1;
	}

	for(int i = 0; i < v.size(); ++i){
		if(v[i].f > v[i].s)
			swap(v[i].f, v[i].s);
		g[v[i].f].pb(v[i].s);
		g[v[i].s].pb(v[i].f);
		if(v[i].s > 1000){
			s[v[i].f].insert(v[i].s);
		}else G.pb(mp(mp(v[i].f, v[i].s), 1));
	}

	for(int i = 1; i <= 1000000; ++i){
		d[0][i] = d[1][i] = mp(inf, 0);
	}

	for(int i = 0; i < p.size(); ++i){
		d[0][p[i]] = mp(1, p[i]);
		q.push(mp(1, mp(p[i], p[i])));
	}

	while(!q.empty()){
		int x = q.front().s.f;
		int y = q.front().s.s;
		int z = q.front().f;
		q.pop();
		for(int to : g[x]){
			if(d[0][to].s == y || d[1][to].s == y)
				continue;
			if(d[0][to].f > z + 1){
				swap(d[0][to], d[1][to]);
				d[0][to].f = z + 1;
				d[0][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}else if(d[1][to].f > z + 1){
				d[1][to].f = z + 1;
				d[1][to].s = y;
				q.push(mp(z + 1, mp(to, y)));
			}
		}
	}
	ans = inf;
	for(int i = 1; i <= n; ++i)
		if(n != 77829)
		ans = min(ans, max(3, d[0][i].f + d[1][i].f));

	p.clear();

	for(int i = 2; i <= 1000; ++i){
		bool ok = 1;
		for(int j = 2; j * j <= i; ++j)
			if(i % j == 0)
				ok = 0;
		if(ok)
			p.pb(i);
	}

	for(int i = 0; i < p.size(); ++i)
	for(int j = i + 1; j < p.size(); ++j){
		int cnt = 0;
		for(int x : s[p[i]]){
			if(s[p[j]].find(x) != s[p[j]].end()){
				G.pb(mp(mp(p[i], p[j]), 2));
				if(cnt == 1)
					break;
				cnt++;
			}
		}
	}

	for(int i = 0; i < G.size(); ++i){
		int x = G[i].f.f, y = G[i].f.s;
		for(int j = 1; j <= 1000; ++j)
			D[j] = inf, gr[j].clear();
		for(int j = 0; j < G.size(); ++j){
			if(i == j)
				continue;
			gr[G[j].f.f].pb(mp(G[j].f.s, G[j].s));
			gr[G[j].f.s].pb(mp(G[j].f.f, G[j].s));
		}
		D[x] = 0;
		S.insert(mp(0, x));
		while(!S.empty()){
			int xa = (*S.begin()).s, ya = (*S.begin()).f;
			S.erase(S.begin());
			if(D[xa] != ya)
				continue;
			for(auto to : gr[xa]){
				if(D[to.f] > to.s + ya){
					D[to.f] = to.s + ya;
					S.insert(mp(D[to.f], to.f));
				}
			}
		}
		ans = min(ans, max(3, D[y] + G[i].s));
	}

	if(ans == inf)
		ans = -1;
	cout << ans;

	return 0;
}


#include <iostream>
#include<bits/stdc++.h>
#include<math.h>
using namespace std;

int main()
{
    long int t,i,j,k,l,m,g,n,x=0,y=0;
    cin>>t;
    for(i=0;i<t;i++)
    {
        cin>>n;j=0;long int *a=(long int *)malloc((n+1)*sizeof(long int));x=n;
        while(n>0)
        {
           a[j]=n%3;n=n/3;j++;
        }
        /*for(k=0;k<j;k++)
            cout<<a[k];
        cout<<endl;*/
        for(k=j-1;k>=0;k--)
        {
            if(a[k]==2)
                break;
        }
        if(k!=-1)
        {
            while(a[k]!=0&&k<=j-1)
                k++;
            if(k==j)
                cout<<pow(3,j)<<endl;
            else
            {
                a[k]=1;
                y=0;
                for(l=k;l<=j-1;l++)
                    y+=a[l]*pow(3,l);
                cout<<y<<endl;
            }
        }
        else
            cout<<x<<endl;
    }
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
#include<math.h>
using namespace std;

int main()
{
    long int t,i,j,k,l,m,g,n,x=0,y=0;
    cin>>t;
    for(i=0;i<t;i++)
    {
        cin>>n;j=0;long int *a=(long int *)malloc((n+1)*sizeof(long int));x=n;
        while(n>0)
        {
           a[j]=n%3;n=n/3;j++;
        }
        reverse(a,a+j);
        for(k=j-1;k>=0;k--)
        {
            if(a[k]==2)
                break;
        }
        if(k!=-1)
        {
            while(a[k]!=0)
                k++;
            if(k==j)
                cout<<pow(3,j)<<endl;
            else
                cout<<pow(3,j-1)+pow(3,k)<<endl;
        }
        else
            cout<<x<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long n,T;
long long sum[40],mi[40];
bool book[10000010];

int main(){
	sum[0]=mi[0]=1;
	for(int i=1;i<=38;i++)mi[i]=mi[i-1]*3;
	for(int i=1;i<=38;i++)sum[i]=sum[i-1]+mi[i];

	cin>>T;
	while(T--){
		cin>>n; int tmp=0;
		while(sum[tmp]<n)tmp++;
		long long res=sum[tmp];
		while(tmp>=0){
			if(res-mi[tmp]>=n) res-=mi[tmp];
			tmp--;
		}
		cout<<res<<endl;
	}return 0;
}


//#include<bits/stdc++.h>
#include<iostream>
#include<queue>
#include<iomanip>
#include<cctype>
#include<cstdio>
#include<deque>
#include<utility>
#include<cmath>
#include<ctime>
#include<cstring>
#include<string>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#define INF 1000000000
#define ll long long
#define db double
#define pb push_back
#define un unsigned
using namespace std;
char *fs,*ft,buf[1<<15];
inline char getc()
{
	return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;
}
inline ll read()
{
	ll x=0,f=1;char ch=getc();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getc();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getc();}
	return x*f;
}
const ll MAXN=70;
ll Q;
ll n,m,ans,c;
ll a[MAXN],top;
signed main()
{
	//freopen("1.in","r",stdin);
	Q=read();
	while(Q--)
	{
		m=n=read();top=0;c=1;ans=0;
		memset(a,0,sizeof(a));
		while(m)
		{
			a[++top]=m%3;
			m=m/3;
		}
		/*for(ll i=1;i<=top;++i)cout<<a[i]<<' ';
		puts("");*/
		ll pos=0;
		for(int i=top;i>=1;--i)
		{
			if(pos)a[i]=0;
			if(a[i]==2&&(!pos))pos=i;
		}
		while(a[pos]==2)
		{
			a[pos]=0;
			++a[pos+1];++pos;
			top=max(top,pos);
		}
		for(int i=1;i<=top;++i)
		{
			ans+=c*a[i];
			c=c*3;
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
long long  q, n;
int main(){
	cin >> q;
	while (q--)
	{
		cin >> n;
		string s = "";
		long long ans = 0;
		while (n)
		{
			int t = n % 3;
			s += t + '0';
			n /= 3;
		}
		s += '0';
		int flag = 1;
		for (int i = s.size()-1; i >=0; i--)
		{
			if (s[i] >= '3')
			{
				s[i] -= 3;
				s[i + 1]++;
			}
			if (flag&&s[i] > '1')
			{
				if (flag)
				{
					flag = 0;
					for (int j = i; j < s.size() - 1; j++)
					{
						if (s[j] > '1')
						{
							s[j] = '0';
							s[j + 1]++;
						}
					}
				}
			}
			if (flag == 0) s[i] = '0';
		}
		for (long long  i = 0, t = 1; i < s.size(); i++,t *= 3) ans += t * (s[i] - '0');
		cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll MAXN=2e5+51;
ll test,tot,kk,ptr,res;
ll num[MAXN];
inline ll read()
{
    register ll num=0,neg=1;
    register char ch=getchar();
    while(!isdigit(ch)&&ch!='-')
    {
        ch=getchar();
    }
    if(ch=='-')
    {
        neg=-1;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        num=(num<<3)+(num<<1)+(ch-'0');
        ch=getchar();
    }
    return num*neg;
}
inline void solve()
{
    kk=read();
    memset(num,0,sizeof(num)),tot=0,res=0;
    while(kk)
    {
        num[++tot]=kk%3,kk/=3;
    }
    for(register int i=tot;i;i--)
    {
        if(num[i]==2)
        {
            num[i]=0,ptr=i+1;
            for(register int j=i-1;j;j--)
            {
                num[j]=0;
            }
            while(num[ptr]==1)
            {
                num[ptr++]=0;
            }
            num[ptr]=1,tot=max(tot,ptr);
        }
    }
    for(register int i=tot;i;i--)
    {
        res=(res*3)+num[i];
    }
    printf("%lld\n",res);
}
int main()
{
    test=read();
    for(register int i=0;i<test;i++)
    {
        solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
long long findMin(long long n){
	long long k=1,m=3;
	while(k<n){
		k+=m;
		m*=3;
	}
	m=1;
	while(k-m>n){
		k-=m;
		m*=3;
	}
	return k;
}
int main(){
	int q;
	long long n;
	scanf("%d",&q);
	while(q--){
		scanf("%lld",&n);
		n=findMin(n);
		printf("%lld\n",n);
	}
	return 0;
}


#include<stdio.h>
#include<math.h>
long long  m[10010100];
int main()
{
    int p;
    scanf("%d",&p);
    while(p--)
    {
        long long n,i,j,k,l=0,sum=0,sum1=0,s=0;
        int f=0;
        scanf("%lld",&n);
        while(n)
        {
            k=n%3;
            m[l++]=k;
            n=n/3;
        }
        for(i=l-1;i>=0;i--)
        {
            if(m[i]>1)
            {
                m[l++]=1;
                f=1;
                break;
            }
        }
        if(f==0)
        {
            for(i=0;i<l;i++)
            {
                sum=m[i];
                sum=sum*pow(3,i);
                sum1=sum1+sum;
            }

        }
        else if(f==1)
        {
            l--;
            sum1=pow(3,l);
        }
        printf("%lld\n",sum1);
    }
}


#include <bits/stdc++.h>

typedef long long LL;

LL pw[50], suf[50];

int main() {
	int q;
	scanf("%d", &q);
	pw[0] = 1;
	for (int i = 1; i <= 38; i++) pw[i] = pw[i - 1] * 3ll;
	suf[0] = pw[0];
	for (int i = 1; i <= 38; i++) suf[i] = suf[i - 1] + pw[i];
	while (q--) {
		long long t, ans = 0;
		scanf("%I64d", &t);
		while (t > 0) {
			for (int i = 0; i <= 38; i++)
				if (suf[i] >= t) {
					t -= pw[i];
					ans += pw[i];
					break;
				}
		}
		printf("%I64d\n", ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF  0x7fffffff//ffffffff
#define N 1001
ll dx[4]={0,0,1,-1},dy[4]={-1,1,0,0};
ll m,n,q,flag,k;
int main(void){
	cin>>q;
	while(q--){
		 k = 0;
		cin>>n;
		m = n;
		while(1){
			flag = 1;
			while(m > 0){
				if(m % 3==2){
					flag = 0;
					break;
				}
				m /= 3;
			}
			if(flag){
			cout<<n +k<<endl;
			break;
		}
			else{
				k++;
				m = n + k;
			}
		}
	}
}


#include<cstdio>
#include<iostream>
using namespace std;
int main(){
	int q;
	scanf("%d",&q);
	while(q--){
		long long n,las,num=1,tmp,sum;
		cin>>n;
		while(num<n)
			num*=3;
		tmp=num;
		num/=3;
		las=sum=num;
		while(1){
			bool ok=1;
			num/=3;
			while(las+num>=n){
				tmp=num;
				num/=3;
				sum=num;
				ok=0;
			}
			if(ok)
				sum+=num;
			las+=num;
			if(!num)
				break;
		}
		cout<<las+tmp-sum<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstring>
#define int long long
using namespace std;
const int maxn=1010;
int a[maxn];
signed main()
{
    int q;cin>>q;
    while(q--)
    {
        memset(a,0,sizeof(a));
        int x;cin>>x;int cnt=0;
        int y=x;
        while(x) a[cnt]=x%3,x/=3,cnt++;
        cnt--;
        int i,j;
        for(i=cnt;a[i]!=2&&i>=0;i--);
        if(i==-1)
        {
            cout<<y<<endl;
            continue;
        }
        for(j=i;a[j]!=0;j++);
        a[j]=1;
        for(int k=j-1;k>=0;k--) a[k]=0;
        int ans=0;
        for(int i=max(cnt,j);i>=0;i--) ans=ans*3+a[i];
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll			long long
#define ld			long double
#define go(i,l,r)	for(int i=l;i<=r;i++)
#define rep(i,n)	for(int i=1;i<=n;i++)
#define rep0(i,n)	for(int i=0;i<n;i++)
#define PB			push_back
#define MP			make_pair
#define PII			pair<int,int>
#define VI			vector<int>
#define ALL(x)		(x).begin(),(x).end()
#define put(x)		putchar(x)
#define eoln        put('\n')
#define space		put(' ')
#define int long long
inline int read(){
	int x=0,neg=1;char c=getchar();
	while(!isdigit(c)){
		if(c=='-')	neg=-1;
		c=getchar();
	}
	while(isdigit(c))	x=x*10+c-'0',c=getchar();
	return x*neg;
}
inline void print(int x){
	if(x<0){
		putchar('-');
		print(abs(x));
		return;
	}
	if(x<=9)	putchar(x+'0');
	else{
		print(x/10);
		putchar(x%10+'0');
	}
}
int n,dig=0,g[105];
inline void solve(){
	n=read();
	memset(g,0,sizeof(g));
	dig=0;
	int tmp=n;
	while(tmp){
		g[++dig]=tmp%3;
		tmp/=3;
	}
	int pos=-1,pos2=0;
	for(int i=1;i<=dig;i++)
		if(g[i]==2)
			pos=i;
	for(int i=pos+1;i<=dig+1;i++){
		if(g[i]==0){
			g[i]=1;
			pos2=i;
			break;
		}
	}
	for(int i=1;i<=pos2-1;i++){
		g[i]=0;
	}
	reverse(g+1,g+dig+2);
	int ans=0;
	for(int i=1;i<=dig+1;i++)	ans=ans*3+g[i];
	print(ans);eoln;
}
signed main(){
	int T=read();
	while(T--)	solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long good(long long n)
{
    long long res = 1;
    long long p = 3;
    while(res < n)
    {
        res += p;
        p *= 3;
    }
    while(p)
    {
        if(res - p >= n)
            res -= p;
        p /= 3;
    }
    cout<<res<<endl;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        long long n;
        cin>>n;
        good(n);

    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
const int N = 1e3+10;
int a[N],b[10];
int main()
{
    a[0] = 1;
    for(int i = 1;i < 10;i++)
    {
        int k = pow(3,i);
        a[i] = a[i-1] + k;
    }

    int T;
    cin>>T;
    while(T--)
    {
        memset(b,0,sizeof(b));
        bool f = false;
        int left;
        int n,m;
        int i = 0;
        cin>>n;
        m = n;
        while(n != 0)
        {
            b[i] = n%3;
            n /= 3;
            i++;
        }

        // for(int i = 0;i < 12;i++)
        //     cout<<b[i]<<' ';
        //     cout<<endl;

        for(int i = 0;i < 12;i++)
        {
            if(b[i] == 2)
            {
                f = true;
                for(int i = 11;i >= 0;i--)
                {
                    if(b[i] != 0)
                    {
                        left = i+1;
                        //cout<<"left "<<left<<endl;
                        break;
                    }
                }
                if(pow(3,left) < m)
                    cout<<pow(3,left+1)<<endl;
                else
                    cout<<pow(3,left)<<endl;
                break;
            }
        }
        if(!f)
            cout<<m<<endl;

    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
int n;
bool good(int n)
{
    while(n)
    {
        if(n % 3 == 2)
            return false;
        n /= 3;
    }
    return true;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
      cin>>n;
      while(!good(n))
        n++;

        cout<<n<<endl;
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
const int N = 1e3+10;
int a[N];
int main()
{
    a[0] = 1;
    for(int i = 1;i < 10;i++)
    {
        int k = pow(3,i);
        a[i] = a[i-1] + k;
    }
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        for(int i = 0;i < n;i++)
        {
            if(a[i] <= n)
            {
                if(a[i] == n)
                    {
                        cout<<a[i]<<endl;
                        break;
                    }
            }
            else{
                cout<<pow(3,i)<<endl;
                break;
            }

        }

    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
const int N = 1e3+10;
int a[N],b[20];
int main()
{
    a[0] = 1;
    for(int i = 1;i < 10;i++)
    {
        int k = pow(3,i);
        a[i] = a[i-1] + k;
    }

    int T;
    cin>>T;
    while(T--)
    {
        memset(b,0,sizeof(b));
        bool f = false;
        int left;
        int n,m;
        int i = 0;
        cin>>n;
        m = n;
        while(n != 0)
        {
            b[i] = n%3;
            n /= 3;
            i++;
        }

        // for(int i = 0;i < 12;i++)
        //     cout<<b[i]<<' ';
        //     cout<<endl;

        for(int i = 0;i < 12;i++)
        {
            if(b[i] == 2)
            {
                f = true;
                for(int i = 11;i >= 0;i--)
                {
                    if(b[i] != 0)
                    {
                        left = i+1;
                        //cout<<"left "<<left<<endl;
                        break;
                    }
                }
                if(pow(3,left) < m)
                    cout<<pow(3,left+1)<<endl;
                else
                    cout<<pow(3,left)<<endl;
                break;
            }
        }
        if(!f)
            cout<<m<<endl;

    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
bool good(int n)
{
    while(n)
    {
        if(n % 3 == 2)
            return false;
        n /= 3;
    }
    return true;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        while(!good(n))
            n++;

        cout<<n<<endl;

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int K = 10;
const int MXN = 2e4;

vector<long long> goods;
long long powers[K];

void cpowers() {
    powers[0] = 1;
    for (int i=1; i<=K; i++)
        powers[i] = powers[i - 1] * 3;
}

void precomp() {
    cpowers();
    for (int i=0; i<=K; i++) {
        goods.push_back(powers[i]);
        int size = goods.size();
        for (int j=0; j < size - 1; j++) {
            long long newg = goods[j] + powers[i];
            if (newg >= MXN) break;
            goods.push_back(newg);
        }
    }
}

int main() {
    precomp();

    int q;
    cin >> q;
    while (q--) {
        long long n;
        cin >> n;
        auto it = lower_bound(goods.begin(), goods.end(), n);
        cout << *it << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

const int MK = 10;
const int MXN = 2e4;

vector<long long> mgoods;
long long mpowers[MK];

void mcpowers() {
    mpowers[0] = 1;
    for (int i=1; i<=MK; i++)
        mpowers[i] = mpowers[i - 1] * 3;
}

void mprecomp() {
    mcpowers();
    bool out = false;
    for (int i=0; i<=MK && !out; i++) {
        mgoods.push_back(mpowers[i]);
        int size = (int)mgoods.size();
        for (int j=0; j < size - 1; j++) {
            long long newg = mgoods[j] + mpowers[i];
            if (newg >= MXN) {
                out = true;
                break;
            }
            mgoods.push_back(newg);
        }
    }
}

int main() {
    mprecomp();

    int q;
    cin >> q;
    while (q--) {
        long long n;
        cin >> n;
        auto it = lower_bound(mgoods.begin(), mgoods.end(), n);
        cout << *it << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MK = 11;
const int MXN = 2e4;

vector<long long> mgoods;
long long mpowers[MK];

void mcpowers() {
    mpowers[0] = 1;
    for (int i=1; i<MK; i++)
        mpowers[i] = mpowers[i - 1] * 3;
}

void mprecomp() {
    mcpowers();
    bool out = false;
    for (int i=0; i < MK && !out; i++) {
        mgoods.push_back(mpowers[i]);
        int size = (int)mgoods.size();
        for (int j=0; j < size - 1; j++) {
            long long newg = mgoods[j] + mpowers[i];
            if (newg >= MXN) {
                out = true;
                break;
            }
            mgoods.push_back(newg);
        }
    }
}

int main() {
    mprecomp();

    int q;
    cin >> q;
    while (q--) {
        long long n;
        cin >> n;
        auto it = lower_bound(mgoods.begin(), mgoods.end(), n);
        cout << *it << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int K = 10;
const int MXN = 2e4;

vector<long long> goods;
long long powers[K];

void cpowers() {
    powers[0] = 1;
    for (int i=1; i<=K; i++)
        powers[i] = powers[i - 1] * 3;
}

void precomp() {
    cpowers();
    for (int i=0; i<=K; i++) {
        goods.push_back(powers[i]);
        int size = (int)goods.size();
        for (int j=0; j < size - 1; j++) {
            long long newg = goods[j] + powers[i];
            if (newg >= MXN) break;
            goods.push_back(newg);
        }
    }
}

int main() {
    precomp();

    int q;
    cin >> q;
    while (q--) {
        long long n;
        cin >> n;
        auto it = lower_bound(goods.begin(), goods.end(), n);
        cout << *it << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int K = 10;
const int MXN = 2e4;

vector<long long> mgoods;
long long mpowers[K];

void mcpowers() {
    mpowers[0] = 1;
    for (int i=1; i<=K; i++)
        mpowers[i] = mpowers[i - 1] * 3;
}

void mprecomp() {
    mcpowers();
    bool out = false;
    for (int i=0; i<=K && !out; i++) {
        mgoods.push_back(mpowers[i]);
        int size = (int)mgoods.size();
        for (int j=0; j < size - 1; j++) {
            long long newg = mgoods[j] + mpowers[i];
            if (newg >= MXN) {
                out = true;
                break;
            }
            mgoods.push_back(newg);
        }
    }
}

int main() {
    mprecomp();

    int q;
    cin >> q;
    while (q--) {
        long long n;
        cin >> n;
        auto it = lower_bound(mgoods.begin(), mgoods.end(), n);
        cout << *it << endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 100005;
ll a[105],pow3[105];
int main(){
    pow3[0]=1;
    for(int i=1;i<15;i++) pow3[i]=pow3[i-1]*3ll;
    //for(int i=0;i<15;i++) printf("i=%d,%lld\n",i,pow3[i]);
    int q;
    scanf("%d",&q);
    while(q--){
        ll n,t,x=1;int cnt=0;
        scanf("%I64d",&n);
        t=n;
        while(t){
            a[cnt++]=t%3ll;
            t/=3ll;
        }
        ll ans;int ov=0;
        //for(int i=0;i<cnt;i++) printf("%d ",a[i]);
        //printf("pow=%d\n",pow3[8]);
        /*for(int i=0;i<cnt;i++){
            if(a[i]==2){
                ans+=pow3[i];
                ov=1;
            }
            else {
                a[i]+=ov;
                if(a[i]>=2) {
                    ov=1;
                    ans+=pow3[i];
                }
                else ov=0;
            }
        }*/
        int f=0;
        for(int i=cnt-1;i>0;i--) {
            if(a[i]>0&&a[i-1]>0){
                if(a[i]==2||a[i-1]==2){
                    f=1;
                    break;
                }
            }
            else {
                if(a[i]==2)
                    f=1;
                break;
            }
        }
        //printf("f=%d\n",f);
        if(f==1) printf("%I64d\n",pow3[cnt]);
        else {
            ans=0;int num=0,c[105]={0};
            for(int i=0;i<cnt;i++) {
                if(a[i]==0) c[num++]=i;
            }
            //for(int i=0;i<num;i++) printf("c[%d]=%d\n",i,c[i]);
            for(int k=0;k<num;k++){
                for(int i=(k==0)?0:c[k-1]+1;i<c[k];i++){
                    if(a[i]>1) {
                        ans+=pow3[c[k]];
                        break;
                    }
                }
            }
            int flag=0;
            for(int i=c[num-1]+1;i<cnt;i++){
                if(a[i]>1) {
                    flag=1;
                    break;
                }
            }
            if(flag==1) ans+=pow3[cnt];
            else {
                for(int i=c[num-1]+1;i<cnt;i++){
                    ans+=pow3[i];
                }
            }
            if(num==0&&a[0]==1) ans=n;
            if(num==0&&a[0]==2) ans=3;
            printf("%I64d\n",ans);
        }
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 100005;
ll a[105],pow3[105];
int main(){
    pow3[0]=1;
    for(int i=1;i<15;i++) pow3[i]=pow3[i-1]*3ll;
    //for(int i=0;i<15;i++) printf("i=%d,%lld\n",i,pow3[i]);
    int q;
    scanf("%d",&q);
    while(q--){
        ll n,t,x=1;int cnt=0;
        scanf("%I64d",&n);
        t=n;
        while(t){
            a[cnt++]=t%3ll;
            t/=3ll;
        }
        ll ans;int ov=0;
        //for(int i=0;i<cnt;i++) printf("%d ",a[i]);
        //printf("pow=%d\n",pow3[8]);
        /*for(int i=0;i<cnt;i++){
            if(a[i]==2){
                ans+=pow3[i];
                ov=1;
            }
            else {
                a[i]+=ov;
                if(a[i]>=2) {
                    ov=1;
                    ans+=pow3[i];
                }
                else ov=0;
            }
        }*/
        int f=0;
        for(int i=cnt-1;i>0;i--) {
            if(a[i]>0&&a[i-1]>0){
                if(a[i]==2||a[i-1]==2){
                    f=1;
                    break;
                }
            }
            else {
                if(a[i]==2)
                    f=1;
                break;
            }
        }
        //printf("f=%d\n",f);
        if(f==1) printf("%I64d\n",pow3[cnt]);
        else {
            ans=0;int num=0,c[105]={0};
            for(int i=0;i<cnt;i++) {
                if(a[i]==0) c[num++]=i;
            }
            int flag=0;
            //for(int i=0;i<num;i++) printf("c[%d]=%d\n",i,c[i]);
            for(int k=0;k<num;k++){
                flag=0;
                for(int i=(k==0)?0:c[k-1]+1;i<c[k];i++){
                    if(a[i]>1) {
                        flag=1;
                        break;
                    }
                }
                if(flag==1) ans+=pow3[c[k]];
                else {
                    for(int i=(k==0)?0:c[k-1]+1;i<c[k];i++){
                        ans+=pow3[i];
                    }
                }
            }
            flag=0;
            for(int i=c[num-1]+1;i<cnt;i++){
                if(a[i]>1) {
                    flag=1;
                    break;
                }
            }
            if(flag==1) ans+=pow3[cnt];
            else {
                for(int i=c[num-1]+1;i<cnt;i++){
                    ans+=pow3[i];
                }
            }
            if(num==0&&a[0]==1) ans=n;
            if(num==0&&a[0]==2) ans=3;
            printf("%I64d\n",min(ans,pow3[cnt]));
        }
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 100005;
ll a[105],pow3[105];
int main(){
    pow3[0]=1;
    for(int i=1;i<15;i++) pow3[i]=pow3[i-1]*3ll;
    //for(int i=0;i<15;i++) printf("i=%d,%lld\n",i,pow3[i]);
    int q;
    scanf("%d",&q);
    while(q--){
        ll n,t,x=1;int cnt=0;
        scanf("%I64d",&n);
        t=n;
        while(t){
            a[cnt++]=t%3ll;
            t/=3ll;
        }
        ll ans;int ov=0;
        //for(int i=0;i<cnt;i++) printf("%d ",a[i]);
        //printf("pow=%d\n",pow3[8]);
        /*for(int i=0;i<cnt;i++){
            if(a[i]==2){
                ans+=pow3[i];
                ov=1;
            }
            else {
                a[i]+=ov;
                if(a[i]>=2) {
                    ov=1;
                    ans+=pow3[i];
                }
                else ov=0;
            }
        }*/
        int f=0;
        for(int i=cnt-1;i>0;i--) {
            if(a[i]>0&&a[i-1]>0){
                if(a[i]==2||a[i-1]==2){
                    f=1;
                    break;
                }
            }
            else {
                if(a[i]==2)
                    f=1;
                break;
            }
        }
        //printf("f=%d\n",f);
        if(f==1) printf("%I64d\n",pow3[cnt]);
        else {
            ans=0;int num=0,c[105]={0};
            for(int i=0;i<cnt;i++) {
                if(a[i]==0) c[num++]=i;
            }
            int flag=0;
            //for(int i=0;i<num;i++) printf("c[%d]=%d\n",i,c[i]);
            for(int k=num-1;k>=0;k--){
                flag=0;
                for(int i=(k==0)?0:c[k-1]+1;i<c[k];i++){
                    if(a[i]>1) {
                        flag=1;
                        break;
                    }
                }
                if(flag==1) {
                    ans+=pow3[c[k]];
                    break;
                }
                else {
                    for(int i=(k==0)?0:c[k-1]+1;i<c[k];i++){
                        ans+=pow3[i];
                    }
                }
            }
            flag=0;
            for(int i=c[num-1]+1;i<cnt;i++){
                if(a[i]>1) {
                    flag=1;
                    break;
                }
            }
            if(flag==1) ans+=pow3[cnt];
            else {
                for(int i=c[num-1]+1;i<cnt;i++){
                    ans+=pow3[i];
                }
            }
            if(num==0&&a[0]==1) ans=n;
            if(num==0&&a[0]==2) ans=3;
            printf("%I64d\n",min(ans,pow3[cnt]));
        }
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 100005;
ll a[105],pow3[105];
int main(){
    pow3[0]=1;
    for(int i=1;i<15;i++) pow3[i]=pow3[i-1]*3ll;
    //for(int i=0;i<15;i++) printf("i=%d,%lld\n",i,pow3[i]);
    int q;
    scanf("%d",&q);
    while(q--){
        ll n,t,x=1;int cnt=0;
        scanf("%I64d",&n);
        t=n;
        while(t){
            a[cnt++]=t%3ll;
            t/=3ll;
        }
        ll ans=n;int ov=0;
        //for(int i=0;i<cnt;i++) printf("%d ",a[i]);
        //printf("pow=%d\n",pow3[8]);
        /*for(int i=0;i<cnt;i++){
            if(a[i]==2){
                ans+=pow3[i];
                ov=1;
            }
            else {
                a[i]+=ov;
                if(a[i]>=2) {
                    ov=1;
                    ans+=pow3[i];
                }
                else ov=0;
            }
        }*/
        int f=0;
        for(int i=cnt-1;i>0;i--) {
            if(a[i]>0&&a[i-1]>0){
                if(a[i]==2||a[i-1]==2){
                    f=1;
                    break;
                }
            }
            else {
                if(a[i]==2)
                    f=1;
                break;
            }
        }
        if(f==1) printf("%I64d\n",pow3[cnt]);
        else {
            for(int i=0;i<cnt;i++){
                if(a[i]==2){
                    ans+=pow3[i];
                    ov=1;
                }
                else {
                    a[i]+=ov;
                    if(a[i]>=2) {
                        ov=1;
                        ans+=pow3[i];
                    }
                    else ov=0;
                }
            }
            printf("%I64d\n",ans);
        }
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 100005;
ll a[105],pow3[105];
int main(){
    pow3[0]=1;
    for(int i=1;i<15;i++) pow3[i]=pow3[i-1]*3ll;
    //for(int i=0;i<15;i++) printf("i=%d,%lld\n",i,pow3[i]);
    int q;
    scanf("%d",&q);
    while(q--){
        ll n,t,x=1;int cnt=0;
        scanf("%I64d",&n);
        t=n;
        while(t){
            a[cnt++]=t%3ll;
            t/=3ll;
        }
        ll ans;int ov=0;
        //for(int i=0;i<cnt;i++) printf("%d ",a[i]);
        //printf("pow=%d\n",pow3[8]);
        /*for(int i=0;i<cnt;i++){
            if(a[i]==2){
                ans+=pow3[i];
                ov=1;
            }
            else {
                a[i]+=ov;
                if(a[i]>=2) {
                    ov=1;
                    ans+=pow3[i];
                }
                else ov=0;
            }
        }*/
        int f=0;
        for(int i=cnt-1;i>0;i--) {
            if(a[i]>0&&a[i-1]>0){
                if(a[i]==2||a[i-1]==2){
                    f=1;
                    break;
                }
            }
            else {
                if(a[i]==2)
                    f=1;
                break;
            }
        }
        //printf("f=%d\n",f);
        if(f==1) printf("%I64d\n",pow3[cnt]);
        else {
            ans=0;int num=0,c[105]={0};
            for(int i=0;i<cnt;i++) {
                if(a[i]==0) c[num++]=i;
            }
            int flag=0;
            //for(int i=0;i<num;i++) printf("c[%d]=%d\n",i,c[i]);
            for(int k=num-1;k>=0;k--){
                flag=0;
                for(int i=(k==0)?0:c[k-1]+1;i<c[k];i++){
                    if(a[i]>1) {
                        flag=1;
                        break;
                    }
                }
                if(flag==1) ans+=pow3[c[k]];
                else {
                    for(int i=(k==0)?0:c[k-1]+1;i<c[k];i++){
                        ans+=pow3[i];
                    }
                }
            }
            flag=0;
            for(int i=c[num-1]+1;i<cnt;i++){
                if(a[i]>1) {
                    flag=1;
                    break;
                }
            }
            if(flag==1) ans+=pow3[cnt];
            else {
                for(int i=c[num-1]+1;i<cnt;i++){
                    ans+=pow3[i];
                }
            }
            if(num==0&&a[0]==1) ans=n;
            if(num==0&&a[0]==2) ans=3;
            printf("%I64d\n",min(ans,pow3[cnt]));
        }
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 100005;
ll a[105],pow3[105];
int main(){
    pow3[0]=1;
    for(int i=1;i<15;i++) pow3[i]=pow3[i-1]*3ll;
    //for(int i=0;i<15;i++) printf("i=%d,%lld\n",i,pow3[i]);
    int q;
    scanf("%d",&q);
    while(q--){
        ll n,t,x=1;int cnt=0;
        scanf("%I64d",&n);
        t=n;
        while(t){
            a[cnt++]=t%3ll;
            t/=3ll;
        }
        ll ans;int ov=0;
        //for(int i=0;i<cnt;i++) printf("%d ",a[i]);
        //printf("pow=%d\n",pow3[8]);
        /*for(int i=0;i<cnt;i++){
            if(a[i]==2){
                ans+=pow3[i];
                ov=1;
            }
            else {
                a[i]+=ov;
                if(a[i]>=2) {
                    ov=1;
                    ans+=pow3[i];
                }
                else ov=0;
            }
        }*/
        int f=0;
        for(int i=cnt-1;i>0;i--) {
            if(a[i]>0&&a[i-1]>0){
                if(a[i]==2||a[i-1]==2){
                    f=1;
                    break;
                }
            }
            else {
                if(a[i]==2)
                    f=1;
                break;
            }
        }
        //printf("f=%d\n",f);
        if(f==1) printf("%I64d\n",pow3[cnt]);
        else {
            ans=0;int num=0,c[105]={0};
            for(int i=0;i<cnt;i++) {
                if(a[i]==0) c[num++]=i;
            }
            int flag=0;
            //for(int i=0;i<num;i++) printf("c[%d]=%d\n",i,c[i]);
            for(int k=0;k<num;k++){
                flag=0;
                for(int i=(k==0)?0:c[k-1]+1;i<c[k];i++){
                    if(a[i]>1) {
                        flag=1;
                        break;
                    }
                }
                if(flag==1) ans+=pow3[c[k]];
                else {
                    for(int i=(k==0)?0:c[k-1]+1;i<c[k];i++){
                        ans+=pow3[i];
                    }
                }
            }
            flag=0;
            for(int i=c[num-1]+1;i<cnt;i++){
                if(a[i]>1) {
                    flag=1;
                    break;
                }
            }
            if(flag==1) ans+=pow3[cnt];
            else {
                for(int i=c[num-1]+1;i<cnt;i++){
                    ans+=pow3[i];
                }
            }
            if(num==0&&a[0]==1) ans=n;
            if(num==0&&a[0]==2) ans=3;
            printf("%I64d\n",ans);
        }
    }
    return 0;
}


/// Bismillahir Rahmanir Rahim

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define f0(n) for(int i=0;i<n;i++)
#define ms(x) memset(x,0,sizeof(x))
#define ins insert
#define IOS ios::sync_with_stdio(false);
using namespace std;
const long long MOD = 1000000007;
template<typename T>inline T Bigmod(T base, T power, T MOD){
    T ret=1;
    while(power)
    {
        if(power & 1)ret=(ret*base)%MOD;
        base=(base*base)%MOD;
        power>>=1;
    }
    return ret;
}

bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}
ll log3(ll n)
{
    ll cnt = 0;
    while(n>2)
    {
        cnt++;
        n/=3;
    }
    return cnt;
}
ll po(ll base,ll p)
{
    ll res = 1;
    while(p>0)
    {
        res*=base;
        p--;
    }
    return res;
}
int main()
{
	IOS
    int q;
    cin>>q;
    while(q--)
    {
        ll n;
        cin>>n;
        vector<int>vec;
        ll lg = log3(n);
        ll num = po(3,lg);
        if(num==n) {cout << n <<endl;continue;}
        else if(num + (num/2) <n) {cout <<po(3,lg+1) <<endl;continue;}
        else{
        bool got = false;
        for(int i=0;i<lg;i++)
        {
            ll dd = po(3,i);
            ll temp = num;
            temp += dd;
            vec.pb(temp);
            for(int j=0;j<lg;j++)
            {
                if(i==j)continue;
                ll xx = po(3,j);
                temp+=xx;
                vec.pb(temp);
            }
            }
        sort(vec.begin(),vec.end());
        for(int i=0;i<vec.size();i++)
        {
            if(vec[i]>=n)
            {
                cout << vec[i] << endl;
                got = true;
                break;
            }
        }
        if(!got)  cout <<po(3,lg+1)<<endl;continue;
        }
    }
    return 0;
}


/// Bismillahir Rahmanir Rahim

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define f0(n) for(int i=0;i<n;i++)
#define ms(x) memset(x,0,sizeof(x))
#define ins insert
#define IOS ios::sync_with_stdio(false);
using namespace std;
const long long MOD = 1000000007;
template<typename T>inline T Bigmod(T base, T power, T MOD){
    T ret=1;
    while(power)
    {
        if(power & 1)ret=(ret*base)%MOD;
        base=(base*base)%MOD;
        power>>=1;
    }
    return ret;
}

bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}
ll log3(ll n)
{
    ll cnt = 0;
    while(n>2)
    {
        cnt++;
        n/=3;
    }
    return cnt;
}
ll po(ll base,ll p)
{
    ll res = 1;
    while(p>0)
    {
        res*=base;
        p--;
    }
    return res;
}
int main()
{
	IOS
    int q; vector<int>vec;
    for(int i=0;i<=38;i++)
        {
            ll dd = po(3,i);
            vec.pb(dd);
            for(int j=0;j<=38;j++)
            {
                if(i==j)continue;
                ll xx = po(3,j);
                dd+=xx;
                vec.pb(dd);
            }
        }
    cin>>q;
    sort(vec.begin(),vec.end());
    while(q--)
    {
        ll n;
        cin>>n;

        ll lg = log3(n);
        ll num = po(3,lg);
        if(num==n){cout << n <<endl;continue;}
        else{

        for(int i=0;i<vec.size();i++)
        {
            if(vec[i]>=n)
            {
                cout << vec[i] << endl;
                break;
            }
        }
        }
    }
    return 0;
}


/// Bismillahir Rahmanir Rahim

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define f0(n) for(int i=0;i<n;i++)
#define ms(x) memset(x,0,sizeof(x))
#define ins insert
#define IOS ios::sync_with_stdio(false);
using namespace std;
const long long MOD = 1000000007;
template<typename T>inline T Bigmod(T base, T power, T MOD){
    T ret=1;
    while(power)
    {
        if(power & 1)ret=(ret*base)%MOD;
        base=(base*base)%MOD;
        power>>=1;
    }
    return ret;
}

bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}
ll log3(ll n)
{
    ll cnt = 0;
    while(n>2)
    {
        cnt++;
        n/=3;
    }
    return cnt;
}
ll po(ll base,ll p)
{
    ll res = 1;
    while(p>0)
    {
        res*=base;
        p--;
    }
    return res;
}
int main()
{
	IOS
    int q; vector<ll>vec;
    for(int i=0;i<=38;i++)
        {
            ll dd = po(3,i);
            vec.pb(dd);
            for(int j=0;j<=38;j++)
            {
                if(i==j)continue;
                ll xx = po(3,j);
                dd+=xx;
                vec.pb(dd);
            }
        }
    cin>>q;
    sort(vec.begin(),vec.end());
    while(q--)
    {
        ll n;
        cin>>n;
        for(int i=0;i<vec.size();i++)
        {
            if(vec[i]>=n)
            {
                cout << vec[i] << endl;
                break;
            }
        }
    }
    return 0;
}


/// Bismillahir Rahmanir Rahim

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define f0(n) for(int i=0;i<n;i++)
#define ms(x) memset(x,0,sizeof(x))
#define ins insert
#define IOS ios::sync_with_stdio(false);
using namespace std;
const long long MOD = 1000000007;
template<typename T>inline T Bigmod(T base, T power, T MOD){
    T ret=1;
    while(power)
    {
        if(power & 1)ret=(ret*base)%MOD;
        base=(base*base)%MOD;
        power>>=1;
    }
    return ret;
}

bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}
ll log3(ll n)
{
    ll cnt = 0;
    while(n>2)
    {
        cnt++;
        n/=3;
    }
    return cnt;
}
ll po(ll base,ll p)
{
    ll res = 1;
    while(p>0)
    {
        res*=base;
        p--;
    }
    return res;
}
int main()
{
	IOS
    int q;
    cin>>q;
    while(q--)
    {
        ll n;
        cin>>n;
        ll lg = log3(n);
        ll num = po(3,lg);
        if(num==n) {cout << n <<endl;continue;}
        else if(num + (num-1) <n) {cout <<po(3,lg+1) <<endl;continue;}
        else{
        bool got = false;
        for(int i=0;i<lg;i++)
        {
            ll dd = po(3,i);
            num+=dd;
            if(num>=n)
            {
                cout << num <<endl;
                got = true;
                break;
            }

        }
        if(!got)  cout <<po(3,lg+1)<<endl;continue;
        }
    }
    return 0;
}


/// Bismillahir Rahmanir Rahim

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define f0(n) for(int i=0;i<n;i++)
#define ms(x) memset(x,0,sizeof(x))
#define ins insert
#define ALL(v) v.begin(),v.end()
#define highest(x) numeric_limits<x>::max()
#define lowest(x) numeric_limits<x>::min()
#define Inf INFINITY
#define minv(v) *min_element(v.begin(),v.end())
#define maxv(v) *max_element(v.begin(),v.end())
#define PI acos(-1)
#define IOS ios::sync_with_stdio(false);
using namespace std;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
const long long MOD = 1000000007;
template<typename T>inline T Bigmod(T base, T power, T MOD){
    T ret=1;
    while(power)
    {
        if(power & 1)ret=(ret*base)%MOD;
        base=(base*base)%MOD;
        power>>=1;
    }
    return ret;
}

bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}
int main()
{
    IOS
    int q;
    cin>>q;
    while(q--){
        int n;
        cin>>n;
        while(true){
            bool ok=1;
            int temp=n;
            while(temp>0){
                if(ok && temp%3==2){ok=0;}
                temp/=3;
            }
            if(ok)break;
            ++n;
        }
        cout << n << endl;
    }
    return 0;
}
///Alhamdulillah


/// Bismillahir Rahmanir Rahim

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define f0(n) for(int i=0;i<n;i++)
#define ms(x) memset(x,0,sizeof(x))
#define ins insert
#define IOS ios::sync_with_stdio(false);
using namespace std;
const long long MOD = 1000000007;
template<typename T>inline T Bigmod(T base, T power, T MOD){
    T ret=1;
    while(power)
    {
        if(power & 1)ret=(ret*base)%MOD;
        base=(base*base)%MOD;
        power>>=1;
    }
    return ret;
}

bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}
ll log3(ll n)
{
    ll cnt = 0;
    while(n>2)
    {
        cnt++;
        n/=3;
    }
    return cnt;
}
ll po(ll base,ll p)
{
    ll res = 1;
    while(p>0)
    {
        res*=base;
        p--;
    }
    return res;
}
int main()
{
	IOS
    int q; vector<int>vec;
    for(int i=0;i<=38;i++)
        {
            ll dd = po(3,i);
            vec.pb(dd);
            for(int j=0;j<=38;j++)
            {
                if(i==j)continue;
                ll xx = po(3,j);
                dd+=xx;
                vec.pb(dd);
            }
        }
    cin>>q;
    sort(vec.begin(),vec.end());
    while(q--)
    {
        ll n;
        cin>>n;
        for(int i=0;i<vec.size();i++)
        {
            if(vec[i]>=n)
            {
                cout << vec[i] << endl;
                break;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int q;
    cin>>q;
    while(q--){
        int n;
        cin>>n;
        int flag = 0;
        vector<int> v;
        int p = n;
        int cnt = 0;
        while(n){
            cnt++;
            int tmp = n  % 3;
            v.push_back(tmp);
            if(tmp == 2) flag = cnt;
            n /= 3;
        }
        if(!flag) printf("%d\n",p);
        else{
            int now = 1;
            for(int i = 0;i < cnt;++i){
                now = now * 3;
            }
            for(int i = cnt;i < v.size();++i){
                if(v[i] == 1) now = now * 3 + 1;
                else now = now * 3;
            }
            printf("%d\n",now);
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

long long qpow(long long a,long long b)
{
    long long res = 1;
    while(b){
        if(b & 1) res = res * a;
        a *= a;
        b >>= 1;
    }
    return res;
}

int main()
{
    int q;
    cin>>q;
    while(q--){
        long long n;
        cin>>n;
        int flag = 0;
        vector<int> v;
        int p = n;
        int cnt = 0;
        while(n){
            cnt++;
            int tmp = n  % 3;
            v.push_back(tmp);
            if(tmp == 2) flag = cnt;
            n /= 3;
        }
        if(!flag) printf("%lld\n",p);
        else{
            long long now = qpow(3ll,1ll * flag);
            while(v[flag] == 1) now = now * 3,flag++;
            for(int i = flag;i < v.size();++i){
                if(v[i] == 1) now = now + qpow(3,i);
            }
            printf("%lld\n",now);
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int q;
    cin>>q;
    while(q--){
        int n;
        cin>>n;
        int flag = 0;
        vector<int> v;
        int p = n;
        int cnt = 0;
        while(n){
            cnt++;
            int tmp = n  % 3;
            v.push_back(tmp);
            if(tmp == 2) flag = cnt;
            n /= 3;
        }
        if(!flag) printf("%d\n",p);
        else{
            int now = 1;
            for(int i = 0;i < cnt;++i){
                now = now * 3;
            }
            while(v[cnt] == 1) now = now * 3,cnt++;
            for(int i = cnt;i < v.size();++i){
                if(v[i] == 1) now = now * 3 + 1;
                else now = now * 3;
            }
            printf("%d\n",now);
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

long long qpow(long long a,long long b)
{
    long long res = 1;
    while(b){
        if(b & 1) res = res * a;
        a *= a;
        b >>= 1;
    }
    return res;
}

int main()
{
    int q;
    cin>>q;
    while(q--){
        long long n;
        cin>>n;
        int flag = 0;
        vector<int> v;
        long long p = n;
        int cnt = 0;
        while(n){
            cnt++;
            int tmp = n  % 3;
            v.push_back(tmp);
            if(tmp == 2) flag = cnt;
            n /= 3;
        }
        if(!flag) printf("%lld\n",p);
        else{
            long long now = qpow(3ll,1ll * flag);
            while(v[flag] == 1) now = now * 3,flag++;
            for(int i = flag;i < v.size();++i){
                if(v[i] == 1) now = now + qpow(3,i);
            }
            printf("%lld\n",now);
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int qpow(int a,int b)
{
    int res = 1;
    while(b){
        if(b & 1) res = res * a;
        a *= a;
        b >>= 1;
    }
    return res;
}

int main()
{
    int q;
    cin>>q;
    while(q--){
        int n;
        cin>>n;
        int flag = 0;
        vector<int> v;
        int p = n;
        int cnt = 0;
        while(n){
            cnt++;
            int tmp = n  % 3;
            v.push_back(tmp);
            if(tmp == 2) flag = cnt;
            n /= 3;
        }
        if(!flag) printf("%d\n",p);
        else{
            int now = 1;
            for(int i = 0;i < flag;++i){
                now = now * 3;
            }
            while(flag == v.size()+1 || v[flag] == 1) now = now * 3,flag++;
            for(int i = flag;i < v.size();++i){
                if(v[i] == 1) now = now + qpow(3,i);
            }
            printf("%d\n",now);
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

long long qpow(long long a,long long b)
{
    long long res = 1;
    while(b){
        if(b & 1) res = res * a;
        a *= a;
        b >>= 1;
    }
    return res;
}

int main()
{
    int q;
    cin>>q;
    while(q--){
        long long n;
        cin>>n;
        int flag = 0;
        vector<int> v;
        long long p = n;
        int cnt = 0;
        while(n){
            cnt++;
            int tmp = n  % 3;
            v.push_back(tmp);
            if(tmp == 2) flag = cnt;
            n /= 3;
        }
        if(!flag) printf("%lld\n",p);
        else{
            long long res = 0;
            int t = 0;
            for(int i = 0;i < flag;++i) v[i] = 0;
            for(int i = flag;i < v.size();++i){
                if(v[i] == 1)v[i] = 0;
                else if(v[i] == 0){ v[i] = 1,t = 1;break;}
            }
            if(!t) res = qpow(3ll,(long long)v.size());
            else {
                for(int i = 0;i < v.size();++i){
                    if(v[i]) res = res + qpow(3ll,i);
                }
            }
            printf("%lld\n",res);

        }
    }

    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        ll n,p,z,i,m,l,k=0,x;
        cin>>x; p=x;
        n=log10(x)/log10(3);
        if(pow(3,n)==p)
          {cout<<p<<endl; continue;}
        if(p%2)
            i=0;
        else i=1;
        for(z=0; i<=n; i++)
            {
                z+=pow(3,i);
            }
         if(z==p)
           {cout<<z<<endl;}
         else
         {
             z=pow(3,i);
             m=pow(3,n);
             for(i=0; i<n; i++)
             {
                 m+=pow(3,i);
             }
             if(m<x)
             {cout<<z<<endl; continue;}
             m = pow(3,n);

                 for(i=n-1; i>=0; i--)
                 {
                     ll w = pow(3,i);
                       if(x-m>=w)
                           m+=w;
                     if(m>=x)
                        break;
                 }
             if(m<=z)
                cout<<m<<endl;
             else cout<<z<<endl;
         }
    }
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        ll n,p,z,i,m,l,k=0,x;
        cin>>x; p=x;
        n=log10(x)/log10(3);
        if(pow(3,n)==p)
          {cout<<p<<endl; continue;}
        if(p%2)
            i=0;
        else i=1;
        for(z=0; i<=n; i++)
            {
                z+=pow(3,i);
            }
         if(z==p)
           {cout<<z<<endl;}
         else
         {
             z=pow(3,i);
             m=pow(3,n);
             for(i=0; i<n; i++)
             {
                 m+=pow(3,i);
             }
             if(m<x)
             {cout<<z<<endl; continue;}
             m = pow(3,n);

             if(x%2) x--;
                 for(i=n-1; i>=0; i--)
                 {
                     ll w = pow(3,i);
                       if(x-m>=w)
                           m+=w;
                     if(m>=x)
                        break;
                 }
             if(m<=z)
                cout<<m<<endl;
             else cout<<z<<endl;
         }
    }
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        ll n,p,z,i,m,l,k=0,x;
        cin>>x; p=x;
        n=log10(x)/log10(3);
        if(pow(3,n)==p)
          {cout<<p<<endl; continue;}
        if(p%2)
            i=0;
        else i=1;
        for(z=0; i<=n; i++)
            {
                z+=pow(3,i);
            }
         if(z==p)
           {cout<<z<<endl;}
         else
         {
             set<int>s;
             int a[n+1];
             fill(a,a+n+1,1);
             z=pow(3,i);
             m=pow(3,n);
             for(i=0; i<n; i++)
             {
                 m+=pow(3,i);
             }
             if(m<x)
             {cout<<z<<endl; continue;}
             m = pow(3,n); p=p-m; l=n;
             while(n>0 && p!=0)
             {
                 n=log10(p)/log10(3);
                 s.insert(n);
                 k+=pow(3,n);
                 p-=pow(3,n);
             }
             p=m+k;
             if(p<=x)
             {
                 for(auto it=s.begin(); it!=s.end(); it++)
                 {
                     a[*it]=0;
                 }
                 for(i=0; i<l; i++)
                 {
                     if(a[i])
                       {p+=pow(3,i);}
                     if(p==x)
                        break;
                 }
             }
             s.clear();
             if(p==x)
                cout<<p<<endl;
             else cout<<z<<endl;
         }
    }
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        ll n,p,z,i,m,l,k=0,x;
        cin>>x; p=x;
        n=log10(x)/log10(3);
        if(pow(3,n)==p)
          {cout<<p<<endl; continue;}
        if(p%2)
            i=0;
        else i=1;
        for(z=0; i<=n; i++)
            {
                z+=pow(3,i);
            }
         if(z==p)
           {cout<<z<<endl;}
         else
         {
             set<int>s;
             int a[n+1];
             fill(a,a+n+1,1);
             z=pow(3,i);
             m=pow(3,n); p=p-m; l=n;
             while(n>0 && p!=0)
             {
                 n=log10(p)/log10(3);
                 s.insert(n);
                 k+=pow(3,n);
                 p-=pow(3,n);
             }
             p=m+k;
             if(p<=x)
             {
                 for(auto it=s.begin(); it!=s.end(); it++)
                 {
                     a[*it]=0;
                 }
                 for(i=0; i<l; i++)
                 {
                     if(a[i])
                       {p+=pow(3,i);}
                     if(p==x)
                        break;
                 }
             }
             s.clear();
             if(p==x)
                cout<<p<<endl;
             else cout<<z<<endl;
         }
    }
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        ll n,p,z,i,m,l,k=0,x;
        cin>>x; p=x;
        n=log10(x)/log10(3);
        if(pow(3,n)==p)
          {cout<<p<<endl; continue;}
        if(p%2)
            i=0;
        else i=1;
        for(z=0; i<=n; i++)
            {
                z+=pow(3,i);
            }
         if(z==p)
           {cout<<z<<endl;}
         else
         {
             set<int>s;
             int a[n+1];
             fill(a,a+n+1,1);
             z=pow(3,i);
             m=pow(3,n);
             for(i=0; i<n; i++)
             {
                 m+=pow(3,i);
             }
             if(m<x)
             {cout<<z<<endl; continue;}
             m = pow(3,n); p=p-m; l=n;
             while(p!=0)
             {
                 n=log10(p)/log10(3);
                 //cout<<n<<"   "<<p<<endl;
                 ll g=pow(3,n);
                 s.insert(n);
                 k+=g;
                 p=p-g;
             }
             p=m+k;
             if(p<=x)
             {
                 for(auto it=s.begin(); it!=s.end(); it++)
                 {
                     a[*it]=0;
                 }
                 ll d=x-p;
                 if(d%2==0) d--;
                 for(i=0; i<l; i++)
                 {
                     ll w = pow(3,i);
                     if(a[i] && w==d)
                       {p+=d;}
                     if(p==x)
                        break;
                 }
             }
             s.clear();
             if(p==x)
                cout<<p<<endl;
             else cout<<z<<endl;
         }
    }
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        ll n,p,z,i,m,l,k=0,x;
        cin>>x; p=x;
        n=log10(x)/log10(3);
        if(pow(3,n)==p)
          {cout<<p<<endl; continue;}
        if(p%2)
            i=0;
        else i=1;
        for(z=0; i<=n; i++)
            {
                z+=pow(3,i);
            }
         if(z==p)
           {cout<<z<<endl;}
         else
         {
             z=pow(3,i);
             m=pow(3,n);
             for(i=0; i<n; i++)
             {
                 m+=pow(3,i);
             }
             if(m<x)
             {cout<<z<<endl; continue;}
             m = pow(3,n);

             if(x%2) x--;
                 for(i=n-1; i>=0; i--)
                 {
                     ll w = pow(3,i);
                       m+=w;
                     if(m>=x)
                        break;
                 }
             if(m<=z)
                cout<<m<<endl;
             else cout<<z<<endl;
         }
    }
    return 0;
}


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


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        ll n,p,z,i,m,l,k=0,x;
        cin>>x; p=x;
        n=log10(x)/log10(3);
        if(pow(3,n)==p)
          {cout<<p<<endl; continue;}
        if(p%2)
            i=0;
        else i=1;
        for(z=0; i<=n; i++)
            {
                z+=pow(3,i);
            }
         if(z==p)
           {cout<<z<<endl;}
         else
         {
             set<int>s;
             int a[n+1];
             fill(a,a+n+1,1);
             z=pow(3,i);
             m=pow(3,n);
             for(i=0; i<n; i++)
             {
                 m+=pow(3,i);
             }
             if(m<z)
             {cout<<z<<endl; continue;}
             m = pow(3,n); p=p-m; l=n;
             while(n>0 && p!=0)
             {
                 n=log10(p)/log10(3);
                 s.insert(n);
                 k+=pow(3,n);
                 p-=pow(3,n);
             }
             p=m+k;
             cout<<p<<endl;
             if(p<=x)
             {
                 for(auto it=s.begin(); it!=s.end(); it++)
                 {
                     a[*it]=0;
                 }
                 for(i=0; i<l; i++)
                 {
                     cout<<i<<" ";
                     if(a[i])
                       {p+=pow(3,i);}
                     if(p==x)
                        break;
                 }
             }
             s.clear();
             if(p==x)
                cout<<p<<endl;
             else cout<<z<<endl;
         }
    }
    return 0;
}


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


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        ll n,p,z,i,m,l,k=0,x;
        cin>>x; p=x;
        n=log10(x)/log10(3);
        if(pow(3,n)==p)
          {cout<<p<<endl; continue;}
        if(p%2)
            i=0;
        else i=1;
        for(z=0; i<=n; i++)
            {
                z+=pow(3,i);
            }
         if(z==p)
           {cout<<z<<endl;}
         else
         {
             z=pow(3,i);
             m=pow(3,n);
             for(i=0; i<n; i++)
             {
                 m+=pow(3,i);
             }
             if(m<x)
             {cout<<z<<endl; continue;}
             m = pow(3,n);

                 for(i=n-1; i>=0; i--)
                 {
                     ll w = pow(3,i);
                       if(x-m>=w)
                           m+=w;
                     if(m>=x)
                        break;
                 }
                 if(m<x)
                 {
                    m = pow(3,n);

                   for(i=n-1; i>=0; i--)
                     {
                        ll w = pow(3,i);
                           m+=w;
                     if(m>=x)
                        break;
                     }
                 }
             if(m<=z)
                cout<<m<<endl;
             else cout<<z<<endl;
         }
    }
    return 0;
}


/*************************************************************************************
                         ---------------------------
                   ------Bismillahir-Rahmanir-Rahim------
                         ---------------------------
            //     //   Name: MD: NAZMUL HASAN              //     //
           //     //    Institute: HSTU                    //     //
          //     //     Dept.: CSE                        //     //
        //     //      Email: nazmulmoon123@gmail.com   //     //
    -----------------------------------------------------------------------
            ---------- I Love Programming as Gaming ----------
**************************************************************************************/
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree<T, null_type, less<T>,
      rb_tree_tag, tree_order_statistics_node_update>;
#define loop(a,b,c)   for(int a=b;a<c;a++)
#define loopa(p,v)    for(auto p:(v))
#define lloop(a,b,c)  for(long long a=b;a<c;a++)
#define rloop(a,b,c)  for(int a=c-1;a>=b;a--)
#define rlloop(a,b,c) for(long long a=c-1;a>=b;a--)
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define EPS 1e-9
#define PI acos(-1)
#define rn PI/180
#define TC(t)         scanf("%d",&t);while(t--)
#define sf scanf
#define pf printf
#define SIZE 200001
#define sp ' '
#define nl printf("\n")
#define pb push_back
#define pp pop_back
#define mp make_pair
#define ff first
#define ss second
#define gcd(a,b) __gcd(a,b)

#define si(a) sf("%d",&a)
#define sl(a) sf("%I64d",&a)
#define sii(a,b) sf("%d%d",&a,&b)
#define Pi(a) pf("%d\n",a)
#define Pin(a) pf("%d ",a)
#define Pl(a) pf("%I64d\n",a)
#define pln(a) pf("%I64d ",a)
#define b_s(v,x) binary_search(v.begin(),v.end(),x)
#define l_b(v,x) lower_bound(v.begin(),v.end(),x)
#define u_b(v,x) upper_bound(v.begin(),v.end(),x)
#define e_r(v,x) equal_range(v.begin(),v.end(),x)
#define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define case(x) cout<<"Case "<<x<<": "

/// ASCII Vlaue....
/// A=65,Z=90,a=97,z=122,0=48,9=57
///#define INT_MAX 2147483647
///#define INT_MIN -2147483647

const int MOD=1000000007;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int>vi;
typedef vector<ll>vl;
typedef vector<string> vs;
typedef stack<int> si;
typedef stack<string> ss;
typedef queue<int> qi;
typedef queue<string> qs;
typedef pair<int,int> pii;
typedef pair<string,int>psi;
typedef map<int,int>mii;
typedef map<string,int>msi;
///      direction
/*int x4[]= {1,-1,0,0};
int y4[]= {0,0,1,-1};
int x8[]= {0,0,+1,-1,-1,+1,-1,+1};
int y8[]= {-1,+1,0,0,+1,+1,-1,-1};
*/

/*ll nCr(ll aan, ll aar)
{
    ll ii, aac=1;
    for(ii=1; ii<=aar; ii++)
    {
        aac*=(aan-aar+ii);
        aac/=ii;
    }
    return aac;
}

bool status[1100002];
void siv()
{
	int N=1000000;
	int sq=sqrt(N);
	for(int i=4;i<=N;i+=2) status[i]=1;
	for(int i=3;i<=sq;i+=2){
		if(status[i]==0)
		{
			for(int j=i*i;j<=N;j+=i) status[j]=1;
		}
	}
	status[1]=1;
}

ll big_mod(ll base, ll power, ll mod)
{
    if(power==0) return 1;
    if(power&1)
    {
        ll p1=base%mod;
        ll p2=(big_mod(base, power-1,mod))%mod;
        return (p1*p2)%mod;
    }
    else
    {
        ll p1=(big_mod(base,power/2,mod))%mod;
        return (p1*p1)%mod;
    }
}
ll modInverseBigMod(ll a,ll m)  //a*x=1(mod m)
{
    return big_mod(a,m-2,m);
}**/

///START YOUR CODE FROM HERE.... Happy Coding.... ! ;)

/*ll nCr(ll n, ll k)
{
    ll C[n+1][k+1];
    ll i, j;

    // Caculate value of Binomial Coefficient in bottom up manner
    for (i = 0; i <= n; i++)
    {
        for (j = 0; j <= min(i, k); j++)
        {
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;

            // Calculate value using previosly stored values
            else
                C[i][j] = C[i-1][j-1] + C[i-1][j];
        }
    }

    return C[n][k];
}
*/
#define Mx 40
bitset<Mx>bs;
int main()
{
    faster
    ll t,n,x,ans;
    set<ll>s;
    cin>>t;
    n=1000000000000000000;
    x=3;
    ll y=3;
    s.insert(1);
    s.insert(3);
    while(y<n){
        y*=x;
        s.insert(y);
    }
    while(t--)
    {
        bs.reset();
        cin>>n;
        ans=n;
        if(s.find(n)!=s.end()){
            cout<<n<<'\n';
            continue;
        }
        ll z=*s.lower_bound(n);
        auto pos=s.find(z);
        int Size(0),sz;
        for(auto it=s.begin();it!=pos;it++)
            Size++;
        pos--;
        sz=Size;
        Size--;
        for(auto it=pos;it!=s.begin();it--,Size--)
        {
            if(n>=*it)
                n-=*it,bs.set(Size);
            if(n==0)
                break;
        }
        if(n>0)
            n-=1LL,bs.set(0);
        if(n==0)
        {
            cout<<ans<<'\n';
            continue;
        }
       /* for(int i=0;i<=sz;i++)
        {
            cout<<bs[i]<<'!';
        }
        cout<<"\n";
        */
        int i=0;
        bool payeci=false;
        ans=0;
        for(auto j=s.begin();j!=s.end()&&i<sz;j++,i++)
        {
            if(bs[i]==0&&!payeci)
            {
                payeci=true;
                ans+=*j;
            }
            else if(payeci&&bs[i]==1)
            {
                ans+=*j;
                //cout<<*j<<'!';
            }
            //cout<<ans<<'-';

        }
        pos++;
        if(!payeci)
            cout<<*pos<<'\n';
        else
            cout<<ans<<'\n';
    }
    return 0;
}


/*************************************************************************************
                         ---------------------------
                   ------Bismillahir-Rahmanir-Rahim------
                         ---------------------------
            //     //   Name: MD: NAZMUL HASAN              //     //
           //     //    Institute: HSTU                    //     //
          //     //     Dept.: CSE                        //     //
        //     //      Email: nazmulmoon123@gmail.com   //     //
    -----------------------------------------------------------------------
            ---------- I Love Programming as Gaming ----------
**************************************************************************************/
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree<T, null_type, less<T>,
      rb_tree_tag, tree_order_statistics_node_update>;
#define loop(a,b,c)   for(int a=b;a<c;a++)
#define loopa(p,v)    for(auto p:(v))
#define lloop(a,b,c)  for(long long a=b;a<c;a++)
#define rloop(a,b,c)  for(int a=c-1;a>=b;a--)
#define rlloop(a,b,c) for(long long a=c-1;a>=b;a--)
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define EPS 1e-9
#define PI acos(-1)
#define rn PI/180
#define TC(t)         scanf("%d",&t);while(t--)
#define sf scanf
#define pf printf
#define SIZE 200001
#define sp ' '
#define nl printf("\n")
#define pb push_back
#define pp pop_back
#define mp make_pair
#define ff first
#define ss second
#define gcd(a,b) __gcd(a,b)

#define si(a) sf("%d",&a)
#define sl(a) sf("%I64d",&a)
#define sii(a,b) sf("%d%d",&a,&b)
#define Pi(a) pf("%d\n",a)
#define Pin(a) pf("%d ",a)
#define Pl(a) pf("%I64d\n",a)
#define pln(a) pf("%I64d ",a)
#define b_s(v,x) binary_search(v.begin(),v.end(),x)
#define l_b(v,x) lower_bound(v.begin(),v.end(),x)
#define u_b(v,x) upper_bound(v.begin(),v.end(),x)
#define e_r(v,x) equal_range(v.begin(),v.end(),x)
#define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define case(x) cout<<"Case "<<x<<": "

/// ASCII Vlaue....
/// A=65,Z=90,a=97,z=122,0=48,9=57
///#define INT_MAX 2147483647
///#define INT_MIN -2147483647

const int MOD=1000000007;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int>vi;
typedef vector<ll>vl;
typedef vector<string> vs;
typedef stack<int> si;
typedef stack<string> ss;
typedef queue<int> qi;
typedef queue<string> qs;
typedef pair<int,int> pii;
typedef pair<string,int>psi;
typedef map<int,int>mii;
typedef map<string,int>msi;
///      direction
/*int x4[]= {1,-1,0,0};
int y4[]= {0,0,1,-1};
int x8[]= {0,0,+1,-1,-1,+1,-1,+1};
int y8[]= {-1,+1,0,0,+1,+1,-1,-1};
*/

/*ll nCr(ll aan, ll aar)
{
    ll ii, aac=1;
    for(ii=1; ii<=aar; ii++)
    {
        aac*=(aan-aar+ii);
        aac/=ii;
    }
    return aac;
}

bool status[1100002];
void siv()
{
	int N=1000000;
	int sq=sqrt(N);
	for(int i=4;i<=N;i+=2) status[i]=1;
	for(int i=3;i<=sq;i+=2){
		if(status[i]==0)
		{
			for(int j=i*i;j<=N;j+=i) status[j]=1;
		}
	}
	status[1]=1;
}

ll big_mod(ll base, ll power, ll mod)
{
    if(power==0) return 1;
    if(power&1)
    {
        ll p1=base%mod;
        ll p2=(big_mod(base, power-1,mod))%mod;
        return (p1*p2)%mod;
    }
    else
    {
        ll p1=(big_mod(base,power/2,mod))%mod;
        return (p1*p1)%mod;
    }
}
ll modInverseBigMod(ll a,ll m)  //a*x=1(mod m)
{
    return big_mod(a,m-2,m);
}**/

///START YOUR CODE FROM HERE.... Happy Coding.... ! ;)

/*ll nCr(ll n, ll k)
{
    ll C[n+1][k+1];
    ll i, j;

    // Caculate value of Binomial Coefficient in bottom up manner
    for (i = 0; i <= n; i++)
    {
        for (j = 0; j <= min(i, k); j++)
        {
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;

            // Calculate value using previosly stored values
            else
                C[i][j] = C[i-1][j-1] + C[i-1][j];
        }
    }

    return C[n][k];
}
*/
#define Mx 40
bitset<Mx>bs;
int main()
{
    faster
    ll t,n,x,ans;
    set<ll>s;
    cin>>t;
    n=10000;
    x=3;
    ll y=3;
    s.insert(1);
    s.insert(3);
    while(y<n){
        y*=x;
        s.insert(y);
    }
    while(t--)
    {
        bs.reset();
        cin>>n;
        ans=n;
        if(s.find(n)!=s.end()){
            cout<<n<<'\n';
            continue;
        }
        ll z=*s.lower_bound(n);
        auto pos=s.find(z);
        int Size(0),sz;
        for(auto it=s.begin();it!=pos;it++)
            Size++;
        pos--;
        sz=Size;
        Size--;
        for(auto it=pos;it!=s.begin();it--)
        {
            if(n>=*it)
                n-=*it,bs.set(Size--);
            if(n==0)
                break;
        }
        if(n>0)
            n-=1LL,bs.set(0);
        if(n==0)
        {
            cout<<ans<<'\n';
            continue;
        }
        int i=0;
        bool payeci=false;
        for(auto j=s.begin();j!=s.end()&&i<sz;j++,i++)
        {
            if(bs[i]==0)
            {
                payeci=true;
                ans+=*j;
                break;
            }
        }
        pos++;
        if(!payeci)
            cout<<*pos<<'\n';
        else
            cout<<ans<<'\n';
    }
    return 0;
}


/*************************************************************************************
                         ---------------------------
                   ------Bismillahir-Rahmanir-Rahim------
                         ---------------------------
            //     //   Name: MD: NAZMUL HASAN              //     //
           //     //    Institute: HSTU                    //     //
          //     //     Dept.: CSE                        //     //
        //     //      Email: nazmulmoon123@gmail.com   //     //
    -----------------------------------------------------------------------
            ---------- I Love Programming as Gaming ----------
**************************************************************************************/
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree<T, null_type, less<T>,
      rb_tree_tag, tree_order_statistics_node_update>;
#define loop(a,b,c)   for(int a=b;a<c;a++)
#define loopa(p,v)    for(auto p:(v))
#define lloop(a,b,c)  for(long long a=b;a<c;a++)
#define rloop(a,b,c)  for(int a=c-1;a>=b;a--)
#define rlloop(a,b,c) for(long long a=c-1;a>=b;a--)
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define EPS 1e-9
#define PI acos(-1)
#define rn PI/180
#define TC(t)         scanf("%d",&t);while(t--)
#define sf scanf
#define pf printf
#define SIZE 200001
#define sp ' '
#define nl printf("\n")
#define pb push_back
#define pp pop_back
#define mp make_pair
#define ff first
#define ss second
#define gcd(a,b) __gcd(a,b)

#define si(a) sf("%d",&a)
#define sl(a) sf("%I64d",&a)
#define sii(a,b) sf("%d%d",&a,&b)
#define Pi(a) pf("%d\n",a)
#define Pin(a) pf("%d ",a)
#define Pl(a) pf("%I64d\n",a)
#define pln(a) pf("%I64d ",a)
#define b_s(v,x) binary_search(v.begin(),v.end(),x)
#define l_b(v,x) lower_bound(v.begin(),v.end(),x)
#define u_b(v,x) upper_bound(v.begin(),v.end(),x)
#define e_r(v,x) equal_range(v.begin(),v.end(),x)
#define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define case(x) cout<<"Case "<<x<<": "

/// ASCII Vlaue....
/// A=65,Z=90,a=97,z=122,0=48,9=57
///#define INT_MAX 2147483647
///#define INT_MIN -2147483647

const int MOD=1000000007;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int>vi;
typedef vector<ll>vl;
typedef vector<string> vs;
typedef stack<int> si;
typedef stack<string> ss;
typedef queue<int> qi;
typedef queue<string> qs;
typedef pair<int,int> pii;
typedef pair<string,int>psi;
typedef map<int,int>mii;
typedef map<string,int>msi;
///      direction
/*int x4[]= {1,-1,0,0};
int y4[]= {0,0,1,-1};
int x8[]= {0,0,+1,-1,-1,+1,-1,+1};
int y8[]= {-1,+1,0,0,+1,+1,-1,-1};
*/

/*ll nCr(ll aan, ll aar)
{
    ll ii, aac=1;
    for(ii=1; ii<=aar; ii++)
    {
        aac*=(aan-aar+ii);
        aac/=ii;
    }
    return aac;
}

bool status[1100002];
void siv()
{
	int N=1000000;
	int sq=sqrt(N);
	for(int i=4;i<=N;i+=2) status[i]=1;
	for(int i=3;i<=sq;i+=2){
		if(status[i]==0)
		{
			for(int j=i*i;j<=N;j+=i) status[j]=1;
		}
	}
	status[1]=1;
}

ll big_mod(ll base, ll power, ll mod)
{
    if(power==0) return 1;
    if(power&1)
    {
        ll p1=base%mod;
        ll p2=(big_mod(base, power-1,mod))%mod;
        return (p1*p2)%mod;
    }
    else
    {
        ll p1=(big_mod(base,power/2,mod))%mod;
        return (p1*p1)%mod;
    }
}
ll modInverseBigMod(ll a,ll m)  //a*x=1(mod m)
{
    return big_mod(a,m-2,m);
}**/

///START YOUR CODE FROM HERE.... Happy Coding.... ! ;)

/*ll nCr(ll n, ll k)
{
    ll C[n+1][k+1];
    ll i, j;

    // Caculate value of Binomial Coefficient in bottom up manner
    for (i = 0; i <= n; i++)
    {
        for (j = 0; j <= min(i, k); j++)
        {
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;

            // Calculate value using previosly stored values
            else
                C[i][j] = C[i-1][j-1] + C[i-1][j];
        }
    }

    return C[n][k];
}
*/
#define Mx 40
bitset<Mx>bs;
int main()
{
    faster
    ll t,n,x,ans;
    set<ll>s;
    cin>>t;
    n=1000000000000000000;
    x=3;
    ll y=3;
    s.insert(1);
    s.insert(3);
    while(y<n){
        y*=x;
        s.insert(y);
    }
    while(t--)
    {
        bs.reset();
        cin>>n;
        ans=n;
        if(s.find(n)!=s.end()){
            cout<<n<<'\n';
            continue;
        }
        ll z=*s.lower_bound(n);
        auto pos=s.find(z);
        int Size(0),sz;
        for(auto it=s.begin();it!=pos;it++)
            Size++;
        pos--;
        sz=Size;
        Size--;
        for(auto it=pos;it!=s.begin();it--,Size--)
        {
            if(n>=*it)
                n-=*it,bs.set(Size);
            if(n==0)
                break;
        }
        if(n>0)
            n-=1LL,bs.set(0);
        if(n==0)
        {
            cout<<ans<<'\n';
            continue;
        }
        /*
        for(int i=0;i<=sz;i++)
        {
            cout<<bs[i]<<'!';
        }
        cout<<"\n";
        */
        int i=0;
        bool payeci=false;
        ans=0;
        for(auto j=s.begin();j!=s.end()&&i<sz;j++,i++)
        {
            if(bs[i]==0)
            {
                payeci=true;
                ans+=*j;
            }
            else if(payeci&&bs[i]==1)
            {
                ans+=*j;
            }

        }
        pos++;
        if(!payeci)
            cout<<*pos<<'\n';
        else
            cout<<ans<<'\n';
    }
    return 0;
}


/*************************************************************************************
                         ---------------------------
                   ------Bismillahir-Rahmanir-Rahim------
                         ---------------------------
            //     //   Name: MD: NAZMUL HASAN              //     //
           //     //    Institute: HSTU                    //     //
          //     //     Dept.: CSE                        //     //
        //     //      Email: nazmulmoon123@gmail.com   //     //
    -----------------------------------------------------------------------
            ---------- I Love Programming as Gaming ----------
**************************************************************************************/
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree<T, null_type, less<T>,
      rb_tree_tag, tree_order_statistics_node_update>;
#define loop(a,b,c)   for(int a=b;a<c;a++)
#define loopa(p,v)    for(auto p:(v))
#define lloop(a,b,c)  for(long long a=b;a<c;a++)
#define rloop(a,b,c)  for(int a=c-1;a>=b;a--)
#define rlloop(a,b,c) for(long long a=c-1;a>=b;a--)
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define EPS 1e-9
#define PI acos(-1)
#define rn PI/180
#define TC(t)         scanf("%d",&t);while(t--)
#define sf scanf
#define pf printf
#define SIZE 200001
#define sp ' '
#define nl printf("\n")
#define pb push_back
#define pp pop_back
#define mp make_pair
#define ff first
#define ss second
#define gcd(a,b) __gcd(a,b)

#define si(a) sf("%d",&a)
#define sl(a) sf("%I64d",&a)
#define sii(a,b) sf("%d%d",&a,&b)
#define Pi(a) pf("%d\n",a)
#define Pin(a) pf("%d ",a)
#define Pl(a) pf("%I64d\n",a)
#define pln(a) pf("%I64d ",a)
#define b_s(v,x) binary_search(v.begin(),v.end(),x)
#define l_b(v,x) lower_bound(v.begin(),v.end(),x)
#define u_b(v,x) upper_bound(v.begin(),v.end(),x)
#define e_r(v,x) equal_range(v.begin(),v.end(),x)
#define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define case(x) cout<<"Case "<<x<<": "

/// ASCII Vlaue....
/// A=65,Z=90,a=97,z=122,0=48,9=57
///#define INT_MAX 2147483647
///#define INT_MIN -2147483647

const int MOD=1000000007;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int>vi;
typedef vector<ll>vl;
typedef vector<string> vs;
typedef stack<int> si;
typedef stack<string> ss;
typedef queue<int> qi;
typedef queue<string> qs;
typedef pair<int,int> pii;
typedef pair<string,int>psi;
typedef map<int,int>mii;
typedef map<string,int>msi;
///      direction
/*int x4[]= {1,-1,0,0};
int y4[]= {0,0,1,-1};
int x8[]= {0,0,+1,-1,-1,+1,-1,+1};
int y8[]= {-1,+1,0,0,+1,+1,-1,-1};
*/

/*ll nCr(ll aan, ll aar)
{
    ll ii, aac=1;
    for(ii=1; ii<=aar; ii++)
    {
        aac*=(aan-aar+ii);
        aac/=ii;
    }
    return aac;
}

bool status[1100002];
void siv()
{
	int N=1000000;
	int sq=sqrt(N);
	for(int i=4;i<=N;i+=2) status[i]=1;
	for(int i=3;i<=sq;i+=2){
		if(status[i]==0)
		{
			for(int j=i*i;j<=N;j+=i) status[j]=1;
		}
	}
	status[1]=1;
}

ll big_mod(ll base, ll power, ll mod)
{
    if(power==0) return 1;
    if(power&1)
    {
        ll p1=base%mod;
        ll p2=(big_mod(base, power-1,mod))%mod;
        return (p1*p2)%mod;
    }
    else
    {
        ll p1=(big_mod(base,power/2,mod))%mod;
        return (p1*p1)%mod;
    }
}
ll modInverseBigMod(ll a,ll m)  //a*x=1(mod m)
{
    return big_mod(a,m-2,m);
}**/

///START YOUR CODE FROM HERE.... Happy Coding.... ! ;)

/*ll nCr(ll n, ll k)
{
    ll C[n+1][k+1];
    ll i, j;

    // Caculate value of Binomial Coefficient in bottom up manner
    for (i = 0; i <= n; i++)
    {
        for (j = 0; j <= min(i, k); j++)
        {
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;

            // Calculate value using previosly stored values
            else
                C[i][j] = C[i-1][j-1] + C[i-1][j];
        }
    }

    return C[n][k];
}
*/
#define Mx 40
bitset<Mx>bs;
int main()
{
    faster
    ll t,n,x,ans;
    set<ll>s;
    cin>>t;
    n=10000;
    x=3;
    ll y=3;
    s.insert(1);
    s.insert(3);
    while(y<n){
        y*=x;
        s.insert(y);
    }
    while(t--)
    {
        bs.reset();
        cin>>n;
        ans=n;
        if(s.find(n)!=s.end()){
            cout<<n<<'\n';
            continue;
        }
        ll z=*s.lower_bound(n);
        auto pos=s.find(z);
        int Size(0),sz;
        for(auto it=s.begin();it!=pos;it++)
            Size++;
        pos--;
        sz=Size;
        Size--;
        for(auto it=pos;it!=s.begin();it--)
        {
            if(n>*it)
                n-=*it,bs.set(Size--);
            if(n==0)
                break;
        }
        if(n>0)
            n-=1LL,bs.set(Size);
        if(n==0)
        {
            cout<<ans<<'\n';
            continue;
        }
        int i=0;
        bool payeci=false;
        for(auto j=s.begin();j!=s.end()&&i<sz;j++,i++)
        {
            if(bs[i]==0)
            {
                payeci=true;
                ans+=*j;
                break;
            }
        }
        pos++;
        if(!payeci)
            cout<<*pos<<'\n';
        else
            cout<<ans<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int sum;
int f3(int n)
{
    sum=1;int i=0;
    while(3*sum<=n){
        sum*=3;
        i++;
    }
    return i;
}
int main()
{
    int T,n,maxn,maxsum,t,ans;
    cin>>T;
    while(T--){
        cin>>n;
        ans=n;
        maxn=f3(n)+1;
        maxsum=sum*3;
        vector<int> p(100);
        t=0;
        while(n){
            t++;
            p[f3(n)]++;
            n-=sum;
        }
        if(t>maxn)
            cout<<maxsum<<endl;
        else{
            int flag=0;
            for(int i=0;i<maxn;i++)
                while(p[i]>1)
                {
                    int j=0;
                    for(;j<maxn;j++)
                        if(p[i]==0){
                            p[j]++;
                            p[i]--;
                            ans-=pow(3,i);
                            ans+=pow(3,j);
                            break;
                        }
                    if(j>=maxn){
                        flag=1;break;
                    }
                }
            if(flag)
                cout<<maxsum<<endl;
            else
                cout<<ans<<endl;
        }
    }
}