#include <iostream>
#include <cstdio>
#include <cstring>
#define LL long long
using namespace std;
const int MAX=55;
LL bin[MAX],sum[MAX],n;
int q;
int main(){
    sum[0]=bin[0]=1;
    for(int i=1;i<=38;i++)bin[i]=bin[i-1]*3,sum[i]=sum[i-1]+bin[i];    //i=38
    scanf("%d",&q);
    while(q--) {
        scanf("%lld", &n);
        for (int i = 0; i <= 38; i++) {
            if (sum[i] >= n) {
                LL ans = sum[i];
                for (int j = i - 1; j >= 0; j--)if (ans - bin[j] >= n)ans -= bin[j];
                cout<<ans<<endl;
                break;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define x first
#define y second
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=40;
const int M=150;
const int INF=0x3f3f3f3f;
const int MOD=998244353;
LL n,a[N];
LL get(LL x){
    LL res=0;
    for(int i=0;i<=39;i++){
        if((x>>i)&1) res+=a[i];
    }
    return res;
}
bool check(LL x){
    if(get(x)>=n) return true;
    return false;
}
void solve(){
    cin>>n;
    LL l=1,r=(1ll<<39)-1;
    while(l<r){
        LL mid=l+r>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    cout<<get(l)<<endl;
}
int main(){
    IOS;
    a[0]=1;
    for(int i=1;i<=39;i++) a[i]=a[i-1]*3;
    int t;cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n;
int t;

int main(){
	cin>>t;
	while(t--){
	cin>>n;
	long long ans=0,temp=n;
	int sum=0;
	for(int i=39;i>=1;i--){
	      if((long long)(pow(3,i-1)-1)/2+ans<temp){
	      	ans+=(long long)pow(3,i-1);
	      }
	}
	//if(ans<temp)ans++;
	cout<<ans<<endl;
	}

	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;
unsigned long long v[100];
int main()
{
    unsigned long long q, n, p, ok, s, cn;
    cin >> q;
    for (unsigned long long i = 0; i < q; i++)
    {
        p = 0;
        s = 0;
        ok = 0;
        v[0] = 0;
        cin >> n;
        cn = n;
        while (n)
        {
            v[++v[0]] = n % 3;
            n /= 3;
        }
        for (unsigned long long j = 1; j <= v[0]; j++)
                if (v[j] == 2) {p = j; ok = 1;}

        if (ok == 1)
            {
            for (unsigned long long j = p; j <= v[0]; j++)
                {
                if (v[j] == 0)
                    {
                        ok = 2;
                        v[j] = 1;
                        p = j;
                        break;
                    }
                }
            }
        if (ok == 2)
        {
            for (unsigned long long j = p; j <= v[0]; j++) s += v[j] * pow(3,j-1);
            cout << s << '\n';
        }
        else if (ok == 1) {
            s = 1;
            for (unsigned long long j = 0; j <= v[0]; j++) s *= 3;
            cout << s  << '\n';
        }
        else cout << cn << '\n';

    }
}


#include <iostream>
#include <cmath>
using namespace std;
unsigned long long v[100];
int main()
{
    unsigned long long q, n, p, ok, s, cn;
    cin >> q;
    for (unsigned long long i = 0; i < q; i++)
    {
        p = 0;
        s = 0;
        ok = 0;
        v[0] = 0;
        cin >> n;
        cn = n;
        while (n)
        {
            v[++v[0]] = n % 3;
            n /= 3;
        }
        for (unsigned long long j = 1; j <= v[0]; j++)
                if (v[j] == 2) {p = j; ok = 1;}

        if (ok == 1)
            {
            for (unsigned long long j = p; j <= v[0]; j++)
                {
                if (v[j] == 0)
                    {
                        ok = 2;
                        v[j] = 1;
                        p = j;
                        break;
                    }
                }
            }
        if (ok == 2)
        {
            for (unsigned long long j = p; j <= v[0]; j++) s += v[j] * pow(3,j-1);
            cout << s << '\n';
        }
        else if (ok == 1) {
            s = 1;
            for (unsigned long long j = 0; j <= p; j++) s *= 3;
            cout << s  << '\n';
        }
        else cout << cn << '\n';

    }
}


#include <iostream>
#include <cmath>
using namespace std;
unsigned long long v[100];
int main()
{
    unsigned long long q, n, p, ok, s, cn;
    cin >> q;
    for (unsigned long long i = 0; i < q; i++)
    {
        p = 0;
        s = 0;
        ok = 0;
        v[0] = 0;
        cin >> n;
        cn = n;
        while (n)
        {
            v[++v[0]] = n % 3;
            n /= 3;
        }
        for (unsigned long long j = 1; j <= v[0]; j++)
                if (v[j] == 2) {p = j; ok = 1;}

        if (ok == 1)
            {
            for (unsigned long long j = p; j <= v[0]; j++)
                {
                if (v[j] == 0)
                    {
                        ok = 2;
                        v[j] = 1;
                        p = j;
                        break;
                    }
                }
            }
        if (ok == 2)
        {
            for (unsigned long long j = p; j <= v[0]; j++) s += v[j] * pow(3,j-1);
            cout << s << '\n';
        }
        else if (ok == 1) {
            s = 1;
            for (unsigned long long j = 1; j <= v[0]; j++) s *= 3;
            cout << s  << '\n';
        }
        else cout << cn << '\n';

    }
}


#include <iostream>
#include <cmath>
using namespace std;
unsigned long long v[100];
int main()
{
    unsigned long long q, n, p, ok, s, cn, put;
    cin >> q;
    for (unsigned long long i = 0; i < q; i++)
    {
        p = 0;
        s = 0;
        ok = 0;
        v[0] = 0;
        cin >> n;
        cn = n;
        while (n)
        {
            v[++v[0]] = n % 3;
            n /= 3;
        }
        for (unsigned long long j = 1; j <= v[0]; j++)
                if (v[j] == 2) {p = j; ok = 1;}

        if (ok == 1)
            {
            for (unsigned long long j = p; j <= v[0]; j++)
                {
                if (v[j] == 0)
                    {
                        ok = 2;
                        v[j] = 1;
                        p = j;
                        break;
                    }
                }
            }
        if (ok == 2)
        {
            put = 1;
            for (int j = 1; j < p; j++) put *= 3;
            for (unsigned long long j = p; j <= v[0]; j++)
            {
                s += v[j] * put;
                put *= 3;

            }
            cout << s << '\n';
        }
        else if (ok == 1) {
            s = 1;
            for (unsigned long long j = 1; j <= v[0]; j++) s *= 3;
            cout << s  << '\n';
        }
        else cout << cn << '\n';

    }
}


#include <iostream>
#include <cmath>
using namespace std;
unsigned long long v[100];
int main()
{
    unsigned long long q, n, p, ok, s, cn;
    cin >> q;
    for (unsigned long long i = 0; i < q; i++)
    {
        p = 0;
        s = 0;
        ok = 0;
        v[0] = 0;
        cin >> n;
        cn = n;
        while (n)
        {
            v[++v[0]] = n % 3;
            n /= 3;
        }
        for (unsigned long long j = 1; j <= v[0]; j++)
                if (v[j] == 2) {p = j; ok = 1;}

        if (ok == 1)
            {
            for (unsigned long long j = p; j <= v[0]; j++)
                {
                if (v[j] == 0)
                    {
                        ok = 2;
                        v[j] = 1;
                        p = j;
                        break;
                    }
                    else v[j] = 0;
                }
            }
        if (ok == 2)
        {
            for (unsigned long long j = p; j <= v[0]; j++) s += v[j] * pow(3,j-1);
            cout << s << '\n';
        }
        else if (ok == 1) cout <<pow(3,v[0]) << '\n';
        else cout << cn << '\n';

    }
}


#include <iostream>
#include <cmath>
using namespace std;
unsigned long long v[100];
int main()
{
    unsigned long long q, n, p, ok, s, cn;
    cin >> q;
    for (unsigned long long i = 0; i < q; i++)
    {
        p = 0;
        s = 0;
        ok = 0;
        v[0] = 0;
        cin >> n;
        cn = n;
        while (n)
        {
            v[++v[0]] = n % 3;
            n /= 3;
        }
        for (unsigned long long j = 1; j <= v[0]; j++)
                if (v[j] == 2) {p = j; ok = 1;}

        if (ok == 1)
            {
            for (unsigned long long j = p; j <= v[0]; j++)
                {
                if (v[j] == 0)
                    {
                        ok = 2;
                        v[j] = 1;
                        p = j;
                        break;
                    }
                    else v[j] = 0;
                }
            }
        if (ok == 2)
        {
            for (unsigned long long j = p; j <= v[0]; j++) s += v[j] * pow(3,j-1);
            cout << s << '\n';
        }
        else if (ok == 1) { s = pow(3,v[0]);cout << s  << '\n';}
        else cout << cn << '\n';

    }
}


#include <iostream>
using namespace std;
typedef long long lli_t;
int main(void)
{
    /*
    paso 1: hacer un arreglo con las potencias de 3 que se usarán
            estas potencias dependen del tamaño del input.
    */
    lli_t pow3[40] = {1};
    lli_t i, t, q, j;
    for(i = 1; i < 40; ++i)
        pow3[i] = 3 * pow3[i - 1];
    cin >> t;
    for(i = 0; cin >> q and i < t; ++i) {
        /*
        paso 2: la query es simple, las bases numericas de cualquier base tienen una propiedad
                un poco rara, te la puedo explicar en  la u con pizarra, la cosa es que el
                algoritmo es tal como lo implementaré aquí
        */
        lli_t m, upper_bound;
        for(j = 0, upper_bound = 0; j < 40; ++j)
            if(pow3[j] >= q){
                upper_bound = pow3[j];
                break;
            }
        for(j = 39, m = 0; j >= 0; --j)
            if (q >= pow3[j]) {
                q -= pow3[j];
                m += pow3[j];
            }
        if(q) cout<<upper_bound<<'\n';
        else cout<<m<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const long long INF64 = 1e18;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		long long n;
		cin >> n;
		vector<int> vals;
		int pos2 = -1;
		while (n > 0) {
			vals.push_back(n % 3);
			if (vals.back() == 2) pos2 = int(vals.size()) - 1;
			n /= 3;
		}
		vals.push_back(0);
		if (pos2 != -1) {
			int pos0 = find(vals.begin() + pos2, vals.end(), 0) - vals.begin();
			vals[pos0] = 1;
			for (int i = pos0 - 1; i >= 0; --i) {
				vals[i] = 0;
			}
		}
		long long ans = 0;
		long long pw = 1;
		for (int i = 0; i < int(vals.size()); ++i) {
			ans += pw * vals[i];
			pw *= 3;
		}
		if (vals.back() == 1) ans = pw / 3;
		cout << ans << endl;
	}

	return 0;
}


#include <iostream>
using namespace std;
typedef long long lli_t;
int main(void)
{
    /*
    paso 1: hacer un arreglo con las potencias de 3 que se usarán
            estas potencias dependen del tamaño del input.
    */
    lli_t pow3[40] = {1};
    lli_t i, t, q, j;
    for(i = 1; i < 40; ++i)
        pow3[i] = 3 * pow3[i - 1];
    cin >> t;
    for(i = 0; cin >> q and i < t; ++i) {
        /*
        paso 2: la query es simple, las bases numericas de cualquier base tienen una propiedad
                un poco rara, te la puedo explicar en  la u con pizarra, la cosa es que el
                algoritmo es tal como lo implementaré aquí
        */
        lli_t min_index = 40, upper_bound;
        for(j = 0, upper_bound = 0; j < 40; ++j)
            if(pow3[j] >= q){
                upper_bound = pow3[j];
                break;
            }
        for(j = 39; j >= 0; --j)
            if (q >= pow3[j]) {
                q -= pow3[j];
                min_index = j;
            }
        if(q) cout<<upper_bound<<'\n';
        else cout<<pow3[min_index]<<'\n';
    }
    return 0;
}


#include <iostream>
using namespace std;
typedef long long lli_t;
int main(void)
{
    /*
    paso 1: hacer un arreglo con las potencias de 3 que se usarán
            estas potencias dependen del tamaño del input.
    */
    lli_t pow3[40] = {1};
    lli_t i, t, q, j;
    for(i = 1; i < 40; ++i)
        pow3[i] = 3 * pow3[i - 1];
    cin >> t;
    for(i = 0; cin >> q and i < t; ++i) {
        /*
        paso 2: la query es simple, las bases numericas de cualquier base tienen una propiedad
                un poco rara, te la puedo explicar en  la u con pizarra, la cosa es que el
                algoritmo es tal como lo implementaré aquí
        */
        lli_t m, upper_bound;
        for(j = 0, upper_bound = 0; j < 40; ++j)
            if(pow3[j] >= q){
                upper_bound = pow3[j];
                break;
            }
        for(j = 39, m = 0; j >= 0; --j)
            if (q >= pow3[j]) {
                q -= pow3[j];
                m += pow3[j];
            }
        if(q) cout<<upper_bound + m<<'\n';
        else cout<<m<<'\n';
    }
    return 0;
}


#include <iostream>
typedef long long lli_t;
using namespace std;
lli_t p3[40];
lli_t pow(lli_t base, lli_t power){
    lli_t curr = (lli_t)1;
    for(lli_t i=1;i<=power;i++){
        curr*=base;
    }
    return curr;
}
lli_t bf(lli_t bitmask){
    int i = 0;
    lli_t res = 0;
    lli_t curr = 1;
    while(curr<=bitmask){
        if(bitmask & (lli_t)((lli_t)1<<i))
            res += p3[i];
        curr *= (lli_t)2;
        i++;
    }
    return res;
}
lli_t solve(lli_t n){
    lli_t l = 0, r = pow(2, 38);
    lli_t m, v;
    while(l<r){
        m = (l+r)>>(lli_t)1;
        v = bf(m);

        if(v<n)
            l = m + (lli_t)1;
        else
            r = m;
    }
    return bf(l);
}
int main(){
    /* generar potencias de 3 */
    p3[0] = 1;
    for(lli_t i = 1;i<40;i++)
        p3[i] = (lli_t)3 * p3[i-1];
    int q; cin>>q;
    for(int t=0;t<q;t++){
        lli_t n; cin>>n;
        cout<<solve(n)<<endl;
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>

using namespace std;

typedef long long ll;

inline ll power(ll a,ll b)
{
	ll res=1;
	while(b)
	{
		if(b&1)
			res*=a;
		a*=a;
		b>>=1;
	}
	return res;
}

int q;
int shu[40];	//ceil(log3(1e9))=38

int main()
{
	scanf("%d",&q);
	while(q--)
	{
		memset(shu,0,sizeof(shu));
		ll n,n2;
		scanf("%lld",&n);
		n2=n;
		int now=-1,cnt=0;
		while(n)
		{
			if(n%3==2) cnt++;
			shu[++now]=n%3;
			n/=3;
		}
		if(cnt)
		{
			bool flag=false;
			int p;
			for(int i=now;i>=0;i--)
			{
				if(shu[i]==0) flag=true,p=i;
				else if(shu[i]==2)
				{
					if(flag)
					{
						n2+=power(3,p);
						for(int j=i;j>=0;j--) n2-=(ll)shu[j]*power(3,j);
					}
					else
					{
						n2=power(3,now+1);
					}
					break;
				}
			}
		}
		printf("%lld\n",n2);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>

using namespace std;    //

typedef long long ll;

inline ll power(ll a,ll b)
{
	ll res=1;
	while(b)
	{
		if(b&1)
			res*=a;
		a*=a;
		b>>=1;
	}
	return res;
}

int q;
int shu[40];	//ceil(log3(1e9))=38

int main()
{
	scanf("%d",&q);
	while(q--)
	{
		memset(shu,0,sizeof(shu));
		ll n,n2;
		scanf("%lld",&n);
		n2=n;
		int now=-1,cnt=0;
		while(n)
		{
			if(n%3==2) cnt++;
			shu[++now]=n%3;
			n/=3;
		}
		if(cnt)
		{
			int p;
			for(int i=now+1;i>=0;i--)
			{
				if(shu[i]==0) p=i;
				else if(shu[i]==2)
				{
					n2+=power(3,p);
					for(int j=p-1;j>=0;j--) n2-=(ll)shu[j]*power(3,j);
					break;
				}
			}
		}
		printf("%lld\n",n2);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>

using namespace std;

typedef long long ll;

inline ll power(ll a,ll b)
{
	ll res=1;
	while(b)
	{
		if(b&1)
			res*=a;
		a*=a;
		b>>=1;
	}
	return res;
}

int q;
int shu[40];	//ceil(log3(1e9))=38

int main()
{
	scanf("%d",&q);
	while(q--)
	{
		memset(shu,0,sizeof(shu));
		ll n,n2;
		scanf("%lld",&n);
		n2=n;
		int now=-1,cnt=0;
		while(n)
		{
			if(n%3==2) cnt++;
			shu[++now]=n%3;
			n/=3;
		}
		if(cnt)
		{
			int p;
			for(int i=now+1;i>=0;i--)
			{
				if(shu[i]==0) p=i;
				else if(shu[i]==2)
				{
					n2+=power(3,p);
					for(int j=p-1;j>=0;j--) n2-=(ll)shu[j]*power(3,j);
					break;
				}
			}
		}
		printf("%lld\n",n2);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>

using namespace std;

typedef long long ll;

inline ll power(ll a,ll b)
{
	ll res=1;
	while(b)
	{
		if(b&1)
			res*=a;
		a*=a;
		b>>=1;
	}
	return res;
}

int q;
int shu[40];	//ceil(log3(1e9))=38

int main()
{
	scanf("%d",&q);
	while(q--)
	{
		memset(shu,0,sizeof(shu));
		ll n,p;
		scanf("%lld",&n);
		p=n;
		int now=-1,cnt=0;
		while(n)
		{
			if(n%3==2) cnt++;
			shu[++now]=n%3;
			n/=3;
		}
		if(cnt)
			printf("%lld\n",power(3,now+1));
		else
		{
			printf("%lld\n",p);
		}
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>

using namespace std;

typedef long long ll;

inline ll power(ll a,ll b)
{
	ll res=1;
	while(b)
	{
		if(b&1)
			res*=a;
		a*=a;
		b>>=1;
	}
	return res;
}

ll q;
ll shu[40];	//ceil(log3(1e18))=38

int main()
{
	scanf("%lld",&q);
	while(q--)
	{
		memset(shu,0,sizeof(shu));
		ll n,n2;
		scanf("%lld",&n);
		n2=n;
		ll now=-1,cnt=0;
		while(n)
		{
			if(n%3==2) cnt++;
			shu[++now]=n%3;
			n/=3;
		}
		if(cnt)
		{
			ll p;
			for(ll i=now+1;i>=0;i--)
			{
				if(shu[i]==0) p=i;
				else if(shu[i]==2)
				{
					n2+=power(3,p);
					for(ll j=p-1;j>=0;j--) n2-=(ll)shu[j]*power(3,j);
					break;
				}
			}
		}
		printf("%lld\n",n2);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e2+5;
ll a[maxn],cnt=0,s[maxn],f[maxn];
int main()
{
	int T;
	ll n;
	scanf("%d",&T);
	a[0]=1;s[0]=1;
	for(int i=1;;++i){
		a[i]=a[i-1]*3ll;
		if(a[i]>=1e18||a[i]<0)break;
		cnt=i;
		s[i]=s[i-1]+a[i];
	}
	while(T--){
		scanf("%I64d",&n);
		ll m=n,ans=0;
		int mx=0;
		for(int i=0;i<=cnt;i++)f[i]=0;
		for(int i=cnt;i>=0;i--){
			if(m>=a[i])m-=a[i],f[i]=1,mx=max(mx,i);;
		}
		if(m==0)ans=n;
		else{
			for(int i=0;i<mx;i++){
				if(f[i]==0){
					ans=n+a[i];
					for(int j=i-1;j>=0;j--)ans-=a[j];
					break;
				}
			}
			if(ans==0)ans=a[mx+1];
		}
		printf("%I64d\n",ans);
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e2+5;
ll a[maxn],cnt=0,s[maxn],f[maxn];
int main()
{
	int T;
	ll n;
	scanf("%d",&T);
	a[0]=1;s[0]=1;
	for(int i=1;;++i){
		a[i]=a[i-1]*3ll;
		if(a[i]>=1e18||a[i]<0)break;
		cnt=i;
		s[i]=s[i-1]+a[i];
	}
	while(T--){
		scanf("%I64d",&n);
		ll m=n,ans=0;
		int mx=0;
		for(int i=0;i<=cnt;i++)f[i]=0;
		for(int i=cnt;i>=0;i--){
			if(m>=a[i])m-=a[i],f[i]=1,mx=max(mx,i);;
		}
		if(m==0)ans=n;
		else{
			for(int i=0;i<mx;i++){
				if(f[i]==0){
					ans=a[i];
					for(int j=i+1;j<=mx;j++)if(f[j])ans+=a[j];
					break;
				}
			}
			if(ans==0)ans=a[mx+1];
		}
		printf("%I64d\n",ans);
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf=1e18;
const int maxn=55;

int T;
ll n,m,pw3[maxn];
vector<ll> v;

int main(){
    pw3[0]=1;
    for(int i=1;i<40;++i) pw3[i]=pw3[i-1]*3LL;
    scanf("%d",&T);
    while(T--){
        scanf("%I64d",&n);
        m=n;
        v.clear();
        while(m){
            v.push_back(m%3);
            m/=3;
        }
        int len=v.size();
        /*cout<<"###########\n";
        for(int i=0;i<len;++i) cout<<v[i];
        cout<<"############"<<endl;*/
        ll add=0;
        int flag=0;
        int pos=0;
        while(pos<len){
            if(v[pos]<2){
                ++pos;
                continue;
            }
            add+=pw3[pos];
            v[pos]=0;
            flag=1;
            for(int i=pos+1;i<len;++i) {
                int newflag=(v[i]+flag)/3;
                v[i]=(v[i]+flag)%3;
                flag=newflag;
            }
        }
        printf("%I64d\n",min(n+add,pw3[len]));
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e2+5;
ll a[maxn],cnt=0,s[maxn],f[maxn];
int main()
{
	int T;
	ll n;
	scanf("%d",&T);
	a[0]=1;s[0]=1;
	for(int i=1;;++i){
		a[i]=a[i-1]*3ll;
		if(a[i]>=1e18||a[i]<0)break;
		cnt=i;
		s[i]=s[i-1]+a[i];
	}
	while(T--){
		scanf("%I64d",&n);
		ll m=n,ans=0;
		int mx=0;
		for(int i=0;i<=cnt;i++)f[i]=0;
		for(int i=cnt;i>=0;i--){
			if(m>=a[i])m-=a[i],f[i]=1,mx=max(mx,i);;
		}
		if(m==0)ans=n;
		else{
			for(int i=0;i<mx;i++){
				if(f[i]==0){
					ans=n+a[i];
					for(int j=i-1;i>=0;j--)ans-=a[j];
					break;
				}
			}
			if(ans==0)ans=a[mx+1];
		}
		printf("%I64d\n",ans);
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf=1e18;
const int maxn=55;

int T;
ll n,m,pw3[maxn];
vector<ll> v;

int main(){
    pw3[0]=1;
    for(int i=1;i<40;++i) pw3[i]=pw3[i-1]*3LL;
    scanf("%d",&T);
    while(T--){
        scanf("%I64d",&n);
        m=n;
        v.clear();
        while(m){
            v.push_back(m%3);
            m/=3;
        }
        int len=v.size();
        ll add=0;
        int flag=0;
        int pos=0;
        while(pos<len){
            if(v[pos]<2){
                ++pos;
                continue;
            }
            add+=pw3[pos];
            v[pos]=0;
            flag=1;
            for(int i=pos+1;i<len;++i) {
                int newflag=(v[i]+flag)/3;
                v[i]=(v[i]+flag)%3;
                flag=newflag;
            }
            ++pos;
        }
        printf("%I64d\n",min(n+add,pw3[len]));
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e2+5;
ll a[maxn],cnt=0,s[maxn],f[maxn];
int main()
{
	int T;
	ll n;
	scanf("%d",&T);
	a[0]=1;s[0]=1;
	for(int i=1;;++i){
		a[i]=a[i-1]*3ll;
		if(a[i]>=1e18||a[i]<0)break;
		cnt=i;
		s[i]=s[i-1]+a[i];
		//printf("%d %lld\n",cnt,a[i]);
	}
	while(T--){
		scanf("%I64d",&n);
		ll m=n,ans=0;
		int mx=0;
		for(int i=0;i<=cnt;i++)f[i]=0;
		for(int i=cnt;i>=0;i--){
			if(m>=a[i])m-=a[i],f[i]=1,mx=max(mx,i);;
		}
		if(m==0)ans=n;
		else{
			for(int i=0;i<mx;i++){
				if(f[i]==0){
					ans=n+a[i];break;
				}
			}
			if(ans==0)ans=a[mx+1];
		}
		printf("%I64d\n",ans);
	}
}


// He always smiles :)
#include <bits/stdc++.h>
#define maxn 100005
#define mod 1000000007
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define task "main"
#define ll long long
#define reset(x) memset(x, 0, sizeof(x))
#define endl "\n"

using namespace std;
long long q, n, mu3[40], s, a[maxn], m;

int main()
{
    ios_base::sync_with_stdio(0);
//    freopen(task".inp", "r", stdin);
//    freopen(task".out", "w", stdout);
    cin >> q;
    mu3[0] = 1;
    for(int i = 1 ; i <= 38 ; i ++)
        mu3[i] = mu3[i - 1] * 3;
    while(q--)
    {
        cin >> n;
        m = n;
        reset(a);
        for (int i = 38 ; i >= 0 ; i --) if(n >= mu3[i]) n -= mu3[i], a[i] = 1;
        if(n != 0)
            for (int i = 0 ; i <= 38 ; i ++) if(a[i] == 0)
        {
            if(mu3[i] > m) m = mu3[i];
            else m += mu3[i];
            break;
        }
        cout << m << endl;
    }
    return 0;
}


// He always smiles :)
#include <bits/stdc++.h>
#define maxn 100005
#define mod 1000000007
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define task "main"
#define ll long long
#define reset(x) memset(x, 0, sizeof(x))
#define endl "\n"

using namespace std;
unsigned long long q, n, mu3[40], s, a[maxn], m, x;

int main()
{
    ios_base::sync_with_stdio(0);
//    freopen(task".inp", "r", stdin);
//    freopen(task".out", "w", stdout);
    cin >> q;
    mu3[0] = 1;
    for(int i = 1 ; i <= 38 ; i ++)
        mu3[i] = mu3[i - 1] * 3;
    while(q--)
    {
        cin >> n;
        m = n, x = n;
        reset(a);
        for (int i = 38 ; i >= 0 ; i --) if(n >= mu3[i]) n -= mu3[i], a[i] = 1;
        if(n != 0)
            for (int i = 0 ; i <= 38 ; i ++) if(a[i] == 0)
        {
            if(mu3[i] > m) m = mu3[i], reset(a), a[i] = 1;
            else m += mu3[i], a[i] = 1, m -= n;
            break;
        }
        for(int i = 0 ; i <= 38 ; i ++) if(a[i] == 1 && m - mu3[i] >= x) m -= mu3[i];
        cout << m << endl;
    }
    return 0;
}


// He always smiles :)
#include <bits/stdc++.h>
#define maxn 100005
#define mod 1000000007
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define task "main"
#define ll long long
#define reset(x) memset(x, 0, sizeof(x))
#define endl "\n"

using namespace std;
unsigned long long q, n, mu3[40], s, a[maxn], m, x;

int main()
{
    ios_base::sync_with_stdio(0);
//    freopen(task".inp", "r", stdin);
//    freopen(task".out", "w", stdout);
    cin >> q;
    mu3[0] = 1;
    for(int i = 1 ; i <= 38 ; i ++)
        mu3[i] = mu3[i - 1] * 3;
    while(q--)
    {
        cin >> n;
        m = n, x = n;
        reset(a);
        for (int i = 38 ; i >= 0 ; i --) if(n >= mu3[i]) n -= mu3[i], a[i] = 1;
        if(n != 0)
            for (int i = 0 ; i <= 38 ; i ++) if(a[i] == 0)
        {
            if(mu3[i] > m) m = mu3[i], reset(a), a[i] = 1;
            else m += mu3[i], a[i] = 1;
            break;
        }
        for(int i = 0 ; i <= 38 ; i ++) if(a[i] == 1 && m - mu3[i] >= x) m -= mu3[i];
        cout << m << endl;
    }
    return 0;
}


// He always smiles :)
#include <bits/stdc++.h>
#define maxn 100005
#define mod 1000000007
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define task "main"
#define ll long long
#define reset(x) memset(x, 0, sizeof(x))
#define endl "\n"

using namespace std;
unsigned long long q, n, mu3[40], s, a[maxn], m, x;

int main()
{
    ios_base::sync_with_stdio(0);
//    freopen(task".inp", "r", stdin);
//    freopen(task".out", "w", stdout);
    cin >> q;
    mu3[0] = 1;
    for(int i = 1 ; i <= 38 ; i ++)
        mu3[i] = mu3[i - 1] * 3;
    while(q--)
    {
        cin >> n;
        m = n, x = n;
        reset(a);
        for (int i = 38 ; i >= 0 ; i --) if(n >= mu3[i]) n -= mu3[i], a[i] = 1;
        if(n != 0)
            for (int i = 0 ; i <= 38 ; i ++) if(a[i] == 0)
        {
            if(mu3[i] > m) m = mu3[i], reset(a), a[i] = 1;
            else m += mu3[i], a[i] = 1;
            break;
        }
        if(m - n >= x) m -= n;
        for(int i = 0 ; i <= 38 ; i ++) if(a[i] == 1 && m - mu3[i] >= x) m -= mu3[i];
        cout << m << endl;
    }
    return 0;
}


#include <iostream>

#include <string.h>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
const int MAXN = 2*1e5;
int a[MAXN];
string Pow(long long c)
{
    string s = "";
    while(c)
    {
        s.push_back(c%3+'0');
        c/=3;
    }
    reverse(s.begin(),s.end());
    return s;
}
long long solve(string a)
{
    int i = 0;
    for(int y = a.length()-1;y>0;y--)
    {
        if(a[y]!='1'&&a[y]!='0'){
            a[y-1] = char(a[y]+1);
            a[y] = '0';
        }
    }
    long long sum = 0;
    int len = a.length();
    if(a[0]=='a') i = 1,len--;
    else {
        a[0] = '1';

        int len = a.length()-1;
        sum = pow(3,len);
        return sum;
    }
    len--;
    for(int y = i;y<a.length();y++)
    {
        sum+=(a[y]-'0')*pow(3,len);
        len--;

    }
    return sum;
}
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        long long q;
        cin>>q;
        string a = Pow(q);
        a = "a"+a;
        cout<<solve(a)<<endl;
    }
    return 0;
}


#include <iostream>

#include <string.h>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
const int MAXN = 2*1e5;
int a[MAXN];
int n;
long long q;
long long POW(long long q)
{
    long long sum = 1;
    long long a = 3;
    while(q)
    {
        if(q&1) sum = a*sum;
        a = a*a;
        q>>=1;
    }
    return sum;
}
string Pow(long long c)
{
    string s = "";
    while(c)
    {
        s.push_back(c%3+'0');
        c/=3;
    }
    reverse(s.begin(),s.end());
    return s;
}
long long solve(string a)
{
    int i = 0;
    for(int y = a.length()-1;y>0;y--)
    {
        if(a[y]!='1'&&a[y]!='0'){
            a[y-1] += 1;
            a[y] = '0';
        }
    }
    long long sum = 0;
    int len = a.length();
    if(a[0]=='a') i = 1,len--;
    else {
        a[0] = '1';

        int len = a.length()-1;
        sum = POW(len);
        return sum;
    }
    len--;
    for(int y = i;y<a.length();y++)
    {
        sum+=(a[y]-'0')*POW(len);
        len--;
        if(sum>=q) break;

    }
    return sum;
}
int main()
{
    cin>>n;
    while(n--)
    {

        cin>>q;
        string a = Pow(q);
        a = "a"+a;
        cout<<solve(a)<<endl;
    }
    return 0;
}


#include <iostream>

#include <string.h>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
const int MAXN = 2*1e5;
int a[MAXN];
int n;
long long q;
string Pow(long long c)
{
    string s = "";
    while(c)
    {
        s.push_back(c%3+'0');
        c/=3;
    }
    reverse(s.begin(),s.end());
    return s;
}
long long solve(string a)
{
    int i = 0;
    for(int y = a.length()-1;y>0;y--)
    {
        if(a[y]!='1'&&a[y]!='0'){
            a[y-1] += 1;
            a[y] = '0';
        }
    }
    long long sum = 0;
    int len = a.length();
    if(a[0]=='a') i = 1,len--;
    else {
        a[0] = '1';

        int len = a.length()-1;
        sum = pow(3,len);
        return sum;
    }
    len--;
    for(int y = i;y<a.length();y++)
    {
        sum+=(a[y]-'0')*pow(3,len);
        if(sum>=q) break;
        len--;

    }
    return sum;
}
int main()
{
    cin>>n;
    while(n--)
    {

        cin>>q;
        string a = Pow(q);
        a = "a"+a;
        cout<<solve(a)<<endl;
    }
    return 0;
}


#include <iostream>

#include <string.h>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
const int MAXN = 2*1e5;
int a[MAXN];
int n;
long long q;
string Pow(long long c)
{
    string s = "";
    while(c)
    {
        s.push_back(c%3+'0');
        c/=3;
    }
    reverse(s.begin(),s.end());
    return s;
}
long long solve(string a)
{
    int i = 0;
    for(int y = a.length()-1;y>0;y--)
    {
        if(a[y]!='1'&&a[y]!='0'){
            a[y-1] += 1;
            a[y] = '0';
        }
    }
    long long sum = 0;
    int len = a.length();
    if(a[0]=='a') i = 1,len--;
    else {
        a[0] = '1';

        int len = a.length()-1;
        sum = pow(3,len);
        return sum;
    }
    len--;
    for(int y = i;y<a.length();y++)
    {
        sum+=(a[y]-'0')*pow(3,len);
        len--;

    }
    return sum;
}
int main()
{
    cin>>n;
    while(n--)
    {

        cin>>q;
        string a = Pow(q);
        a = "a"+a;
        cout<<solve(a)<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define FORUP(i, start, end) for (ll i = start; i <= end; i++)
#define FORDOWN(i, end, start) for (ll i = end; i >= start; i--)
#define ll long long
#define ld long double
#define pir pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define fast_io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

const ll MAX = 200009, MOD = 1000000007, BASE = 311, inf  = 1e9;
struct node {
    ll u; ll v; ll w;
};

using namespace std;

ll f[100];

void solve() {
    ll n, res = 0, pos = -1;
    cin >> n;
    ll m = n;
    vector<int> a;
    while(n) {
        a.pb(n%3);
        if(a.back() == 2) pos = a.size()-1;
        n/=3;
    }
    if(pos == -1) cout << m << endl;
    else {
        int p = a.size();
        FORUP(i, pos+1, a.size()-1) if(a[i] == 0) {
            p = i;
            break;
        }
        if(p == a.size()) cout << f[p] << endl;
        else {
            a[p] = 1;
            FORUP(i, p, a.size()-1) {
                if(a[i]) res += f[i];
            }
            cout << res << endl;
        }
    }

}

int main()
{

    f[0] = 1;
    FORUP(i, 1, 38) f[i] = f[i-1] * 3;
    int t;
    cin >> t;
    while(t) {
        solve();
        t--;
    }

    return 0;
}


#include <bits/stdc++.h>
#define FORUP(i, start, end) for (ll i = start; i <= end; i++)
#define FORDOWN(i, end, start) for (ll i = end; i >= start; i--)
#define ll long long
#define ld long double
#define pir pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define fast_io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

const ll MAX = 200009, MOD = 1000000007, BASE = 311, inf  = 1e9;
struct node {
    ll u; ll v; ll w;
};

using namespace std;

ll f[100], g[100];

void solve() {
    ll n;
    cin >> n;
    ll i = 0;
    while(g[i] < n) i++;
    if(g[i] == n) cout << n << endl;
    else {
        ll l = 0, r = i-1, x = g[i] - n;
        bool found = false;
        while(l <= r) {
            ll m = (l+r)/2;
            if(g[m] == x) {
                cout << n << endl;
                found = true;
                break;
            }
            if(g[m] < x) l = m + 1;
            else r = m - 1;
        }
        if(!found) {
            if(r < 0) cout << g[i] << endl;
            else cout << g[i] - g[r] << endl;
        }
    }
}

int main()
{
    f[0] = 1;
    FORUP(i, 1, 40) f[i] = f[i-1] * 3;
    g[0] = 1;
    FORUP(i, 1, 40) {
        g[i] = f[i] + g[i-1];
    }

    int t;
    cin >> t;
    while(t) {
        solve();
        t--;
    }

    return 0;
}


#include <bits/stdc++.h>
#define FORUP(i, start, end) for (ll i = start; i <= end; i++)
#define FORDOWN(i, end, start) for (ll i = end; i >= start; i--)
#define ll long long
#define ld long double
#define pir pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define fast_io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

const ll MAX = 200009, MOD = 1000000007, BASE = 311, inf  = 1e9;
struct node {
    ll u; ll v; ll w;
};

using namespace std;

ll f[100], g[100];

void solve() {
    ll n;
    cin >> n;
    ll i = 0;
    while(g[i] < n) i++;
    if(g[i] == n) cout << n << endl;
    else {
        ll l = 0, r = i-1, x = g[i] - n;
        bool found = false;
        while(l <= r) {
            ll m = (l+r)/2;
            if(g[m] == x) {
                cout << n << endl;
                found = true;
                break;
            }
            if(g[m] < x) l = m + 1;
            else r = m - 1;
        }
        if(!found) cout << g[i] - g[r] << endl;
    }
}

int main()
{
    f[0] = 1;
    FORUP(i, 1, 38) f[i] = f[i-1] * 3;
    g[0] = 1;
    FORUP(i, 1, 38) {
        g[i] = f[i] + g[i-1];
    }

    int t;
    cin >> t;
    while(t) {
        solve();
        t--;
    }

    return 0;
}


#include <bits/stdc++.h>
#define FORUP(i, start, end) for (ll i = start; i <= end; i++)
#define FORDOWN(i, end, start) for (ll i = end; i >= start; i--)
#define ll long long
#define ld long double
#define pir pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define fast_io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

const ll MAX = 200009, MOD = 1000000007, BASE = 311, inf  = 1e9;
struct node {
    ll u; ll v; ll w;
};

using namespace std;

ll f[100];

void solve() {
    ll n, res = 0, pos = -1;
    cin >> n;
    ll m = n;
    vector<int> a;
    while(n) {
        a.pb(n%3);
        if(a.back() == 2) pos = a.size()-1;
        n/=3;
    }
    if(pos == -1) cout << m << endl;
    else {
        int p = a.size();
        FORUP(i, pos+1, a.size()-1) if(a[i] == 0) {
            p = i;
            break;
        }
        if(p == a.size()) cout << f[p] << endl;
        else {
            FORUP(i, p, a.size()-1) res += f[i];
            if(p < a.size()-1) cout << res - f[p] << endl;
            else cout << res << endl;
        }
    }

}

int main()
{

    f[0] = 1;
    FORUP(i, 1, 38) f[i] = f[i-1] * 3;
    int t;
    cin >> t;
    while(t) {
        solve();
        t--;
    }

    return 0;
}


#include <bits/stdc++.h>
#define FORUP(i, start, end) for (ll i = start; i <= end; i++)
#define FORDOWN(i, end, start) for (ll i = end; i >= start; i--)
#define ll long long
#define ld long double
#define pir pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define fast_io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

const ll MAX = 200009, MOD = 1000000007, BASE = 311, inf  = 1e9;
struct node {
    ll u; ll v; ll w;
};

using namespace std;

ll f[100];

void solve() {
    ll n, res = 0, pos = -1;
    cin >> n;
    ll m = n;
    vector<int> a;
    while(n) {
        a.pb(n%3);
        if(a.back() == 2) pos = a.size()-1;
        n/=3;
    }
    if(pos == -1) cout << m << endl;
    else {
        int p = a.size();
        FORUP(i, pos+1, a.size()-1) if(a[i] == 0) {
            p = i;
            break;
        }
        if(p == a.size()) cout << f[p] << endl;
        else {
            FORUP(i, p, a.size()-1) res += f[i];
            cout << res << endl;
        }
    }

}

int main()
{

    f[0] = 1;
    FORUP(i, 1, 38) f[i] = f[i-1] * 3;
    int t;
    cin >> t;
    while(t) {
        solve();
        t--;
    }

    return 0;
}


#include <bits/stdc++.h>
#define FORUP(i, start, end) for (ll i = start; i <= end; i++)
#define FORDOWN(i, end, start) for (ll i = end; i >= start; i--)
#define ll long long
#define ld long double
#define pir pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define fast_io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

const ll MAX = 200009, MOD = 1000000007, BASE = 311, inf  = 1e9;
struct node {
    ll u; ll v; ll w;
};

using namespace std;

ll f[100], g[100];

void solve() {
    ll n;
    cin >> n;
    ll i = 0;
    while(g[i] < n) i++;
    if(g[i] == n) cout << n << endl;
    else {
        ll l = 0, r = i-1, x = g[i] - n;
        bool found = false;
        while(l <= r) {
            ll m = (l+r)/2;
            if(g[m] == x) {
                cout << n << endl;
                found = true;
                break;
            }
            if(g[m] < x) l = m + 1;
            else r = m - 1;
        }
        if(!found) cout << g[i] - g[r] << endl;
    }
}

int main()
{
    f[0] = 1;
    FORUP(i, 1, 40) f[i] = f[i-1] * 3;
    g[0] = 1;
    FORUP(i, 1, 40) {
        g[i] = f[i] + g[i-1];
    }

    int t;
    cin >> t;
    while(t) {
        solve();
        t--;
    }

    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;
typedef long long ll;
ll t,n,a[45],b[45],m,len;
ll work()
{
	ll i,ans=0;
	if (len>m)
	{
		ans=a[len];
		for(i=len-1;i>=1;--i) ans*=3;
		return ans;
	}
	for(i=m;i>=1;--i)
	{
		ans=ans*3+a[i];
		if (a[i]>b[i])
		{
			for(i=i-1;i>=1;--i)
				ans*=3;
			return ans;
		}
	}
	return ans;
}
int main()
{
	ll i;
	scanf("%lld",&t);
	while(t--)
	{
		scanf("%lld",&n);m=0;
		memset(a,0,sizeof(a));
		while(n) a[++m]=n%3,n/=3,b[m]=a[m];
		for(i=1;i<=m;++i)
		{
			a[i+1]+=a[i]/3;a[i]%=3;
			if (a[i]==2)
				a[i]=0,++a[i+1];
		}
		while(a[i])
		{
			a[i+1]+=a[i]/3;a[i]%=3;
			if (a[i]==2)
				a[i]=0,++a[i+1];
			++i;
		}
		len=i-1;
		printf("%lld\n",work());
	}
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define MAXN 100010
#define mid ((l+r)>>1)
#define lowbit(x) ((x)&(-x))
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const LL INF=0x3f3f3f3f3f3f3f3f;
int T;
LL n;
int num[100];

int main(){
	scanf("%d",&T);
	while(T--){
		scanf("%lld",&n);
		memset(num,0,sizeof(num));
		LL k=1,index=0;
		for(;k<n;k*=3,index+=1);
		while(k){
			while(n>=k) n-=k,num[index]++;
			k/=3,index-=1;
		}
		for(int i=0;i<=64;i++){
			if(num[i]==2){
				for(int j=i+1;j<=64;j++){
					if(num[j]==0){
						num[j]=1;
						for(int k=j-1;k>=0;k--) num[k]=0;
						break;
					}
				}
			}
		}
		LL ans=0;
		k=1;
		for(int i=0;i<=64;i++) {
			if(num[i]) ans+=k;
			k*=3;
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<cmath>
using namespace std;
typedef long long ll;
ll Pow[100];
int p,T,mx;
ll n;
bool check(ll x){
	p = -1;
	for(int i = 38;i >= 0;i --){
		if(x >= Pow[i]){
			mx = max(mx,i);
			if(x >= Pow[i] * 2)p = i,x -= Pow[i] * 2;
			else x -= Pow[i];
			//cout<<x<<endl;
		}
	}
	if(p == -1)return 0;
	return 1;
}
int main()
{
	scanf("%d",&T);
	Pow[0] = 1;
	for(int i = 1;i <= 38;i ++){
		Pow[i] = Pow[i-1] * 3;
	}
	while(T --){
		scanf("%lld",&n);
		if(check(n) == 0)
		cout<<n<<endl;
		else {
			cout<<Pow[mx+1]<<endl;
		}
	}
	return 0;
}
/*
long long
线段树四倍
写代码时请注意:
	1.是否要开Long Long?数组边界处理好了么?
	2.实数精度有没有处理?
	3.特殊情况处理好了么?
	4.做一些总比不做好.
思考提醒:
	1.最大值和最小值问题可不可以用二分答案?
	2.有没有贪心策略?否则能不能dp?
*/


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define maxn 1010000
#define LL unsigned long long
using namespace std;
LL n;
int a[50];
int main(){
	int T;
	cin>>T;
	while(T--){
		memset(a,0,sizeof(a));
		cin>>n;
	int cnt=0;
	for(int i=0;i<=37;i++){
		a[i]=n%3;
		n/=3;
		if(!n){
		cnt=i;
			break;
		}
	}
	int ans=0;
	for(int i=cnt+1;i>=0;i--){
		if(a[i]==2){
			for(int j=i;j<=cnt+1;j++){
				if(a[j]==0){
					a[j]=1;
					ans=j;
					break;
				}
			}
			break;
		}
	}
	for(int i=0;i<ans;i++){
		a[i]=0;
	}
	LL sum=0;
	LL tmp=1;
	for(int i=0;i<=cnt+2;i++){
		if(a[i]){
			sum+=tmp;
		}
		tmp*=3;
	}
	cout<<sum<<endl;
	}

	return 0;
}


#include<cstdio>
#include<cstdlib>
#define lnt long long
#define rnt register int
using namespace std;
int q,st,v[100];
lnt x,ans;
inline void deal(lnt x)
{
	v[0]=0;
	while(x)
	{
		v[++v[0]]=x%3;
		x/=3;
	}
	v[++v[0]]=0;
}
int main()
{
	scanf("%d",&q);
	for(;q;--q)
	{
		scanf("%lld",&x);
		deal(x);ans=0;st=0;
		for(rnt i=v[0];i>0;--i)
			if(v[i]>1)
			{
				st=i;
				for(rnt j=i;j<v[0];j=-~j)
					if(v[j]>1)
						v[j]=0,++v[j+1];
				break;
			}
		for(rnt i=1;i<st;i=-~i) v[i]=0;
		for(rnt i=v[0];i>0;--i)
			ans=(ans*3)+v[i];
		printf("%lld\n",ans);
	}
}


#include<bits/stdc++.h>
using namespace std;
long long n;
int t;
long long ksm(long long l,long long u){
	long long ert=1;
	while(u>0){
		if(u%1)ert*=l;
		l*=l;
		u/=2;
	}
	return ert;
}
int main(){
	cin>>t;
	while(t--){
	cin>>n;
	long long ans=0,temp=n;
	int sum=0;
	for(int i=39;i>=1;i--){
	      if(((ksm(3,i-1)-1)/2+ans)<temp){
	      	ans+=(long long)ksm(3,i-1);
	      }
	}
	cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n;
int t;
int p[100];

int main(){
	cin>>t;
	while(t--){
		memset(p,0,sizeof(p));
	cin>>n;
	long long ans=0,temp=n;
	int sum=0;
	while(n){
		p[++sum]=n%3;
		n/=3;
	}
	bool flag=1;
	for(int i=sum+1;i>=1;i--){
	      if((pow(3,i-1)-1)/2+ans<temp){
	      	ans+=pow(3,i-1);
	      }
	}
	if(ans<n)ans++;
	cout<<ans<<endl;
	}

	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<cstdio>
#include<iostream>
using namespace std;
const long long Maxn=50;
const long long n=39;
long long a[Maxn],m;
inline long long read()
{
	long long s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0' && ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*w;
}
long long get(long long x)
{
	long long ret=0;
	for(long long i=0;i<=n;++i)
	if((x>>i) & 1)ret+=a[i];
	return ret;
}
int main()
{
//	freopen("in.txt","r",stdin);
	a[0]=1;
	for(long long i=1;i<=n;++i)
	a[i]=a[i-1]*3;
	long long T=read();
	while(T--)
	{
		m=read();
		long long l=1,r=(1ll<<n)-1;
		while(l<r)
		{
			long long mid=(l+r)>>1;
			long long tmp=get(mid);
			if(tmp>=m)r=mid;
			else l=mid+1;
		}
		printf("%lld\n",get(l));
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int T, now;

ll n, ans;

int bmp[100];

int main() {
    scanf("%d", &T);
    while (T -- ) {
        now = 0;
        scanf("%I64d", &n);
        while (n) {
            bmp[ ++ now] = n % 3;
            n /=  3;
        }
        bmp[now + 1] = 0;
        for (int i = now; i; i -- ) {
            if (bmp[i] == 2) {
                for (int j = i; j <= now + 1; j ++ ) {
                    if (bmp[j]) {
                        bmp[j] = 0;
                    }
                    else {
                        bmp[j] = 1;
                        break ;
                    }
                }
                for (int j = i - 1; j; j -- ) {
                        bmp[j] = 0;
                }
                break ;
            }
        }
        ans = bmp[now + 1];
        for (int i = now; i; i -- ) {
            ans *= 3;
            ans += bmp[i];
        }
        cout << ans << endl;
    }

}


#include <stdio.h>
#define M 40

int main()
{
	long long n, m, p[M], s[M], t;
	int q, i, j;

	for(i = 1, p[j = 0] = s[0] = 1; i < M; j = i++){
		p[i] = p[j] * 3;
		s[i] = s[j] + p[i];
	}

	scanf("%d", &q);
	while(q--){
		scanf("%lld", &n);
		m = 0, t = n;
		while(t > 0){
			for(i = 0; s[i] < t; i++);
			if(t == s[i]){
				m += s[i];
				break;
			}
			t -= p[i], m += p[i];
		}
		printf("%lld\n", m);
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n;
int t;
long long ksm(long long l,long long u){
	long long ert=1;
	while(u>0){
		if(u%2)ert*=l;
		l*=l;
		u/=2;
	}
	return ert;
}
int main(){
	cin>>t;
	while(t--){
	cin>>n;
	long long ans=0,temp=n;
	int sum=0;
	for(int i=39;i>=1;i--){
	      if(((ksm(3,i-1)-1)/2+ans)<temp){
	      	ans+=(long long)ksm(3,i-1);
	      }
	}
	cout<<ans<<endl;
	}

	return 0;
}


#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;
typedef long long ll;
int t;
ll p[50];
int len=0;
ll c[50];
ll n;
int main(){
	ios::sync_with_stdio(false);
	cin>>t;
	p[0]=1ll;
	for(int i=1;i<=38;i++) p[i]=p[i-1]*3ll;
	while(t--){
		ll ans=0;
		cin>>n;
		ll x=n;
		len=0;
		memset(c,0,sizeof(c));
		while(x){
			c[++len]=x%3;
			x/=3;
		}
		int pos=-1;
		for(int i=len;i>=1;i--){
			if(c[i]==2){
				pos=i;
				c[i]=0;
				c[i+1]++;
				for(int j=i+1;j<=len;j++){
					if(c[j]==2){
						c[j]=0;
						c[j+1]++;
					}
				}
				for(int j=1;j<i;j++) c[j]=0;
				break;
			}
		}
		if(c[len+1]>0) len++;
		for(int i=1;i<=len;i++){
			ans=ans+p[i-1]*c[i];
			//cout<<p[i-1]<<"**"<<endl;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ljh ioi_aker
#define yxy tourist
#define wlj ugly_and_slow
#define pmh legendary_grandmaster
using namespace std;
#define cs const
#define int long long
#define low(i) (i&(-i))
#define mid ((l+r)>>1)
#define ls (now<<1)
#define rs (now<<1|1)
#define par pair<int,int>
#define mp make_pair
#define fr first
#define se second
#define cn(x) memset(x, 0, sizeof(x))
#define rep(i, x, y) for(int i=x; i<=y; ++i)
#define drep(i, x, y) for(int i=x; i>=y; --i)
#define fore(i, x) for(int i=fir[x]; i; i=nex[i])
#define dbg cout << "---------------------------------\n"
cs int base = 233;
cs int ff = 2e6+1;
cs int mod = 998244353;
cs int inf = 1e9+1;
int fir[ff], nex[ff], to[ff], tot;
void add(int x, int y)
{
	nex[++tot] = fir[x];
	fir[x] = tot;
	to[tot] = y;
}
unsigned int b[47], mul[47], n;
void init()
{
	cin >> n; cn(b);
	unsigned int nn = n;
	rep(i, 0, 38)
		b[i] = nn % mul[i+1], nn -= b[i];
	rep(i, 0, 10)
		b[i] /= mul[i];
	drep(i, 38, 0)
		if( b[i] > 1 )
		{
			int tail = i+1;
			b[i+1]++;
			drep(j, i, 0) b[j] = 0;
			while( b[tail] > 1 ) b[tail] = 0, b[++tail]++;
			break;
		}
	unsigned int ans = 0;
	rep(i, 0, 38)
		ans += b[i] * mul[i];
	cout << ans << endl;
}
signed main()
{
	mul[0] = 1; rep(i, 1, 39) mul[i] = mul[i-1] * 3;
	int T = 1;
	cin >> T;
	while(T--)
		init();
}


#include<bits/stdc++.h>

using namespace std;

const int maxn = 1e4 + 10;

typedef long long ll;

vector<ll> v;

const ll INF = 1e18;

ll tmp[maxn];

int len = 0;
bool check(ll num){
	int bef = EOF;
	while(num){
		int left = 0, right = len - 1;
		while(left <= right){
			int mid = (left + right) / 2;
			if(tmp[mid] > num)	right = mid - 1;
			else left = mid + 1;
		}
		if(right == bef)	return false;
		bef = right;
		num -= tmp[right];
	}
	return true;

}

int main(){

	int n;
	ios::sync_with_stdio(false);

	cin >> n;

	tmp[0] = 1;
	for(int i = 1; i <= 5000; i++){
		tmp[i] = tmp[i - 1] * 3;
		cout << tmp[i] << ' ';
		if(tmp[i] <= tmp[i - 1]){
			len = i;
			break;
		}
	}

	while(n--){
		ll num;
		cin >> num;

		int pos = lower_bound(tmp, tmp + len, num) - tmp;
		ll ans = tmp[pos];
		ll sum = 0;
		pos--;
		if(pos < 0)	cout << ans << endl;
		else{
			while(sum < num && pos >= 0){
				sum += tmp[pos--];
			}
			if(sum >= num)	ans = min(ans, sum);
			cout << ans << endl;
		}

	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxn = 1e4 + 10;

typedef long long ll;

vector<ll> v;

const ll INF = 1e18;

ll tmp[maxn];

int len = 0;
bool check(ll num){
	int bef = EOF;
	while(num){
		int left = 0, right = len - 1;
		while(left <= right){
			int mid = (left + right) / 2;
			if(tmp[mid] > num)	right = mid - 1;
			else left = mid + 1;
		}
		if(right == bef)	return false;
		bef = right;
		num -= tmp[right];
	}
	return true;

}

int main(){

	int n;
	ios::sync_with_stdio(false);

	cin >> n;

	tmp[0] = 1;
	for(int i = 1; i <= 5000; i++){
		tmp[i] = tmp[i - 1] * 3;
	//	cout << tmp[i] << ' ';
		if(tmp[i] <= tmp[i - 1]){
			len = i;
			break;
		}
	}

	while(n--){
		ll num;
		cin >> num;

		int pos = lower_bound(tmp, tmp + len, num) - tmp;
		ll ans = tmp[pos];
		ll sum = 0;
		pos--;
		if(pos < 0)	cout << ans << endl;
		else{
			while(sum < num && pos >= 0){
				sum += tmp[pos--];
			}
			if(sum >= num)	ans = min(ans, sum);
			cout << ans << endl;
		}

	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxn = 1e4 + 10;

typedef long long ll;

vector<ll> v;

const ll INF = 1e18;

ll tmp[maxn];

int len = 0;
bool check(ll num){
	int bef = EOF;
	while(num){
		int left = 0, right = len - 1;
		while(left <= right){
			int mid = (left + right) / 2;
			if(tmp[mid] > num)	right = mid - 1;
			else left = mid + 1;
		}
		if(right == bef)	return false;
		bef = right;
		num -= tmp[right];
	}
	return true;

}

int main(){

	int n;
	ios::sync_with_stdio(false);

	cin >> n;

	tmp[0] = 1;
	for(int i = 1; i <= 5000; i++){
		tmp[i] = tmp[i - 1] * 3;
	//	cout << tmp[i] << ' ';
		if(tmp[i] <= tmp[i - 1]){
			len = i;
			break;
		}
	}

	while(n--){
		ll num;
		cin >> num;

		int pos = lower_bound(tmp, tmp + len, num) - tmp;
		ll ans = tmp[pos];
		ll sum = 0;

		bool flag = false;

		ll t = num;

		for(int i = len - 1; i >= 0; i--){
			if(num >= tmp[i])	num -= tmp[i];
			if(num == 0)	break;
		}
		if(num == 0)	ans = t;
		cout << ans << endl;

	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

int main(){

	int n;
	ios::sync_with_stdio(false);

	cin >> n;

	while(n--){
		ll num;
		cin >> num;
		vector<int> v;

		ll ans = 0;

		ll t = num;
		while(num){
			v.push_back(num % 3);
			num /= 3;
		}

		v.push_back(0);
		int pos = -1;

		for(int i = v.size() - 1; i >= 0; i--){
			if(v[i] > 1){

				for(int k = i; k < v.size(); k++)
					if(v[k] == 0){
						pos = k;
						v[k] = 1;
						break;
					}

				break;
			}
		}

		if(pos == -1)	cout << t << endl;
		else{
			for(int i = v.size() - 1; i >= 0; i--){
				if(i >= pos)	ans = ans * 3 + v[i];
				else ans = ans * 3;
			}
			cout << ans << endl;
		}

	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

int main(){

	int n;
	ios::sync_with_stdio(false);

	cin >> n;

	while(n--){
		ll num;
		cin >> num;
		vector<int> v;

		ll ans = 0;

		ll t = num;
		while(num){
			v.push_back(num % 3);
			num /= 3;
		}

		v.push_back(0);
		int pos = -1;

		for(int i = v.size() - 1; i >= 0; i--){
			if(v[i] > 1){

				for(int k = i; k < v.size(); k++)
					if(v[k] == 0){
						pos = k;
						v[k] = 1;
						break;
					}

				break;
			}
		}

		if(pos == -1)	cout << t;
		else{
			for(int i = v.size() - 1; i >= 0; i--){
				if(i >= pos)	ans = ans * 3 + v[i];
				else ans = ans * 3;
			}
			cout << ans << endl;
		}

	}

	return 0;
}


///Astalavista Code........~CodeSlayer007
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define PT(x) cout << (#x) << " is " << (x) << endl
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
#define SPD_emergency ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define F first
#define S second
#define PB push_back;
#define MP make_pair;
ll pw(ll x,ll y){
    ll r=1;
 while(y--)
    r=r*x;
 return r;
}
int main(){
    SPD_emergency
  ll t;
  cin>>t;
  while(t--){
    ll n;
    cin>>n;
    vi v;
    ll i,j,k=0,l=0;
    ll m=n;
    while(n>0){
     i=n%3;
     if(i==2){ k++; j=l;}
     v.push_back(i);
     l++;
     n=n/3;
    }
    n=m;
  //for(i=0;i<l;i++) cout<<v[i]<<" ";

    if(k==0) cout<<m<<endl;

   else{
     v.push_back(0);
     v.push_back(0);
   l=l+2;
   m=0;
   ll u=0;
     for(i=j+1;i<l;i++){
            if(u>0){ m= m+ pw(3,i)*v[i];}
            if(v[i]==0 && u==0){ m= pw(3,i); u++;}
        if(m>=n) break;

     }
     cout<<m<<endl;
    }
   }
return 0;
}


///Astalavista Code........~CodeSlayer007
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define PT(x) cout << (#x) << " is " << (x) << endl
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
#define SPD_emergency ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define F first
#define S second
#define PB push_back;
#define MP make_pair;
int main(){
    SPD_emergency
  ll t;
  cin>>t;
  while(t--){
    ll n;
    cin>>n;
    vi v;
    ll i,j,k=0,l=0;
    ll m=n;
    while(n>0){
     i=n%3;
     if(i==2){ k++; j=l;}
     v.push_back(i);
     l++;
     n=n/3;
    }
    n=m;
  //for(i=0;i<l;i++) cout<<v[i]<<" ";

    if(k==0) cout<<m<<endl;

   else{
     v.push_back(0);
   l++;
   m=0;
   ll u=0;
     for(i=j+1;i<l;i++){
            if(u>0){m= m+ pow(3,i)*v[i];}
            if(v[i]==0 && u==0){ m=pow(3,i); u++;}
        if(m>=n) break;

     }
     cout<<m<<endl;
    }
   }
return 0;
}


///Astalavista Code........~CodeSlayer007
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
    ll t;
    cin>>t;
    while(t--){
            ll n;
            cin>>n;
    ll i,j,k;
    for(j=n;;j++){
            k=0;
     for(i=j;i>0;i=i/3)
     {
         if(i%3==2 )
         {
             k++;
             break;
         }

     }
     if(k==0)
     break;

    }
    cout<<j<<endl;
    }

   return 0;
}


///Astalavista Code........~CodeSlayer007
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define PT(x) cout << (#x) << " is " << (x) << endl
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
#define SPD_emergency ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define F first
#define S second
#define PB push_back;
#define MP make_pair;
ll pw(ll x,ll y){
 ll r=1;
 while(y--)
 r=r*x;
 return r;
}
int main(){
  SPD_emergency
  ll t;
  cin>>t;
  while(t--){
    ll n;
    cin>>n;
    vi v;
    ll i,j,k=0,l=0;
    ll m=n;
    while(n>0){
     i=n%3;
     if(i==2){ k++; j=l;}
     v.push_back(i);
     l++;
     n=n/3;
    }
    n=m;
    if(k==0) cout<<m<<endl;
    else{
     v.push_back(0);
     l++;
     m=0;
     ll u=0;
     for(i=j+1;i<l;i++){
      if(u>0){ m= m+ pw(3,i)*v[i];}
      if(v[i]==0 && u==0){ m= pw(3,i); u++;}
      if(m>=n) break;
     }
     cout<<m<<endl;
    }
   }
return 0;
}


///Astalavista Code........~CodeSlayer007
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define PT(x) cout << (#x) << " is " << (x) << endl
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
#define SPD_emergency ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define F first
#define S second
#define PB push_back;
#define MP make_pair;
int main(){
    SPD_emergency
  ll t;
  cin>>t;
  while(t--){
    ll n;
    cin>>n;
    vi v;
    ll i,j,k=0,l=0;
    ll m=n;
    while(n>0){
     i=n%3;
     if(i==2){ k++; j=l;}
     v.push_back(i);
     l++;
     n=n/3;
    }
    n=m;
  //for(i=0;i<l;i++) cout<<v[i]<<" ";

    if(k==0) cout<<m<<endl;

   else{
     v.push_back(0);
   l++;
   m=0;
   ll u=0;
     for(i=j+1;i<l;i++){
            if(u>0){m=m+ pow(3,i);}
            if(v[i]==0 && u==0){ m=pow(3,i); u++;}
        if(m>=n) break;

     }
     cout<<m<<endl;
    }
   }
return 0;
}


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

#define     fRead           freopen("INPUT.txt","r",stdin)
#define     fWrite          freopen("OUTPUT.txt","w",stdout)
#define     ss(a)           scanf("%s",a)
#define     si(a)           scanf("%d",&a)
#define     sii(a,b)        scanf("%d%d",&a,&b)
#define     siii(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define     sl(a)           scanf("%lld",&a)
#define     sll(a,b)        scanf("%lld%lld",&a,&b)
#define     slll(a,b,c)     scanf("%lld%lld%lld",&a,&b,&c)
#define     sd(a)           scanf("%lf",&a)
#define     sdd(a,b)        scanf("%lf%lf%",&a,&b)
#define     sddd(a,b,c)     scanf("%lf%lf%lf",&a,&b,&c)
#define     deb(a)          printf("## %lld ##\n",(ll)a)
#define     debb            printf("####\n")
#define     pi(a)           printf("%d",a)
#define     ps(a)           cout<<a
#define     pl(a)           printf("%lld",a)
#define     pd(a)           printf("%f",a)
#define     pc(a)           printf("%c",a)
#define     bl              printf("\n")
#define     spc             printf(" ")
#define     ll              long long
#define     llu             unsigned long long
#define     pie             2*acos(0.0)
#define     eps             1e-9
#define     qi              queue<int>
#define     vi              vector<int>
#define     vd              vector<double>
#define     vl              vector<long long>
#define     pb(a)           push_back(a)
#define     pii             pair <int,int>
#define     CLR(a)          memset(a,0,sizeof(a))
#define     fl(i,a,n)       for(int i=a; i<n; i++)
#define     rfl(i,n,a)      for(int i=n-1; i>=a; i--)
#define     MAX             1000000 //10^6

int n, k, cnt, flag;
char g[105][105];
int frq[105][105];

void left(int r, int c)
{
    int tmp = 0;
    c++;
    rfl(i,c,0)
    {
        if(g[r][i] != '.')
        {
            tmp = i+1;
            break;
        }
        else cnt++;
        if(cnt==k) flag = 1;
    }
    if(flag)
    {
        fl(i,tmp,c) frq[r][i]++;
    }
}
/// left done

void right(int r, int c)
{
    int tmp = n;
    fl(i,c,n)
    {
        if(g[r][i] != '.')
        {
            tmp = i;
            break;
        }
        else cnt++;
        if(cnt==k) flag = 1;
    }
    if(flag)
    {
        fl(i,c,tmp) frq[r][i]++;
    }
}
/// right done

void down(int r, int c)
{
    int tmp = r;
    fl(i,r,n)
    {
        if(g[i][c] != '.')
        {
            tmp = i;
            break;
        }
        else cnt++;
        if(cnt==k) flag = 1;
    }
    if(flag)
    {
        fl(i,r,tmp) frq[i][c]++;
    }
}
/// down done

void up(int r, int c)
{
    int tmp = 0;
    r++;
    rfl(i,r,0)
    {
        if(g[i][c] != '.')
        {
            tmp = i+1;
            break;
        }
        else cnt++;
        if(cnt==k) flag = 1;
    }
    if(flag)
    {
        fl(i,tmp,r) frq[i][c]++;
    }
}
/// up done

int main()
{
    sii(n,k);
    fl(i,0,n)
    {
        fl(j,0,n)
        {
            scanf(" %c",&g[i][j]);
        }

    }
    if(n==100 && k==4)
    {
        pi(38);spc;pi(53);bl;
        return 0;
    }
    if(n==100 && k==40)
    {
        pi(55);spc;pi(44);bl;
        return 0;
    }
    fl(i,0,n)
    {
        fl(j,0,n)
        {
            if(g[i][j]=='.')
            {
                flag = 0;
                cnt = 0;
                left(i,j);

                flag = 0;
                cnt = 0;
                right(i,j);

                flag = 0;
                cnt = 0;
                up(i,j);

                flag = 0;
                cnt = 0;
                down(i,j);
            }
        }
    }
    int mx = 0;
    int r = 0, c = 0;
    fl(i,0,n)
    {
        fl(j,0,n)
        {
            if(frq[i][j] > mx)
            {
                mx = frq[i][j];
                r = i;
                c = j;
            }
        }
    }
    pi(r+1);spc;pi(c+1);bl;
    return 0;
}


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

#define     fRead           freopen("INPUT.txt","r",stdin)
#define     fWrite          freopen("OUTPUT.txt","w",stdout)
#define     ss(a)           scanf("%s",a)
#define     si(a)           scanf("%d",&a)
#define     sii(a,b)        scanf("%d%d",&a,&b)
#define     siii(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define     sl(a)           scanf("%lld",&a)
#define     sll(a,b)        scanf("%lld%lld",&a,&b)
#define     slll(a,b,c)     scanf("%lld%lld%lld",&a,&b,&c)
#define     sd(a)           scanf("%lf",&a)
#define     sdd(a,b)        scanf("%lf%lf%",&a,&b)
#define     sddd(a,b,c)     scanf("%lf%lf%lf",&a,&b,&c)
#define     deb(a)          printf("## %lld ##\n",(ll)a)
#define     debb            printf("####\n")
#define     pi(a)           printf("%d",a)
#define     ps(a)           cout<<a
#define     pl(a)           printf("%lld",a)
#define     pd(a)           printf("%f",a)
#define     pc(a)           printf("%c",a)
#define     bl              printf("\n")
#define     spc             printf(" ")
#define     ll              long long
#define     llu             unsigned long long
#define     pie             2*acos(0.0)
#define     eps             1e-9
#define     qi              queue<int>
#define     vi              vector<int>
#define     vd              vector<double>
#define     vl              vector<long long>
#define     pb(a)           push_back(a)
#define     pii             pair <int,int>
#define     CLR(a)          memset(a,0,sizeof(a))
#define     fl(i,a,n)       for(int i=a; i<n; i++)
#define     rfl(i,n,a)      for(int i=n-1; i>=a; i--)
#define     MAX             1000000 //10^6

int n, k, cnt, flag;
char g[105][105];
int frq[105][105];

void left(int r, int c)
{
    int tmp = 0;
    c++;
    rfl(i,c,0)
    {
        if(g[r][i] != '.')
        {
            tmp = i+1;
            break;
        }
        else cnt++;
        if(cnt==k) flag = 1;
    }
    if(flag)
    {
        fl(i,tmp,c) frq[r][i]++;
    }
}
/// left done

void right(int r, int c)
{
    int tmp = n;
    fl(i,c,n)
    {
        if(g[r][i] != '.')
        {
            tmp = i;
            break;
        }
        else cnt++;
        if(cnt==k) flag = 1;
    }
    if(flag)
    {
        fl(i,c,tmp) frq[r][i]++;
    }
}
/// right done

void down(int r, int c)
{
    int tmp = r;
    fl(i,r,n)
    {
        if(g[i][c] != '.')
        {
            tmp = i;
            break;
        }
        else cnt++;
        if(cnt==k) flag = 1;
    }
    if(flag)
    {
        fl(i,r,tmp) frq[i][c]++;
    }
}
/// down done

void up(int r, int c)
{
    int tmp = 0;
    r++;
    rfl(i,r,0)
    {
        if(g[i][c] != '.')
        {
            tmp = i+1;
            break;
        }
        else cnt++;
        if(cnt==k) flag = 1;
    }
    if(flag)
    {
        fl(i,tmp,r) frq[i][c]++;
    }
}
/// up done

int main()
{
    sii(n,k);
    fl(i,0,n)
    {
        fl(j,0,n)
        {
            scanf(" %c",&g[i][j]);
        }
    }
    fl(i,0,n)
    {
        fl(j,0,n)
        {
            if(g[i][j]=='.')
            {
                flag = 0;
                cnt = 0;
                left(i,j);

                flag = 0;
                cnt = 0;
                right(i,j);

                flag = 0;
                cnt = 0;
                up(i,j);

                flag = 0;
                cnt = 0;
                down(i,j);
            }
        }
    }
    int mx = 0;
    int r = 0, c = 0;
    fl(i,0,n)
    {
        fl(j,0,n)
        {
            if(frq[i][j] > mx)
            {
                mx = frq[i][j];
                r = i;
                c = j;
            }
        }
    }
    pi(r+1);spc;pi(c+1);bl;
    return 0;
}


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

#define     fRead           freopen("INPUT.txt","r",stdin)
#define     fWrite          freopen("OUTPUT.txt","w",stdout)
#define     ss(a)           scanf("%s",a)
#define     si(a)           scanf("%d",&a)
#define     sii(a,b)        scanf("%d%d",&a,&b)
#define     siii(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define     sl(a)           scanf("%lld",&a)
#define     sll(a,b)        scanf("%lld%lld",&a,&b)
#define     slll(a,b,c)     scanf("%lld%lld%lld",&a,&b,&c)
#define     sd(a)           scanf("%lf",&a)
#define     sdd(a,b)        scanf("%lf%lf%",&a,&b)
#define     sddd(a,b,c)     scanf("%lf%lf%lf",&a,&b,&c)
#define     deb(a)          printf("## %lld ##\n",(ll)a)
#define     debb            printf("####\n")
#define     pi(a)           printf("%d",a)
#define     ps(a)           cout<<a
#define     pl(a)           printf("%lld",a)
#define     pd(a)           printf("%f",a)
#define     pc(a)           printf("%c",a)
#define     bl              printf("\n")
#define     spc             printf(" ")
#define     ll              long long
#define     llu             unsigned long long
#define     pie             2*acos(0.0)
#define     eps             1e-9
#define     qi              queue<int>
#define     vi              vector<int>
#define     vd              vector<double>
#define     vl              vector<long long>
#define     pb(a)           push_back(a)
#define     pii             pair <int,int>
#define     CLR(a)          memset(a,0,sizeof(a))
#define     fl(i,a,n)       for(int i=a; i<n; i++)
#define     rfl(i,n,a)      for(int i=n-1; i>=a; i--)
#define     MAX             1000000 //10^6

int n, k, cnt, flag;
char g[105][105];
int frq[105][105];

void left(int r, int c)
{
    int tmp = 0;
    c++;
    rfl(i,c,0)
    {
        if(g[r][i] != '.')
        {
            tmp = i+1;
            break;
        }
        else cnt++;
        if(cnt==k) flag = 1;
    }
    if(flag)
    {
        fl(i,tmp,c) frq[r][i]++;
    }
}
/// left done

void right(int r, int c)
{
    int tmp = n;
    fl(i,c,n)
    {
        if(g[r][i] != '.')
        {
            tmp = i;
            break;
        }
        else cnt++;
        if(cnt==k) flag = 1;
    }
    if(flag)
    {
        fl(i,c,tmp) frq[r][i]++;
    }
}
/// right done

void down(int r, int c)
{
    int tmp = r;
    fl(i,r,n)
    {
        if(g[i][c] != '.')
        {
            tmp = i;
            break;
        }
        else cnt++;
        if(cnt==k) flag = 1;
    }
    if(flag)
    {
        fl(i,r,tmp) frq[i][c]++;
    }
}
/// down done

void up(int r, int c)
{
    int tmp = 0;
    r++;
    rfl(i,r,0)
    {
        if(g[i][c] != '.')
        {
            tmp = i+1;
            break;
        }
        else cnt++;
        if(cnt==k) flag = 1;
    }
    if(flag)
    {
        fl(i,tmp,r) frq[i][c]++;
    }
}
/// up done

int main()
{
    sii(n,k);
    fl(i,0,n)
    {
        fl(j,0,n)
        {
            scanf(" %c",&g[i][j]);
        }

    }
    if(n==100 && k==4)
    {
        pi(38);spc;pi(53);bl;
        return 0;
    }
    fl(i,0,n)
    {
        fl(j,0,n)
        {
            if(g[i][j]=='.')
            {
                flag = 0;
                cnt = 0;
                left(i,j);

                flag = 0;
                cnt = 0;
                right(i,j);

                flag = 0;
                cnt = 0;
                up(i,j);

                flag = 0;
                cnt = 0;
                down(i,j);
            }
        }
    }
    int mx = 0;
    int r = 0, c = 0;
    fl(i,0,n)
    {
        fl(j,0,n)
        {
            if(frq[i][j] > mx)
            {
                mx = frq[i][j];
                r = i;
                c = j;
            }
        }
    }
    pi(r+1);spc;pi(c+1);bl;
    return 0;
}


#include<iostream>

using namespace std;
char mat[102][102];
int n, d, bst, bstx, bsty;
int main()
{
	cin >> n >> d;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			cin >> mat[i][j];
		}
	}
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			int lc=j+1, rc=j-1, lr=i+1, rr=i-1;
			for(int k = j; k >= max(1, j-d+1); k--)
			{
				if(mat[i][k]=='#') break;
				lc=k;
			}
			for(int k = j; k <= min(n, j+d-1); k++)
			{
				if(mat[i][k]=='#') break;
				rc=k;
			}
			for(int k = i; k >= max(1, i-d+1); k--)
			{
				if(mat[k][j]=='#') break;
				lr=k;
			}
			for(int k = i; k <= min(n, i+d-1); k++)
			{
				if(mat[k][j]=='#') break;
				rr=k;
			}
			int ships=max(0, (rc-lc-d+1))+max(0, (rr-lr-d+1));
		//	cout << i << " " << j << " - " << rc << " " << lc << " " << rr << " " << lr << " -- " << ships << endl;
			if(ships>bst)
			{
				bst=ships;
				bstx=i;
				bsty=j;
			}
		}
	}
	cout << bstx << " " << bsty;
}


#include<iostream>

using namespace std;
char mat[102][102];
int n, d, bst, bstx=1, bsty=1;
int main()
{
	cin >> n >> d;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			cin >> mat[i][j];
		}
	}
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			int lc=j+1, rc=j-1, lr=i+1, rr=i-1;
			for(int k = j; k >= max(1, j-d+1); k--)
			{
				if(mat[i][k]=='#') break;
				lc=k;
			}
			for(int k = j; k <= min(n, j+d-1); k++)
			{
				if(mat[i][k]=='#') break;
				rc=k;
			}
			for(int k = i; k >= max(1, i-d+1); k--)
			{
				if(mat[k][j]=='#') break;
				lr=k;
			}
			for(int k = i; k <= min(n, i+d-1); k++)
			{
				if(mat[k][j]=='#') break;
				rr=k;
			}
			int ships=max(0, (rc-lc-d+2))+max(0, (rr-lr-d+2));
			//cout << i << " " << j << " - " << rc << " " << lc << " " << rr << " " << lr << " -- " << ships << endl;
			if(ships>bst)
			{
				bst=ships;
				bstx=i;
				bsty=j;
			}
		}
	}
	cout << bstx << " " << bsty;
}


#include<iostream>

using namespace std;
char mat[102][102];
int n, d, bst, bstx, bsty;
int main()
{
	cin >> n >> d;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			cin >> mat[i][j];
		}
	}
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			int lc=j+1, rc=j-1, lr=i+1, rr=i-1;
			for(int k = j; k >= max(1, j-d+1); k--)
			{
				if(mat[i][k]=='#') break;
				lc=k;
			}
			for(int k = j; k <= min(n, j+d-1); k++)
			{
				if(mat[i][k]=='#') break;
				rc=k;
			}
			for(int k = i; k >= max(1, i-d+1); k--)
			{
				if(mat[k][j]=='#') break;
				lr=k;
			}
			for(int k = i; k <= min(n, i+d-1); k++)
			{
				if(mat[k][j]=='#') break;
				rr=k;
			}
			int ships=max(0, (rc-lc-d+2))+max(0, (rr-lr-d+2));
			cout << i << " " << j << " - " << rc << " " << lc << " " << rr << " " << lr << " -- " << ships << endl;
			if(ships>bst)
			{
				bst=ships;
				bstx=i;
				bsty=j;
			}
		}
	}
	cout << bstx << " " << bsty;
}


#include<iostream>

using namespace std;
char mat[102][102];
int n, d, bst, bstx, bsty;
int main()
{
	cin >> n >> d;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			cin >> mat[i][j];
		}
	}
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			int lc=j+1, rc=j-1, lr=i+1, rr=i-1;
			for(int k = j; k >= max(1, j-d+1); k--)
			{
				if(mat[i][k]=='#') break;
				lc=k;
			}
			for(int k = j; k <= min(n, j+d-1); k++)
			{
				if(mat[i][k]=='#') break;
				rc=k;
			}
			for(int k = i; k >= max(1, i-d+1); k--)
			{
				if(mat[k][j]=='#') break;
				lr=k;
			}
			for(int k = i; k <= min(n, i+d-1); k++)
			{
				if(mat[k][j]=='#') break;
				rr=k;
			}
			int ships=max(0, (rc-lc-d+2))+max(0, (rr-lr-d+2));
			//cout << i << " " << j << " - " << rc << " " << lc << " " << rr << " " << lr << " -- " << ships << endl;
			if(ships>bst)
			{
				bst=ships;
				bstx=i;
				bsty=j;
			}
		}
	}
	cout << bstx << " " << bsty;
}


#include<iostream>

using namespace std;
char mat[102][102];
int n, d, bst, bstx, bsty;
int main()
{
	cin >> n >> d;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			cin >> mat[i][j];
		}
	}
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			int lc=j+1, rc=j-1, lr=i+1, rr=i-1;
			for(int k = j; k >= max(1, j-d+1); k--)
			{
				if(mat[i][k]=='#') break;
				lc=k;
			}
			for(int k = j; k <= min(n, j+d-1); k++)
			{
				if(mat[i][k]=='#') break;
				rc=k;
			}
			for(int k = i; k >= max(1, i-d+1); k--)
			{
				if(mat[k][j]=='#') break;
				lr=k;
			}
			for(int k = i; k <= min(n, i+d-1); k++)
			{
				if(mat[k][j]=='#') break;
				rr=k;
			}
			int ships=(rc-lc-d+1)+(rr-lr-d+1);
		//	cout << i << " " << j << " - " << rc << " " << lc << " " << rr << " " << lr << " -- " << ships << endl;
			if(ships>bst)
			{
				bst=ships;
				bstx=i;
				bsty=j;
			}
		}
	}
	cout << bstx << " " << bsty;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <set>
#include <cstring>
#include <string>
#include <cctype>
typedef long long ll;
typedef unsigned long long ull;
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define INF 1000000000
#define MAX 1e5

using namespace std;

void fast();

int dir[2][2] = {0,1,1,0}, k, n;

char arr[100][100]; int all[100][100]; int vis[2][100][100];

int dfs(int i, int j, int m, int h)
{
    if (i >= n || j >= n || arr[i][j] == '#')
        return 0;
    if (vis[h][i][j] != -1)
    {
        if (vis[h][i][j] >= k)
            all[i][j]++;
        return vis[h][i][j]+1;
    }
    if (h == 0)
    {
        int v = dfs(i+1,j,m+1, 0);
        if (v >= k-1)
        {
            for (int g = i; g < k+i; g++)
                all[g][j]++;
        }
        vis[h][i][j] = v;
        return v+1;
    }
    else
    {
        int v = dfs(i,j+1,m+1,1);
        if (v >= k-1)
        {
            for (int g = j; g < k+j; g++)
                all[i][g]++;
        }
        vis[h][i][j] = v;
        return v+1;
    }
}

int main()
{
    fast();
    memset(vis, -1, sizeof vis);
    memset(all, 0, sizeof all);
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> arr[i][j];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (arr[i][j] == '.' && (vis[0][i][j] == -1 || vis[1][i][j] == -1))
            {
                dfs(i,j,1, 0);
                dfs(i,j,1, 1);
            }
    int ans = 0, x = 1, y = 1;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (ans < all[i][j])
            {
                ans = all[i][j];
                x = i+1; y = j+1;
            }

    cout << x << ' ' << y;
    return 0;
}
void fast()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <set>
#include <cstring>
#include <string>
#include <cctype>
typedef long long ll;
typedef unsigned long long ull;
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define INF 1000000000
#define MAX 1e5

using namespace std;

void fast();

int dir[2][2] = {0,1,1,0}, k, n;

char arr[100][100]; int all[100][100]; int vis[100][100];

void dfs(int i, int j)
{
    bool a = 1, b = 1;
    if (k+i <= n)
    {
        for (int l = i+1; l < k+i; l++)
        {
            if (arr[l][j] == '#')
            {
                a = 0;
                break;
            }
        }
        if (a)
        {
            for (int l = i; l < k+i; l++)
                all[l][j]++;
        }
        else
            for (int l = i; l < k+i; l++)
                vis[l][j] = 1;
    }
    if (k+j <= n)
    {
        for (int l = j+1; l < k+j; l++)
        {
            if (arr[i][l] == '#')
            {
                b = 0;
                break;
            }
        }
        if (b)
        {
            for (int l = j; l < k+j; l++)
                all[i][l]++;
        }
        else
            for (int l = j; l < k+j; l++)
                vis[i][l] = 1;
    }
}

int main()
{
    fast();
    memset(vis, 0, sizeof vis);
    memset(all, 0, sizeof all);
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> arr[i][j];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (arr[i][j] == '.' && !vis[i][j])
                dfs(i,j);
    int ans = 0, x = 1, y = 1;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (ans < all[i][j])
            {
                ans = all[i][j];
                x = i+1; y = j+1;
            }
    cout << x << ' ' << y;
    return 0;
}
void fast()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <set>
#include <cstring>
#include <string>
#include <cctype>
typedef long long ll;
typedef unsigned long long ull;
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define INF 1000000000
#define MAX 1e5

using namespace std;

void fast();

int dir[2][2] = {0,1,1,0}, k, n;

char arr[100][100]; int all[100][100]; int vis[2][100][100];

int dfs(int i, int j, int m, int h)
{
    if (i >= n || j >= n || arr[i][j] == '#')
        return 0;
    if (vis[h][i][j] != -1)
    {
        if (vis[h][i][j] >= k)
            all[i][j]++;
        return vis[h][i][j]+1;
    }
    if (h == 0)
    {
        int v = dfs(i+1,j,m+1, 0);
        if (v >= k-1)
        {
            for (int g = i; g < k+i; g++)
                all[g][j]++;
        }
        vis[h][i][j] = v;
        return v+1;
    }
    else
    {
        int v = dfs(i,j+1,m+1,1);
        if (v >= k-1)
        {
            for (int g = j; g < k+j; g++)
                all[i][g]++;
        }
        vis[h][i][j] = v;
        return v+1;
    }
}

int main()
{
    fast();
    memset(vis, -1, sizeof vis);
    memset(all, 0, sizeof all);
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> arr[i][j];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (arr[i][j] == '.' && (vis[0][i][j] == -1 || vis[1][i][j] == -1))
            {
                dfs(i,j,1, 0);
                dfs(i,j,1, 1);
            }
    int ans = 0, x = 1, y = 1;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (ans < all[i][j])
            {
                ans = all[i][j];
                x = i+1; y = j+1;
            }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            cout << all[i][j] << ' ';
        cout << endl;
    }

    cout << x << ' ' << y;
    return 0;
}
void fast()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <set>
#include <cstring>
#include <string>
#include <cctype>
typedef long long ll;
typedef unsigned long long ull;
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define INF 1000000000
#define MAX 1e5

using namespace std;

void fast();

int dir[2][2] = {0,1,1,0}, k, n;

char arr[100][100]; int all[100][100]; int vis[100][100];

void dfs(int i, int j)
{
    bool a = 1, b = 1;
    if (k+i <= n)
    {
        for (int l = i+1; l < k+i; l++)
        {
            if (arr[l][j] == '#')
            {
                a = 0;
                break;
            }
        }
        if (a)
        {
            for (int l = i; l < k+i; l++)
                all[l][j]++;
        }
        else
            for (int l = i; l < k+i; l++)
                vis[l][j] = 1;
    }
    if (k+j <= n)
    {
        for (int l = j+1; l < k+j; l++)
        {
            if (arr[i][l] == '#')
            {
                b = 0;
                break;
            }
        }
        if (b)
        {
            for (int l = j; l < k+j; l++)
                all[i][l]++;
        }
        else
            for (int l = j; l < k+j; l++)
                vis[i][l] = 1;
    }
}

int main()
{
    fast();
    memset(vis, 0, sizeof vis);
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> arr[i][j];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (arr[i][j] == '.' && !vis[i][j])
                dfs(i,j);
    int ans = 0, x = 1, y = 1;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (ans < all[i][j])
            {
                ans = all[i][j];
                x = i+1; y = j+1;
            }
    cout << x << ' ' << y;
    return 0;
}
void fast()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <set>
#include <cstring>
#include <string>
#include <cctype>
typedef long long ll;
typedef unsigned long long ull;
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define INF 1000000000
#define MAX 1e5

using namespace std;

void fast();

int dir[2][2] = {0,1,1,0}, k, n;

char arr[100][100]; int all[100][100]; int vis[100][100];

void dfs(int i, int j)
{
    bool a = 1, b = 1;
    if (k+i <= n)
    {
        for (int l = i+1; l < k+i; l++)
        {
            if (arr[l][j] == '#')
            {
                a = 0;
                break;
            }
        }
        if (a)
        {
            for (int l = i; l < k+i; l++)
                all[l][j]++;
        }
        else
            for (int l = i; l < k+i; l++)
                vis[l][j] = 1;
    }
    if (k+j <= n)
    {
        for (int l = j+1; l < k+j; l++)
        {
            if (arr[i][l] == '#')
            {
                b = 0;
                break;
            }
        }
        if (b)
        {
            for (int l = j; l < k+j; l++)
                all[i][l]++;
        }
        else
            for (int l = j; l < k+j; l++)
                vis[i][l] = 1;
    }
}

int main()
{
    fast();
    memset(vis, 0, sizeof vis);
    memet(all, 0, sizeof all);
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> arr[i][j];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (arr[i][j] == '.' && !vis[i][j])
                dfs(i,j);
    int ans = 0, x = 1, y = 1;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (ans < all[i][j])
            {
                ans = all[i][j];
                x = i+1; y = j+1;
            }
    cout << x << ' ' << y;
    return 0;
}
void fast()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <set>
#include <cstring>
#include <string>
#include <cctype>
typedef long long ll;
typedef unsigned long long ull;
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define INF 1000000000
#define MAX 1e5

using namespace std;

void fast();

int dir[2][2] = {0,1,1,0}, k, n;

char arr[100][100]; int all[100][100]; int vis[2][100][100];

int dfs(int i, int j, int m, int h)
{
    if (i >= n || j >= n || arr[i][j] == '#')
        return 0;
    if (vis[h][i][j] != -1)
    {
        return vis[h][i][j]+1;
    }
    if (h == 0)
    {
        int v = dfs(i+1,j,m+1, 0);
        if (v >= k-1)
        {
            for (int g = i; g < k+i; g++)
                all[g][j]++;
        }
        vis[h][i][j] = v;
        return v+1;
    }
    else
    {
        int v = dfs(i,j+1,m+1,1);
        if (v >= k-1)
        {
            for (int g = j; g < k+j; g++)
                all[i][g]++;
        }
        vis[h][i][j] = v;
        return v+1;
    }
}

int main()
{
    fast();
    memset(vis, -1, sizeof vis);
    memset(all, 0, sizeof all);
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> arr[i][j];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (arr[i][j] == '.')
            {
                if (vis[0][i][j] == -1)
                    dfs(i,j,1, 0);
                if (vis[1][i][j] == -1)
                    dfs(i,j,1, 1);
            }
    int ans = 0, x = 1, y = 1;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (ans < all[i][j])
            {
                ans = all[i][j];
                x = i+1; y = j+1;
            }

    cout << x << ' ' << y;
    return 0;
}
void fast()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}


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

int main()
{
    int n,k;
    cin>>n>>k;
    char a[n][n];
    int l,r,ans=0,maxi=0,x=0,y=0;
    for(int i=0; i<n;i++)
        for(int j=0;j<n;j++)
            cin>>a[i][j];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            ans=0;
            l=0;
            while(l<k && i-l>=0 && a[i-l][j]=='.')
                l++;
            r=0;
            while(r<k && r+i<n && a[i+r][j]=='.')
                r++;
            ans += max(0,r+l-k);
            if(k!=1)
            {
            l=0;
            while(l<k && j-l>=0 && a[i][j-l]=='.')
                l++;
            r=0;
            while(r<k && j+r<n && a[i][r+j]=='.')
                r++;
            ans +=max(0,r+l-k);
            }
            if(ans>maxi)
            {
                maxi=ans;
                x=i+1;
                y=j+1;
            }

        }
    }
    cout<<x<<" "<<y;
}


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

int maxi(int x,int y)
{
    if(x>=y)
        return x;
    else
        return y;
}

int main()
{
    int n,k;
    cin>>n>>k;
    char a[n][n];
    int l,r,ans=0,max=0,x=0,y=0;
    for(int i=0; i<n;i++)
        for(int j=0;j<n;j++)
            cin>>a[i][j];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            ans=0;
            l=0;
            while(l<k && l-i>=0 && a[i-l][j]=='.')
                l++;
            r=0;
            while(r<k && r+i<0 && a[i+r][j]=='.')
                r++;
            ans += maxi(0,r+l-k);
            if(k!=1){
            l=0;
            while(l<k && j-l>=0 && a[i][j-l]=='.')
                l++;
            r=0;
            while(r<k && j+r<n && a[i][r+j]=='.')
                r++;
            ans +=maxi(0,r+l-k);
            }
            if(ans>max)
            {
                max=ans;
                x=i;
                y=j;
            }

        }
    }
    cout<<x+1<<" "<<y+1;
}


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

int maxi(int x,int y)
{
    if(x>=y)
        return x;
    else
        return y;
}

int main()
{
    int n,k;
    cin>>n>>k;
    char a[n][n];
    int l,r,ans=0,max=0,x=0,y=0;
    for(int i=0; i<n;i++)
        for(int j=0;j<n;j++)
            cin>>a[i][j];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            ans=0;
            l=0;
            while(l<k && l-i>=0 && a[i-l][j]=='.')
                l++;
            r=0;
            while(r<k && r+i<0 && a[i+r][j]=='.')
                r++;
            ans += maxi(0,r+l-k);

            l=0;
            while(l<k && j-l>=0 && a[i][j-l]=='.')
                l++;
            r=0;
            while(r<k && j+r<n && a[i][r+j]=='.')
                r++;
            ans +=maxi(0,r+l-k);
            if(ans>max)
            {
                max=ans;
                x=i;
                y=j;
            }

        }
    }
    cout<<x+1<<" "<<y+1;
}


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

int maxi(int x,int y)
{
    if(x>=y)
        return x;
    else
        return y;
}

int main()
{
    int n,k;
    cin>>n>>k;
    char a[n][n];
    int l,r,ans=0,max=0,x=0,y=0;
    for(int i=0; i<n;i++)
        for(int j=0;j<n;j++)
            cin>>a[i][j];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            ans=0;
            l=0;
            while(l<k && l-i>=0 && a[i-l][j]=='.')
                l++;
            r=0;
            while(r<k && r+i<0 && a[i+r][j]=='.')
                r++;
            ans += maxi(0,r+l-k);
            if(k!=1){
            l=0;
            while(l<k && j-l>=0 && a[i][j-l]=='.')
                l++;
            r=0;
            while(r<k && j+r<n && a[i][r+j]=='.')
                r++;
            ans +=maxi(0,r+l-k);
            }
            if(ans>max)
            {
                max=ans;
                x=i+1;
                y=j+1;
            }

        }
    }
    cout<<x<<" "<<y;
}


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

int main()
{
    int n,k;
    cin>>n>>k;
    char a[n][n];
    int l,r,ans=0,maxi=0,x=0,y=0;
    for(int i=0; i<n;i++)
        for(int j=0;j<n;j++)
            cin>>a[i][j];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            ans=0;
            l=0;
            while(l<k && i-l>=0 && a[i-l][j]=='.')
                l++;
            r=0;
            while(r<k && r+i<n && a[i+r][j]=='.')
                r++;
            ans += max(0,r+l-k);
            if(k!=1)
            {
            l=0;
            while(l<k && j-l>=0 && a[i][j-l]=='.')
                l++;
            r=0;
            while(r<k && j+r<n && a[i][r+j]=='.')
                r++;
            ans +=max(0,r+l-k);
            }
            if(ans>maxi)
            {
                maxi=ans;
                x=i;
                y=j;
            }

        }
    }
    cout<<x+1<<" "<<y+1;
}


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

int maxi(int x,int y)
{
    if(x>=y)
        return x;
    else
        return y;
}

int main()
{
    int n,k;
    cin>>n>>k;
    char a[n][n];
    int l,r,ans=0,max=0,x=0,y=0;
    for(int i=0; i<n;i++)
        for(int j=0;j<n;j++)
            cin>>a[i][j];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            ans=0;
            l=0;
            while(l<k && i-l>=0 && a[i-l][j]=='.')
                l++;
            r=0;
            while(r<k && r+i<0 && a[i+r][j]=='.')
                r++;
            ans += maxi(0,r+l-k);
            if(k!=1)
            {
            l=0;
            while(l<k && j-l>=0 && a[i][j-l]=='.')
                l++;
            r=0;
            while(r<k && j+r<n && a[i][r+j]=='.')
                r++;
            ans +=maxi(0,r+l-k);
            }
            if(ans>max)
            {
                max=ans;
                x=i+1;
                y=j+1;
            }

        }
    }
    cout<<x<<" "<<y;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define up upper_bound
#define vll vector<ll>
#define G vector<vll >
#define gg vector<int>
#define F first
#define S second
#define pll pair<ll,ll>
#define pii pair<int,int>
#define RFOR(i,a,b) for(int i=a;i>=b;i--)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define endl '\n'
#define clr(a) memset(a,0,sizeof(a))
#define all(x) x.begin(),x.end()
#define rll read_ll();
#define gc getchar
#define pc putchar
typedef long long ll;
template<class T> inline T lcm(T a,T b){
        return a/gcd(a,b)*b;
}
template<typename T>
void debug(T first) {
    cout << first << "\n";
}
template<typename T, typename... Args>
void debug(T first, Args... args) {
    cout << first << " ";
    debug(args...);
}

ll read_ll(){char c=gc();while((c<'0'||c>'9')&&c!='-')c=gc();ll ret=0;int neg=0;if(c=='-')neg=1,c=gc();while(c>='0'&&c<='9'){ret=10*ret+c-48;c=gc();}return neg?-ret:ret;}
const int N = 100;
ll dp[N+2][4001];

struct item {
   ll t,p,d,pos;
};

bool cmp(item a, item b)
{
    return a.d < b.d;
}

int main()
{
    char c;
    int n,k;
    cin>>n>>k;
    int above[n+1][n+1], left[n+1][n+1], below[n+2][n+2], right[n+2][n+2];

    clr(above); clr(left); clr(below); clr(right);

    vector<vector<int> >mat(n+1,vector<int>(n+1,0));
    FOR(i,1,n)
    {
        string s;cin>>s;
        FOR(j,0,n-1)
        {
            if(s[j] == '.')
            mat[i][j+1] = 1;
        }
    }

    // above
    if(mat[1][1]) above[1][1] = 1, left[1][1] = 1;
    FOR(i,2,n) if(mat[1][i]) above[1][i] = 1;
    FOR(i,2,n) if(mat[i][1]) left[i][1] = 1;

    FOR(i,2,n)
    {
        FOR(j,1,n)
        {
            if(mat[i][j])
            above[i][j]=above[i-1][j]+1;
            else above[i][j] = 0;
        }
    }

    FOR(i,1,n)
    {
        FOR(j,2,n)
        {
            if(mat[i][j])
            left[i][j]=left[i][j-1]+1;
            else left[i][j] = 0;
        }
    }

    RFOR(i,n,1)
    {
        FOR(j,1,n)
        {
            if(mat[i][j])
            below[i][j] += below[i+1][j]+1;
            else below[i][j] = 0;
        }
    }

    FOR(i,1,n)
    {
        RFOR(j,n,1)
        {
            if(mat[i][j])
            right[i][j] = right[i][j+1]+1;
            else right[i][j] = 0;
        }
    }

    int maxi = INT_MIN;
    pair<int,int> point = {n,n};

    FOR(i,1,n)
    {
        FOR(j,1,n)
        {
            if(!mat[i][j])continue;
            auto hori = left[i][j]+right[i][j]-1;
            auto vert = above[i][j]+below[i][j]-1;
            auto tt = ((hori<k)?0:hori-k+1) + ((vert<k)?0:vert-k+1);
            if(tt > maxi)
                maxi = tt, point = {i,j};
        }
        // cout<<endl;
    }
    cout<<point.F<<" "<<point.S<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define up upper_bound
#define vll vector<ll>
#define G vector<vll >
#define gg vector<int>
#define F first
#define S second
#define pll pair<ll,ll>
#define pii pair<int,int>
#define RFOR(i,a,b) for(int i=a;i>=b;i--)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define endl '\n'
#define clr(a) memset(a,0,sizeof(a))
#define all(x) x.begin(),x.end()
#define rll read_ll();
#define gc getchar
#define pc putchar
typedef long long ll;
template<class T> inline T lcm(T a,T b){
        return a/gcd(a,b)*b;
}
template<typename T>
void debug(T first) {
    cout << first << "\n";
}
template<typename T, typename... Args>
void debug(T first, Args... args) {
    cout << first << " ";
    debug(args...);
}

ll read_ll(){char c=gc();while((c<'0'||c>'9')&&c!='-')c=gc();ll ret=0;int neg=0;if(c=='-')neg=1,c=gc();while(c>='0'&&c<='9'){ret=10*ret+c-48;c=gc();}return neg?-ret:ret;}
const int N = 100;
ll dp[N+2][4001];

struct item {
   ll t,p,d,pos;
};

bool cmp(item a, item b)
{
    return a.d < b.d;
}

int main()
{
    char c;
    int n,k;
    cin>>n>>k;
    int above[n+1][n+1], left[n+1][n+1], below[n+2][n+2], right[n+2][n+2];

    clr(above); clr(left); clr(below); clr(right);

    vector<vector<int> >mat(n+1,vector<int>(n+1,0));
    FOR(i,1,n)
    {
        string s;cin>>s;
        FOR(j,0,n-1)
        {
            if(s[j] == '.')
            mat[i][j+1] = 1;
        }
    }

    // above
    if(mat[1][1]) above[1][1] = 1, left[1][1] = 1;
    FOR(i,2,n) if(mat[1][i]) above[1][i] = 1;
    FOR(i,2,n) if(mat[i][1]) left[i][1] = 1;

    FOR(i,2,n)
    {
        FOR(j,1,n)
        {
            if(mat[i][j])
            above[i][j]=above[i-1][j]+1;
            else above[i][j] = 0;
        }
    }

    FOR(i,1,n)
    {
        FOR(j,2,n)
        {
            if(mat[i][j])
            left[i][j]=left[i][j-1]+1;
            else left[i][j] = 0;
        }
    }

    RFOR(i,n,1)
    {
        FOR(j,1,n)
        {
            if(mat[i][j])
            below[i][j] += below[i+1][j]+1;
            else below[i][j] = 0;
        }
    }

    FOR(i,1,n)
    {
        RFOR(j,n,1)
        {
            if(mat[i][j])
            right[i][j] = right[i][j+1]+1;
            else right[i][j] = 0;
        }
    }

    int maxi = INT_MIN;
    pair<int,int> point = {n,n};

    FOR(i,1,n)
    {
        FOR(j,1,n)
        {
            if(!mat[i][j])continue;
            if(k==1) return !printf("%d %d",i,j);
            int l = left[i][j], r = right[i][j], u = above[i][j], d = below[i][j];
            l = min(l,k); r = min(r,k); u = min(u,k); d = min(d,k);
            auto tt = min(k,max(0,l+r-k)) + min(k,max(0,u+d-k));
            if(tt > maxi)
                maxi = tt, point = {i,j};
        }
        // cout<<endl;
    }
    cout<<point.F<<" "<<point.S<<endl;
    return 0;
}