#include<bits/stdc++.h>
using namespace std;
int lt[100010];
int dt[100010];
int main()
{
    int n,m,cl,ce,v;
    scanf("%d %d %d %d %d",&n,&m,&cl,&ce,&v);
    for(int i=1;i<=cl;i++) scanf("%d",&lt[i]);
    for(int i=1;i<=ce;i++) scanf("%d",&dt[i]);
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int x1,y1,x2,y2;
        scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
        int zbl,zbd,ybl,ybd;
        if(y1>lt[1]){
        int l=1,r=cl;
        while(l<r)
        {
            int mid=(l+r+1)>>1;
            if(lt[mid]<y1)
            {
                l=mid;
            }
            else
            {
                r=mid-1;
            }
        }
        zbl=lt[l];
        }else {zbl=-1;}
        if(y1>dt[1]){
        int l=1,r=ce;
        while(l<r)
        {
            int mid=(l+r+1)>>1;
            if(dt[mid]<y1)
            {
                l=mid;
            }
            else
            {
                r=mid-1;
            }
        }
        zbd=dt[l];
        }else{zbd=-1;}
        if(y1<dt[ce]){
        int l=1,r=ce;
        while(l<r)
        {
            int mid=(l+r)>>1;
            if(dt[mid]>y1)
            {
                r=mid;
            }
            else
            {
                l=mid+1;
            }
        }
        ybd=dt[l];
        }else{ybd=-1;}
        if(y1<lt[cl]){
        int l=1,r=cl;
        while(l<r)
        {
            int mid=(l+r)>>1;
            if(lt[mid]>y1)
            {
                r=mid;
            }
            else
            {
                l=mid+1;
            }
        }
        ybl=lt[l];
        }else{ybl=-1;}
        int ans=1000000000;
        if(zbl!=-1)ans=min(ans,abs(zbl-y1)+abs(zbl-y2)+abs(x1-x2));//printf("%d\n",ans);
        if(ybl!=-1)ans=min(ans,abs(ybl-y1)+abs(ybl-y2)+abs(x1-x2));//printf("%d\n",ans);
        if(zbd!=-1)ans=min(ans,abs(zbd-y1)+abs(zbd-y2)+(abs(x1-x2)/v)+(abs(x1-x2)%v==0?0:1));//printf("%d\n",ans);
        if(ybd!=-1)ans=min(ans,abs(ybd-y1)+abs(ybd-y2)+(abs(x1-x2)/v)+(abs(x1-x2)%v==0?0:1));
        printf("%d\n",ans);
       // printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int lt[100010];
int dt[100010];
int main()
{
    int n,m,cl,ce,v;
    scanf("%d %d %d %d %d",&n,&m,&cl,&ce,&v);
    for(int i=1;i<=cl;i++) scanf("%d",&lt[i]); lt[cl+1]=1000000000;
    for(int i=1;i<=ce;i++) scanf("%d",&dt[i]); dt[ce+1]=1000000000;
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int x1,y1,x2,y2;
        scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
        int zbl,zbd,ybl,ybd;
        int l=0,r=cl+1;
        while(l<r)
        {
            int mid=(l+r+1)>>1;
            if(lt[mid]<y1)
            {
                l=mid;
            }
            else
            {
                r=mid-1;
            }
        }
        zbl=lt[l];
        l=0,r=ce+1;
        while(l<r)
        {
            int mid=(l+r+1)>>1;
            if(dt[mid]<y1)
            {
                l=mid;
            }
            else
            {
                r=mid-1;
            }
        }
        zbd=dt[l];
        l=0,r=ce+1;
        while(l<r)
        {
            int mid=(l+r)>>1;
            if(dt[mid]>y1)
            {
                r=mid;
            }
            else
            {
                l=mid+1;
            }
        }
        ybd=dt[l];
        l=0,r=cl+1;
        while(l<r)
        {
            int mid=(l+r)>>1;
            if(lt[mid]>y1)
            {
                r=mid;
            }
            else
            {
                l=mid+1;
            }
        }
        ybl=lt[l];
        int ans=1000000000;
        ans=min(ans,abs(zbl-y1)+abs(zbl-y2)+abs(x1-x2));//printf("%d\n",ans);
        ans=min(ans,abs(ybl-y1)+abs(ybl-y2)+abs(x1-x2));//printf("%d\n",ans);
        ans=min(ans,abs(zbd-y1)+abs(zbd-y2)+(abs(x1-x2)/v)+(abs(x1-x2)%v==0?0:1));//printf("%d\n",ans);
        ans=min(ans,abs(ybd-y1)+abs(ybd-y2)+(abs(x1-x2)/v)+(abs(x1-x2)%v==0?0:1));printf("%d\n",ans);
       // printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int lt[100010];
int dt[100010];
int js(int a,int b,int v)
{
    if(abs(a-b)%v==0) return abs(a-b)/v;
    else return abs(a-b)/v+1;
}
int main()
{
    int n,m,cl,ce,v;
    scanf("%d %d %d %d %d",&n,&m,&cl,&ce,&v);
    for(int i=1;i<=cl;i++) scanf("%d",&lt[i]);
    for(int i=1;i<=ce;i++) scanf("%d",&dt[i]);
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int x1,y1,x2,y2;
        scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
        if(x1==x2)
        {
          printf("%d\n",abs(y1-y2));
            continue;
        }
        int zbl,zbd,ybl,ybd;
        if(y1>lt[1]&&cl!=0){
        int l=1,r=cl;
        while(l<r)
        {
            int mid=(l+r+1)>>1;
            if(lt[mid]<y1)
            {
                l=mid;
            }
            else
            {
                r=mid-1;
            }
        }
        zbl=lt[l];
        }else {zbl=-1;}
        if(y1>dt[1]&&ce!=0){
        int l=1,r=ce;
        while(l<r)
        {
            int mid=(l+r+1)>>1;
            if(dt[mid]<y1)
            {
                l=mid;
            }
            else
            {
                r=mid-1;
            }
        }
        zbd=dt[l];
        }else{zbd=-1;}
        if(y1<dt[ce]&&ce!=0){
        int l=1,r=ce;
        while(l<r)
        {
            int mid=(l+r)>>1;
            if(dt[mid]>y1)
            {
                r=mid;
            }
            else
            {
                l=mid+1;
            }
        }
        ybd=dt[l];
        }else{ybd=-1;}
        if(y1<lt[cl]&&cl!=0){
        int l=1,r=cl;
        while(l<r)
        {
            int mid=(l+r)>>1;
            if(lt[mid]>y1)
            {
                r=mid;
            }
            else
            {
                l=mid+1;
            }
        }
        ybl=lt[l];
        }else{ybl=-1;}
        int ans=1<<30;
        if(zbl!=-1)ans=min(ans,abs(zbl-y1)+abs(zbl-y2)+abs(x1-x2));//printf("%d\n",ans);
        if(ybl!=-1)ans=min(ans,abs(ybl-y1)+abs(ybl-y2)+abs(x1-x2));//printf("%d\n",ans);
        if(zbd!=-1)ans=min(ans,abs(zbd-y1)+abs(zbd-y2)+js(x1,x2,v));//printf("%d\n",ans);
        if(ybd!=-1)ans=min(ans,abs(ybd-y1)+abs(ybd-y2)+js(x1,x2,v));
        printf("%d\n",ans);
       // printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int lt[100010];
int dt[100010];
int js(int a,int b,int v)
{
    if(abs(a-b)%v==0) return abs(a-b)/v;
    else return abs(a-b)/v+1;
}
int main()
{
    int n,m,cl,ce,v;
    scanf("%d %d %d %d %d",&n,&m,&cl,&ce,&v);
    for(int i=1;i<=cl;i++) scanf("%d",&lt[i]);
    for(int i=1;i<=ce;i++) scanf("%d",&dt[i]);
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int x1,y1,x2,y2;
        scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
        if(x1==x2)
        {
            printf("%d\n",abs(y1-y2));
            continue;
        }
        int zbl,zbd,ybl,ybd;
        if(y1>lt[1]){
        int l=1,r=cl;
        while(l<r)
        {
            int mid=(l+r+1)>>1;
            if(lt[mid]<y1)
            {
                l=mid;
            }
            else
            {
                r=mid-1;
            }
        }
        zbl=lt[l];
        }else {zbl=-1;}
        if(y1>dt[1]){
        int l=1,r=ce;
        while(l<r)
        {
            int mid=(l+r+1)>>1;
            if(dt[mid]<y1)
            {
                l=mid;
            }
            else
            {
                r=mid-1;
            }
        }
        zbd=dt[l];
        }else{zbd=-1;}
        if(y1<dt[ce]){
        int l=1,r=ce;
        while(l<r)
        {
            int mid=(l+r)>>1;
            if(dt[mid]>y1)
            {
                r=mid;
            }
            else
            {
                l=mid+1;
            }
        }
        ybd=dt[l];
        }else{ybd=-1;}
        if(y1<lt[cl]){
        int l=1,r=cl;
        while(l<r)
        {
            int mid=(l+r)>>1;
            if(lt[mid]>y1)
            {
                r=mid;
            }
            else
            {
                l=mid+1;
            }
        }
        ybl=lt[l];
        }else{ybl=-1;}
        int ans=1<<30;
        if(zbl!=-1)ans=min(ans,abs(zbl-y1)+abs(zbl-y2)+abs(x1-x2));//printf("%d\n",ans);
        if(ybl!=-1)ans=min(ans,abs(ybl-y1)+abs(ybl-y2)+abs(x1-x2));//printf("%d\n",ans);
        if(zbd!=-1)ans=min(ans,abs(zbd-y1)+abs(zbd-y2)+js(x1,x2,v));//printf("%d\n",ans);
        if(ybd!=-1)ans=min(ans,abs(ybd-y1)+abs(ybd-y2)+js(x1,x2,v));
        printf("%d\n",ans);
       // printf("%d\n",ans);
    }
    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int n, m, c1, c2,piv,piv2, med, v,ele[100005], es[100005], consultas,x1, x2, y1, y2;
long long b, b2, canti;
void busqueda( int x)
{
    while((piv+2)<piv2)
    {
       // printf("\n");
        med=(piv+piv2)/2;
        if(es[med]<x)
        {
            piv=med;
        }
        else
        {
            piv2=med;
        }
    }
    b=100000;
    canti=100000;
    for(int i=piv; i<=piv2; i++)
    {
        canti=(abs(es[i]-y1))+ (abs(x2-x1))+ (abs(es[i]-y2));
        //printf("%d %d %d\n", (abs(es[i]-y1)), (abs(x2-x1)), (abs(es[i]-y2)));
        b= min(b, canti);
    }
    return;
}
void busqueda2( int x)
{
    while((piv+2)<piv2)
    {
         //printf("\n");
        med=(piv+piv2)/2;
        if(ele[med]<x)
        {
            piv=med;
        }
        else
        {
            piv2=med;
        }
    }
    b2=100000;
    canti=100000;
    for(int i=piv; i<=piv2; i++)
    {
        int o=(abs(x2-x1))/v;
        if((abs(x2-x1))%v>0)
                o++;
        canti=  abs(ele[i]-y1) + o+ (abs(ele[i]-y2));
            //printf("%d %d %d\n",  abs(x2-x1), o,  abs(ele[i]-y2));
        b2=min(b2, canti);
    }
    return;
}

int main()
{
    scanf("%d%d%d%d%d", &n, &m, &c1, &c2, &v);
    for(int i=0; i<c1; i++)
    {
        scanf("%d", &es[i]);
    }
    for(int i=0; i<c2; i++)
    {
        scanf("%d", &ele[i]);
    }
    scanf("%d", &consultas);
    for(int i=0; i<consultas; i++)
    {
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        if(x1==x2)
        {
            printf("%d", abs(y1-y2));
        }
        else
        {
            piv=0;
            piv2=c1-1;
            busqueda(y1);
            piv=0;
            piv2=c2-1;
            busqueda2(y1);
            printf("%I64d\n", min(b, b2));
        }

    }

    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int n, m, c1, c2,piv,piv2, med, v,ele[100005], es[100005], consultas,x1, x2, y1, y2;
long long b, b2, canti;
void busqueda( int x)
{
    while((piv+100)<piv2)
    {
       // printf("\n");
        med=(piv+piv2)/2;
        if(es[med]<x)
        {
            piv=med;
        }
        else
        {
            piv2=med;
        }
    }
    b=100000;
    canti=100000;
    for(int i=piv; i<=piv2; i++)
    {
        canti=(abs(es[i]-y1))+ (abs(x2-x1))+ (abs(es[i]-y2));
        //printf("%d %d %d\n", (abs(es[i]-y1)), (abs(x2-x1)), (abs(es[i]-y2)));
        b= min(b, canti);
    }
    return;
}
void busqueda2( int x)
{
    while((piv+100)<piv2)
    {
         //printf("\n");
        med=(piv+piv2)/2;
        if(ele[med]<x)
        {
            piv=med;
        }
        else
        {
            piv2=med;
        }
    }
    b2=100000;
    canti=100000;
    for(int i=piv; i<=piv2; i++)
    {
        int o=(abs(x2-x1))/v;
        if((abs(x2-x1))%v>0)
                o++;
        canti=  abs(ele[i]-y1) + o+ (abs(ele[i]-y2));
            //printf("%d %d %d\n",  abs(x2-x1), o,  abs(ele[i]-y2));
        b2=min(b2, canti);
    }
    return;
}

int main()
{
    scanf("%d%d%d%d%d", &n, &m, &c1, &c2, &v);
    for(int i=0; i<c1; i++)
    {
        scanf("%d", &es[i]);
    }
    for(int i=0; i<c2; i++)
    {
        scanf("%d", &ele[i]);
    }
    scanf("%d", &consultas);
    for(int i=0; i<consultas; i++)
    {
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        piv=0;
        piv2=c1-1;
        busqueda(y1);
        piv=0;
        piv2=c2-1;
        busqueda2(y1);
        printf("%I64d\n", min(b, b2));

    }

    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int n, m, c1, c2,piv,piv2, med, v,ele[100005], es[100005], consultas,x1, x2, y1, y2;
long long b, b2, canti;
void busqueda( int x)
{
    while((piv+10)<piv2)
    {
       // printf("\n");
        med=(piv+piv2)/2;
        if(es[med]<x)
        {
            piv=med;
        }
        else
        {
            piv2=med;
        }
    }
    b=10000000000;
    canti=10000000000;
    for(int i=piv; i<=piv2; i++)
    {
        canti=(abs(es[i]-y1))+ (abs(x2-x1))+ (abs(es[i]-y2));
        //printf("%d %d %d\n", (abs(es[i]-y1)), (abs(x2-x1)), (abs(es[i]-y2)));
        b= min(b, canti);
    }
    return;
}
void busqueda2( int x)
{
    while((piv+10)<piv2)
    {
         //printf("\n");
        med=(piv+piv2)/2;
        if(ele[med]<x)
        {
            piv=med;
        }
        else
        {
            piv2=med;
        }
    }
    b2=10000000000;
    canti=10000000000;
    for(int i=piv; i<=piv2; i++)
    {
        int o=(abs(x2-x1))/v;
        if((abs(x2-x1))%v>0)
                o++;
        canti=  abs(ele[i]-y1) + o+ (abs(ele[i]-y2));
            //printf("%d %d %d\n",  abs(x2-x1), o,  abs(ele[i]-y2));
        b2=min(b2, canti);
    }
    return;
}

int main()
{
    scanf("%d%d%d%d%d", &n, &m, &c1, &c2, &v);
    for(int i=0; i<c1; i++)
    {
        scanf("%d", &es[i]);
    }
    for(int i=0; i<c2; i++)
    {
        scanf("%d", &ele[i]);
    }
    scanf("%d", &consultas);
    for(int i=0; i<consultas; i++)
    {
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        if(x1==x2)
        {
            printf("%d\n", abs(y1-y2));
        }
        else
        {
            piv=0;
            piv2=c1-1;
            busqueda(y1);
            piv=0;
            piv2=c2-1;
            busqueda2(y1);
            printf("%I64d\n", min(b, b2));
        }

    }
    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int n, m, c1, c2,piv,piv2, med, v,ele[100005], es[100005], consultas,x1, x2, y1, y2;
long long b, b2, canti;
void busqueda( int x)
{
    while((piv+10)<piv2)
    {
       // printf("\n");
        med=(piv+piv2)/2;
        if(es[med]<x)
        {
            piv=med;
        }
        else
        {
            piv2=med;
        }
    }
    b=10000000000;
    canti=10000000000;
    for(int i=piv; i<=piv2; i++)
    {
        canti=(abs(es[i]-y1))+ (abs(x2-x1))+ (abs(es[i]-y2));
        //printf("%d %d %d\n", (abs(es[i]-y1)), (abs(x2-x1)), (abs(es[i]-y2)));
        b= min(b, canti);
    }
    return;
}
void busqueda2( int x)
{
    while((piv+10)<piv2)
    {
         //printf("\n");
        med=(piv+piv2)/2;
        if(ele[med]<x)
        {
            piv=med;
        }
        else
        {
            piv2=med;
        }
    }
    b2=10000000000;
    canti=10000000000;
    for(int i=piv; i<=piv2; i++)
    {
        int o=(abs(x2-x1))/v;
        if((abs(x2-x1))%v>0)
                o++;
        canti=  abs(ele[i]-y1) + o+ (abs(ele[i]-y2));
            //printf("%d %d %d\n",  abs(x2-x1), o,  abs(ele[i]-y2));
        b2=min(b2, canti);
    }
    return;
}

int main()
{
    scanf("%d%d%d%d%d", &n, &m, &c1, &c2, &v);
    for(int i=0; i<c1; i++)
    {
        scanf("%d", &es[i]);
    }
    for(int i=0; i<c2; i++)
    {
        scanf("%d", &ele[i]);
    }
    scanf("%d", &consultas);
    for(int i=0; i<consultas; i++)
    {
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        if(x1==x2)
        {
            printf("%d", abs(y1-y2));
        }
        else
        {
            piv=0;
            piv2=c1-1;
            busqueda(y1);
            piv=0;
            piv2=c2-1;
            busqueda2(y1);
            printf("%I64d\n", min(b, b2));
        }

    }
    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int n, m, c1, c2,piv,piv2, med, v,ele[100005], es[100005], consultas,x1, x2, y1, y2;
long long b, b2, canti;
void busqueda( int x)
{
    while((piv+10)<piv2)
    {
       // printf("\n");
        med=(piv+piv2)/2;
        if(es[med]<x)
        {
            piv=med;
        }
        else
        {
            piv2=med;
        }
    }
    b=100000;
    canti=100000;
    for(int i=piv; i<=piv2; i++)
    {
        canti=(abs(es[i]-y1))+ (abs(x2-x1))+ (abs(es[i]-y2));
        //printf("%d %d %d\n", (abs(es[i]-y1)), (abs(x2-x1)), (abs(es[i]-y2)));
        b= min(b, canti);
    }
    return;
}
void busqueda2( int x)
{
    while((piv+10)<piv2)
    {
         //printf("\n");
        med=(piv+piv2)/2;
        if(ele[med]<x)
        {
            piv=med;
        }
        else
        {
            piv2=med;
        }
    }
    b2=100000;
    canti=100000;
    for(int i=piv; i<=piv2; i++)
    {
        int o=(abs(x2-x1))/v;
        if((abs(x2-x1))%v>0)
                o++;
        canti=  abs(ele[i]-y1) + o+ (abs(ele[i]-y2));
            //printf("%d %d %d\n",  abs(x2-x1), o,  abs(ele[i]-y2));
        b2=min(b2, canti);
    }
    return;
}

int main()
{
    scanf("%d%d%d%d%d", &n, &m, &c1, &c2, &v);
    for(int i=0; i<c1; i++)
    {
        scanf("%d", &es[i]);
    }
    for(int i=0; i<c2; i++)
    {
        scanf("%d", &ele[i]);
    }
    scanf("%d", &consultas);
    for(int i=0; i<consultas; i++)
    {
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        piv=0;
        piv2=c1-1;
        busqueda(y1);
        piv=0;
        piv2=c2-1;
        busqueda2(y1);
        printf("%I64d\n", min(b, b2));

    }

    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int n, m, c1, c2,piv,piv2, med, v,ele[100005], es[100005], consultas,x1, x2, y1, y2;
long long b, b2, canti;
void busqueda( int x)
{
    while((piv+2)<piv2)
    {
       // printf("\n");
        med=(piv+piv2)/2;
        if(es[med]<x)
        {
            piv=med;
        }
        else
        {
            piv2=med;
        }
    }
    b=100000;
    canti=100000;
    for(int i=piv; i<=piv2; i++)
    {
        canti=(abs(es[i]-y1))+ (abs(x2-x1))+ (abs(es[i]-y2));
        //printf("%d %d %d\n", (abs(es[i]-y1)), (abs(x2-x1)), (abs(es[i]-y2)));
        b= min(b, canti);
    }
    return;
}
void busqueda2( int x)
{
    while((piv+2)<piv2)
    {
         //printf("\n");
        med=(piv+piv2)/2;
        if(ele[med]<x)
        {
            piv=med;
        }
        else
        {
            piv2=med;
        }
    }
    b2=100000;
    canti=100000;
    for(int i=piv; i<=piv2; i++)
    {
        int o=(abs(x2-x1))/v;
        if((abs(x2-x1))%v>0)
                o++;
        canti=  abs(ele[i]-y1) + o+ (abs(ele[i]-y2));
            //printf("%d %d %d\n",  abs(x2-x1), o,  abs(ele[i]-y2));
        b2=min(b2, canti);
    }
    return;
}

int main()
{
    scanf("%d%d%d%d%d", &n, &m, &c1, &c2, &v);
    for(int i=0; i<c1; i++)
    {
        scanf("%d", &es[i]);
    }
    for(int i=0; i<c2; i++)
    {
        scanf("%d", &ele[i]);
    }
    scanf("%d", &consultas);
    for(int i=0; i<consultas; i++)
    {
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        piv=0;
        piv2=c1-1;
        busqueda(y1);
        piv=0;
        piv2=c2-1;
        busqueda2(y1);
        printf("%I64d\n", min(b, b2));

    }

    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int n, m, c1, c2,piv,piv2, med, v,ele[100005], es[100005], consultas,x1, x2, y1, y2;
long long b, b2, canti;
int main()
{
    scanf("%d%d%d%d%d", &n, &m, &c1, &c2, &v);
    for(int i=0; i<c1; i++)
    {
        scanf("%d", &es[i]);
    }
    for(int i=0; i<c2; i++)
    {
        scanf("%d", &ele[i]);
    }
    scanf("%d", &consultas);
    for(int i=0; i<consultas; i++)
    {
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        b=100000;
        b2=100000;
        canti=100000;
        for(int i=0; i<c1; i++)
        {
            canti=(abs(es[i]-y1))+ (abs(x2-x1))+ (abs(es[i]-y2));
            //printf("%d %d %d\n", (abs(es[i]-y1)), (abs(x2-x1)), (abs(es[i]-y2)));
            b= min(b, canti);
        }
        canti=100000;
        for(int i=0; i<c2; i++)
        {
            int o=(abs(x2-x1))/v;
            if((abs(x2-x1))%v>0)
                o++;
            canti=  abs(ele[i]-y1) + o+ (abs(ele[i]-y2));
            //printf("%d %d %d\n",  abs(x2-x1), o,  abs(ele[i]-y2));
            b2=min(b2, canti);
        }
        printf("%I64d\n", min(b, b2));

    }

    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int n, m, c1, c2,piv,piv2, med, v,ele[100005], es[100005], consultas,x1, x2, y1, y2;
long long b, b2, canti;
void busqueda( int x)
{
    while((piv+1000)<piv2)
    {
       // printf("\n");
        med=(piv+piv2)/2;
        if(es[med]<x)
        {
            piv=med;
        }
        else
        {
            piv2=med;
        }
    }
    b=100000;
    canti=100000;
    for(int i=piv; i<=piv2; i++)
    {
        canti=(abs(es[i]-y1))+ (abs(x2-x1))+ (abs(es[i]-y2));
        //printf("%d %d %d\n", (abs(es[i]-y1)), (abs(x2-x1)), (abs(es[i]-y2)));
        b= min(b, canti);
    }
    return;
}
void busqueda2( int x)
{
    while((piv+1000)<piv2)
    {
         //printf("\n");
        med=(piv+piv2)/2;
        if(ele[med]<x)
        {
            piv=med;
        }
        else
        {
            piv2=med;
        }
    }
    b2=100000;
    canti=100000;
    for(int i=piv; i<=piv2; i++)
    {
        int o=(abs(x2-x1))/v;
        if((abs(x2-x1))%v>0)
                o++;
        canti=  abs(ele[i]-y1) + o+ (abs(ele[i]-y2));
            //printf("%d %d %d\n",  abs(x2-x1), o,  abs(ele[i]-y2));
        b2=min(b2, canti);
    }
    return;
}

int main()
{
    scanf("%d%d%d%d%d", &n, &m, &c1, &c2, &v);
    for(int i=0; i<c1; i++)
    {
        scanf("%d", &es[i]);
    }
    for(int i=0; i<c2; i++)
    {
        scanf("%d", &ele[i]);
    }
    scanf("%d", &consultas);
    for(int i=0; i<consultas; i++)
    {
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        if(x1==x2)
        {
            printf("%d", abs(y1-y2));
        }
        else
        {
            piv=0;
            piv2=c1-1;
            busqueda(y1);
            piv=0;
            piv2=c2-1;
            busqueda2(y1);
            printf("%I64d\n", min(b, b2));
        }

    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define maxn 100050
#define FFFF 1000000000
int n, m, cl, ce, v, q;
int a[ maxn ], b[ maxn ];
int x1, y1, x2, y2;
int main()
{
	scanf("%d %d %d %d %d", &n, &m, &cl, &ce, &v);
	for (int i=1;i<=cl;i++)	scanf("%d", &b[i]);
	for (int i=1;i<=ce;i++) scanf("%d", &a[i]);
	b[cl + 1] = a[ ce + 1 ] = FFFF;

	scanf("%d", &q);
	for (int i=1;i<=q;i++)
	{
		scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
		int ymi = min(y1, y2), yma = max(y1, y2);

		int key2 = lower_bound(b+1, b+1+cl, ymi) - b;
		int key1 = lower_bound(a+1, a+1+ce, ymi) - a;

		int mi = FFFF;

		int dis = abs(x2 - x1);
	//	cout<<ymi<<' '<<key1 <<"      "<<key2<<endl;

		if (a[ key1 ] <= yma)
		{
			int ans = (  ( dis % v == 0 ) ? dis/v : dis/v + 1  ) +  abs(y1 - y2);
			mi = min(ans, mi);
	//		cout<<"mi1="<<mi<<endl;
		}else
		{
			int ans = (  ( dis % v == 0 ) ? dis/v : dis/v + 1  ), f;

			if (key1 == 1)	f = abs(a[key1] - yma) * 2;
			else  f = abs(min(a[key1] - ymi, ymi - a[key1 - 1]) * 2);

			mi = min(mi, ans + f + abs(y1 - y2));
		//	cout<<"mi2="<<mi<<endl;
		}

		if (b[ key2 ] <= yma)
		{
			int ans = abs(x1 - x2) + abs(y1 - y2);
			mi = min(mi, ans);
	//		cout<<"mi3="<<mi<<endl;
		}else
		{
			int ans = abs(x1 - x2) + abs(y1 - y2), f;
			if (key2 == 1) f = abs(b[key2] - yma) * 2;
			else f = abs(min(b[key2] - ymi ,  ymi - b[key2 - 1]) * 2);

			mi = min(mi, ans + f);
	//		cout<<"mi4="<<mi<<endl;
		}

		printf("%d\n", mi);
	}
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define maxn 100050
#define FFFF 1000000000
int n, m, cl, ce, v, q;
int a[ maxn ], b[ maxn ];
int x1, y1, x2, y2;
int main()
{
	scanf("%d %d %d %d %d", &n, &m, &cl, &ce, &v);
	for (int i=1;i<=cl;i++)	scanf("%d", &b[i]);
	for (int i=1;i<=ce;i++) scanf("%d", &a[i]);
	b[cl + 1] = a[ ce + 1 ] = FFFF;

	scanf("%d", &q);
	for (int i=1;i<=q;i++)
	{
		scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
		int ymi = min(y1, y2), yma = max(y1, y2);

		int key2 = lower_bound(b+1, b+1+cl, ymi) - b;
		int key1 = lower_bound(a+1, a+1+ce, ymi) - a;

		int mi = FFFF;

		int dis = abs(x2 - x1);

		if (a[ key1 ] <= yma && cl != 0)
		{
			int ans = (  ( dis % v == 0 ) ? dis/v : dis/v + 1  ) +  abs(y1 - y2);
			mi = min(ans, mi);
	//		cout<<"mi1="<<mi<<endl;
		}else if (cl != 0)
		{
			int ans = (  ( dis % v == 0 ) ? dis/v : dis/v + 1  ), f;

			if (key1 == 1)	f = abs(a[key1] - yma) * 2;
			else  f = abs(min( abs(a[key1] - yma) , abs(a[key1 - 1] - ymi) ) * 2);

			mi = min(mi, ans + f + abs(y1 - y2));
		}

		if (b[ key2 ] <= yma && ce != 0)
		{
			int ans = abs(x1 - x2) + abs(y1 - y2);
			mi = min(mi, ans);
	//		cout<<"mi3="<<mi<<endl;
		}else if (ce != 0 )
		{
			int ans = abs(x1 - x2) + abs(y1 - y2), f;
			if (key2 == 1) f = abs(b[key2] - yma) * 2;
			else f = abs(min( abs(b[key2] - yma) ,  abs(b[key2 - 1] - ymi)) * 2);

			mi = min(mi, ans + f);
	//		cout<<"mi4="<<mi<<endl;
		}

		printf("%d\n", mi);
	}
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define maxn 100050
#define FFFF 1000000000
int n, m, cl, ce, v, q;
int a[ maxn ], b[ maxn ];
int x1, y1, x2, y2;
int main()
{
	scanf("%d %d %d %d %d", &n, &m, &cl, &ce, &v);
	for (int i=1;i<=cl;i++)	scanf("%d", &b[i]);
	for (int i=1;i<=ce;i++) scanf("%d", &a[i]);
	b[cl + 1] = a[ ce + 1 ] = FFFF;

	scanf("%d", &q);
	for (int i=1;i<=q;i++)
	{
		scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
		int ymi = min(y1, y2), yma = max(y1, y2);

		int key2 = lower_bound(b+1, b+1+cl, ymi) - b;
		int key1 = lower_bound(a+1, a+1+ce, ymi) - a;

		int mi = FFFF;

		int dis = abs(x2 - x1);
	//	cout<<ymi<<' '<<key1 <<"      "<<key2<<endl;

		if (a[ key1 ] <= yma)
		{
			int ans = (  ( dis % v == 0 ) ? dis/v : dis/v + 1  ) +  abs(y1 - y2);
			mi = min(ans, mi);
	//		cout<<"mi1="<<mi<<endl;
		}else
		{
			int ans = (  ( dis % v == 0 ) ? dis/v : dis/v + 1  ), f;

			if (key1 == 1)	f = abs(a[key1] - yma) * 2;
			else  f = abs(min( abs(a[key1] - yma) , abs(a[key1 - 1] - ymi) ) * 2);

			mi = min(mi, ans + f + abs(y1 - y2));
		}

		if (b[ key2 ] <= yma)
		{
			int ans = abs(x1 - x2) + abs(y1 - y2);
			mi = min(mi, ans);
	//		cout<<"mi3="<<mi<<endl;
		}else
		{
			int ans = abs(x1 - x2) + abs(y1 - y2), f;
			if (key2 == 1) f = abs(b[key2] - yma) * 2;
			else f = abs(min( abs(b[key2] - yma) ,  abs(b[key2 - 1] - ymi)) * 2);

			mi = min(mi, ans + f);
	//		cout<<"mi4="<<mi<<endl;
		}

		printf("%d\n", mi);
	}
}


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define maxn 100050
#define FFFF 1000000000
int n, m, cl, ce, v, q;
int a[ maxn ], b[ maxn ];
int x1, y1, x2, y2;
int main()
{
	scanf("%d %d %d %d %d", &n, &m, &cl, &ce, &v);
	for (int i=1;i<=cl;i++)	scanf("%d", &b[i]);
	for (int i=1;i<=ce;i++) scanf("%d", &a[i]);
	b[cl + 1] = a[ ce + 1 ] = FFFF;

	scanf("%d", &q);
	for (int i=1;i<=q;i++)
	{
		scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
		int ymi = min(y1, y2), yma = max(y1, y2);

		int key2 = lower_bound(b+1, b+1+cl, ymi) - b;
		int key1 = lower_bound(a+1, a+1+ce, ymi) - a;

//		cout<<a[key1] <<' '<<b[key2]<<endl;
		int mi = FFFF;

		int dis = abs(x2 - x1);

		if (a[ key1 ] <= yma )
		{
			int ans = (  ( dis % v == 0 ) ? dis/v : dis/v + 1  ) +  abs(y1 - y2);
			mi = min(ans, mi);

		}else
		{
			int ans = (  ( dis % v == 0 ) ? dis/v : dis/v + 1  ) +  abs(y1 - y2), f;

			if (key1 == 1)	f = abs(a[key1] - yma) * 2;
			else  f = min( abs(a[key1] - yma) , abs(a[key1 - 1] - ymi) ) * 2;

			mi = min(mi, ans + f );
		}

		if (b[ key2 ] <= yma )
		{
			int ans = abs(x1 - x2) + abs(y1 - y2);
			mi = min(mi, ans);

		}else
		{
			int ans = abs(x1 - x2) + abs(y1 - y2), f;
			if (key2 == 1) f = abs(b[key2] - yma) * 2;
			else f = min( abs(b[key2] - yma) ,  abs(b[key2 - 1] - ymi) ) * 2;

			mi = min(mi, ans + f);
		}

		printf("%d\n", mi);
	}
}
/*
5 6 0 1 3
5
1
1 3 5 4

3 3 0 1 2
2
1
1 1 3 3

1000000 1000000 0 1 999999
1000000
1
1 1 1000000 1

*/


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define maxn 100050
#define FFFF 80000000
int n, m, cl, ce, v, q;
int a[ maxn ], b[ maxn ];
int x1, y1, x2, y2;
int main()
{
	scanf("%d %d %d %d %d", &n, &m, &cl, &ce, &v);
	for (int i=1;i<=cl;i++)	scanf("%d", &b[i]);
	for (int i=1;i<=ce;i++) scanf("%d", &a[i]);
	b[cl + 1] = a[ ce + 1 ] = FFFF;

	scanf("%d", &q);
	for (int i=1;i<=q;i++)
	{
		scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
		int ymi = min(y1, y2), yma = max(y1, y2);

		int key2 = lower_bound(b+1, b+1+cl, ymi) - b;
		int key1 = lower_bound(a+1, a+1+ce, ymi) - a;

//		cout<<a[key1] <<' '<<b[key2]<<endl;
		int mi = FFFF;

		int dis = abs(x2 - x1);

		if (a[ key1 ] <= yma )
		{
			int ans = dis / v + (dis % v != 0) +  abs(y1 - y2);
			mi = min(mi, ans);
		}else
		{
			int ans = dis / v + (dis % v != 0) + abs(y1 - y2), f;

			if (key1 == 1)	f = abs(a[key1] - yma) * 2;
			else  f = min( abs(a[key1] - yma) , abs(a[key1 - 1] - ymi) ) * 2;

			mi = min(mi, ans + f );
		}

		if (b[ key2 ] <= yma )
		{
			int ans = abs(x1 - x2) + abs(y1 - y2);
			mi = min(mi, ans);

		}else
		{
			int ans = abs(x1 - x2) + abs(y1 - y2), f;
			if (key2 == 1) f = abs(b[key2] - yma) * 2;
			else f = min( abs(b[key2] - yma) ,  abs(b[key2 - 1] - ymi) ) * 2;

			mi = min(mi, ans + f);
		}

		printf("%d\n", mi);
	}
}
/*
5 6 0 1 3
5
1
1 3 5 4

3 3 0 1 2
2
1
1 1 3 3

1000000 1000000 0 1 999999
1000000
1
1 1 1000000 1

*/


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define maxn 100050
#define FFFF 1000000000
int n, m, cl, ce, v, q;
int a[ maxn ], b[ maxn ];
int x1, y1, x2, y2;
int main()
{
	scanf("%d %d %d %d %d", &n, &m, &cl, &ce, &v);
	for (int i=1;i<=cl;i++)	scanf("%d", &b[i]);
	for (int i=1;i<=ce;i++) scanf("%d", &a[i]);
	b[cl + 1] = a[ ce + 1 ] = FFFF;

	scanf("%d", &q);
	for (int i=1;i<=q;i++)
	{
		scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
		int ymi = min(y1, y2), yma = max(y1, y2);

		int key2 = lower_bound(b+1, b+1+cl, ymi) - b;
		int key1 = lower_bound(a+1, a+1+ce, ymi) - a;

		int mi = FFFF;

		int dis = abs(x2 - x1);
	//	cout<<ymi<<' '<<key1 <<"      "<<key2<<endl;

		if (a[ key1 ] <= yma)
		{
			int ans = (  ( dis % v == 0 ) ? dis/v : dis/v + 1  ) +  abs(y1 - y2);
			mi = min(ans, mi);
	//		cout<<"mi1="<<mi<<endl;
		}else
		{
			int ans = (  ( dis % v == 0 ) ? dis/v : dis/v + 1  ), f;

			if (key1 == 1)	f = abs(a[key1] - yma) * 2;
			else  f = abs(min(a[key1] - ymi, ymi - a[key1 - 1]) * 2);

			mi = min(mi, ans + f + abs(y1 - y2));
		//	cout<<"mi2="<<mi<<endl;
		}

		if (b[ key2 ] <= yma)
		{
			int ans = abs(x1 - x2) + abs(y1 - y2);
			mi = min(mi, ans);
	//		cout<<"mi3="<<mi<<endl;
		}else
		{
			int ans = abs(x1 - x2) + abs(y1 - y2), f;
			if (key2 == 1) f = abs(b[key2 - ymi]) * 2;
			else f = abs(min(b[key2] - ymi ,  ymi - b[key2 - 1]) * 2);

			mi = min(mi, ans + f);
	//		cout<<"mi4="<<mi<<endl;
		}

		printf("%d\n", mi);
	}
}


#include <iostream>
#include <string>

using namespace std;

string a[1000];

string Put (string s, int num) {
  string result="";
  for (int i = 0; i < num; i++)
    result += s;
  return result;
}

int main () {
  int n = 0;
  int maxLen = 0;
  while (getline(cin, a[n++])) {if (a[n-1].length() > maxLen) maxLen = a[n-1].length();}
  cout << Put("*", maxLen+2) << endl;
  int flag = 1;
  for (int i = 0; i < n; i++){
    int l = (maxLen - a[i].length())/2;
    int r = (maxLen - a[i].length())/2;
    if (l + r + a[i].length() != maxLen) {
      if (flag != 0) r++; else l++;
      flag = 1-flag;
    }
    cout << "*" << Put(" ", l) << a[i] << Put(" ", r) << "*" << endl;
  }
}


#include <string>
#include <iostream>

using namespace std;

string s[1000];
int sz = 0;

int main () {
  while (getline(cin, s[sz++]));
  int maxLen = 0;
  for (int i = 0; i < sz; i++)
    if (s[i].length() > maxLen) maxLen = s[i].length();
  for (int i = 0; i < maxLen+2; i++) cout << "*";
  cout << endl;
  bool ok = true;
  for (int i = 0; i < sz-1; i++) {
    cout << "*";
    int t1 = 0, t2 = 0;
    if (ok) t2 = 1; else t1 = 1;
    for (int j = 0; j < (maxLen-s[i].length()+t1)/2; j++)
      cout << " ";
    cout << s[i];
    for (int j = 0; j < (maxLen-s[i].length()+t2)/2; j++)
      cout << " ";
    cout << "*" << endl;
  }
  for (int i = 0; i < maxLen+2; i++) cout << "*";
}


#include <iostream>
#include <string>

using namespace std;

string a[1000];

string Put (string s, int num) {
  string result="";
  for (int i = 0; i < num; i++)
    result += s;
}

int main () {
  int n = 0;
  int maxLen = 0;
  while (getline(cin, a[n++])) {if (a[n-1].length() > maxLen) maxLen = a[n-1].length();}
  cout << Put("*", maxLen+2) << endl;
  int flag = 1;
  for (int i = 0; i < n; i++){
    int l = (maxLen - a[i].length())/2;
    int r = (maxLen - a[i].length())/2;
    if (l + r + a[i].length() != maxLen) {
      if (flag != 0) r++; else l++;
      flag = 1-flag;
    }
    cout << "*" << Put(" ", l) << a[i] << Put(" ", r) << "*" << endl;
  }
}


#include <string>
#include <iostream>

using namespace std;

string s[1000];
int sz = 0;

int main () {
  while (getline(cin, s[sz++]));
  int maxLen = 0;
  for (int i = 0; i < sz; i++)
    if (s[i].length() > maxLen) maxLen = s[i].length();
  for (int i = 0; i < maxLen+2; i++) cout << "*";
  cout << endl;
  for (int i = 0; i < sz; i++) {
    for (int j = 0; j < (maxLen-s[i].length()+1)/2; j++)
      cout << "*";
    cout << s[i];
    for (int j = 0; j < (maxLen-s[i].length())/2; j++)
      cout << "*";
    cout << endl;
  }
  for (int i = 0; i < maxLen+2; i++) cout << "*";
}


#include <string>
#include <iostream>

using namespace std;

string s[1000];
int sz = 0;

int main () {
  while (getline(cin, s[sz++]));
  int maxLen = 0;
  for (int i = 0; i < sz; i++)
    if (s[i].length() > maxLen) maxLen = s[i].length();
  for (int i = 0; i < maxLen+2; i++) cout << "*";
  cout << endl;
  for (int i = 0; i < sz-1; i++) {
    cout << "*";
    int t1 = 0, t2 = 0;
    if (s[i].length() == 1 || s[i].length() == 3) t1 = 1; else t2 = 1;
    for (int j = 0; j < (maxLen-s[i].length()+t1)/2; j++)
      cout << " ";
    cout << s[i];
    for (int j = 0; j < (maxLen-s[i].length()+t2)/2; j++)
      cout << " ";
    cout << "*" << endl;
  }
  for (int i = 0; i < maxLen+2; i++) cout << "*";
}


#include <iostream>
#include <string>

using namespace std;

string a[1000];

int main () {
  int n = 0;
  int maxLen = 0;
  while (getline(cin, a[n++])) {if (a[n-1].length() > maxLen) maxLen = a[n-1].length();}
  cout << string("*", maxLen+2) << endl;
  int flag = 1;
  for (int i = 0; i < n; i++){
    int l = (maxLen - a[i].length())/2;
    int r = (maxLen - a[i].length())/2;
    if (l + r + a[i].length() != maxLen) {
      if (flag != 0) r++; else l++;
      flag = 1-flag;
    }
    cout << "*" << string(" ", l) << a[i] << string(" ", r) << "*" << endl;
  }
}


#include <string>
#include <iostream>

using namespace std;

string s[1000];
int sz = 0;

int main () {
  while (getline(cin, s[sz++]));
  int maxLen = 0;
  for (int i = 0; i < sz; i++)
    if (s[i].length() > maxLen) maxLen = s[i].length();
  for (int i = 0; i < maxLen+2; i++) cout << "*";
  cout << endl;
  bool ok = true;
  for (int i = 0; i < sz-1; i++) {
    cout << "*";
    int t1 = 0, t2 = 0;
    if (ok) t1 = 1; else t2 = 1;
    for (int j = 0; j < (maxLen-s[i].length()+t1)/2; j++)
      cout << " ";
    cout << s[i];
    for (int j = 0; j < (maxLen-s[i].length()+t2)/2; j++)
      cout << " ";
    cout << "*" << endl;
  }
  for (int i = 0; i < maxLen+2; i++) cout << "*";
}


#include <iostream>
#include <string>

using namespace std;

string a[1000];

string Put (string s, int num) {
  string result="";
  for (int i = 0; i < num; i++)
    result += s;
  return result;
}

int main () {
  int n = 0;
  int maxLen = 0;
  while (getline(cin, a[n++])) {if (a[n-1].length() > maxLen) maxLen = a[n-1].length();}
  cout << Put("*", maxLen+2) << endl;
  int flag = 1;
  for (int i = 0; i < n-1; i++){
    int l = (maxLen - a[i].length())/2;
    int r = (maxLen - a[i].length())/2;
    if (l + r + a[i].length() != maxLen) {
      if (flag != 0) r++; else l++;
      flag = 1-flag;
    }
    cout << "*" << Put(" ", l) << a[i] << Put(" ", r) << "*" << endl;
  }
  cout << Put("*", maxLen+2) << endl;
}


#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <iostream>
#include <cmath>
#include <sstream>
#include <map>
#include <set>
#include <numeric>
#include <memory.h>
#include <cstdio>
#include <numeric>
#include <functional>

using namespace std;

#define pb push_back
#define INF 1000000001
#define FOR(i,a,b) for (int _n(b), i(a); i < _n; i++)
#define rep(i,n) FOR(i,0,n)
#define ford(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define CL(a,v) memset((a),(v),sizeof(a))
#define mp make_pair
#define X first
#define Y second
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))

typedef long long ll;
typedef vector<int> VI;
typedef pair<int,int> pii;

int main()
{
	#ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
	#endif

   char buff[1024];

   vector<string> dict;
   int mx = 0;

   while(cin.getline(buff,1024))
   {
       dict.pb( string(buff,buff+strlen(buff)) );
       mx = max(mx,(int)dict.back().size());
   }

   bool shift = true;

   rep(i,mx+2) cout << '*'; cout << endl;

   rep(i,dict.size())
   {
       cout << '*';

       int sp = (mx - dict[i].size()) >> 1;

       rep(k,sp) cout << ' ';
       if(dict[i].size() && mx%2 != dict[i].size()%2 && !shift) cout << ' ';
       cout << dict[i];
       if(dict[i].size() && mx%2 != dict[i].size()%2 && shift) cout << ' ';
       rep(k,sp) cout << ' ';

       cout << '*'; cout << endl;

       if(dict[i].size() && mx%2 != dict[i].size()%2) shift = !shift;
   }

   rep(i,mx+2) cout << '*'; cout << endl;

   return 0;
}


#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <iostream>
#include <cmath>
#include <sstream>
#include <map>
#include <set>
#include <numeric>
#include <memory.h>
#include <cstdio>
#include <numeric>
#include <functional>

using namespace std;

#define pb push_back
#define INF 1000000001
#define FOR(i,a,b) for (int _n(b), i(a); i < _n; i++)
#define rep(i,n) FOR(i,0,n)
#define ford(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define CL(a,v) memset((a),(v),sizeof(a))
#define mp make_pair
#define X first
#define Y second
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))

typedef long long ll;
typedef vector<int> VI;
typedef pair<int,int> pii;

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

   char buff[256];

   vector<string> dict;
   int mx = 0;

   while(cin.getline(buff,256))
   {
       int sh = 0;
       while(sh < strlen(buff) && buff[sh] == ' ') sh++;

       dict.pb( string(buff + sh,buff+strlen(buff)) );
       mx = max(mx,(int)dict.back().size());
   }

   bool shift = true;

   rep(i,mx+2) cout << '*'; cout << endl;

   rep(i,dict.size())
   {
       cout << '*';

       int sp = (mx - dict[i].size()) >> 1;

       rep(k,sp) cout << ' ';
       if(dict[i].size() && mx%2 != dict[i].size()%2 && !shift) cout << ' ';
       cout << dict[i];
       if(dict[i].size() && mx%2 != dict[i].size()%2 && shift) cout << ' ';
       rep(k,sp) cout << ' ';

       cout << '*'; cout << endl;

       if(dict[i].size() && mx%2 != dict[i].size()%2) shift = !shift;
   }

   rep(i,mx+2) cout << '*'; cout << endl;

   return 0;
}


#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <iostream>
#include <cmath>
#include <sstream>
#include <map>
#include <set>
#include <numeric>
#include <memory.h>
#include <cstdio>
#include <numeric>
#include <functional>

using namespace std;

#define pb push_back
#define INF 1000000001
#define FOR(i,a,b) for (int _n(b), i(a); i < _n; i++)
#define rep(i,n) FOR(i,0,n)
#define ford(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define CL(a,v) memset((a),(v),sizeof(a))
#define mp make_pair
#define X first
#define Y second
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))

typedef long long ll;
typedef vector<int> VI;
typedef pair<int,int> pii;

int main()
{
	#ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
	#endif

   char buff[1024];

   vector<string> dict;
   int mx = 0;

   while(cin.getline(buff,1024))
   {
       dict.pb( string(buff,buff+strlen(buff)) );
       mx = max(mx,(int)dict.back().size());
   }

   bool shift = true;

   rep(i,mx+2) cout << '*'; cout << endl;

   rep(i,dict.size())
   {
       cout << '*';

       if(dict[i].size()==0) dict[i] = string(' ', mx);

       int sp = (mx - dict[i].size()) >> 1;

       rep(k,sp) cout << ' ';
       if(dict[i].size() && mx%2 != dict[i].size()%2 && !shift) cout << ' ';
       cout << dict[i];
       if(dict[i].size() && mx%2 != dict[i].size()%2 && shift) cout << ' ';
       rep(k,sp) cout << ' ';

       cout << '*'; cout << endl;

       if(dict[i].size() && mx%2 != dict[i].size()%2) shift = !shift;
   }

   rep(i,mx+2) cout << '*'; cout << endl;

   return 0;
}


#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <iostream>
#include <cmath>
#include <sstream>
#include <map>
#include <set>
#include <numeric>
#include <memory.h>
#include <cstdio>
#include <numeric>
#include <functional>

using namespace std;

#define pb push_back
#define INF 1000000001
#define FOR(i,a,b) for (int _n(b), i(a); i < _n; i++)
#define rep(i,n) FOR(i,0,n)
#define ford(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define CL(a,v) memset((a),(v),sizeof(a))
#define mp make_pair
#define X first
#define Y second
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))

typedef long long ll;
typedef vector<int> VI;
typedef pair<int,int> pii;

int main()
{
	#ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
	#endif

   char buff[256];

   vector<string> dict;
   int mx = 0;

   while(cin.getline(buff,256))
   {
       int sh = 0;
       while(sh < strlen(buff) && buff[sh] == ' ') sh++;

       if(strlen(buff) > 100) continue;

       dict.pb( string(buff + sh,buff+strlen(buff)) );
       mx = max(mx,(int)dict.back().size());
   }

   bool shift = true;

   rep(i,mx+2) cout << '*'; cout << endl;

   rep(i,dict.size())
   {
       cout << '*';

       int sp = (mx - dict[i].size()) >> 1;

       rep(k,sp) cout << ' ';
       if(dict[i].size() && mx%2 != dict[i].size()%2 && !shift) cout << ' ';
       cout << dict[i];
       if(dict[i].size() && mx%2 != dict[i].size()%2 && shift) cout << ' ';
       rep(k,sp) cout << ' ';

       cout << '*'; cout << endl;

       if(dict[i].size() && mx%2 != dict[i].size()%2) shift = !shift;
   }

   rep(i,mx+2) cout << '*'; cout << endl;

   return 0;
}


#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <iostream>
#include <cmath>
#include <sstream>
#include <map>
#include <set>
#include <numeric>
#include <memory.h>
#include <cstdio>
#include <numeric>
#include <functional>

using namespace std;

#define pb push_back
#define INF 1000000001
#define FOR(i,a,b) for (int _n(b), i(a); i < _n; i++)
#define rep(i,n) FOR(i,0,n)
#define ford(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define CL(a,v) memset((a),(v),sizeof(a))
#define mp make_pair
#define X first
#define Y second
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))

typedef long long ll;
typedef vector<int> VI;
typedef pair<int,int> pii;

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

   char buff[1024];

   vector<string> dict;
   int mx = 0;

   while(cin.getline(buff,1024))
   {
       dict.pb( string(buff,buff+strlen(buff)) );
       mx = max(mx,(int)dict.back().size());
   }

   bool shift = true;

   rep(i,mx+2) cout << '*'; cout << endl;

   rep(i,dict.size())
   {
       cout << '*';

       int sp = (mx - dict[i].size()) >> 1;

       //if(dict[i].size()==0) dict[i] = string(' ', mx);

       rep(k,sp) cout << ' ';
       if(dict[i].size() && mx%2 != dict[i].size()%2 && !shift) cout << ' ';
       cout << dict[i];
       if(dict[i].size()==0) cout << string(mx, ' ');
       if(dict[i].size() && mx%2 != dict[i].size()%2 && shift) cout << ' ';
       rep(k,sp) cout << ' ';

       cout << '*'; cout << endl;

       if(dict[i].size() && mx%2 != dict[i].size()%2) shift = !shift;
   }

   rep(i,mx+2) cout << '*'; cout << endl;

   return 0;
}


#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <iostream>
#include <cmath>
#include <sstream>
#include <map>
#include <set>
#include <numeric>
#include <memory.h>
#include <cstdio>
#include <numeric>
#include <functional>

using namespace std;

#define pb push_back
#define INF 1000000001
#define FOR(i,a,b) for (int _n(b), i(a); i < _n; i++)
#define rep(i,n) FOR(i,0,n)
#define ford(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define CL(a,v) memset((a),(v),sizeof(a))
#define mp make_pair
#define X first
#define Y second
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))

typedef long long ll;
typedef vector<int> VI;
typedef pair<int,int> pii;

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

   char buff[1024];

   vector<string> dict;
   int mx = 0;

   while(cin.getline(buff,1024))
   {
       dict.pb( string(buff,buff+strlen(buff)) );
       mx = max(mx,(int)dict.back().size());
   }

   bool shift = true;

   rep(i,mx+2) cout << '*'; cout << endl;

   rep(i,dict.size())
   {
       cout << '*';

       int sp = (mx - dict[i].size()) >> 1;

       //if(dict[i].size()==0) dict[i] = string(' ', mx);

       rep(k,sp) cout << ' ';
       if(dict[i].size() && mx%2 != dict[i].size()%2 && !shift) cout << ' ';
       cout << dict[i];
       if(dict[i].size()==0 && sp==0) cout << string(mx, ' ');
       if(dict[i].size() && mx%2 != dict[i].size()%2 && shift) cout << ' ';
       rep(k,sp) cout << ' ';

       cout << '*'; cout << endl;

       if(dict[i].size() && mx%2 != dict[i].size()%2) shift = !shift;
   }

   rep(i,mx+2) cout << '*'; cout << endl;

   return 0;
}


#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <iostream>
#include <cmath>
#include <sstream>
#include <map>
#include <set>
#include <numeric>
#include <memory.h>
#include <cstdio>
#include <numeric>
#include <functional>

using namespace std;

#define pb push_back
#define INF 1000000001
#define FOR(i,a,b) for (int _n(b), i(a); i < _n; i++)
#define rep(i,n) FOR(i,0,n)
#define ford(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define CL(a,v) memset((a),(v),sizeof(a))
#define mp make_pair
#define X first
#define Y second
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))

typedef long long ll;
typedef vector<int> VI;
typedef pair<int,int> pii;

int main()
{
	#ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
	#endif

   char buff[1024];

   vector<string> dict;
   int mx = 0;

   while(cin.getline(buff,256))
   {
       dict.pb( string(buff,buff+strlen(buff)) );
       mx = max(mx,(int)dict.back().size());
   }

   bool shift = true;

   rep(i,mx+2) cout << '*'; cout << endl;

   rep(i,dict.size())
   {
       cout << '*';

       int sp = (mx - dict[i].size()) >> 1;

       rep(k,sp) cout << ' ';
       if(dict[i].size() && mx%2 != dict[i].size()%2 && !shift) cout << ' ';
       cout << dict[i];
       if(dict[i].size() && mx%2 != dict[i].size()%2 && shift) cout << ' ';
       rep(k,sp) cout << ' ';

       cout << '*'; cout << endl;

       if(dict[i].size() && mx%2 != dict[i].size()%2) shift = !shift;
   }

   rep(i,mx+2) cout << '*'; cout << endl;

   return 0;
}


#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <iostream>
#include <cmath>
#include <sstream>
#include <map>
#include <set>
#include <numeric>
#include <memory.h>
#include <cstdio>
#include <numeric>
#include <functional>

using namespace std;

#define pb push_back
#define INF 1000000001
#define FOR(i,a,b) for (int _n(b), i(a); i < _n; i++)
#define rep(i,n) FOR(i,0,n)
#define ford(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define CL(a,v) memset((a),(v),sizeof(a))
#define mp make_pair
#define X first
#define Y second
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))

typedef long long ll;
typedef vector<int> VI;
typedef pair<int,int> pii;

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

   char buff[1024];

   vector<string> dict;
   int mx = 0;

   while(cin.getline(buff,1024))
   {
       dict.pb( string(buff,buff+strlen(buff)) );
       mx = max(mx,(int)dict.back().size());
   }

   bool shift = true;

   rep(i,mx+2) cout << '*'; cout << endl;

   rep(i,dict.size())
   {
       cout << '*';

       int sp = (mx - dict[i].size()) >> 1;

       //if(dict[i].size()==0) dict[i] = string(' ', mx);

       rep(k,sp) cout << ' ';
       if(mx%2 != dict[i].size()%2 && !shift) cout << ' ';
       cout << dict[i];

       if(mx%2 != dict[i].size()%2 && shift) cout << ' ';
       rep(k,sp) cout << ' ';

       cout << '*'; cout << endl;

       if(dict[i].size() && mx%2 != dict[i].size()%2) shift = !shift;
   }

   rep(i,mx+2) cout << '*'; cout << endl;

   return 0;
}


#include <bits/stdc++.h>
//Damned if I don't take what I want.

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 10000;

string s[MAXN]; int n;

int main(){
	ios::sync_with_stdio( false );
	cout.tie(0); cin.tie(0);

	int m = 0;
	while(getline(cin, s[n++]) && s[n -1] != "###") m = max(m, (int)s[n-1].size());
	n--;

	cout << string(m + 2, '*') << "\n";
	int x = 0;
	for (int i = 0; i < n; i++){
		int p = m  / 2 - s[i].size() / 2;
		if (m % 2 != s[i].size() % 2) {
			x++;
			if (x % 2) p--;
		}
		cout << "*";
		if (p) cout << string(p, ' ');
		cout << s[i];
		if (m - p - s[i].size()) cout << string(m - p - s[i].size(), ' ');
		cout << "*";
		cout << "\n";
	}

	cout << string(m + 2, '*') << "\n";
}


#include <bits/stdc++.h>
//Damned if I don't take what I want.

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 10000;

string s[MAXN]; int n;

int main(){
	ios::sync_with_stdio( false );
	cout.tie(0); cin.tie(0);

	int m = 0;
	while(getline(cin, s[n++]) && s[n -1] != "###") m = max(m, (int)s[n-1].size());
	n--;

	cout << string(m + 2, '*') << "\n";
	int x = 0;
	for (int i = 0; i < n; i++){
		int p = m / 2 - s[i].size() / 2;
		if (m % 2 != s[i].size() % 2) {
			x++;
			if (m % 2) p++;
			if (x % 2) p--;
		}
		cout << "*";
		if (p) cout << string(p, ' ');
		cout << s[i];
		if (m - p - s[i].size()) cout << string(m - p - s[i].size(), ' ');
		cout << "*";
		cout << "\n";
	}

	cout << string(m + 2, '*') << "\n";
}


#include <bits/stdc++.h>
//Damned if I don't take what I want.

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 1010;

string s[MAXN]; int n;

int main(){
	ios::sync_with_stdio( false );
	cout.tie(0); cin.tie(0);

	int m = 0;
	while(getline(cin, s[n++]) && s[n - 1] != "#")
		m = max(m, (int)s[n-1].size());
	n--;

	cout << string(m + 2, '*') << "\n";
	int x = 0;
	for (int i = 0; i < n; i++){
		int p = m  / 2 - s[i].size() / 2;
		if (m % 2 != s[i].size() % 2) {
			x++;
			if (x % 2) p--;
		}
		cout << "*";
		if (p) cout << string(p, ' ');
		cout << s[i];
		if (m - p - s[i].size()) cout << string(m - p - s[i].size(), ' ');
		cout << "*";
		cout << "\n";
	}

	cout << string(m + 2, '*') << "\n";
}


#include <bits/stdc++.h>
//Damned if I don't take what I want.

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 10000;

string s[MAXN]; int n;

int main(){
	ios::sync_with_stdio( false );
	cout.tie(0); cin.tie(0);

	int m = 0;
	while(getline(cin, s[n++]) && s[n -1] != "###") m = max(m, (int)s[n-1].size());
	n--;

	cout << string(m + 2, '*') << "\n";
	int x = 1;
	for (int i = 0; i < n; i++){
		int p = (m + 1)  / 2 - (s[i].size() + 1) / 2;
		if (m % 2 != s[i].size() % 2) {
			x++;
			if (x % 2) p++;
		}
		cout << "*";
		if (p) cout << string(p, ' ');
		cout << s[i];
		if (m - p - s[i].size()) cout << string(m - p - s[i].size(), ' ');
		cout << "*";
		cout << "\n";
	}

	cout << string(m + 2, '*') << "\n";
}


#include <bits/stdc++.h>
//Damned if I don't take what I want.

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 1010;

string s[MAXN]; int n;

int main(){
	ios::sync_with_stdio( false );
	cout.tie(0); cin.tie(0);

	int m = 0;
	while(getline(cin, s[n++]) && s[n - 1] != "#")
		m = max(m, (int)s[n-1].size());
	n--;

	cout << string(m + 2, '*') << "\n";
	for (int i = 0; i < n; i++){
		int p = (m + 1) / 2 - (s[i].size() + 1) / 2;
		//if (m % 2 != s[i].size() % 2) p--;
		cout << "*";
		if (p) cout << string(p, ' ');
		cout << s[i];
		if (m - p - s[i].size()) cout << string(m - p - s[i].size(), ' ');
		cout << "*";
		cout << "\n";
	}

	cout << string(m + 2, '*') << "\n";
}


#include <bits/stdc++.h>
//Damned if I don't take what I want.

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 1010;

string s[MAXN]; int n;

int main(){
	ios::sync_with_stdio( false );
	cout.tie(0); cin.tie(0);

	int m = 0;
	while(getline(cin, s[n++])) m = max(m, (int)s[n-1].size());
	n--;

	cout << string(m + 2, '*') << "\n";
	int x = 0;
	for (int i = 0; i < n; i++){
		int p = m  / 2 - s[i].size() / 2;
		if (m % 2 != s[i].size() % 2) {
			x++;
			if (x % 2) p--;
		}
		cout << "*";
		if (p) cout << string(p, ' ');
		cout << s[i];
		if (m - p - s[i].size()) cout << string(m - p - s[i].size(), ' ');
		cout << "*";
		cout << "\n";
	}

	cout << string(m + 2, '*') << "\n";
}


#include <iostream>

using namespace std;

const int MAXN = 1000 + 100;
string arr[MAXN];

int32_t main()
{
	string s;
	int max_l = 0;
	int pt = 0;
	while (getline(cin, arr[pt]))
	{
		max_l = max(max_l, (int)arr[pt].size());
		pt++;
	}
	int n = pt;
	for (int i = 0; i < max_l + 2; i++)
		cout << '*';
	cout << '\n';
	for (int i = 0; i < n; i++)
	{
		cout << '*';
		if (i % 2 == 1)
		{
			for (int j = 0; j < (max_l - arr[i].size() + 1) / 2; j++)
				cout << ' ';
			cout << arr[i];
			for (int j = 0; j < (max_l - arr[i].size()) / 2; j++)
				cout << ' ';
			cout << '*' << '\n';
		}
		else
		{
			for (int j = 0; j < (max_l - arr[i].size()) / 2; j++)
				cout << ' ';
			cout << arr[i];
			for (int j = 0; j < (max_l - arr[i].size() + 1) / 2; j++)
				cout << ' ';
			cout << '*' << '\n';
		}
	}
	for (int i = 0; i < max_l + 2; i++)
		cout << '*';
	cout << '\n';
}


#include <iostream>

using namespace std;

const int MAXN = 1000 + 100;
string arr[MAXN];

int32_t main()
{
	string s;
	int max_l = 0;
	int pt = 0;
	while (getline(cin, arr[pt]))
	{
		max_l = max(max_l, (int)arr[pt].size());
		pt++;
	}
	int n = pt;
	for (int i = 0; i < max_l + 2; i++)
		cout << '*';
	cout << '\n';
	for (int i = 0; i < n; i++)
	{
		cout << '*';
		for (int j = 0; j < (max_l - arr[i].size() + 1) / 2; j++)
			cout << ' ';
		cout << arr[i];
		for (int j = 0; j < (max_l - arr[i].size()) / 2; j++)
			cout << ' ';
		cout << '*' << '\n';
	}
	for (int i = 0; i < max_l + 2; i++)
		cout << '*';
	cout << '\n';
}


#include <iostream>

using namespace std;

const int MAXN = 1000 + 100;
string arr[MAXN];

int32_t main()
{
	string s;
	int max_l = 0;
	int pt = 0;
	while (getline(cin, arr[pt]))
	{
		max_l = max(max_l, (int)arr[pt].size());
		pt++;
	}
	int n = pt;
	for (int i = 0; i < max_l + 2; i++)
		cout << '*';
	cout << '\n';
	for (int i = 0; i < n; i++)
	{
		cout << '*';
		if (i % 2 == 0)
		{
			for (int j = 0; j < (max_l - arr[i].size() + 1) / 2; j++)
				cout << ' ';
			cout << arr[i];
			for (int j = 0; j < (max_l - arr[i].size()) / 2; j++)
				cout << ' ';
			cout << '*' << '\n';
		}
		else
		{
			for (int j = 0; j < (max_l - arr[i].size()) / 2; j++)
				cout << ' ';
			cout << arr[i];
			for (int j = 0; j < (max_l - arr[i].size() + 1) / 2; j++)
				cout << ' ';
			cout << '*' << '\n';
		}
	}
	for (int i = 0; i < max_l + 2; i++)
		cout << '*';
	cout << '\n';
}


#include <iostream>

using namespace std;

const int MAXN = 1000 + 100;
string arr[MAXN];

int32_t main()
{
	string s;
	int max_l = 0;
	int pt = 0;
	while (getline(cin, arr[pt]))
	{
		max_l = max(max_l, (int)arr[pt].size());
		cerr << pt << endl;
		pt++;
	}
	int n = pt;
	for (int i = 0; i < max_l + 2; i++)
		cout << '*';
	cout << '\n';
	pt = 0;
	for (int i = 0; i < n; i++)
	{
		cout << '*';
		if ((max_l - arr[i].size()) % 2 == 1)
		{
			pt++;
			if (pt % 2 == 1)
			{
				for (int j = 0; j < (max_l - arr[i].size()) / 2; j++)
					cout << ' ';
				cout << arr[i];
				for (int j = 0; j < (max_l - arr[i].size() + 1) / 2; j++)
					cout << ' ';
			}
			else
			{
				for (int j = 0; j < (max_l - arr[i].size() + 1) / 2; j++)
					cout << ' ';
				cout << arr[i];
				for (int j = 0; j < (max_l - arr[i].size()) / 2; j++)
					cout << ' ';
			}
		}
		else
		{
			for (int j = 0; j < (max_l - arr[i].size()) / 2; j++)
				cout << ' ';
			cout << arr[i];
			for (int j = 0; j < (max_l - arr[i].size()) / 2; j++)
				cout << ' ';
		}
		cout << '*' << '\n';
	}
	for (int i = 0; i < max_l + 2; i++)
		cout << '*';
	cout << '\n';
}


#include <iostream>

using namespace std;

const int MAXN = 1000 + 100;
string arr[MAXN];

int32_t main()
{
	string s;
	int max_l = 0;
	int pt = 0;
	while (getline(cin, arr[pt]))
	{
		max_l = max(max_l, (int)arr[pt].size());
		pt++;
	}
	int n = pt;
	for (int i = 0; i < max_l + 2; i++)
		cout << '*';
	cout << '\n';
	for (int i = 0; i < n; i++)
	{
		cout << '*';
		for (int j = 0; j < (max_l - arr[i].size()) / 2; j++)
			cout << ' ';
		cout << arr[i];
		for (int j = 0; j < (max_l - arr[i].size() + 1) / 2; j++)
			cout << ' ';
		cout << '*' << '\n';
	}
	for (int i = 0; i < max_l + 2; i++)
		cout << '*';
	cout << '\n';
}


#include <iostream>

using namespace std;

const int MAXN = 1000 + 100;
string arr[MAXN];

int32_t main()
{
	string s;
	int max_l = 0;
	int pt = 0;
	while (getline(cin, arr[pt]))
	{
		max_l = max(max_l, (int)arr[pt].size());
		pt++;
	}
	int n = pt;
	for (int i = 0; i < max_l + 2; i++)
		cout << '*';
	cout << '\n';
	for (int i = 0; i < n; i++)
	{
		cout << '*';
		for (int j = 0; j < (max_l - arr[i].size()) / 2; j++)
			cout << ' ';
		cout << arr[i];
		for (int j = 0; j < (max_l - arr[i].size() + 1) / 2; j++)
			cout << ' ';
		cout << '*' << '\n';
	}
}


#include <iostream>

using namespace std;

const int MAXN = 1000 + 100;
string arr[MAXN];

int32_t main()
{
	string s;
	int max_l = 0;
	int pt = 0;
	while (getline(cin, arr[pt]))
	{
		max_l = max(max_l, (int)arr[pt].size());
		cerr << pt << endl;
		pt++;
	}
	int n = pt;
	for (int i = 0; i < max_l + 2; i++)
		cout << '*';
	cout << '\n';
	for (int i = 0; i < n; i++)
	{
		cout << '*';
		if (i % 2 == 0)
		{
			for (int j = 0; j < (max_l - arr[i].size()) / 2; j++)
				cout << ' ';
			cout << arr[i];
			for (int j = 0; j < (max_l - arr[i].size() + 1) / 2; j++)
				cout << ' ';
			cout << '*' << '\n';
		}
		else
		{
			for (int j = 0; j < (max_l - arr[i].size() + 1) / 2; j++)
				cout << ' ';
			cout << arr[i];
			for (int j = 0; j < (max_l - arr[i].size()) / 2; j++)
				cout << ' ';
			cout << '*' << '\n';
		}
	}
	for (int i = 0; i < max_l + 2; i++)
		cout << '*';
	cout << '\n';
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF )  return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
char a[110][110];
int main()
{
    int x,y,z,i=0,t;
    while(gets(a[i]))i++;
    y=0;
    for(t=0;t<i;t++)
    if(y<strlen(a[t]))
    y=strlen(a[t]);
    for(t=0;t<=y+1;t++)
    cout<<'*';
    cout<<endl;
    int flag=0;
    for(t=0;t<i;t++)
    {
        cout<<"*";
        if(strlen(a[t])!=0)
        {
            int cha=y-strlen(a[t]);
            int zuo=cha/2,you=cha/2;
            if(flag==0)
            {
                if(cha%2!=0)
                you++,flag=!flag;
                for(int j=0;j<zuo;j++)
                cout<<" ";
                cout<<a[t];
                for(int j=0;j<you;j++)
                cout<<" ";
            }
            else
            {
                if(cha%2!=0)
                zuo++,flag=!flag;
                for(int j=0;j<zuo;j++)
                cout<<" ";
                cout<<a[t];
                for(int j=0;j<you;j++)
                cout<<" ";
            }
        }
        else
        {
            for(int j=0;j<y;j++)
            cout<<" ";
        }
        cout<<"*"<<endl;
    }
    for(t=0;t<=y+1;t++)
    cout<<'*';
    cout<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
char a[100][1010];
int main()
{
    int x,y,z,i=0,t;
    while(gets(a[i]))i++;
    y=0;
    for(t=0;t<i;t++)
    if(y<strlen(a[t]))
    y=strlen(a[t]);
    for(t=0;t<=y+1;t++)
    cout<<'*';
    cout<<endl;
    for(t=0;t<i;t++)
    {
        cout<<"*";
        for(int j=0;j<(y-strlen(a[t]))/2;j++)
        cout<<" ";
        cout<<a[t];
        for(int j=0;j<y-(y-strlen(a[t]))/2-strlen(a[t]);j++)
        cout<<" ";
        cout<<'*'<<endl;
    }
    for(t=0;t<=y+1;t++)
    cout<<'*';
    cout<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
char a[100][1010];
int main()
{
    int x,y,z,i=0,t;
    while(gets(a[i]))i++;
    y=0;
    for(t=0;t<i;t++)
    if(y<strlen(a[t]))
    y=strlen(a[t]);
    for(t=0;t<=y+1;t++)
    cout<<'*';
    cout<<endl;
    for(t=0;t<i;t++)
    {
        cout<<"*";
        for(int j=0;j<(y-strlen(a[t]))/2;j++)
        cout<<" ";
        cout<<a[t];
        for(int j=0;j<y-(y-strlen(a[t]))/2-strlen(a[t]);j++)
        cout<<" ";
        cout<<'*'<<endl;
    }
    for(t=0;t<=y+1;t++)
    cout<<'*';
    cout<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF )  return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
char a[1010][1010];
int main()
{
    int x,y,z,i=0,t;
    while(gets(a[i]))i++;
    y=0;
    for(t=0;t<i;t++)
    if(y<strlen(a[t]))
    y=strlen(a[t]);
    for(t=0;t<=y+1;t++)
    cout<<'*';
    cout<<endl;
    int flag=0;
    for(t=0;t<i;t++)
    {
        cout<<"*";
        if(strlen(a[t])!=0)
        {
            int cha=y-strlen(a[t]);
            int zuo=cha/2,you=cha/2;
            if(flag==0)
            {
                if(cha%2!=0)
                you++,flag=!flag;
                for(int j=0;j<zuo;j++)
                cout<<" ";
                cout<<a[t];
                for(int j=0;j<you;j++)
                cout<<" ";
            }
            else
            {
                if(cha%2!=0)
                zuo++,flag=!flag;
                for(int j=0;j<zuo;j++)
                cout<<" ";
                cout<<a[t];
                for(int j=0;j<you;j++)
                cout<<" ";
            }
        }
        else
        {
            for(int j=0;j<y;j++)
            cout<<" ";
        }
        cout<<"*"<<endl;
    }
    for(t=0;t<=y+1;t++)
    cout<<'*';
    cout<<endl;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
char a[100][1010];
int main()
{
    int x,y,z,i=0,t;
    while(gets(a[i]))i++;
    y=0;
    for(t=0;t<i;t++)
    if(y<strlen(a[t]))
    y=strlen(a[t]);
    for(t=0;t<=y+1;t++)
    cout<<'*';
    cout<<endl;
    for(t=0;t<i;t++)
    {
        cout<<"*";
        for(int j=0;j<(y-strlen(a[t]))/2;j++)
        cout<<" ";
        cout<<a[t];
        for(int j=0;j<y-(y-strlen(a[t]))/2-strlen(a[t]);j++)
        cout<<" ";
        cout<<'*'<<endl;
    }
    for(t=0;t<=y+1;t++)
    cout<<'*';
    cout<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF )  return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
char a[1010][1010];
int main()
{
    int x,y,z,i=0,t;
    while(gets(a[i]))i++;
    y=0;
    for(t=0;t<i;t++)
    if(y<strlen(a[t]))
    y=strlen(a[t]);
    for(t=0;t<=y+1;t++)
    cout<<'*';
    cout<<endl;
    int flag=0;
    for(t=0;t<i;t++)
    {
        cout<<"*";
        if(strlen(a[t])!=0)
        {
            int cha=y-strlen(a[t]);
            int zuo=cha/2,you=cha/2;
            if(flag==0)
            {
                if(cha%2!=0)
                you++;
                for(int j=0;j<zuo;j++)
                cout<<" ";
                cout<<a[t];
                for(int j=0;j<you;j++)
                cout<<" ";
            }
            else
            {
                if(cha%2!=0)
                zuo++;
                for(int j=0;j<zuo;j++)
                cout<<" ";
                cout<<a[t];
                for(int j=0;j<you;j++)
                cout<<" ";
            }
            if(strlen(a[t])!=y)
            flag=!flag;
        }
        else
        {
            for(int j=0;j<y;j++)
            cout<<" ";
        }
        cout<<"*"<<endl;
    }
    for(t=0;t<=y+1;t++)
    cout<<'*';
    cout<<endl;
    return 0;
}


#include<cstdio>
#include<cstring>
using namespace std;
char text[1001][1001];
int top=-1,maxl=-1,len[1001];

int main()
{
   int i,j,l,sp,sp2;
   while(gets(text[++top]))
   {
      l=strlen(text[top]);
      maxl=l>maxl?l:maxl;
      len[top]=l;
      if(text[top][0]==' '||text[top][l-1]==' ') break;
   }
   top--;
   for(i=0;i<maxl+2;i++) printf("*");
   printf("\n");
   for(i=0;i<=top;i++)
   {
       sp=sp2=(maxl-len[i])/2;
       if((maxl-len[i])%2) sp++;
       printf("*");
       for(j=0;j<sp;j++) printf(" ");
       printf("%s",text[i]);
       for(j=0;j<sp2;j++) printf(" ");
       printf("*\n");
   }
   for(i=0;i<maxl+2;i++) printf("*");
   printf("\n");
   return 0;
}


#include<cstdio>
#include<cstring>
using namespace std;
char text[1001][1001];
int top=-1,maxl=-1,len[1001];

int main()
{
   int i,j,l,sp,sp2,n=0;
   while(gets(text[++top]))
   {
      l=strlen(text[top]);
      maxl=l>maxl?l:maxl;
      len[top]=l;
      if(text[top][0]==' '||text[top][l-1]==' ') break;
   }
   top--;
   for(i=0;i<maxl+2;i++) printf("*");
   printf("\n");
   for(i=0;i<=top;i++)
   {
       sp=sp2=(maxl-len[i])/2;
       if((maxl-len[i])%2){
        if(n==0) sp2++;
        else sp++;
        n=1;
       }
       printf("*");
       for(j=0;j<sp;j++) printf(" ");
       printf("%s",text[i]);
       for(j=0;j<sp2;j++) printf(" ");
       printf("*\n");
   }
   for(i=0;i<maxl+2;i++) printf("*");
   printf("\n");
   return 0;
}


#include<cstdio>
#include<cstring>
using namespace std;
char text[1001][1001];
int top=-1,maxl=-1,len[1001];

int main()
{
   int i,j,l,sp,sp2,n=0;
   while(gets(text[++top]))
   {
      l=strlen(text[top]);
      maxl=l>maxl?l:maxl;
      len[top]=l;
      if(text[top][0]==' '||text[top][l-1]==' ') break;
   }
   top--;
   for(i=0;i<maxl+2;i++) printf("*");
   printf("\n");
   for(i=0;i<=top;i++)
   {
       sp=sp2=(maxl-len[i])/2;
       if((maxl-len[i])%2){
        if(n%2==0) sp2++;
        else sp++;
        n++;
       }
       printf("*");
       for(j=0;j<sp;j++) printf(" ");
       printf("%s",text[i]);
       for(j=0;j<sp2;j++) printf(" ");
       printf("*\n");
   }
   for(i=0;i<maxl+2;i++) printf("*");
   printf("\n");
   return 0;
}


#include<cstdio>
#include<cstring>
using namespace std;
char text[1001][1001];
int top=-1,maxl=-1,len[1001];

int main()
{
   int i,j,l,sp,sp2;
   while(gets(text[++top]))
   {
      l=strlen(text[top]);
      maxl=l>maxl?l:maxl;
      len[top]=l;
      if(text[top][0]==' '||text[top][l-1]==' ') break;
   }
   top--;
   for(i=0;i<maxl+2;i++) printf("*");
   printf("\n");
   for(i=0;i<=top;i++)
   {
       sp=sp2=(maxl-len[i])/2;
       if((maxl-len[i])%2) sp2++;
       printf("*");
       for(j=0;j<sp;j++) printf(" ");
       printf("%s",text[i]);
       for(j=0;j<sp2;j++) printf(" ");
       printf("*\n");
   }
   for(i=0;i<maxl+2;i++) printf("*");
   printf("\n");
   return 0;
}


#include<cstdio>
#include<cstring>
using namespace std;
char text[1001][1001];
int top=-1,maxl=-1,len[1001];

int main()
{
   int i,j,l,sp,sp2,n=0;
   while(gets(text[++top]))
   {
      l=strlen(text[top]);
      maxl=l>maxl?l:maxl;
      len[top]=l;
      if(text[top][0]==' '||text[top][l-1]==' ') break;
   }
   top--;
   for(i=0;i<maxl+2;i++) printf("*");
   printf("\n");
   for(i=0;i<=top;i++)
   {
       sp=sp2=(maxl-len[i])/2;
       if((maxl-len[i])%2){
        if(!n%2) sp2++;
        else sp++;
        n++;
       }
       printf("*");
       for(j=0;j<sp;j++) printf(" ");
       printf("%s",text[i]);
       for(j=0;j<sp2;j++) printf(" ");
       printf("*\n");
   }
   for(i=0;i<maxl+2;i++) printf("*");
   printf("\n");
   return 0;
}


#include<cstdio>
#include<cstring>
using namespace std;
char text[1001][1001];
int top=-1,maxl=-1,len[1001];

int main()
{
   int i,j,l,sp,sp2,n=0;
   while(gets(text[++top]))
   {
      l=strlen(text[top]);
      maxl=l>maxl?l:maxl;
      len[top]=l;
      if(text[top][0]==' '||text[top][l-1]==' ') break;
   }
   top--;
   for(i=0;i<maxl+2;i++) printf("*");
   printf("\n");
   for(i=0;i<=top;i++)
   {
       sp=sp2=(maxl-len[i])/2;
       if((maxl-len[i])%2){
        if(!n) sp2++;
        else sp++;
        n=1;
       }
       printf("*");
       for(j=0;j<sp;j++) printf(" ");
       printf("%s",text[i]);
       for(j=0;j<sp2;j++) printf(" ");
       printf("*\n");
   }
   for(i=0;i<maxl+2;i++) printf("*");
   printf("\n");
   return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[5030][5030];
int leng[10030];
int main()
{
    while (gets(s[0])!=NULL)
    {
        int cnt=0,maxn=strlen(s[0]);
        leng[cnt]=strlen(s[cnt]);
       // getchar();
        while(gets(s[++cnt])!=NULL)
        {
            if (maxn<strlen(s[cnt]))
            {
                maxn=strlen(s[cnt]);

            }
            leng[cnt]=strlen(s[cnt]);
        }

        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");

        int count=0,flag=1,ch=0;
        while (count<cnt)
        {
           ch=0;
           printf("*");
           for (int i=0;i<(maxn-leng[count])/2;i++)
            printf(" ");

           if ((maxn-leng[count])%2!=0 && leng[count]!=maxn && flag==0){
            printf(" ");
            ch=1;
           }
           printf("%s",s[count]);
           if ((maxn-leng[count])%2!=0 && leng[count]!=maxn && flag==1){
            printf(" ");
            ch=1;
           }
           for (int i=0;i<(maxn-leng[count])/2;i++)
            printf(" ");
           printf("*");
           printf("\n");
           count++;
           if (ch) flag=!flag;
        }
        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[5030][5030],leng[10030];
int main()
{
    while (gets(s[0])!=NULL)
    {
        int cnt=0,maxn=strlen(s[0]);
        leng[cnt]=strlen(s[cnt]);
       // getchar();
        while(gets(s[++cnt])!=NULL)
        {
            if (maxn<strlen(s[cnt]))
            {
                maxn=strlen(s[cnt]);

            }
            leng[cnt]=strlen(s[cnt]);
        }

        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");

        int count=0,flag=1,ch=0;
        while (count<cnt)
        {
           ch=0;
           printf("*");
           for (int i=0;i<(maxn-leng[count])/2;i++)
            printf(" ");

           if ((maxn-leng[count])%2!=0 && !flag){
            printf(" ");

           }

           printf("%s",s[count]);

           if ((maxn-leng[count])%2!=0){
            if(flag) printf(" ");
            flag=!flag;
           }

           for (int i=0;i<(maxn-leng[count])/2;i++)
            printf(" ");
           printf("*");
           printf("\n");
           count++;
        }
        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[5030][5030],leng[10030];
int main()
{
    while (gets(s[0])!=NULL)
    {
        int cnt=0,maxn=strlen(s[0]);
        leng[cnt]=strlen(s[cnt]);
       // getchar();
        while(gets(s[++cnt])!=NULL)
        {
            if (maxn<strlen(s[cnt]))
            {
                maxn=strlen(s[cnt]);

            }
            leng[cnt]=strlen(s[cnt]);
        }

        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");

        int count=0,flag=1,ch=0;
        while (count<cnt)
        {
           int l=strlen(s[count]);
          int t=maxn-l;
           printf("*");
           for (int i=0;i<t/2;i++)
            printf(" ");

           if (t%2!=0 && !flag){
            printf(" ");

           }

           printf("%s",s[count]);

           if (t%2!=0){
            if(flag) printf(" ");
            flag=!flag;
           }

           for (int i=0;i<t/2;i++)
            printf(" ");
           printf("*");
           printf("\n");
           count++;
        }
        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[5030][5030],leng[10030];
int main()
{
    while (gets(s[0])!=NULL)
    {
        int cnt=0,maxn=strlen(s[0]);
        leng[cnt]=strlen(s[cnt]);
       // getchar();
        while(gets(s[++cnt])!=NULL)
        {
            if (maxn<strlen(s[cnt]))
            {
                maxn=strlen(s[cnt]);

            }
            leng[cnt]=strlen(s[cnt]);
        }

        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");

        int count=0,flag=1,ch=0,i,j;
       for(i=0;i<cnt;++i)
    {
        printf("*");
        int l=strlen(s[i]);
        int t=maxn-l;
        if(t&1)
            if(!flag) printf(" ");

        for(j=1;j<=t/2;++j) printf(" ");

        printf("%s",s[i]);

        for(j=1;j<=t/2;++j) printf(" ");
        if(t&1)
        {
            if(flag) printf(" ");
            flag=!flag;
        }
        printf("*");
        puts("");
    }
        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[5030][5030],leng[10030];
int main()
{
    while (gets(s[0])!=NULL)
    {
        int cnt=0,maxn=strlen(s[0]);
        leng[cnt]=strlen(s[cnt]);
       // getchar();
        while(gets(s[++cnt])!=NULL)
        {
            if (maxn<strlen(s[cnt]))
            {
                maxn=strlen(s[cnt]);

            }
            leng[cnt]=strlen(s[cnt]);
        }

        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");

        int count=0,flag=1,ch=0;
        while (count<cnt)
        {
           int l=strlen(s[i]);
          int t=maxn-l;
           printf("*");
           for (int i=0;i<t/2;i++)
            printf(" ");

           if (t%2!=0 && !flag){
            printf(" ");

           }

           printf("%s",s[count]);

           if (t%2!=0){
            if(flag) printf(" ");
            flag=!flag;
           }

           for (int i=0;i<(maxn-leng[count])/2;i++)
            printf(" ");
           printf("*");
           printf("\n");
           count++;
        }
        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[1030][1030],leng[1030];
int main()
{
    while (gets(s[0])!=NULL)
    {
        int cnt=0,maxn=strlen(s[0]);
        leng[cnt]=strlen(s[cnt]);
       // getchar();
        while(gets(s[++cnt])!=NULL)
        {
            if (maxn<strlen(s[cnt]))
            {
                maxn=strlen(s[cnt]);

            }
            leng[cnt]=strlen(s[cnt]);
        }

        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");

        int count=0,flag=1,ch=0;
        while (count<cnt)
        {
           ch=0;
           printf("*");
           for (int i=0;i<(maxn-leng[count])/2;i++)
            printf(" ");

           if ((maxn-leng[count])%2!=0 && leng[count]!=maxn && flag==0){
            printf(" ");
            ch=1;
           }
           printf("%s",s[count]);
           if ((maxn-leng[count])%2!=0 && leng[count]!=maxn && flag==1){
            printf(" ");
            ch=1;
           }
           for (int i=0;i<(maxn-leng[count])/2;i++)
            printf(" ");
           printf("*");
           printf("\n");
           count++;
           if (ch) flag=!flag;
        }
        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[5030][5030],leng[10030];
int main()
{
    while (gets(s[0])!=NULL)
    {
        int cnt=0,maxn=strlen(s[0]);
        leng[cnt]=strlen(s[cnt]);
       // getchar();
        while(gets(s[++cnt])!=NULL)
        {
            if (maxn<strlen(s[cnt]))
            {
                maxn=strlen(s[cnt]);

            }
            leng[cnt]=strlen(s[cnt]);
        }

        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");

        int count=0,flag=1,ch=0;
        while (count<cnt)
        {
           ch=0;
           printf("*");
           for (int i=0;i<(maxn-leng[count])/2;i++)
            printf(" ");

           if ((maxn-leng[count])%2!=0 && leng[count]!=maxn && flag==0 && strlen(s[count])!=0){
            printf(" ");
            ch=1;
           }
           printf("%s",s[count]);
           if ((maxn-leng[count])%2!=0 && leng[count]!=maxn && flag==1 && strlen(s[count])!=0){
            printf(" ");
            ch=1;
           }
           if ((maxn-leng[count])%2!=0 && leng[count]!=maxn && flag==1 && strlen(s[count])==0)
           {
               printf(" ");
           }
           if ((maxn-leng[count])%2!=0 && leng[count]!=maxn && flag==0 && strlen(s[count])==0)
           {
               printf(" ");
           }
           for (int i=0;i<(maxn-leng[count])/2;i++)
            printf(" ");
           printf("*");
           printf("\n");
           count++;
           if (ch) flag=!flag;
        }
        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[5030][5030],leng[10030];
int main()
{
    while (gets(s[0])!=NULL)
    {
        int cnt=0,maxn=strlen(s[0]);
        leng[cnt]=strlen(s[cnt]);
       // getchar();
        while(gets(s[++cnt])!=NULL)
        {
            if (maxn<strlen(s[cnt]))
            {
                maxn=strlen(s[cnt]);

            }
            leng[cnt]=strlen(s[cnt]);
        }

        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");

        int count=0,flag=1,ch=0;
        while (count<cnt)
        {
           int l=strlen(s[count]);
          int t=maxn-l;
           printf("*");
           for (int i=0;i<t/2;i++)
            printf(" ");

           if (t%2!=0 && !flag){
            printf(" ");

           }

           printf("%s",s[count]);

           if (t%2!=0){
            if(flag) printf(" ");
            flag=!flag;
           }

           for (int i=0;i<(maxn-leng[count])/2;i++)
            printf(" ");
           printf("*");
           printf("\n");
           count++;
        }
        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[5030][5030],leng[10030];
int main()
{
    while (gets(s[0])!=NULL)
    {
        int cnt=0,maxn=strlen(s[0]);
        leng[cnt]=strlen(s[cnt]);
       // getchar();
        while(gets(s[++cnt])!=NULL)
        {
            if (maxn<strlen(s[cnt]))
            {
                maxn=strlen(s[cnt]);

            }
            leng[cnt]=strlen(s[cnt]);
        }

        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");

        int count=0,flag=1,ch=0;
        while (count<cnt)
        {
           int l=strlen(s[count]);
          int t=maxn-l;
           printf("*");
           for (int i=0;i<t/2;i++)
            printf(" ");

           if (t%2!=0 && !flag){
            printf(" ");

           }

           printf("%s",s[count]);

           if (t%2!=0){
            if(flag) printf(" ");
            flag=!flag;
           }

           for (int i=0;i<(maxn-strlen(s[count]))/2;i++)
            printf(" ");
           printf("*");
           printf("\n");
           count++;
        }
        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[10030][10030],leng[10030];
int main()
{
    while (gets(s[0])!=NULL)
    {
        int cnt=0,maxn=strlen(s[0]);
        leng[cnt]=strlen(s[cnt]);
       // getchar();
        while(gets(s[++cnt])!=NULL)
        {
            if (maxn<strlen(s[cnt]))
            {
                maxn=strlen(s[cnt]);

            }
            leng[cnt]=strlen(s[cnt]);
        }

        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");

        int count=0,flag=1,ch=0;
        while (count<cnt)
        {
           ch=0;
           printf("*");
           for (int i=0;i<(maxn-leng[count])/2;i++)
            printf(" ");

           if ((maxn-leng[count])%2!=0 && leng[count]!=maxn && flag==0){
            printf(" ");
            ch=1;
           }
           printf("%s",s[count]);
           if ((maxn-leng[count])%2!=0 && leng[count]!=maxn && flag==1){
            printf(" ");
            ch=1;
           }
           for (int i=0;i<(maxn-leng[count])/2;i++)
            printf(" ");
           printf("*");
           printf("\n");
           count++;
           if (ch) flag=!flag;
        }
        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[5030][5030],leng[10030];
int main()
{
    while (gets(s[0])!=NULL)
    {
        int cnt=0,maxn=strlen(s[0]);
        leng[cnt]=strlen(s[cnt]);
       // getchar();
        while(gets(s[++cnt])!=NULL)
        {
            if (maxn<strlen(s[cnt]))
            {
                maxn=strlen(s[cnt]);

            }
            leng[cnt]=strlen(s[cnt]);
        }

        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");

        int count=0,flag=1,ch=0;
        while (count<cnt)
        {
           ch=0;
           printf("*");
           for (int i=0;i<(maxn-leng[count])/2;i++)
            printf(" ");

           if ((maxn-leng[count])%2!=0 && leng[count]!=maxn && flag==0){
            printf(" ");
            ch=1;
           }
           printf("%s",s[count]);
           if ((maxn-leng[count])%2!=0 && leng[count]!=maxn && flag==1){
            printf(" ");
            ch=1;
           }
           for (int i=0;i<(maxn-leng[count])/2;i++)
            printf(" ");
           printf("*");
           printf("\n");
           count++;
           if (ch) flag=!flag;
        }
        for (int i=0;i<=maxn+1;i++)
         printf("*");
         printf("\n");
    }
}


#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector <string> textarray;
    string s,maxs,star;

    getline(cin,s);
    textarray.push_back(s);
    maxs=textarray[0];

    while (getline(cin,s))
    {
        textarray.push_back(s);
        if (s.length()>maxs.length()) maxs=s;
    }

    for (int i=0; i<maxs.length()+2; i++)
        star+='*';

    for (int i=0; i<textarray.size(); i++)
    {
        while (textarray[i].length() < maxs.length())
        {
            if ((i+1)%2==0)
                {
                    textarray[i]=' '+textarray[i];
                    if (textarray[i].length()>=maxs.length()) break;
                    textarray[i]=textarray[i]+' ';
                }
            else
            {
                textarray[i]=textarray[i]+' ';
                if (textarray[i].length()>=maxs.length()) break;
                textarray[i]=' '+textarray[i];
            }
        }
    }

    cout << star << endl;
    for (int i=0; i<textarray.size(); i++)
        cout << '*' << textarray.at(i) << '*' << endl;
    cout << star;

    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
    vector <string> textarray;
    string s,maxs,star;

    getline(cin,s);
    textarray.push_back(s);
    maxs=textarray[0];

    for (int i=1; i!=EOF; i++)
    {
        getline(cin,s);
        textarray.push_back(s);
        if (s.length()>maxs.length()) maxs=s;
    }

    for (int i=0; i<maxs.length()+2; i++)
        star+='*';

    for (int i=0; i<textarray.size(); i++)
    {
        while (textarray[i].length() < maxs.length())
        {
            textarray[i]=textarray[i]+' ';
            if (textarray[i].length()>=maxs.length()) break;
            textarray[i]=' '+textarray[i];
        }
    }

    cout << star << endl;
    for (int i=0; i<textarray.size(); i++)
        cout << '*' << textarray.at(i) << '*' << endl;
    cout << star;

    return 0;
}


#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector <string> textarray;
    string s,maxs,star;

    getline(cin,s);
    textarray.push_back(s);
    maxs=textarray[0];

    for (int i=1; i<1000; i++)
    {
        getline(cin,s);
        textarray.push_back(s);
        if (s.length()>maxs.length()) maxs=s;
    }

    for (int i=0; i<maxs.length()+2; i++)
        star+='*';

    for (int i=0; i<textarray.size(); i++)
    {
        while (textarray[i].length() < maxs.length())
        {
            textarray[i]=textarray[i]+' ';
            if (textarray[i].length()>=maxs.length()) break;
            textarray[i]=' '+textarray[i];
        }
    }

    cout << star << endl;
    for (int i=0; i<textarray.size(); i++)
        cout << '*' << textarray.at(i) << '*' << endl;
    cout << star;

    return 0;
}


#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector <string> textarray;
    string s,maxs,star;

    getline(cin,s);
    textarray.push_back(s);
    maxs=textarray[0];

    while (getline(cin,s))
    {
        textarray.push_back(s);
        if (s.length()>maxs.length()) maxs=s;
    }

    for (int i=0; i<maxs.length()+2; i++)
        star+='*';

    for (int i=0; i<textarray.size(); i++)
    {
        while (textarray[i].length() < maxs.length())
        {
            textarray[i]=textarray[i]+' ';
            if (textarray[i].length()>=maxs.length()) break;
            textarray[i]=' '+textarray[i];
        }
    }

    cout << star << endl;
    for (int i=0; i<textarray.size(); i++)
        cout << '*' << textarray.at(i) << '*' << endl;
    cout << star;

    return 0;
}


#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector <string> textarray;
    string s,maxs,star;

    getline(cin,s);
    textarray.push_back(s);
    maxs=textarray[0];

    while (getline(cin,s))
    {
        textarray.push_back(s);
        if (s.length()>maxs.length()) maxs=s;
    }

    for (int i=0; i<maxs.length()+2; i++)
        star+='*';

    for (int i=0; i<textarray.size(); i++)
    {
        while (textarray[i].length() < maxs.length())
        {
            textarray[i]=' '+textarray[i];
            if (textarray[i].length()>=maxs.length()) break;
            textarray[i]=textarray[i]+' ';
        }
    }

    cout << star << endl;
    for (int i=0; i<textarray.size(); i++)
        cout << '*' << textarray.at(i) << '*' << endl;
    cout << star;

    return 0;
}


#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector <string> textarray;
    string s,maxs,star;
    bool ok=false;

    getline(cin,s);
    textarray.push_back(s);
    maxs=textarray[0];

    while (getline(cin,s))
    {
        textarray.push_back(s);
        if (s.length()>maxs.length()) maxs=s;
    }

    for (int i=0; i<maxs.length()+2; i++)
        star+='*';

    for (int i=0; i<textarray.size(); i++)
    {
        if ((maxs.length()%2)==(textarray[i].length()%2))
            while (textarray[i].length() < maxs.length()) textarray[i]=' '+textarray[i]+' ';
        else if (ok)
        {
            ok=!ok;
            while (textarray[i].length() < maxs.length())
                {
                    textarray[i]=' '+textarray[i];
                    if (textarray[i].length()>=maxs.length()) break;
                    textarray[i]=textarray[i]+' ';
                }
        }
        else
            {
                ok=!ok;
                while (textarray[i].length() < maxs.length())
                {
                    textarray[i]=textarray[i]+' ';
                    if (textarray[i].length()>=maxs.length()) break;
                    textarray[i]=' '+textarray[i];
                }

            }
    }

    cout << star << endl;
    for (int i=0; i<textarray.size(); i++)
        cout << '*' << textarray.at(i) << '*' << endl;
    cout << star;

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
#define MD 1000000007
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
char s[1010][110];
int main()
{
    //freopen("H:\\in.txt","r",stdin);
    //freopen("H:\\out.txt","w",stdout);
    int k = 0;
    int mmax = -1;
    while (gets(s[k]) != NULL)
    {
        mmax = max(mmax, (int)strlen(s[k]));
        k++;
    }
    for (int i = 0; i < mmax + 2; i++)
       printf("*");
    printf("\n");
    for (int i = 0; i < k; i++)
    {
       printf("*");
        int flag = (mmax - strlen(s[i])) / 2;
        for (int j = 0; j < flag; j++)
            printf(" ");
        printf("%s", s[i]);
        for (int j = flag + strlen(s[i]); j < mmax; j++)
            printf(" ");
       printf("*");
        printf("\n");
    }
    for (int i = 0; i < mmax + 2; i++)
        printf("*");
    printf("\n");
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
#define MD 1000000007
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
char s[1010][110];
int main()
{
    //freopen("H:\\in.txt","r",stdin);
    //freopen("H:\\out.txt","w",stdout);
    int k = 0;
    int mmax = -1;
    while (gets(s[k]) != NULL)
    {
        mmax = max(mmax, (int)strlen(s[k]));
        k++;
    }
    for (int i = 0; i < mmax + 2; i++)
       printf("*");
    printf("\n");
    bool ok = true;
    for (int i = 0; i < k; i++)
    {
       printf("*");
        int flag = (mmax - strlen(s[i])) / 2;
        if ((mmax - strlen(s[i])) % 2 == 0)
        {
        for (int j = 0; j < flag; j++)
            printf(" ");
        printf("%s", s[i]);
        for (int j = 0; j < flag; j++)
            printf(" ");
        }
        else if (ok)
        {
            for (int j = 0; j < flag; j++)
            printf(" ");
        printf("%s", s[i]);
        for (int j = 0; j < flag + 1; j++)
            printf(" ");
        ok = false;
        }
        else
        {
            for (int j = 0; j < flag + 1; j++)
                printf(" ");
            printf("%s", s[i]);
            for (int j = 0; j < flag; j++)
                printf(" ");
            ok = true;
        }
       printf("*");
        printf("\n");
    }
    for (int i = 0; i < mmax + 2; i++)
        printf("*");
    printf("\n");
    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define M 1010

char s[M][M];
int l[M];

int main(){
	int n = 0, mx = 0, st, en;
	while(gets(s[n])){
		l[n] = strlen(s[n]); if(mx < l[n]) mx = l[n]; n++;
	}
	int fl = 0;
	int m = mx + 2;
	for(int i = 0; i < m; i++) putchar('*'); puts("");
	for(int i = 0; i < n; i++){
		st = (mx - l[i]) / 2;
		if(((mx-l[i])&1)) {if(fl) st++; fl ^= 1;}
		en = st + l[i];
		putchar('*'); for(int j = 0; j < st; j++) putchar(' ');
		printf("%s", s[i]); for(int j = en; j < mx; j++) putchar(' '); putchar('*');puts("");
	}
	for(int i = 0; i < m; i++) putchar('*');
	return 0;
}


#include <cstdio>
#include <cstring>

#define N 1010

char s[N][N];
int len[N];
int MaxLen;

main() {
//	freopen("4620.in", "r", stdin);
	int n;
	MaxLen = 0;
	while (gets(s[n]) > 0) {
		len[n] = strlen(s[n]);
		if (MaxLen < len[n]) MaxLen = len[n];
		n ++;
	}
	for (int j = 0; j < MaxLen + 2; j ++) putchar('*'); puts("");
	bool bf = 0;
	for (int i = 0; i < n; i ++) {
		putchar('*');
		int rem = MaxLen - len[i];
		if (!len[i]) {
			for (int j = 0; j < rem; j ++) putchar(' ');
		} else if (rem & 1) {
			if (bf) {
				for (int j = 0; j < (rem + 1) / 2; j ++) putchar(' ');
				for (int j = 0; j < len[i]; j ++) putchar(s[i][j]);
				for (int j = 0; j < rem / 2; j ++) putchar(' ');
			} else {
				for (int j = 0; j < rem / 2; j ++) putchar(' ');
				for (int j = 0; j < len[i]; j ++) putchar(s[i][j]);
				for (int j = 0; j < (rem + 1) / 2; j ++) putchar(' ');
			}
			bf ^= 1;
		} else {
			for (int j = 0; j < rem / 2; j ++) putchar(' ');
			for (int j = 0; j < len[i]; j ++) putchar(s[i][j]);
			for (int j = 0; j < rem / 2; j ++) putchar(' ');
		}
		puts("*");
	}
	for (int j = 0; j < MaxLen + 2; j ++) putchar('*'); puts("");
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define M 1100

char data[M][M];

int main(){
	int mnl = 0, cnt = 0, i, j, k;
	while(gets(data[++ cnt]) > 0){
		int l = strlen(data[cnt]);
		mnl = max(mnl, l);
	}
	for(i = 0; i < mnl + 2; i ++) printf("*"); puts("");
	bool fl = 0;
	for(i = 1; i < cnt; i ++){
		printf("*");
		int len = strlen(data[i]);
		if(!len){ for(j = 0; j < mnl; j ++) printf(" "); printf("*\n");	continue; }
		int t = (mnl - len) / 2;
		if((mnl-len)%2){
			if(!fl) for(j = 0; j < t; j ++) printf(" ");
			else for(j = 0; j < t + 1; j ++) printf(" ");
			printf("%s", data[i]);
			if(!fl) for(j = 0; j < t + 1; j ++) printf(" ");
			else for(j = 0; j < t; j ++) printf(" ");
			printf("*\n");
			fl = 1 - fl;
		}else {
			for(j = 0; j < t; j ++) printf(" ");
			printf("%s", data[i]);
			for(j = 0; j < t; j ++) printf(" ");
			printf("*\n");
		}
	}
	for(i = 0; i < mnl + 2; i ++) printf("*");
	return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int n = 0;
string s[1005];

int main()
{
    while(getline(cin,s[n])) n++;
    int width = 0;
    for(int i = 0; i < n; i++)
        if(width < s[i].size()) width = s[i].size();

    for(int i = 0; i < width + 2; i++)
        printf("*");
    printf("\n");
    for(int i = 0; i < n; i++)
    {
        printf("*");
        int l = (width - s[i].size()) >> 1;
        int r = width - s[i].size() - l;
        for(int j = 0; j < l; j++)
            printf(" ");
        cout << s[i];
        for(int j = 0; j < r; j++)
            printf(" ");
        printf("*\n");
    }
    for(int i = 0; i < width + 2; i++)
        printf("*");
    printf("\n");

    return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int n = 0;
string s[1005];

int main()
{
    while(getline(cin,s[n])) n++;
    int width = 0;
    for(int i = 0; i < n; i++)
        if(width < s[i].size()) width = s[i].size();

    for(int i = 0; i < width + 2; i++)
        printf("*");
    printf("\n");
    int cnt = 0;
    for(int i = 0; i < n; i++)
    {
        printf("*");
        int l = (width - s[i].size()) >> 1;
        int r = width - s[i].size() - l;
        if(l != r) cnt++;
        if(cnt % 2 == 0) swap(l, r);
        for(int j = 0; j < l; j++)
            printf(" ");
        cout << s[i];
        for(int j = 0; j < r; j++)
            printf(" ");
        printf("*\n");
    }
    for(int i = 0; i < width + 2; i++)
        printf("*");
    printf("\n");

    return 0;
}