#include<bits/stdc++.h>
using namespace std;
int t[100001];
struct line
{
	int s,t;
	int next;
}a[200001];
int head[100001];
int edge;
inline void add(int s,int t)
{
	a[edge].next=head[s];
	head[s]=edge;
	a[edge].s=s;
	a[edge].t=t;
}
bool v[100001];
int dis[100001][101];
int Q[200001];
inline bool bfs(int x,int d)
{
//	memset(v,false,sizeof(v));
	dis[x][d]=0;
	int l=1,r=1;
	//Q.push(x);
	Q[r]=x;
	while(l<=r)
	{
	//	int dd=Q.front();
		int dd=Q[l];
		l++;
	//	Q.pop();
		int i;
		for(i=head[dd];i!=0;i=a[i].next)
		{
			int t=a[i].t;
		//	if(!v[t])
		//	{
				if(dis[dd][d]+1<dis[t][d])
				{
					dis[t][d]=dis[dd][d]+1;
				//	Q.push(t);
					r++;
					Q[r]=t;
				}
		//	}
		}
	}
}
int main()
{
	int n,m,s,k;
	scanf("%d%d%d%d",&n,&m,&k,&s);
	int i,j;
	for(i=1;i<=n;i++)
		scanf("%d",&t[i]);
	int ss,tt;
	for(i=1;i<=m;i++)
	{
		scanf("%d%d",&ss,&tt);
		edge++;
		add(ss,tt);
		edge++;
		add(tt,ss);
	}
	memset(dis,127/3,sizeof(dis));
	for(i=1;i<=n;i++)
		bfs(i,t[i]);
/*	for(i=1;i<=n;i++)
	{
		for(j=1;j<=k;j++)
			printf("%d ",dis[i][j]);
		printf("\n");
	}*/
	for(i=1;i<=n;i++)
		sort(dis[i]+1,dis[i]+1+k);
/*	for(i=1;i<=n;i++)
	{
		for(j=1;j<=k;j++)
			printf("%d ",dis[i][j]);
		printf("\n");
	}*/
	for(i=1;i<=n;i++)
	{
		int sum=0;
		for(j=1;j<=s;j++)
			sum+=dis[i][j];
		printf("%d\n",sum);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
inline char nc(){
    static char buf[6000000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,6000000,stdin),p1==p2)?EOF:*p1++;
}
inline int read(){
    char ch=nc();int sum=0;
    while(!(ch>='0'&&ch<='9'))ch=nc();
    while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc();
    return sum;
}
int t[100001];
struct line
{
	int s,t;
	int next;
}a[200001];
int head[100001];
int edge;
inline void add(int s,int t)
{
	a[edge].next=head[s];
	head[s]=edge;
	a[edge].s=s;
	a[edge].t=t;
}
bool v[100001];
int dis[100001][101];
int Q[200001];
inline bool bfs(int x,int d)
{
	memset(v,false,sizeof(v));
	dis[x][d]=0;
	int l=1,r=1;
	//Q.push(x);
	Q[r]=x;
	v[x]=true;
	while(l<=r)
	{
	//	int dd=Q.front();
		int dd=Q[l];
		l++;
	//	Q.pop();
		int i;
		for(i=head[dd];i!=0;i=a[i].next)
		{
			int t=a[i].t;
		//	if(!v[t])
		//	{
				if(dis[dd][d]+1<dis[t][d])
				{
					dis[t][d]=dis[dd][d]+1;
					if(!v[t])
					{
						v[t]=true;
				//	Q.push(t);
						r++;
						Q[r]=t;
					}
				}
		//	}
		}
	}
}
int main()
{
	int n,m,s,k;
//	scanf("%d%d%d%d",&n,&m,&k,&s);
	n=read();
	m=read();
	k=read();
	s=read();
	int i,j;
	for(i=1;i<=n;i++)
		//scanf("%d",&t[i]);
		t[i]=read();
	int ss,tt;
	for(i=1;i<=m;i++)
	{
	//	scanf("%d%d",&ss,&tt);
		ss=read();
		tt=read();
		edge++;
		add(ss,tt);
		edge++;
		add(tt,ss);
	}
	memset(dis,127/3,sizeof(dis));
	for(i=1;i<=n;i++)
		bfs(i,t[i]);
/*	for(i=1;i<=n;i++)
	{
		for(j=1;j<=k;j++)
			printf("%d ",dis[i][j]);
		printf("\n");
	}*/
	for(i=1;i<=n;i++)
	{
		sort(dis[i]+1,dis[i]+1+k);
		int sum=0;
		for(j=1;j<=s;j++)
			sum+=dis[i][j];
		printf("%d\n",sum);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
inline char nc(){
    static char buf[6000000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,6000000,stdin),p1==p2)?EOF:*p1++;
}
inline int read(){
    char ch=nc();int sum=0;
    while(!(ch>='0'&&ch<='9'))ch=nc();
    while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc();
    return sum;
}
int t[1000001];
struct line
{
	int s,t;
	int next;
}a[2000001];
int head[1000001];
int edge;
inline void add(int s,int t)
{
	a[edge].next=head[s];
	head[s]=edge;
	a[edge].s=s;
	a[edge].t=t;
}
bool v[1000001];
int dis[1000001][101];
int Q[2000001];
inline bool bfs(int x,int d)
{
	memset(v,false,sizeof(v));
	dis[x][d]=0;
	int l=1,r=1;
	//Q.push(x);
	Q[r]=x;
	v[x]=true;
	while(l<=r)
	{
	//	int dd=Q.front();
		int dd=Q[l];
		l++;
	//	Q.pop();
		int i;
		for(i=head[dd];i!=0;i=a[i].next)
		{
			int t=a[i].t;
		//	if(!v[t])
		//	{
				if(dis[dd][d]+1<dis[t][d])
				{
					dis[t][d]=dis[dd][d]+1;
					if(!v[t])
					{
						v[t]=true;
				//	Q.push(t);
						r++;
						Q[r]=t;
					}
				}
		//	}
		}
	}
}
int main()
{
	int n,m,s,k;
//	scanf("%d%d%d%d",&n,&m,&k,&s);
	n=read();
	m=read();
	k=read();
	s=read();
	int i,j;
	for(i=1;i<=n;i++)
		//scanf("%d",&t[i]);
		t[i]=read();
	int ss,tt;
	for(i=1;i<=m;i++)
	{
	//	scanf("%d%d",&ss,&tt);
		ss=read();
		tt=read();
		edge++;
		add(ss,tt);
		edge++;
		add(tt,ss);
	}
	memset(dis,127/3,sizeof(dis));
	for(i=1;i<=n;i++)
	{
		bfs(i,t[i]);
		assert(i<=50000);
	}
/*	for(i=1;i<=n;i++)
	{
		for(j=1;j<=k;j++)
			printf("%d ",dis[i][j]);
		printf("\n");
	}*/
	for(i=1;i<=n;i++)
	{
		sort(dis[i]+1,dis[i]+1+k);
		int sum=0;
		for(j=1;j<=s;j++)
			sum+=dis[i][j];
		printf("%d\n",sum);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
inline char nc(){
    static char buf[6000000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,6000000,stdin),p1==p2)?EOF:*p1++;
}
inline int read(){
    char ch=nc();int sum=0;
    while(!(ch>='0'&&ch<='9'))ch=nc();
    while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc();
    return sum;
}
struct th
{
	int x,p;
	bool operator <(th y) const
	{
		return x<y.x;
	}
}t[1000001];
struct line
{
	int s,t;
	int next;
}a[2000001];
int head[1000001];
int edge;
inline void add(int s,int t)
{
	a[edge].next=head[s];
	head[s]=edge;
	a[edge].s=s;
	a[edge].t=t;
}
bool v[1000001];
int dis[1000001][101];
//int Q[2000001];
queue<int> Q;
inline bool bfs(int d)
{
//	memset(v,false,sizeof(v));
//	dis[x][d]=0;
//	int l=1,r=1;
//	Q.push(x);
//	Q[r]=x;
	//v[x]=true;
	while(!Q.empty())
	{
		int dd=Q.front();
	//	int dd=Q[l];
	//	l++;
		Q.pop();
		int i;
		for(i=head[dd];i!=0;i=a[i].next)
		{
			int t=a[i].t;
		//	if(!v[t])
		//	{
				if(dis[dd][d]+1<dis[t][d])
				{
					dis[t][d]=dis[dd][d]+1;
				//	if(!v[t])
				//	{
				//		v[t]=true;
						Q.push(t);
				//		r++;
				//		Q[r]=t;
				//	}
				}
		//	}
		}
	}
}
int main()
{
	int n,m,s,k;
	scanf("%d%d%d%d",&n,&m,&k,&s);
	//n=read();
//	m=read();
//	k=read();
//	s=read();
	int i,j;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&t[i]);
	//	t[i].x=read();
		t[i].p=i;
	}
	int ss,tt;
	for(i=1;i<=m;i++)
	{
		scanf("%d%d",&ss,&tt);
	//	ss=read();
	//	tt=read();
		edge++;
		add(ss,tt);
		edge++;
		add(tt,ss);
	}
	memset(dis,127/3,sizeof(dis));
	sort(t+1,t+1+n);
	for(i=1;i<=n;i++)
	{
		dis[t[i].p][t[i].x]=0;

		if(t[i].x==t[i-1].x)
		{

		}
		else
			bfs(t[i-1].x);
		Q.push(t[i].p);
	}
	bfs(t[i-1].x);
		//assert(i<=200);
//	}
/*	for(i=1;i<=n;i++)
	{
		for(j=1;j<=k;j++)
			printf("%d ",dis[i][j]);
		printf("\n");
	}*/
	for(i=1;i<=n;i++)
	{
		sort(dis[i]+1,dis[i]+1+k);
		int sum=0;
		for(j=1;j<=s;j++)
			sum+=dis[i][j];
		printf("%d\n",sum);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10,inf=0x3f3f3f3f3f;
int n,m,s,k,ans[maxn];
vector<int> edge[maxn],color[maxn];
int dis[107][maxn];
void bfs(vector<int>color,int *dis){
    fill(dis+1,dis+1+n,-1);
    queue<int>q;
    for (auto u:color){
        dis[u]=0;
        q.push(u);
    }
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for (int v:edge[u])
            if(dis[v]==-1) dis[v]=dis[u]+1,q.push(v);
    }
}
int main(){
    scanf("%d%d%d%d",&n,&m,&k,&s);
    for (int i=1,tmp;i<=n;i++) scanf("%d",&tmp),color[tmp].push_back(i);
    for (int i=1,u,v;i<=m;i++){
        scanf("%d%d",&u,&v);
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    for (int i=1;i<=k;i++) bfs(color[i],dis[i]);
    for (int i=1;i<=n;i++){
        for (int j=1;j<=k;j++)
            ans[j]=dis[j][i];
        sort(ans+1,ans+1+k);
        printf("%d",accumulate(ans+1,ans+1+s,0));
        printf("%c",i==n?'\n':' ');
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10,inf=0x3f3f3f3f3f;
int n,m,s,k,ans[maxn];
vector<int> edge[maxn],color[maxn];
int dis[107][maxn];
void bfs(vector<int>color,int *dis){
    fill(dis+1,dis+1+n,-1);
    queue<int>q;
    for (auto u:color){
        dis[u]=0;
        q.push(u);
    }
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for (int v:edge[u])
            if(dis[v]==-1) dis[v]=dis[u]+1,q.push(v);
    }
}
int main(){
    scanf("%d%d%d%d",&n,&m,&k,&s);
    for (int i=1,tmp;i<=n;i++) scanf("%d",&tmp),color[tmp].push_back(i);
    for (int i=1,u,v;i<=m;i++){
        scanf("%d%d",&u,&v);
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    for (int i=1;i<=k;i++) bfs(color[i],dis[i]);
    for (int i=1;i<=n;i++){
        for (int j=1;j<=k;j++)
            ans[j]=dis[j][i];
        sort(ans+1,ans+1+k);
        printf("%d",accumulate(ans+1,ans+1+s,0));
        printf("%c",i==n?'\n':' ');
    }
    return 0;
}
————————————————


#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int maxn = 10e5+5;
int n,m,k,s;
int a[maxn];
bool in_que[maxn];

vector <int>goods[maxn];
int list[maxn][100+5] = {0};
vector <int>chart[maxn];

struct bit{
	int id;
	int money;
};

void bfs(int x){

	queue<bit> q;

	for (int i = 0; i<goods[x].size();i++){
		bit tmp;
		tmp.id=goods[x][i];
		tmp.money=0;
		//in_que[i]=true;  i?
		in_que[tmp.id] = true;
		q.push(tmp);
	}

	//cur是当前的

	while(!q.empty()){

		bit cur = q.front();
		q.pop();
		list[cur.id][x] = min(cur.money,list[cur.id][x]);
		for (int i = 0; i < chart[cur.id].size(); i++){

			int next = chart[cur.id][i];
			if (!in_que[next]){

				bit tmp2;
				tmp2.id=next;
				tmp2.money=cur.money+1;
				q.push(tmp2);
				in_que[tmp2.id]=true;

			}

		}

	}

}

int main(){
	cin >> n >> m >> k >> s;
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		goods[a[i]].push_back(i);
	}

	for (int i = 1; i <= m; i++){
		int u,v;
		cin >> u >> v;
		chart[u].push_back(v);
		chart[v].push_back(u);
	}

	memset(list,0x3f,sizeof(list));

	for (int i = 1; i <= k; i++){
		memset(in_que,false,sizeof(in_que));
		bfs(i);
	}

	for(int i=1;i<=n;i++){
        sort(list[i]+1,list[i]+k+1);
        long long sum=0;
        for(int j=1;j<=s;j++){
            sum+=list[i][j];
        }
        cout << sum;
        if (i < n) cout << " ";
    }
    cout << endl;

    return 0;

}


#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int maxn = 10e5+5;
int n,m,k,s;
int a[maxn];
bool in_que[maxn];

vector <int>goods[maxn];
int list[maxn][100+5] = {0};
vector <int>chart[maxn];

struct bit{
	int id;
	int money;
};

void bfs(int x){

	queue<bit> q;

	for (int i = 0; i<goods[x].size();i++){
		bit tmp;
		tmp.id=goods[x][i];
		tmp.money=0;
		//in_que[i]=true;  i?
		in_que[tmp.id] = true;
		q.push(tmp);
	}

	//cur是当前的

	while(!q.empty()){

		bit cur = q.front();
		q.pop();
		list[cur.id][x] = min(cur.money,list[cur.id][x]);
		for (int i = 0; i < chart[cur.id].size(); i++){

			int next = chart[cur.id][i];
			if (!in_que[next]){

				bit tmp2;
				tmp2.id=next;
				tmp2.money=cur.money+1;
				q.push(tmp2);
				in_que[tmp2.id]=true;

			}

		}

	}

}

int main(){
	freopen("a.in","r",stdin);
	cin >> n >> m >> k >> s;
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		goods[a[i]].push_back(i);
	}

	for (int i = 1; i <= m; i++){
		int u,v;
		cin >> u >> v;
		chart[u].push_back(v);
		chart[v].push_back(u);
	}

	memset(list,0x3f,sizeof(list));

	for (int i = 1; i <= k; i++){
		memset(in_que,false,sizeof(in_que));
		bfs(i);
	}

	for(int i=1;i<=n;i++){
        sort(list[i]+1,list[i]+k+1);
        long long sum=0;
        for(int j=1;j<=s;j++){
            sum+=list[i][j];
        }
        cout << sum;
        if (i < n) cout << " ";
    }
    cout << endl;

    return 0;

}


#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int maxn = 10e5+5;
int n,m,k,s;
int a[maxn];
bool in_que[maxn];

vector <int>goods[maxn];
int list[maxn][100+5] = {0};
vector <int>chart[maxn];

struct bit{
	int id;
	int money;
};

void bfs(int x){

	queue<bit> q;

	for (int i = 0; i<goods[x].size();i++){
		bit tmp;
		tmp.id=goods[x][i];
		tmp.money=0;
		//in_que[i]=true;  i?
		in_que[tmp.id] = true;
		q.push(tmp);
	}

	//cur是当前的

	while(!q.empty()){

		bit cur = q.front();
		q.pop();
		list[cur.id][x] = min(cur.money,list[cur.id][x]);
		for (int i = 0; i < chart[cur.id].size(); i++){

			int next = chart[cur.id][i];
			if (!in_que[next]){

				bit tmp2;
				tmp2.id=next;
				tmp2.money=cur.money+1;
				q.push(tmp2);
				in_que[tmp2.id]=true;

			}

		}

	}

}

int main(){

	cin >> n >> m >> k >> s;
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		goods[a[i]].push_back(i);
	}

	for (int i = 1; i <= m; i++){
		int u,v;
		cin >> u >> v;
		chart[u].push_back(v);
		chart[v].push_back(u);
	}

	memset(list,0x3f,sizeof(list));

	for (int i = 1; i <= k; i++){
		memset(in_que,false,sizeof(in_que));
		bfs(i);
	}

	for(int i=1;i<=n;i++){
        sort(list[i]+1,list[i]+k+1);
        long long sum=0;
        for(int j=1;j<=s;j++){
            sum+=list[i][j];
        }
        // cout << sum;
        // if (i < n) cout << " ";
        cout << sum << " ";
    }
    // cout << endl;

    return 0;

}


#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int maxn = 10e5+5;
int n,m,k,s;
int a[maxn];
bool in_que[maxn];

vector <int>goods[maxn];
int list[maxn][100+5] = {0};
vector <int>chart[maxn];

struct bit{
	int id;
	int money;
};

void bfs(int x){

	queue<bit> q;

	for (int i = 0; i<goods[x].size();i++){
		bit tmp;
		tmp.id=goods[x][i];
		tmp.money=0;
		//in_que[i]=true;  i?
		in_que[tmp.id] = true;
		q.push(tmp);
	}

	//cur是当前的

	while(!q.empty()){

		bit cur = q.front();
		q.pop();
		list[cur.id][x] = min(cur.money,list[cur.id][x]);
		for (int i = 0; i < chart[cur.id].size(); i++){

			int next = chart[cur.id][i];
			if (!in_que[next]){

				bit tmp2;
				tmp2.id=next;
				tmp2.money=cur.money+1;
				q.push(tmp2);
				in_que[tmp2.id]=true;

			}

		}

	}

}

int main(){
	freopen("a.in","r",stdin);
	cin >> n >> m >> k >> s;
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		goods[a[i]].push_back(i);
	}

	for (int i = 1; i <= m; i++){
		int u,v;
		cin >> u >> v;
		chart[u].push_back(v);
		chart[v].push_back(u);
	}

	memset(list,0x3f,sizeof(list));

	for (int i = 1; i <= k; i++){
		memset(in_que,false,sizeof(in_que));
		bfs(i);
	}

	for(int i=1;i<=n;i++){
        sort(list[i]+1,list[i]+k+1);
        long long sum=0;
        for(int j=1;j<=s;j++){
            sum+=list[i][j];
        }
        cout << sum << ' ';
    }

    return 0;

}


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

using namespace std;

template <typename T> void chkmax (T &x, T y) {x = x > y ? x : y;}
template <typename T> void chkmin (T &x, T y) {x = x > y ? y : x;}

typedef long long ll;
typedef unsigned long long ull;

const int INF = 2139062143;

#define DEBUG(x) std::cerr << #x << " = " << x << std::endl

template <typename T> void read (T &x) {
    x = 0; bool f = 1; char ch;
    do {ch = getchar(); if (ch == '-') f = 0;} while (ch > '9' || ch < '0');
    do {x = x * 10 + ch - '0'; ch = getchar();} while (ch >= '0' && ch <= '9');
    x = f ? x : -x;
}

template <typename T> void write (T x) {
    if (x < 0) x = ~x + 1, putchar ('-');
    if (x > 9) write (x / 10);
    putchar (x % 10 + '0');
}

const int N = 1e5 + 7;
const int K = 100 + 5;

struct EDGE {
    int to, nxt;
} edge[N];
vector < int > v[K];

int n, m, s, k, E, cnt, a[N], dis[N][K], head[N];
bool vis[N];

inline void addedge(int u, int v) {
    edge[++E].to = v;
    edge[E].nxt = head[u];
    head[u] = E;
}

inline void BFS(int col) {
    queue < pair<int, int> > q;
    memset(vis, 0, sizeof(vis));
    for(int i = 0; i < v[col].size(); i++) q.push(make_pair(v[col][i], 0)), vis[v[col][i]] = true;
    while(!q.empty()) {
        pair<int, int> now = q.front(); q.pop();
        dis[now.first][col] = now.second;
        for(int i = head[now.first]; i; i = edge[i].nxt) {
            int v = edge[i].to;
            if(vis[v]) continue;
            vis[v] = true;
            q.push(make_pair(v, now.second + 1));
        }
    }
}

int main() {
    read(n); read(m); read(k); read(s);
    for(int i = 1; i <= n; i++) read(a[i]), v[a[i]].push_back(i);
    memset(dis, 0x3f, sizeof(dis));
    for(int i = 1, u, v; i <= m; i++) {
        read(u); read(v);
        addedge(u, v);
        addedge(v, u);
    }
    for(int i = 1; i <= k; i++) BFS(i);

    // for(int i = 1; i <= n; i++) for(int j = 1; j <= k; j++) printf("f[%d][%d] = %d\n", i, j, dis[i][j]);

    for(int i = 1; i <= n; i++) {
        sort(dis[i] + 1, dis[i] + k + 1);
        int ans = 0;
        for(int j = 1; j <= s; j++) ans += dis[i][j];
        printf("%d ", ans);
    }
    return 0;
}


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

using namespace std;

template <typename T> void chkmax (T &x, T y) {x = x > y ? x : y;}
template <typename T> void chkmin (T &x, T y) {x = x > y ? y : x;}

typedef long long ll;
typedef unsigned long long ull;

const int INF = 2139062143;

#define DEBUG(x) std::cerr << #x << " = " << x << std::endl

template <typename T> void read (T &x) {
    x = 0; bool f = 1; char ch;
    do {ch = getchar(); if (ch == '-') f = 0;} while (ch > '9' || ch < '0');
    do {x = x * 10 + ch - '0'; ch = getchar();} while (ch >= '0' && ch <= '9');
    x = f ? x : -x;
}

template <typename T> void write (T x) {
    if (x < 0) x = ~x + 1, putchar ('-');
    if (x > 9) write (x / 10);
    putchar (x % 10 + '0');
}

const int N = 3E5 + 7;
const int K = 150 + 5;

struct EDGE {
    int to, nxt;
} edge[N << 1];

vector < int > v[N];

int n, m, s, k, E, cnt, a[N], dis[N][K], head[N];
bool vis[N];

inline void addedge(int u, int v) {
    edge[++E].to = v;
    edge[E].nxt = head[u];
    head[u] = E;
}

inline void BFS(int col) {
    queue < pair< int, int> > q;
    memset(vis, 0, sizeof(vis));
    for(int i = 0; i < v[col].size(); i++) q.push(make_pair(v[col][i], 0)), vis[v[col][i]] = true;
    while(!q.empty()) {
        pair< int, int> now = q.front(); q.pop();
        dis[now.first][col] = now.second;
        for(int i = head[now.first]; i; i = edge[i].nxt) {
            int v = edge[i].to;
            if(vis[v]) continue;
            vis[v] = true;
            q.push(make_pair(v, now.second + 1));
        }
    }
}

int main() {
    read(n); read(m); read(k); read(s);
    for(int i = 1; i <= n; i++) read(a[i]), v[a[i]].push_back(i);
    memset(dis, 0x3f, sizeof(dis));
    for(int i = 1, u, v; i <= m; i++) {
        read(u); read(v);
        addedge(u, v);
        addedge(v, u);
    }
    for(int i = 1; i <= k; i++) BFS(i);

    // for(int i = 1; i <= n; i++) for(int j = 1; j <= k; j++) printf("f[%d][%d] = %d\n", i, j, dis[i][j]);

    for(int i = 1; i <= n; i++) {
        sort(dis[i] + 1, dis[i] + k + 1);
        int ans = 0;
        for(int j = 1; j <= s; j++) ans += dis[i][j];
        printf("%d ", ans);
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
int n, m, k, s;
int c[100001], dist[100001][101];
bool vis[100001];
vector <int> a[100001];
int main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(0); cout.tie(0);
        //freopen("a.inp","r",stdin);
        cin >> n >> m >> k >> s;
        for (int i=1; i<=n; i++) cin >> c[i];
        for (int i=1; i<=m; i++)
        {
                int x, y;
                cin >> x >> y;
                a[x].push_back(y);
                a[y].push_back(x);
        }
        memset(dist,0,sizeof(dist));
        for (int i=1; i<=k; i++)
        {
                queue <int> q;
                memset(vis,false,sizeof(vis));
                for (int j=1; j<=n; j++)
                {
                        if (c[j]==i)
                        {
                                q.push(j);
                                vis[j] = true;
                                dist[j][i] = 0;
                        }
                }
                while (!q.empty())
                {
                        int v = q.front();
                        q.pop();
                        int p = a[v].size();
                        for (int j=0; j<p; j++)
                        {
                                int to = a[v][j];
                                if (!vis[to])
                                {
                                        dist[to][i] = dist[v][i] + 1;
                                        q.push(to);
                                        vis[to] = true;
                                }
                        }
                }
        }

        for (int i=1; i<=n; i++)
        {
                sort(dist[i]+1, dist[i]+k+1);
                int ans = 0;
                for (int j=1; j<=s; j++)
                        ans+=dist[i][j];
                cout << ans << " ";
        }
        return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;

struct Dijk
{
    int v, w;
    bool operator < (const Dijk &oth) const
    {
        return w>oth.w;
    }
};

int dist[N][105], ans[N];
int n, m, s, k;
queue<Dijk> pq[105];
vector<int> adj[N];

void Solve()
{
    for (int i=1; i<=k; i++)
    {
        queue<Dijk> q = pq[i];
        while (q.size())
        {
            int u = q.front().v; q.pop();
            for (int j=0; j<adj[u].size(); j++)
            {
                int v = adj[u][j];
                if(dist[v][i] > dist[u][i] + 1)
                {
                    dist[v][i] = dist[u][i] + 1;
                    q.push({v, dist[v][i]});
                }
            }
        }
    }
    for (int i=1; i<=n; i++)
    {
        sort(dist[i]+1,dist[i]+k+1);
        for (int j=1; j<=s; j++) ans[i] += dist[i][j];
    }
    for (int i=1; i<=n; i++) cout << ans[i] << " ";
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n >> m >> k >> s;
    memset(dist,0x3f,sizeof dist);
    for (int i=1; i<=n; i++)
    {
        int x; cin >> x;
        dist[i][x] = 0;
        pq[x].push({i, 0});
    }
    for (int i=1; i<=m; i++)
    {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    Solve();
}


#include<bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;

struct Dijk
{
    int v, w;
    bool operator < (const Dijk &oth) const
    {
        return w>oth.w;
    }
};

int dist[N][105], ans[N];
int n, m, s, k;
priority_queue<Dijk> pq[105];
vector<int> adj[N];

void Solve()
{
    for (int i=1; i<=k; i++)
    {
        priority_queue<Dijk> q = pq[i];
        while (q.size())
        {
            int u = q.top().v; q.pop();
            for (int j=0; j<adj[u].size(); j++)
            {
                int v = adj[u][j];
                if(dist[v][i] > dist[u][i] + 1)
                {
                    dist[v][i] = dist[u][i] + 1;
                    q.push({v, dist[v][i]});
                }
            }
        }
    }
    for (int i=1; i<=n; i++)
    {
        vector<int> v;
        for (int j=1; j<=k; j++) v.push_back(dist[i][j]);
        sort(v.begin(), v.end());
        for (int j=0; j<s; j++) ans[i] += v[j];
    }
    for (int i=1; i<=n; i++) cout << ans[i] << " ";
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n >> m >> k >> s;
    memset(dist,0x3f,sizeof dist);
    for (int i=1; i<=n; i++)
    {
        int x; cin >> x;
        dist[i][x] = 0;
        pq[x].push({i, 0});
    }
    for (int i=1; i<=m; i++)
    {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    Solve();
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
int n, m, k, s;
int c[100001], dist[100001][101];
bool vis[100001];
vector <int> a[100001];
int main()
{
        freopen("a.inp","r",stdin);
        cin >> n >> m >> k >> s;
        for (int i=1; i<=n; i++) cin >> c[i];
        for (int i=1; i<=m; i++)
        {
                int x, y;
                cin >> x >> y;
                a[x].push_back(y);
                a[y].push_back(x);
        }
        memset(dist,0,sizeof(dist));
        for (int i=1; i<=k; i++)
        {
                queue <int> q;
                memset(vis,false,sizeof(vis));
                for (int j=1; j<=n; j++)
                {
                        if (c[j]==i)
                        {
                                q.push(j);
                                vis[j] = true;
                                dist[j][i] = 0;
                        }
                }
                while (!q.empty())
                {
                        int v = q.front();
                        q.pop();
                        int p = a[v].size();
                        for (int j=0; j<p; j++)
                        {
                                int to = a[v][j];
                                if (!vis[to])
                                {
                                        dist[to][i] = dist[v][i] + 1;
                                        q.push(to);
                                        vis[to] = true;
                                }
                        }
                }
        }

        for (int i=1; i<=n; i++)
        {
                sort(dist[i]+1, dist[i]+k+1);
                int ans = 0;
                for (int j=1; j<=s; j++)
                        ans+=dist[i][j];
                cout << ans << " ";
        }
        return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;

struct Dijk
{
    int v, w;
    bool operator < (const Dijk &oth) const
    {
        return w>oth.w;
    }
};

int dist[N][105], ans[N];
int n, m, s, k;
priority_queue<Dijk> pq[105];
vector<int> adj[N];

void Solve()
{
    for (int i=1; i<=k; i++)
    {
        priority_queue<Dijk> q = pq[i];
        while (q.size())
        {
            int u = q.top().v; q.pop();
            for (int j=0; j<adj[u].size(); j++)
            {
                int v = adj[u][j];
                if(dist[v][i] > dist[u][i] + 1)
                {
                    dist[v][i] = dist[u][i] + 1;
                    q.push({v, dist[v][i]});
                }
            }
        }
    }
    for (int i=1; i<=n; i++)
    {
        sort(dist[i],dist[i]+k+1);
        for (int j=0; j<s; j++) ans[i] += dist[i][j];
    }
    for (int i=1; i<=n; i++) cout << ans[i] << " ";
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n >> m >> k >> s;
    memset(dist,0x3f,sizeof dist);
    for (int i=1; i<=n; i++)
    {
        int x; cin >> x;
        dist[i][x] = 0;
        pq[x].push({i, 0});
    }
    for (int i=1; i<=m; i++)
    {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    Solve();
}


#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

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

	int n, m, k, s;
	vector <vector<int> > good_vertices, adjList, cost;

	scanf(" %d %d %d %d", &n, &m, &k, &s);

	good_vertices.resize(k);
	adjList.resize(n);
	cost.resize(k);

	for (int i = 0; i < n; i++) {
		int good;
		scanf(" %d", &good);
		good_vertices[good - 1].push_back(i);
	}

/*	for (int i = 0; i < k; i++) {
		printf("%d : ", i + 1 );
		for (int j = 0; j < (int) good_vertices[i].size(); j++) {
			printf("%d ",good_vertices[i][j] + 1);
		}
		printf("\n");
	}
*/

	for (int i = 0; i < m; i++) {
		int u, v;
		scanf(" %d %d", &u, &v);
		adjList[u - 1].push_back(v - 1);
		adjList[v - 1].push_back(u - 1);
	}

//	printf("taken input\n");

	for (int i = 0; i < k; i++) {
		cost[i].assign(n, -1);

		queue <int> q;
		for (int j = 0; j < (int) good_vertices[i].size(); j++) {
			q.push(good_vertices[i][j]);
			cost[i][good_vertices[i][j]] = 0;
		}

		while (!q.empty()) {
			int u = q.front(); q.pop();
//			printf("%d\n", u);

			for (int j = 0; j < (int) adjList[u].size(); j++) {
				int v = adjList[u][j];
				if (cost[i][v] == -1){
					cost[i][v] = cost[i][u] + 1;
					q.push(v);
				}
			}
		}
	}

/*	for (int i = 0; i < k; i++) {
		for (int j = 0; j < n; j++) {
			printf("%d ", cost[i][j] );
		}
		printf("\n");
	}
*/
	for (int i = 0; i < n; i++) {
		vector <int> v;
		for (int j = 0; j < k; j++) {
			v.push_back(cost[j][i]);
			sort(v.begin(), v.end());
		}
		int t = 0;
		for (int j = 0; j < s; j++) {
			t += v[j];
		}
		if (i) printf(" ");
		printf("%d", t );

	}
	printf("\n");

}


#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

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

	int n, m, k, s;
	vector <vector<int> > good_vertices, adjList;
	int cost[10005][105];

	scanf(" %d %d %d %d", &n, &m, &k, &s);

	good_vertices.resize(k);
	adjList.resize(n);

	for (int i = 0; i < n; i++) {
		int good;
		scanf(" %d", &good);
		good_vertices[good - 1].push_back(i);
	}

/*	for (int i = 0; i < k; i++) {
		printf("%d : ", i + 1 );
		for (int j = 0; j < (int) good_vertices[i].size(); j++) {
			printf("%d ",good_vertices[i][j] + 1);
		}
		printf("\n");
	}
*/

	for (int i = 0; i < m; i++) {
		int u, v;
		scanf(" %d %d", &u, &v);
		adjList[u - 1].push_back(v - 1);
		adjList[v - 1].push_back(u - 1);
	}

	//printf("taken input\n");

	memset(cost, -1, sizeof cost);
	for (int i = 0; i < k; i++) {
		queue <int> q;
		for (int j = 0; j < (int) good_vertices[i].size(); j++) {
			q.push(good_vertices[i][j]);
			cost[good_vertices[i][j]][i] = 0;
		}

		while (!q.empty()) {
			int u = q.front(); q.pop();
//			printf("%d\n", u);

			for (int j = 0; j < (int) adjList[u].size(); j++) {
				int v = adjList[u][j];
				if (cost[v][i] == -1){
					cost[v][i] = cost[u][i] + 1;
					q.push(v);
				}
			}
		}
	}

/*	for (int i = 0; i < k; i++) {
		for (int j = 0; j < n; j++) {
			printf("%d ", cost[i][j] );
		}
		printf("\n");
	}
*/
	for (int i = 0; i < n; i++) {
		sort(cost[i], cost[i] + k);
		int t = 0;
		for (int j = 0; j < s; j++) {
			t += cost[i][j];
		}
		if (i) printf(" ");
		printf("%d", t );

	}
	printf("\n");

}


#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

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

	int n, m, k, s;
	vector <vector<int> > good_vertices, adjList;
	short cost[100005][105];

	scanf(" %d %d %d %d", &n, &m, &k, &s);

	good_vertices.resize(k);
	adjList.resize(n);

	for (int i = 0; i < n; i++) {
		int good;
		scanf(" %d", &good);
		good_vertices[good - 1].push_back(i);
	}

/*	for (int i = 0; i < k; i++) {
		printf("%d : ", i + 1 );
		for (int j = 0; j < (int) good_vertices[i].size(); j++) {
			printf("%d ",good_vertices[i][j] + 1);
		}
		printf("\n");
	}
*/

	for (int i = 0; i < m; i++) {
		int u, v;
		scanf(" %d %d", &u, &v);
		adjList[u - 1].push_back(v - 1);
		adjList[v - 1].push_back(u - 1);
	}

	//printf("taken input\n");

	memset(cost, -1, sizeof cost);
	for (int i = 0; i < k; i++) {
		queue <int> q;
		for (int j = 0; j < (int) good_vertices[i].size(); j++) {
			q.push(good_vertices[i][j]);
			cost[good_vertices[i][j]][i] = 0;
		}

		while (!q.empty()) {
			int u = q.front(); q.pop();
//			printf("%d\n", u);

			for (int j = 0; j < (int) adjList[u].size(); j++) {
				int v = adjList[u][j];
				if (cost[v][i] == -1){
					cost[v][i] = cost[u][i] + 1;
					q.push(v);
				}
			}
		}
	}

/*	for (int i = 0; i < k; i++) {
		for (int j = 0; j < n; j++) {
			printf("%d ", cost[i][j] );
		}
		printf("\n");
	}
*/
	for (int i = 0; i < n; i++) {
		sort(cost[i], cost[i] + k);
		int t = 0;
		for (int j = 0; j < s; j++) {
			t += cost[i][j];
		}
		if (i) printf(" ");
		printf("%d", t );

	}
	printf("\n");

}


#include <cstring>
#include <stdio.h>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;

const int maxn = 1e5 + 5;

struct Node
{
    int v;
    int d;
};

int a[maxn];
vector<int> G[maxn];
bool vis[maxn];

int main()
{
    int n, m, k, s;
    scanf("%d%d%d%d", &n, &m, &k, &s);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    for (int i = 0; i < m; ++i)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    for (int i = 1; i <= n; ++i)
    {
        int cnt = 0;
        memset(vis, 0, sizeof(vis));
        queue<Node> q;
        q.push(Node{i, 0});
        vis[a[i]] = 1;
        cnt++;
        int w = 0;
        while (!q.empty() && cnt != s)
        {
            Node t = q.front();
            q.pop();
            for (int j = 0; j < G[t.v].size(); ++j)
            {
                if (cnt == s)
                    break;
                int od = G[t.v][j];
                q.push(Node{od, t.d + 1});
                if (vis[a[od]])
                    continue;
                vis[a[od]] = 1;
                cnt += 1;
                w += t.d + 1;
                if (cnt == s)
                    break;
            }
        }
        printf("%d ", w);
    }
    return 0;
}


#include <cstring>
#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;

const int maxn = 1e5 + 5;

struct Node
{
    int v;
    int d;
};

vector<int> a;
vector<vector<int>> G;
bool vis[maxn];
bool vis_n[maxn];

int main()
{
    int n, m, k, s;
    scanf("%d%d%d%d", &n, &m, &k, &s);
    a.resize(n + 1);
    G.resize(n + 1);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    for (int i = 0; i < m; ++i)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    for (int i = 1; i <= n; ++i)
    {
        int cnt = 0;
        memset(vis, 0, sizeof(vis));
        memset(vis_n, 0, sizeof(vis_n));
        queue<Node> q;
        q.push(Node{i, 0});
        vis[a[i]] = 1;
        vis_n[i] = 1;
        cnt++;
        int w = 0;
        while (cnt < s)
        {
            Node t = q.front();
            q.pop();
            for (int j = 0; j < G[t.v].size(); ++j)
            {
                if (cnt == s)
                    break;
                int od = G[t.v][j];
                if (vis_n[od])
                    continue;
                q.push(Node{od, t.d + 1});
                vis_n[od] = 1;
                if (vis[a[od]])
                    continue;
                vis[a[od]] = 1;
                cnt += 1;
                w += t.d + 1;
                if (cnt == s)
                    break;
            }
        }
        printf("%d ", w);
    }
    return 0;
}


#include <iostream>
#include<queue>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
vector<int> G[100010];//存储I的联通节点
int a[100006];//ai 储存i城市的货物
vector<int> food[1000];//i食物的城镇
int vis[100006];
int dis[107][101000];//x食物到y城市最小
int n,m,k,s;
void bfs(int x)//x食物到各城市最小
{
	memset(vis,0,sizeof(vis));
	queue<int>q;
	for(int i=0;i<food[x].size();i++)
	{
		q.push(food[x][i]); dis[x][food[x][i]]=0;vis[food[x][i]]=1;
	 }
	while(!q.empty())
	{
		int f=q.front();
		q.pop(); vis[f]=1;
		for(int i=0;i<G[f].size();i++)
		{
			 if(dis[x][G[f][i]] > dis[x][f] + 1)
            {
                dis[x][G[f][i]] = dis[x][f] + 1;
                q.push(G[f][i]);}
		}
	}
}
long long int fina(int x)//x城市寻找s种的最小
{
   long long int buf[1000];
   memset(buf,0,sizeof(buf));
   for(int i=1;i<=k;i++)buf[i]=dis[i][x];
   sort(buf+1,buf+k+1);
   long long int ans=0;
   for(int i=1;i<=s;i++)ans+=buf[i];
   return ans;
}
int main()
{
	cin>>n>>m>>k>>s;
	memset(dis,111,sizeof(dis));
	for(int i=1;i<=n;i++){cin>>a[i];food[a[i]].push_back(i);}
	for(int i=1;i<=m;i++)
	{
		int u,v;cin>>u>>v;
		G[u].push_back(v); G[v].push_back(u);
	}
	for(int i=1;i<=k;i++)bfs(i);
    for(int i=1;i<=n;i++) cout<<fina(i)<<" ";
}


#include<bits/stdc++.h>
#define Inf (1<<29)
using namespace std;

int n, m, k, s;
long long ans;
int dis[100010][110];
int L, R, Q[100010];

vector<int> E[100010], P[110];

int main()
{
	int a, b, x;
	scanf("%d%d%d%d",&n,&m,&k,&s);
	for(int i=1; i<=n; i++)
	{
		scanf("%d",&a);
		P[a].push_back(i);
	}
	for(int i=1; i<=m; i++)
	{
		scanf("%d%d",&a,&b);
		E[a].push_back(b);
		E[b].push_back(a);
	}
	for(int i=1; i<=k; i++)
	{
		L=R=1;
		for(int j=1; j<=n; j++) dis[j][i]=Inf;
		for(int j=0; j<P[i].size(); j++)
		{
			Q[R++]=P[i][j];
			dis[P[i][j]][i]=0;
		}
		while(L<R)
		{
			x=Q[L++];
			for(int j=0; j<E[x].size(); j++)
				if(dis[E[x][j]][i]>dis[x][i]+1)
				{
					dis[E[x][j]][i]=dis[x][i]+1;
					Q[R++]=E[x][j];
				}
		}
	}
	for(int i=1; i<=n; i++)
	{
		sort(dis[i]+1,dis[i]+1+k);
		ans=0;
		for(int j=1; j<=s; j++) ans+=dis[i][j];
		printf("%lld ",ans);
	}
	puts("");
	return 0;
}


#include <iostream>
#include<queue>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
vector<int> G[1000];//存储I的联通节点
int a[10000];//ai 储存i城市的货物
vector<int> food[1000];//i食物的城镇
int vis[100000];
int dis[107][107];
int mi[107][107];
int n,m,k,s;
void bfs(int x)
{
	memset(vis,0,sizeof(vis));
	queue<int>q;
	q.push(x);
	while(!q.empty())
	{
		int f=q.front();
		q.pop(); vis[f]=1;
		for(int i=0;i<G[f].size();i++)
		{
			if(vis[G[f][i]])continue;
			q.push(G[f][i]);
			dis[x][G[f][i]]=dis[x][f]+1;dis[G[f][i]][x]=dis[x][G[f][i]];
		}
	}
}
long long int find(int x,int i) //x城市寻找i的最小
{
	long long int ans=100000;
	for(int j=0;j<food[i].size();j++)
	{
		if(dis[x][food[i][j]]<ans)ans=dis[x][food[i][j]];
	}
	return ans;
 }
int fina(int x)//x城市寻找s种的最小
{
   int buf[1000];
   for(int i=1;i<=k;i++)buf[i-1]=mi[x][i];
   sort(buf,buf+k);
   int ans=0;
   for(int i=0;i<s;i++)ans+=buf[i];
   return ans;
}
int main()
{
	cin>>n>>m>>k>>s;
	for(int i=1;i<=n;i++){cin>>a[i];food[a[i]].push_back(i);}
	for(int i=1;i<=m;i++)
	{
		int u,v;cin>>u>>v;
		G[u].push_back(v); G[v].push_back(u);
	}
	for(int i=1;i<=k;i++)
	{
		for(int j=0;j<food[i].size();j++)bfs(food[i][j]);
	}
	for(int i=1;i<=k;i++)
	{
		for(int j=1;j<=n;j++)mi[j][i]=find(j,i);
	}
    for(int i=1;i<=n;i++) cout<<fina(i)<<" ";
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 100005;

vector<int> G[MAXN];

int a[MAXN];

int n, m, k, s, x, y;

vector<int> v[MAXN], st[MAXN];
queue<int> q;

int dis[MAXN];

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m >> k >> s;
	for (int i = 1; i <= n; i++)
		cin >> a[i], st[a[i]].push_back(i);
	for (int i = 1; i <= m; i++)
	{
		cin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	for (int i = 1; i <= k; i++)
	{
		for (int j = 1; j <= n; j++)
			dis[j] = -1;
		for (auto x : st[i])
			dis[x] = 0, q.push(x);
		while (!q.empty())
		{
			int u = q.front();
			q.pop();
			for (auto v : G[u])
				if (dis[v] == -1)
					dis[v] = dis[u] + 1, q.push(v);
		}
//		cout << "i: " << i << endl;
		for (int j = 1; j <= n; j++)
		{
//			cout << "dis[" << j << "]: " << dis[j] << endl;
			v[j].push_back(dis[j]);
		}
	}
	for (int i = 1; i <= n; i++)
	{
		sort(v[i].begin(), v[i].end());
		int res = 0;
		for (int j = 0; j < s; j++)
			res += v[i][j];
		cout << res << " ";
	}
	cout << endl;
	return 0;
}


#include <iostream>
#include<queue>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
vector<int> G[100010];//存储I的联通节点
int a[100006];//ai 储存i城市的货物
vector<int> food[1000];//i食物的城镇
int vis[100006];
int dis[107][101000];//x食物到y城市最小
int n,m,k,s;
void bfs(int x)//x食物到各城市最小
{
	memset(vis,0,sizeof(vis));
	queue<int>q;
	for(int i=0;i<food[x].size();i++)
	{
		q.push(food[x][i]); dis[x][food[x][i]]=0;vis[food[x][i]]=1;
	 }
	while(!q.empty())
	{
		int f=q.front();
		q.pop(); vis[f]=1;
		for(int i=0;i<G[f].size();i++)
		{
			if(vis[G[f][i]])continue;
			else dis[x][G[f][i]]=dis[x][f]+1;
			q.push(G[f][i]);
		}
	}
}
long long int fina(int x)//x城市寻找s种的最小
{
   long long int buf[1000];
   for(int i=1;i<=k;i++)buf[i-1]=dis[i][x];
   sort(buf,buf+k);
   long long int ans=0;
   for(int i=0;i<s;i++)ans+=buf[i];
   return ans;
}
int main()
{
	cin>>n>>m>>k>>s;
	for(int i=1;i<=n;i++){cin>>a[i];food[a[i]].push_back(i);}
	for(int i=1;i<=m;i++)
	{
		int u,v;cin>>u>>v;
		G[u].push_back(v); G[v].push_back(u);
	}
	for(int i=1;i<=k;i++)bfs(i);
    for(int i=1;i<=n;i++) cout<<fina(i)<<" ";
}


#include <iostream>
#include<queue>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
vector<int> G[100010];//存储I的联通节点
int a[100006];//ai 储存i城市的货物
vector<int> food[1000];//i食物的城镇
int vis[100006];
int dis[107][101000];//x食物到y城市最小
int n,m,k,s;
void bfs(int x)//x食物到各城市最小
{
	memset(vis,0,sizeof(vis));
	queue<int>q;
	for(int i=0;i<food[x].size();i++)
	{
		q.push(food[x][i]); dis[x][food[x][i]]=0;vis[food[x][i]]=1;
	 }
	while(!q.empty())
	{
		int f=q.front();
		q.pop();
		for(int i=0;i<G[f].size();i++)
		{
			if(vis[G[f][i]])continue;
			dis[x][G[f][i]]=dis[x][f]+1;vis[G[f][i]]=1;
			q.push(G[f][i]);
		}
	}
}
long long int fina(int x)//x城市寻找s种的最小
{
   long long int buf[1000];
   for(int i=1;i<=k;i++)buf[i-1]=dis[i][x];
   sort(buf,buf+k);
   long long int ans=0;
   for(int i=0;i<s;i++)ans+=buf[i];
   return ans;
}
int main()
{
	memset(dis,110,sizeof(dis));
	cin>>n>>m>>k>>s;
	for(int i=1;i<=n;i++){cin>>a[i];food[a[i]].push_back(i);}
	for(int i=1;i<=m;i++)
	{
		int u,v;cin>>u>>v;
		G[u].push_back(v); G[v].push_back(u);
	}
	for(int i=1;i<=k;i++)bfs(i);
    for(int i=1;i<=n;i++) cout<<fina(i)<<" ";
}


#include<stdio.h>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100510;
int n,m,k,s,len,head[maxn],a[maxn],dis[maxn];
bool v[maxn],c[maxn];
//priority_queue<int> p[200];
int p[200];
struct edge
{
	int to,next;
}e[maxn*2];
void insert(int x,int y)
{
	e[++len].to=y;e[len].next=head[x];head[x]=len;
}
void bfs(int x)
{
	memset(dis,0x7f,sizeof(dis));
	memset(v,0,sizeof(v));
	v[x]=1;dis[x]=0;
	queue<int> q;q.push(x);
	while(!q.empty())
	{
		int x=q.front();q.pop();;
		for(int i=head[x];i;i=e[i].next)
		{

			int y=e[i].to;//printf("%d %d\n",x,y);system("pause");
			if(dis[x]+1<dis[y])
			{
				dis[y]=dis[x]+1;
				if(!v[y])v[y]=1,q.push(y);
			}
		}
	}
	for(int i=1;i<=n;++i)if(dis[i]<p[a[i]])p[a[i]]=dis[i];//,printf("%d %d %d\n",dis[i],a[i],p[a[i]]);
	//printf("\n");
}
int main()
{
	scanf("%d%d%d%d",&n,&m,&k,&s);
	for(int i=1;i<=n;++i)scanf("%d",&a[i]);
	for(int x,y,i=1;i<=m;++i)
	{
		scanf("%d%d",&x,&y);
		insert(x,y);
		insert(y,x);
	}
	for(int i=1;i<=n;++i)
	{
		memset(p,0x7f,sizeof(p));
		bfs(i);int ans=0,cnt=1;
		sort(p+1,p+1+k);
		for(int j=1;j<=s;++j)
		{
			ans+=p[j];
			//printf("aaa %d %d %d\n",ans,p[j],cnt);
		}
		printf("%d ",ans);
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#define MAXN 101000
using namespace std;
int f[MAXN],d[MAXN][110];   //d[i][j] i到对应颜色的长度
vector<int> edge[MAXN];
queue<int> q;
int n,m,k,s,u,v,color[MAXN];
int main()
{
	cin>>n>>m>>k>>s;
	for(int i=1;i<=n;i++)
		cin>>color[i];
	for(int i=0;i<m;i++)
	{
		cin>>u>>v;
		edge[u].push_back(v);
		edge[v].push_back(u);
	}
	for(int i=1;i<=n;i++)
	    for(int j=1;j<=k;j++)
	        d[i][j]=MAXN;
	for(int i=1;i<=k;i++)
	{
		memset(f,0,sizeof(f));
		for(int j=1;j<=n;j++)
		{
			if(i==color[j])     //i到自身为0
			{
			   d[j][i]=0;
			   f[j]=1;
			   q.push(j);
		    }
		}
		while(!q.empty())
		{
			int t=q.front();
			for(int j=0;j<edge[t].size();j++)    //由t到与其相连的点
			{
				int p=edge[t][j];
			    if(f[p])	continue;
			    f[p]=1;
			    d[p][i]=min(d[p][i],d[t][i]+1);
			    q.push(p);
			}
			q.pop();
		}
	}
	for(int i=1;i<=n;i++)
	{
		int ans=0;
		sort(d[i]+1,d[i]+1+k);
		for(int j=1;j<=s;j++)
		    ans+=d[i][j];
		cout<<ans<<" ";
	}
	return 0;
}


#include <iostream>
#include<queue>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
vector<int> G[100010];//存储I的联通节点
int a[100006];//ai 储存i城市的货物
vector<int> food[1000];//i食物的城镇
int vis[100006];
int dis[107][101000];//x食物到y城市最小
int n,m,k,s;
void bfs(int x)//x食物到各城市最小
{
	memset(vis,0,sizeof(vis));
	queue<int>q;
	for(int i=0;i<food[x].size();i++)
	{
		q.push(food[x][i]); dis[x][food[x][i]]=0;vis[food[x][i]]=1;
	 }
	while(!q.empty())
	{
		int f=q.front();
		q.pop(); vis[f]=1;
		for(int i=0;i<G[f].size();i++)
		{
			if(vis[G[f][i]])continue;
			dis[x][G[f][i]]=dis[x][f]+1;
			q.push(G[f][i]);
		}
	}
}
long long int fina(int x)//x城市寻找s种的最小
{
   long long int buf[1000];
   for(int i=1;i<=k;i++)buf[i-1]=dis[i][x];
   sort(buf,buf+k);
   long long int ans=0;
   for(int i=0;i<s;i++)ans+=buf[i];
   return ans;
}
int main()
{
	memset(dis,110,sizeof(dis));
	cin>>n>>m>>k>>s;
	for(int i=1;i<=n;i++){cin>>a[i];food[a[i]].push_back(i);}
	for(int i=1;i<=m;i++)
	{
		int u,v;cin>>u>>v;
		G[u].push_back(v); G[v].push_back(u);
	}
	for(int i=1;i<=k;i++)bfs(i);
    for(int i=1;i<=n;i++) cout<<fina(i)<<" ";
}


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

long long n, m, k, s, a[100005], d[100005], x, y, ans, po, matr[110][110];
vector<long long> edges[100005], goroda[100005];
queue<int> q1;

void BFS(queue<int> q)
{
    while (!q.empty())
    {
        int v = q.front();
        q.pop();
        for (int i = 0; i < (int)edges[v].size(); ++i)
        {
            if (d[edges[v][i]] > d[v] + 1)
            {
                d[edges[v][i]] = d[v] + 1;
                q.push(edges[v][i]);
            }
        }
    }
}

int main()
{
	cin >> n >> m >> k >> s;
	for (int i = 1; i <= n; i ++)
    {
        cin >> a[i];
        goroda[a[i]].push_back(i);
    }
	for (int i = 1; i <= m; i ++)
	{
		cin >> x >> y;
		edges[x].push_back(y);
		edges[y].push_back(x);
	}
	for (int i = 1; i <= k; i ++)
	{
		for (int j = 1; j <= n; j ++)
		{
		    d[j] = 100000000;
		}
        while (!q1.empty())
            q1.pop();
        for (int j = 0; j < goroda[i].size(); j ++)
        {
            q1.push(goroda[i][j]);
            d[goroda[i][j]] = 0;
        }
		BFS(q1);
		for (int j = 1; j <= n; j ++)
            matr[j][i] = d[j];
	}
	for (int i = 1; i <= n; i ++)
    {
        ans = 0;
        nth_element(matr[i] + 1, matr[i] + 1 + s, matr[i] + 1 + k);
        for (int j = 1; j <= s; j ++)
            ans += matr[i][j];
       cout << ans << " ";
    }
	return 0;
}


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

long long n, m, k, s, a[100005], d[100005], x, y, ans, po, mas[100005];
bool mark[100005];
set<long long> myset;
vector<long long> edges[100005];
map<long long, long long> m1;
//typedef pair<long long, long long> para;
//bool fcmp(para a, para b)
//{
//	return (a.second <= b.second);
//}

void BFS(long long start)
{
    queue<int> q;
    q.push(start);
    d[start] = 0;
    mark[start] = 1;
    while (!q.empty())
    {
        int v = q.front();
        q.pop();
        for (int i = 0; i < (int)edges[v].size(); ++i)
        {
            if (mark[edges[v][i]] == 0)
            {
                d[edges[v][i]] = d[v] + 1;
                mark[edges[v][i]] = 1;
                q.push(edges[v][i]);
            }
        }
    }
}

int main()
{
	cin >> n >> m >> k >> s;
	for (int i = 1; i <= n; i ++)
		cin >> a[i];
	for (int i = 1; i <= m; i ++)
	{
		cin >> x >> y;
		edges[x].push_back(y);
		edges[y].push_back(x);
	}
	for (int i = 1; i <= n; i ++)
	{
		for (int j = 1; j <= n; j ++)
			d[j] = mark[j] = 0;
		BFS(i);
		m1.clear();
		for (int j = 1; j <= k; j ++)
            m1[j] = -1;
		for (int j = 1; j <= n; j ++)
        {
            po = m1[a[j]];
            if ((po == -1) || (po > d[j]))
                m1[a[j]] = d[j];
        }
        for (int j = 1; j <= k; j ++)
        {
            //cout << m1[j] << " ";
            mas[j] = m1[j];
        }
        //cout << "\n";
        nth_element(mas + 1, mas + s + 1, mas + k + 1);
        ans = 0;
        for (int j = 1; j <= s; j ++)
            ans += mas[j];
        cout << ans << " ";
	}
	return 0;
}


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

long long n, m, k, s, a[100005], d[100005], x, y, ans, po, matr[100010][110];
vector<long long> edges[100005], goroda[100005];
queue<int> q1;

void BFS(queue<int> q)
{
    while (!q.empty())
    {
        int v = q.front();
        q.pop();
        for (int i = 0; i < (int)edges[v].size(); ++i)
        {
            if (d[edges[v][i]] > d[v] + 1)
            {
                d[edges[v][i]] = d[v] + 1;
                q.push(edges[v][i]);
            }
        }
    }
}

int main()
{
	cin >> n >> m >> k >> s;
	for (int i = 1; i <= n; i ++)
    {
        cin >> a[i];
        goroda[a[i]].push_back(i);
    }
	for (int i = 1; i <= m; i ++)
	{
		cin >> x >> y;
		edges[x].push_back(y);
		edges[y].push_back(x);
	}
	for (int i = 1; i <= k; i ++)
	{
		for (int j = 1; j <= n; j ++)
		{
		    d[j] = 100000000;
		}
        while (!q1.empty())
            q1.pop();
        for (int j = 0; j < goroda[i].size(); j ++)
        {
            q1.push(goroda[i][j]);
            d[goroda[i][j]] = 0;
        }
		BFS(q1);
		for (int j = 1; j <= n; j ++)
            matr[j][i] = d[j];
	}
	for (int i = 1; i <= n; i ++)
    {
        ans = 0;
        nth_element(matr[i] + 1, matr[i] + 1 + s, matr[i] + 1 + k);
        for (int j = 1; j <= s; j ++)
            ans += matr[i][j];
       cout << ans << " ";
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%d",&a)
#define pf(a) printf("%d",a)
#define ford(i,n) for(i=1;i<=n;i++)
#define for0(i,n) for(i=0;i<n;i++)
#define mst(a) memset(a,0,sizeof(a))
typedef long long ll;
typedef double db;
#define ddd cout<<"ddd"<<endl
#define maxn 100010
#define inf 0x7fffffff
struct path{
	int coin;
	int r;
};
int n,m,k,s;
int a[100010];
int v[200010];
int book2[110];
int nex[200010];
int first[200010];
int book[100010];
int amount;
void add(int start,int end)
{
	v[++amount]=end;
	nex[amount]=first[start];
	first[start]=amount;
}
int bfs(int now)
{
	mst(book);
	mst(book2);
	path head;
	head.r=now;
	head.coin=0;
	book2[a[now]]=1;
	book[now]=1;
	int sum=1;
	int all=0;
	queue<path>q;
	q.push(head);
	while(!q.empty())
	{
		head=q.front();
		q.pop();
		int tail=first[head.r];
//		cout<<v[tail]<<endl;
		while(tail)
		{
			if(book[v[tail]]==0)
			{
				book[v[tail]]=1;
				path temp;
				temp.r=v[tail];
				temp.coin=head.coin+1;
				if(book2[a[v[tail]]]==0)
				{
					sum++;
					all+=temp.coin;
					book2[a[v[tail]]]=1;
					if(sum==s)
					return all;
				}
				q.push(temp);
			}
			tail=nex[tail];
		}
	}
}
int main()
{
	//ios_base::sync_with_stdio(false);
	//ddd;
	cin>>n>>m>>k>>s;
	int i;
	ford(i,n)
	sf(a[i]);
	ford(i,m)
	{
		int b,c;
		sf(b);
		sf(c);
		add(b,c);
		add(c,b);
	}
	ford(i,n)
	{
		cout<<bfs(i)<<" ";
	}
}


#include <bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%d",&a)
#define pf(a) printf("%d",a)
#define ford(i,n) for(i=1;i<=n;i++)
#define for0(i,n) for(i=0;i<n;i++)
#define mst(a) memset(a,0,sizeof(a))
typedef long long ll;
typedef double db;
#define ddd cout<<"ddd"<<endl
#define maxn 100010
#define inf 0x7ffffff
struct path{
	int coin;
	int r;
};
int n,m,k,s;
int a[100010];
int v[200010];
int book2[110][100010];
int nex[200010];
int first[200010];
int book[110];
int dis[100010];
int ans[100010][110];
int amount;
void add(int start,int end)
{
	v[++amount]=end;
	nex[amount]=first[start];
	first[start]=amount;
}
void bfs(int now)
{
	int i;
	ford(i,n)
	dis[i]=inf;
	queue<int>q;
	ford(i,book[now])
	{
		dis[book2[now][i]]=0;
		q.push(book2[now][i]);
	}
	while(!q.empty())
	{
		int head=q.front();
		int tail=first[head];
		while(tail)
		{
			if(dis[v[tail]]>dis[head]+1)
			{
				dis[v[tail]]=dis[head]+1;
				q.push(v[tail]);
			}
			tail=nex[tail];
		}
		q.pop();
	}
	ford(i,n)
	ans[i][now]=dis[i];
}
int main()
{
	//ios_base::sync_with_stdio(false);
	//ddd;
	cin>>n>>m>>k>>s;
	int i;
	ford(i,n)
	{
		int b;
		sf(b);
		book2[b][++book[b]]=i;
	}
	ford(i,m)
	{
		int b,c;
		sf(b);
		sf(c);
		add(b,c);
		add(c,b);
	}
	ford(i,k)
	{
		bfs(i);
	}
	ford(i,n)
	{
		int sum=0;
		int j;
		ford(j,k)
		dis[j]=ans[i][j];
		sort(dis+1,dis+1+k);
		ford(j,s)
		sum+=dis[j];
		pf(sum);
		printf(" ");
	}
}


#include "bits/stdc++.h"
using namespace std;
#define all(v) (v).begin(), (v).end()
#define io ios::sync_with_stdio(0)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rson rt << 1 | 1, mid + 1, r
#define lson rt << 1, l, mid
#define lll __int128
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eps 1e-12
#define int long long
const int mod = 1e9 + 7;
inline int ksm(int a, int b)
{
	int ans = 1;
	for (; b; b >>= 1, a = a * a % mod)
		if (b & 1)
			ans = ans * a % mod;
	return ans;
}

vector<int> v[200000],G[200000];
int dis[200000];
int a[200000];
int n,m,k,s;
signed main()
{
	io;
    cin>>n>>m>>k>>s;
    rep(i,1,n)cin>>a[i];
    rep(i,1,m){
        int l,r;cin>>l>>r;
        G[l].pb(r); G[r].pb(l);
    }
    rep(i,1,k){
        queue<int> q; memset(dis,-1,sizeof dis);
        rep(j,1,n)if(a[j]==i)q.push(j),dis[j]=0;;
        while(!q.empty()){
            int t=q.front();q.pop();
            for(auto &i:G[t]){
                if(dis[i]==-1){
                    dis[i]=dis[t]+1;
                    q.push(i);
                }
            }
        }
        rep(j,1,n){
            v[j].pb(dis[j]);
        }
    }
    rep(i,1,n)nth_element(v[i].begin(),v[i].begin()+s,v[i].end());
    rep(i,1,n){
        int ans=0;
        rep(j,1,s)ans+=v[i][j-1];
        cout<<ans<<" ";
    }
    cout<<endl;

	getchar(); //
	getchar();
}


#include "bits/stdc++.h"
using namespace std;
#define all(v) (v).begin(), (v).end()
#define io ios::sync_with_stdio(0)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rson rt << 1 | 1, mid + 1, r
#define lson rt << 1, l, mid
#define lll __int128
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eps 1e-12
#define int long long
const int mod = 1e9 + 7;
inline int ksm(int a, int b)
{
	int ans = 1;
	for (; b; b >>= 1, a = a * a % mod)
		if (b & 1)
			ans = ans * a % mod;
	return ans;
}

vector<int> v[200000],G[200000];
int dis[200000];
int a[200000];
int n,m,k,s;
signed main()
{

	io;
    cin>>n>>m>>k>>s;
    rep(i,1,n)cin>>a[i];
    rep(i,1,m){
        int l,r;cin>>l>>r;
        G[l].pb(r); G[r].pb(l);
    }
    rep(i,1,k){
        queue<int> q; memset(dis,-1,sizeof dis);
        rep(j,1,n)if(a[j]==i)q.push(j),dis[j]=0;;
        while(!q.empty()){
            int t=q.front();q.pop();
            for(auto &i:G[t]){
                if(dis[i]==-1){
                    dis[i]=dis[t]+1;
                    q.push(i);
                }
            }
        }
        rep(j,1,n){
            v[j].pb(dis[j]);
        }
    }
    rep(i,1,n)sort(all(v[i]));
    rep(i,1,n){
        int ans=0;
        rep(j,1,s)ans+=v[i][j-1];
        cout<<ans<<" ";
    }
    cout<<endl;

	getchar(); //
	getchar();
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int N=20010;
struct node
{
	int next,to;
}g[N<<1];
vector<int>c[N];
int head[N],cnt;
int dist[N][110];
int col[N];
int n,m,k,s;
int ans;

inline void addedge(int u,int v)
{
	g[++cnt].next=head[u];
	g[cnt].to=v;
	head[u]=cnt;
}

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

void bfs(int color)
{
	queue<int>q;
	for(int i=0;i<c[color].size();i++)
	q.push(c[color][i]),dist[c[color][i]][color]=0;
	while(!q.empty())
	{
		int u=q.front();q.pop();
		for(int i=head[u];i;i=g[i].next)
		{
			int v=g[i].to;
			if(dist[v][color]==-1)
			{
				dist[v][color]=dist[u][color]+1;
				q.push(v);
			}
		}
	}
}

int main()
{
	n=read();m=read();k=read();s=read();
	for(int i=1;i<=n;i++)
	{
		col[i]=read();
		c[col[i]].push_back(i);
	}
	for(int i=1;i<=m;i++)
	{
		int x=read(),y=read();
		addedge(x,y);addedge(y,x);
	}
	memset(dist,-1,sizeof(dist));
	for(int i=1;i<=k;i++)
	{
		bfs(i);
	}
	for(int i=1;i<=n;i++)
	{
		ans=0;
		sort(dist[i]+1,dist[i]+1+k);
		for(int j=1;j<=s;j++)
		ans+=dist[i][j];
		printf("%d ",ans);
	}
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int N=200010;
struct node
{
	int next,to;
}g[N<<1];
vector<int>c[N];
int head[N],cnt;
int dist[N][110];
int col[N];
int n,m,k,s;
int ans;

inline void addedge(int u,int v)
{
	g[++cnt].next=head[u];
	g[cnt].to=v;
	head[u]=cnt;
}

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

void bfs(int color)
{
	queue<int>q;
	for(int i=0;i<c[color].size();i++)
	q.push(c[color][i]),dist[c[color][i]][color]=0;
	while(!q.empty())
	{
		int u=q.front();q.pop();
		for(int i=head[u];i;i=g[i].next)
		{
			int v=g[i].to;
			if(dist[v][color]==-1)
			{
				dist[v][color]=dist[u][color]+1;
				q.push(v);
			}
		}
	}
}

int main()
{
	n=read();m=read();k=read();s=read();
	for(int i=1;i<=n;i++)
	{
		col[i]=read();
		c[col[i]].push_back(i);
	}
	for(int i=1;i<=m;i++)
	{
		int x=read(),y=read();
		addedge(x,y);addedge(y,x);
	}
	memset(dist,-1,sizeof(dist));
	for(int i=1;i<=k;i++)
	{
		bfs(i);
	}
	for(int i=1;i<=n;i++)
	{
		ans=0;
		sort(dist[i]+1,dist[i]+1+k);
		for(int j=1;j<=s;j++)
		ans+=dist[i][j];
		printf("%d ",ans);
	}
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int N=10010;
struct node
{
	int next,to;
}g[N<<1];
vector<int>c[110];
int head[N],cnt;
int dist[N][110];
int col[N];
int n,m,k,s;
int ans;

inline void addedge(int u,int v)
{
	g[++cnt].next=head[u];
	g[cnt].to=v;
	head[u]=cnt;
}

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

void bfs(int color)
{
	queue<int>q;
	for(int i=0;i<c[color].size();i++)
	q.push(c[color][i]),dist[c[color][i]][color]=0;
	while(!q.empty())
	{
		int u=q.front();q.pop();
		for(int i=head[u];i;i=g[i].next)
		{
			int v=g[i].to;
			if(dist[v][color]==-1)
			{
				dist[v][color]=dist[u][color]+1;
				q.push(v);
			}
		}
	}
}

int main()
{
	n=read();m=read();k=read();s=read();
	for(int i=1;i<=n;i++)
	{
		col[i]=read();
		c[col[i]].push_back(i);
	}
	for(int i=1;i<=m;i++)
	{
		int x=read(),y=read();
		addedge(x,y);addedge(y,x);
	}
	memset(dist,-1,sizeof(dist));
	for(int i=1;i<=k;i++)
	{
		bfs(i);
	}
	for(int i=1;i<=n;i++)
	{
		ans=0;
		sort(dist[i]+1,dist[i]+1+k);
		for(int j=1;j<=s;j++)
		ans+=dist[i][j];
		printf("%d ",ans);
	}
}


#include <bits/stdc++.h>
using namespace std;
int n,m,k,s;
queue <int> Q;
int D[110][101000];
vector <int> G[110],E[101000];
int w[10100],v[10100];
bool comp(int a,int b)
{
	return a<b;
}
void BFS(int a)
{
	int i;
	for(i=0;i<=n;i++)
	{
		v[i]=0;
		D[a][i]=1e9;
	}
	//int f=0,r=0;
	for(int l=0;l<G[a].size();l++)
	{
		int x=G[a][l];
		Q.push(x);
		v[x]=1;
		D[a][x]=0;
	}
	while(!Q.empty())
	{
		int x=Q.front();
		Q.pop();
		for(int l=0;l<E[x].size();l++)
		{
			int y=E[x][l];
			if(!v[y])
			{
				v[y]=1;
				Q.push(y);
				D[a][y]=D[a][x]+1;
			}
		}
	}
}
int main()
{
	int i,j;
	cin>>n>>m>>k>>s;
	for(i=1;i<=n;i++)
	{
		cin>>w[i];
		G[w[i]].push_back(i);
	}
	for(i=0;i<m;i++)
	{
		int a,b;
		cin>>a>>b;
		E[a].push_back(b);
		E[b].push_back(a);
	}
	for(i=1;i<=k;i++)
		BFS(i);
	for(i=1;i<=n;i++)
	{
		vector <int> T;
		for(j=1;j<=k;j++)
			T.push_back(D[j][i]);
		sort(T.begin(),T.end(),comp);
		/*for(j=0;j<T.size();j++)
			cout<<T[j]<<" ";
		cout<<"vt\n";*/
		int res=0;
		for(j=0;j<s && j<T.size();j++)
		{
			res+=T[j];
			//cout<<T[i]<<"yik ";
		}
		//cout<<"uyec\n";
		cout<<res<<" ";
	}
	cout<<"\n";//
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m,k,s;
queue <int> Q;
int D[110][101000];
vector <int> G[110],E[101000];
int w[101000],v[101000];
bool comp(int a,int b)
{
	return a<b;
}
void BFS(int a)
{
	int i;
	for(i=0;i<=n;i++)
	{
		v[i]=0;
		D[a][i]=1e9;
	}
	//int f=0,r=0;
	for(int l=0;l<G[a].size();l++)
	{
		int x=G[a][l];
		Q.push(x);
		v[x]=1;
		D[a][x]=0;
	}
	while(!Q.empty())
	{
		int x=Q.front();
		Q.pop();
		for(int l=0;l<E[x].size();l++)
		{
			int y=E[x][l];
			if(!v[y])
			{
				v[y]=1;
				Q.push(y);
				D[a][y]=D[a][x]+1;
			}
		}
	}
}
int main()
{
	int i,j;
	cin>>n>>m>>k>>s;
	for(i=1;i<=n;i++)
	{
		cin>>w[i];
		G[w[i]].push_back(i);
	}
	for(i=0;i<m;i++)
	{
		int a,b;
		cin>>a>>b;
		E[a].push_back(b);
		E[b].push_back(a);
	}
	for(i=1;i<=k;i++)
		BFS(i);
	for(i=1;i<=n;i++)
	{
		vector <int> T;
		for(j=1;j<=k;j++)
			T.push_back(D[j][i]);
		sort(T.begin(),T.end(),comp);
		/*for(j=0;j<T.size();j++)
			cout<<T[j]<<" ";
		cout<<"vt\n";*/
		int res=0;
		for(j=0;j<s && j<T.size();j++)
		{
			res+=T[j];
			//cout<<T[i]<<"yik ";
		}
		//cout<<"uyec\n";
		cout<<res<<" ";
	}
	cout<<"\n";//
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define N 100010
#define K 111
#define ll long long
using namespace std;
ll n,m,k,s,q[N];
struct EDGE{
	ll nxt,v;
}edge[N*2];
ll point[N],e,f[N][K];
bool inq[N];

void add_edge(ll u,ll v){
	edge[++e]=(EDGE){point[u],v};
	point[u]=e;
}

void bfs(ll w)
{
	queue<ll> qq;
	for (ll i=1;i<=n;i++){
		if (q[i]==w){
			qq.push(i);
			inq[i]=1;
			f[i][w]=0;
		}
		else inq[i]=0,f[i][w]=1e18;
	}
	while (!qq.empty()){
		ll u=qq.front();
		qq.pop();
		inq[u]=0;
		for (ll i=point[u];i!=-1;i=edge[i].nxt){
			ll v=edge[i].v;
			if (f[v][w]>f[u][w]+1){
				f[v][w]=f[u][w]+1;
				if (!inq[v]){
					inq[v]=1;
					qq.push(v);
				}
			}
		}
	}
}

int main()
{
	cin>>n>>m>>k>>s;
	memset(f,0x7f,sizeof(f));
	memset(q,0,sizeof(q));
	for (ll i=1;i<=n;i++){
		int a;
		cin>>a;
		q[i]=a;
	}
	e=0;
	memset(point,-1,sizeof(point));
	for (ll i=1;i<=m;i++){
		ll a,b;
		cin>>a>>b;
		add_edge(a,b);
		add_edge(b,a);
	}
	for (ll i=1;i<=k;i++)
		bfs(i);
	for (ll i=1;i<=n;i++){
		sort(f[i]+1,f[i]+k+1);
		ll sum=0;
		for (int j=1;j<=s;j++) sum+=f[i][j];
		cout<<sum<<" ";
	}
	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
// *st.find_by_order(index), 0-based
// order_of_key(e)

using cd = complex<double>;
const double PI = acos(-1);

void fft(vector<cd> & a, bool invert) {
	int n = a.size();
	if (n == 1)
		return;

	vector<cd> a0(n / 2), a1(n / 2);
	for (int i = 0; 2 * i < n; i++) {
		a0[i] = a[2*i];
		a1[i] = a[2*i+1];
	}
	fft(a0, invert);
	fft(a1, invert);

	double ang = 2 * PI / n * (invert ? -1 : 1);
	cd w(1), wn(cos(ang), sin(ang));
	for (int i = 0; 2 * i < n; i++) {
		a[i] = a0[i] + w * a1[i];
		a[i + n/2] = a0[i] - w * a1[i];
		if (invert) {
			a[i] /= 2;
			a[i + n/2] /= 2;
		}
		w *= wn;
	}
}

vector<int> multiply(vector<int> &a, vector<int> &b) {
	vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
	int n = 1;
	while (n < a.size() + b.size())
		n <<= 1;
	fa.resize(n);
	fb.resize(n);

	fft(fa, false);
	fft(fb, false);
	for (int i = 0; i < n; i++)
		fa[i] *= fb[i];
	fft(fa, true);

	vector<int> result(n);
	for (int i = 0; i < n; i++)
		result[i] = round(fa[i].real());
	return result;
}

const int mx=2e5+777;
vector<int> a(mx,0), b(mx,0);
unordered_set<int> ust;

int main() {
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
	int n,x,y; cin>>n>>x>>y;
	for (int i=0; i<=n; i++) {
		int cur; cin>>cur;
		a[cur]=1; b[mx-1-cur]=1;
	}
	vector<int> c=multiply(a,b);
	set<int> st;
	for (int i=0; i<c.size(); i++) {
		if(c[i]) {
			int cur=i-(mx-1);
			if(cur>0)
				ust.insert(cur);
		}
	}
	int q; cin>>q;
	while(q--) {
		int l; cin>>l; l/=2;
		int ret=-1;
		for (int d=1; d*d<=l; d++) {
			if(l%d==0) {
				for (int u : {d, l/d}) {
					int cur=u-y;
					if(ust.count(cur))
						ret=max(ret,u*2);
				}
			}
		}
		cout<<ret<<" ";
	}
	cout<<"\n";
	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
// *st.find_by_order(index), 0-based
// order_of_key(e)

using cd = complex<double>;
const double PI = acos(-1);

void fft(vector<cd> & a, bool invert) {
	int n = a.size();
	if (n == 1)
		return;

	vector<cd> a0(n / 2), a1(n / 2);
	for (int i = 0; 2 * i < n; i++) {
		a0[i] = a[2*i];
		a1[i] = a[2*i+1];
	}
	fft(a0, invert);
	fft(a1, invert);

	double ang = 2 * PI / n * (invert ? -1 : 1);
	cd w(1), wn(cos(ang), sin(ang));
	for (int i = 0; 2 * i < n; i++) {
		a[i] = a0[i] + w * a1[i];
		a[i + n/2] = a0[i] - w * a1[i];
		if (invert) {
			a[i] /= 2;
			a[i + n/2] /= 2;
		}
		w *= wn;
	}
}

vector<int> multiply(vector<int> &a, vector<int> &b) {
	vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
	int n = 1;
	while (n < a.size() + b.size())
		n <<= 1;
	fa.resize(n);
	fb.resize(n);

	fft(fa, false);
	fft(fb, false);
	for (int i = 0; i < n; i++)
		fa[i] *= fb[i];
	fft(fa, true);

	vector<int> result(n);
	for (int i = 0; i < n; i++)
		result[i] = round(fa[i].real());
	return result;
}

const int mx=2e5+777;
vector<int> a(mx,0), b(mx,0);
vector<bool> flag(2*mx,false);

int main() {
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
	int n,x,y; cin>>n>>x>>y;
	for (int i=0; i<=n; i++) {
		int cur; cin>>cur;
		a[cur]=1; b[mx-1-cur]=1;
	}
	vector<int> c=multiply(a,b);
	set<int> st;
	for (int i=0; i<c.size(); i++) {
		if(c[i]) {
			int cur=i-(mx-1);
			if(cur>0)
				flag[cur]=true;
		}
	}
	int q; cin>>q;
	while(q--) {
		int l; cin>>l; l/=2;
		int ret=-1;
		set<int> div;
		for (int d=1; d*d<=l; d++)
			if(l%d==0) div.insert(d), div.insert(l/d);
		for (auto u : div) {
			int cur=u-y;
			if(cur>0 && flag[cur])
				ret=u*2;
		}
		cout<<ret<<" ";
	}
	cout<<"\n";
	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
// *st.find_by_order(index), 0-based
// order_of_key(e)

using cd = complex<double>;
const double PI = acos(-1);

void fft(vector<cd> & a, bool invert) {
	int n = a.size();
	if (n == 1)
		return;

	vector<cd> a0(n / 2), a1(n / 2);
	for (int i = 0; 2 * i < n; i++) {
		a0[i] = a[2*i];
		a1[i] = a[2*i+1];
	}
	fft(a0, invert);
	fft(a1, invert);

	double ang = 2 * PI / n * (invert ? -1 : 1);
	cd w(1), wn(cos(ang), sin(ang));
	for (int i = 0; 2 * i < n; i++) {
		a[i] = a0[i] + w * a1[i];
		a[i + n/2] = a0[i] - w * a1[i];
		if (invert) {
			a[i] /= 2;
			a[i + n/2] /= 2;
		}
		w *= wn;
	}
}

vector<int> multiply(vector<int> &a, vector<int> &b) {
	vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
	int n = 1;
	while (n < a.size() + b.size())
		n <<= 1;
	fa.resize(n);
	fb.resize(n);

	fft(fa, false);
	fft(fb, false);
	for (int i = 0; i < n; i++)
		fa[i] *= fb[i];
	fft(fa, true);

	vector<int> result(n);
	for (int i = 0; i < n; i++)
		result[i] = round(fa[i].real());
	return result;
}

const int mx=2e5+777;
vector<int> a(mx,0), b(mx,0);
bool flag[mx];

int main() {
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
	int n,x,y; cin>>n>>x>>y;
	for (int i=0; i<=n; i++) {
		int cur; cin>>cur;
		a[cur]=1; b[mx-1-cur]=1;
	}
	vector<int> c=multiply(a,b);
	set<int> st;
	for (int i=0; i<c.size(); i++) {
		if(c[i]) {
			int cur=i-(mx-1);
			if(cur>0 && cur<mx)
				flag[cur]=true;
		}
	}
	int q; cin>>q;
	while(q--) {
		int l; cin>>l; l/=2;
		int ret=-1;
		for (int d=1; d*d<=l; d++) {
			if(l%d==0) {
				for (int u : {d, l/d}) {
					int cur=u-y;
					if(cur>=0 && flag[cur])
						ret=max(ret,u*2);
				}
			}
		}
		cout<<ret<<" ";
	}
	cout<<"\n";
	return 0;
}


// created by mtnshh

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

#define ll long long
#define ld long double

#define rep(i,a,b) for(ll i=a;i<b;i++)
#define repb(i,a,b) for(ll i=a;i>=b;i--)

#define err() cout<<"--------------------------"<<endl;
#define errA(A) for(auto i:A)   cout<<i<<" ";cout<<endl;
#define err1(a) cout<<#a<<" "<<a<<endl
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<endl
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<endl
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<endl

#define pb push_back
#define all(A)  A.begin(),A.end()
#define allr(A)    A.rbegin(),A.rend()
#define ft first
#define sd second

#define pll pair<ll,ll>
#define V vector<ll>
#define S set<ll>
#define VV vector<V>
#define Vpll vector<pll>

#define endl "\n"

const ll logN = 20;
const ll M = 1000000007;
const ll INF = 1e18;
#define PI 3.14159265

const ll N = 1000005;

const ll MOD = 998244353;

constexpr ll mAdd(ll a, ll b, ll c) {
	return (a+b >= c ? a+b-c : a+b);
}
constexpr ll mSub(ll a, ll b, ll c) {
	return (a < b ? a-b+c : a-b);
}
constexpr ll modPow(ll a, ll b, ll c) {
	return ((b & 1) ? (a * modPow(a, b ^ 1, c) % c) : (b == 0) ? 1 : (modPow(a*a % c, b >> 1, c)));
}

// Iterative NTT in O(n log n)
// Preconditions: P is prime, n = 2^m, 2^m | P-1, 3 is a generator of P
template<ll P>
void ntt(vector<ll>& vec, bool inv) {
	// Apply the bit-reverse permutation
	ll n = vec.size();
	for (ll i = 0; i < n; ++i) {
		ll t = 0;
		for (ll j = 0; (1<<j) < n; ++j) {
			if (i & (1<<j)) t |= n >> (j+1);
		}
		if (i < t) swap(vec[i], vec[t]);
	}
	// Do Cooley-Tukey
	ll root = modPow(3, (inv ? (n-1) : 1) * (ll)(P-1) / n, P);
	for (ll h = 1; h < n; h <<= 1) {
		ll curr = 1, mult = modPow(root, n / (2*h), P);
		for (ll j = 0; j < h; ++j) {
			for (ll i = j; i < n; i += 2*h) {
				ll v = (curr * vec[i^h]) % P;
				vec[i^h] = mSub(vec[i], v, P);
				vec[i] = mAdd(vec[i], v, P);
			}
			curr = (curr * mult) % P;
		}
	}
}

// Calculates a * b = c (mod P) for two polynomials a and b. P must be valid for ntt. Assumes 0 <= a[i], b[i] < P.
template<ll P>
vector<ll> polyMult(vector<ll> a, vector<ll> b) {
	ll n = 1, t = (ll)a.size() + b.size() - 1;
	while(n < t) n <<= 1;

	a.resize(n, 0); b.resize(n, 0);
	ntt<P>(a, 0); ntt<P>(b, 0);

	vector<ll> c(n);
	ll n_inv = modPow(n, P-2, P);
	for (ll i = 0; i < n; ++i) c[i] = (ll)a[i]*b[i] % P * n_inv % P;

	ntt<P>(c, 1);
	c.resize(t);
	return c;
}

ll res[N];

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll n, x, y;
    cin >> n >> x >> y;
    V a(x+1, 0), b(x+1, 0);
    rep(i,0,n+1){
    	ll k;
    	cin >> k;
    	a[k] = 1;
    	b[x-k] = 1;
    }
    V prod = polyMult<MOD>(a, b);
    rep(i,0,2*x+1){
    	if(prod[i]>0 and i>x){
    		ll t = 2 * (i - x + y);
    		if(t >= 0 and t < N){
    			res[t] = t;
    		}
    	}
    }
    rep(i,2,N){
    	for(ll j=i;j<N;j+=i){
    		res[j]=max(res[i],res[j]);
    	}
    }
    ll q;
    cin >> q;
    while(q--){
    	ll k;
    	cin >> k;
    	if(res[k]>0)	cout<<res[k]<<" ";
    	else	cout<<-1<<" ";
    }cout<<endl;
    return 0;
}


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

#define ll long long
#define ar array

template<typename T> struct cp {
	T x, y;
	cp<T> operator+(const cp &o) const {
		return {x+o.x, y+o.y};
	}
	cp<T> operator*(const cp &o) const {
		return {x*o.x-y*o.y, x*o.y+y*o.x};
	}
};
template<typename T> struct fft {
	static const int mxN=1<<19;
	cp<T> rt[mxN];
	void fi(int n) {
		T PI=acos((T)-1);
		for(int i=0; i<n/2; ++i) {
			T an=2*PI*i/n;
			rt[i+n/2]={cos(an), sin(an)};
		}
		for(int i=n/2; --i; )
			rt[i]=rt[2*i];
	}
	void ac(vector<cp<T>> &a) {
		int n=a.size();
		for(int i=0, j=0; i<n; ++i) {
			if(i>j)
				swap(a[i], a[j]);
			for(int k=n/2; (j^=k)<k; k/=2);
		}
		for(int st=1; 2*st<=n; st*=2) {
			for(int i=0; i<n; i+=2*st) {
				for(int j=i; j<i+st; ++j) {
					cp<T> z=rt[j-i+st]*a[j+st];
					a[j+st]=a[j]+cp<T>{-1, 0}*z;
					a[j]=a[j]+z;
				}
			}
		}
	}
	void mul(vector<cp<T>> &g, vector<cp<T>> &h) {
		int n=g.size();
		ac(g);
		for(int i=0; i<n; ++i) {
			int j=(n-i)&(n-1);
			cp<T> g2=g[i]*g[i];
			g2.x=-g2.x;
			h[i]=(g[j]*g[j]+g2)*cp<T>{(T)0, (T)-1/4/n};
		}
		ac(h);
	}
};
fft<long double> f;
vector<cp<long double>> c1(1<<19), c2(1<<19);
int c[1000001];

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

	int n, x, y;
	cin >> n >> x >> y;
	for(int i=0; i<=n; ++i) {
		int b;
		cin >> b;
		++c1[b].x;
		++c1[x-b].y;
	}
	f.fi(1<<19);
	f.mul(c1, c2);
	for(int i=x+1; i<=2*x; ++i) {
		c[i-x+y]=round(c2[i].x)>0?i-x:0;
	}
	for(int i=1; i<=1e6; ++i)
		for(int j=i; (j+=i)<=1e6; )
			c[j]=max(c[j], c[i]);
	int q;
	cin >> q;
	while(q--) {
		int l;
		cin >> l;
		int a=(c[l/2]+y)*2;
		cout << (c[l/2]?a:-1) << " ";
	}
}


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

#define ll long long
#define ar array

template<typename T> struct cp {
	T x, y;
	cp<T> operator+(const cp &o) const {
		return {x+o.x, y+o.y};
	}
	cp<T> operator*(const cp &o) const {
		return {x*o.x-y*o.y, x*o.y+y*o.x};
	}
};
template<typename T> struct fft {
	static const int mxN=1<<19;
	cp<T> rt[mxN];
	void fi(int n) {
		T PI=acos((T)-1);
		for(int i=0; i<n/2; ++i) {
			T an=2*PI*i/n;
			rt[i+n/2]={cos(an), sin(an)};
		}
		for(int i=n/2; --i; )
			rt[i]=rt[2*i];
	}
	void ac(vector<cp<T>> &a) {
		int n=a.size();
		for(int i=0, j=0; i<n; ++i) {
			if(i>j)
				swap(a[i], a[j]);
			for(int k=n/2; (j^=k)<k; k/=2);
		}
		for(int st=1; 2*st<=n; st*=2) {
			for(int i=0; i<n; i+=2*st) {
				for(int j=i; j<i+st; ++j) {
					cp<T> z=rt[j-i+st]*a[j+st];
					a[j+st]=a[j]+cp<T>{-1, 0}*z;
					a[j]=a[j]+z;
				}
			}
		}
	}
	void mul(vector<cp<T>> &g, vector<cp<T>> &h) {
		int n=g.size();
		ac(g);
		for(int i=0; i<n; ++i) {
			int j=(n-i)&(n-1);
			cp<T> g2=g[i]*g[i];
			g2.x=-g2.x;
			h[i]=(g[j]*g[j]+g2)*cp<T>{(T)0, (T)-1/4/n};
		}
		ac(h);
	}
};
fft<long double> f;
vector<cp<long double>> c1(1<<19), c2(1<<19);
int c[1000001];

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

	int n, x, y;
	cin >> n >> x >> y;
	for(int i=0; i<=n; ++i) {
		int b;
		cin >> b;
		++c1[b].x;
		++c1[x-b].y;
	}
	f.fi(1<<19);
	f.mul(c1, c2);
	for(int i=x+1; i<=2*x; ++i) {
		c[i-x+y]=round(c2[i].x)>0?i-x:0;
	}
	for(int i=1; i<=1e6; ++i)
		for(int j=i; (j+=i)<=1e6; )
			c[j]=max(c[j], c[i]);
	int q;
	cin >> q;
	while(q--) {
		int l;
		cin >> l;
		int a=(c[l/2]+y)*2;
		cout << (a?a:-1) << " ";
	}
}


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

using cd = complex<double>;
const double PI = acos(-1);

void fft(vector<cd>& a, bool invert) {
    int n = a.size();
    if (n == 1) return;

    vector<cd> a0(n/2), a1(n/2);
    for (int i = 0; 2*i < n; i++) {
        a0[i] = a[2*i];
        a1[i] = a[2*i+1];
    }
    fft(a0, invert);
    fft(a1, invert);

    double ang = 2*PI/n*(invert?-1:1);
    cd w(1), w_n(cos(ang),sin(ang));
    for (int i = 0; 2*i < n; i++) {
        a[i]     = a0[i] + w*a1[i];
        a[i+n/2] = a0[i] - w*a1[i];

        if (invert) { //divide by n in total
            a[i] /= 2;
            a[i+n/2] /= 2;
        }
        w *= w_n;
    }
}

//source: https://cp-algorithms.com/algebra/fft.html
/*
void fft(vector<cd> & a, bool invert) {
    int n = a.size();

    for (int i = 1, j = 0; i < n; i++) {
        int bit = n >> 1;
        for (; j & bit; bit >>= 1)
            j ^= bit;
        j ^= bit;

        if (i < j)
            swap(a[i], a[j]);
    }

    for (int len = 2; len <= n; len <<= 1) {
        double ang = 2 * PI / len * (invert ? -1 : 1);
        cd wlen(cos(ang), sin(ang));
        for (int i = 0; i < n; i += len) {
            cd w(1);
            for (int j = 0; j < len / 2; j++) {
                cd u = a[i+j], v = a[i+j+len/2] * w;
                a[i+j] = u + v;
                a[i+j+len/2] = u - v;
                w *= wlen;
            }
        }
    }

    if (invert) {
        for (cd & x : a)
            x /= n;
    }
}
*/

vector<int> multiply(vector<int> a, vector<int> b) {
    vector<cd> fa(a.begin(),a.end()), fb(b.begin(),b.end());
    int n = 1;
    while (n < a.size() + b.size()) n *= 2;
    fa.resize(n); fb.resize(n);

    fft(fa,false);
    fft(fb,false);
    for (int i = 0; i < n; i++) {
        fa[i] *= fb[i];
    }
    fft(fa,true);

    vector<int> res(n);
    for (int i = 0; i < n; i++) {
        res[i] = round(fa[i].real());
    }
    return res;
}

int N, X, Y, Q;
const int maxn = 200005, off = 200000;
vector<int> a, b, c;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    cin >> N >> X >> Y;
    a.resize(maxn); b.resize(maxn);
    for (int i = 0; i <= N; i++) {
        int ai; cin >> ai;
        a[ai] = 1;
        b[off-ai] = 1;
    }
    c = multiply(a,b);
    cin >> Q;
    while (Q--) {
        int li; cin >> li;
        li /= 2;
        int ans = -1;
        for (int i = 1; i*i <= li; i++) {
            if (li%i) continue;
            for (int L: {li/i-Y,i-Y}) {
                if (L <= 0 || L+off >= c.size()) continue;
                if (c[L+off] != 0) {
                    ans = max(ans,2*(L+Y));
                }
            }
        }
        cout << ans << ' ';
    }
    cout << '\n';
}


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

using cd = complex<double>;
const double PI = acos(-1);

/*
void fft(vector<cd>& a, bool invert) {
    int n = a.size();
    if (n == 1) return;

    vector<cd> a0(n/2), a1(n/2);
    for (int i = 0; 2*i < n; i++) {
        a0[i] = a[2*i];
        a1[i] = a[2*i+1];
    }
    fft(a0, invert);
    fft(a1, invert);

    double ang = 2*PI/n*(invert?-1:1);
    cd w(1), w_n(cos(ang),sin(ang));
    for (int i = 0; 2*i < n; i++) {
        a[i]     = a0[i] + w*a1[i];
        a[i+n/2] = a0[i] - w*a1[i];

        if (invert) { //divide by n in total
            a[i] /= 2;
            a[i+n/2] /= 2;
        }
        w *= w_n;
    }
}
*/

//source: https://cp-algorithms.com/algebra/fft.html
void fft(vector<cd> & a, bool invert) {
    int n = a.size();

    for (int i = 1, j = 0; i < n; i++) {
        int bit = n >> 1;
        for (; j & bit; bit >>= 1)
            j ^= bit;
        j ^= bit;

        if (i < j)
            swap(a[i], a[j]);
    }

    for (int len = 2; len <= n; len <<= 1) {
        double ang = 2 * PI / len * (invert ? -1 : 1);
        cd wlen(cos(ang), sin(ang));
        for (int i = 0; i < n; i += len) {
            cd w(1);
            for (int j = 0; j < len / 2; j++) {
                cd u = a[i+j], v = a[i+j+len/2] * w;
                a[i+j] = u + v;
                a[i+j+len/2] = u - v;
                w *= wlen;
            }
        }
    }

    if (invert) {
        for (cd & x : a)
            x /= n;
    }
}

vector<int> multiply(vector<int> a, vector<int> b) {
    vector<cd> fa(a.begin(),a.end()), fb(b.begin(),b.end());
    int n = 1;
    while (n < a.size() + b.size()) n *= 2;
    fa.resize(n); fb.resize(n);

    fft(fa,false);
    fft(fb,false);
    for (int i = 0; i < n; i++) {
        fa[i] *= fb[i];
    }
    fft(fa,true);

    vector<int> res(n);
    for (int i = 0; i < n; i++) {
        res[i] = round(fa[i].real());
    }
    return res;
}

int N, X, Y, Q;
const int maxn = 200005, off = 200000;
vector<int> divisors[1000006];
vector<int> a, b, c;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    cin >> N >> X >> Y;
    a.resize(maxn); b.resize(maxn);
    for (int i = 0; i <= N; i++) {
        int ai; cin >> ai;
        a[ai] = 1;
        b[off-ai] = 1;
    }
    c = multiply(a,b);
    for (int i = 1; i <= 1000000; i++) {
        divisors[i].reserve(25);
    }
    for (int i = 1; i <= 1000000; i++) {
        for (int j = i; j <= 1000000; j += i) {
            divisors[j].push_back(i);
        }
    }
    cin >> Q;
    while (Q--) {
        int li; cin >> li;
        li /= 2;
        bool found = false;
        for (int i = divisors[li].size()-1; i >= 0; i--) {
            int L = divisors[li][i] - Y;
            if (L <= 0 || L+off >= c.size()) continue;
            //cout << divisors[li][i] << ": " << L << ' ' << L+off << endl;
            //assert(0 <= L+off && L+off < c.size());
            if (c[L+off] != 0) {
                found = true;
                cout << divisors[li][i]*2 << ' ';
                break;
            }
        }
        if (!found) {
            cout << -1 << ' ';
        }
    }
    cout << '\n';
}


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

using cd = complex<double>;
const double PI = acos(-1);

/*
void fft(vector<cd>& a, bool invert) {
    int n = a.size();
    if (n == 1) return;

    vector<cd> a0(n/2), a1(n/2);
    for (int i = 0; 2*i < n; i++) {
        a0[i] = a[2*i];
        a1[i] = a[2*i+1];
    }
    fft(a0, invert);
    fft(a1, invert);

    double ang = 2*PI/n*(invert?-1:1);
    cd w(1), w_n(cos(ang),sin(ang));
    for (int i = 0; 2*i < n; i++) {
        a[i]     = a0[i] + w*a1[i];
        a[i+n/2] = a0[i] - w*a1[i];

        if (invert) { //divide by n in total
            a[i] /= 2;
            a[i+n/2] /= 2;
        }
        w *= w_n;
    }
}
*/

//source: https://cp-algorithms.com/algebra/fft.html
void fft(vector<cd> & a, bool invert) {
    int n = a.size();

    for (int i = 1, j = 0; i < n; i++) {
        int bit = n >> 1;
        for (; j & bit; bit >>= 1)
            j ^= bit;
        j ^= bit;

        if (i < j)
            swap(a[i], a[j]);
    }

    for (int len = 2; len <= n; len <<= 1) {
        double ang = 2 * PI / len * (invert ? -1 : 1);
        cd wlen(cos(ang), sin(ang));
        for (int i = 0; i < n; i += len) {
            cd w(1);
            for (int j = 0; j < len / 2; j++) {
                cd u = a[i+j], v = a[i+j+len/2] * w;
                a[i+j] = u + v;
                a[i+j+len/2] = u - v;
                w *= wlen;
            }
        }
    }

    if (invert) {
        for (cd & x : a)
            x /= n;
    }
}

vector<int> multiply(vector<int> a, vector<int> b) {
    vector<cd> fa(a.begin(),a.end()), fb(b.begin(),b.end());
    int n = 1;
    while (n < a.size() + b.size()) n *= 2;
    fa.resize(n); fb.resize(n);

    fft(fa,false);
    fft(fb,false);
    for (int i = 0; i < n; i++) {
        fa[i] *= fb[i];
    }
    fft(fa,true);

    vector<int> res(n);
    for (int i = 0; i < n; i++) {
        res[i] = round(fa[i].real());
    }
    return res;
}

int N, X, Y, Q;
const int maxn = 200005, off = 200000;
vector<int> a, b, c;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    cin >> N >> X >> Y;
    a.resize(maxn); b.resize(maxn);
    for (int i = 0; i <= N; i++) {
        int ai; cin >> ai;
        a[ai] = 1;
        b[off-ai] = 1;
    }
    c = multiply(a,b);
    cin >> Q;
    while (Q--) {
        int li; cin >> li;
        li /= 2;
        int ans = -1;
        for (int i = 1; i*i <= li; i++) {
            if (li%i) continue;
            for (int L: {li/i-Y,i-Y}) {
                if (L <= 0 || L+off >= c.size()) continue;
                if (c[L+off] != 0) {
                    ans = max(ans,2*(L+Y));
                }
            }
        }
        cout << ans << ' ';
    }
    cout << '\n';
}


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

using cd = complex<double>;
const double PI = acos(-1);

int reverse(int num, int lg_n) {
    int res = 0;
    for (int i = 0; i < lg_n; i++) {
        if (num & (1 << i))
            res |= 1 << (lg_n - 1 - i);
    }
    return res;
}
void fft(vector<cd> & a, bool invert) {
    int n = a.size();
    int lg_n = 0;
    while ((1 << lg_n) < n)
        lg_n++;

    for (int i = 0; i < n; i++) {
        if (i < reverse(i, lg_n))
            swap(a[i], a[reverse(i, lg_n)]);
    }

    for (int len = 2; len <= n; len <<= 1) {
        double ang = 2 * PI / len * (invert ? -1 : 1);
        cd wlen(cos(ang), sin(ang));
        for (int i = 0; i < n; i += len) {
            cd w(1);
            for (int j = 0; j < len / 2; j++) {
                cd u = a[i+j], v = a[i+j+len/2] * w;
                a[i+j] = u + v;
                a[i+j+len/2] = u - v;
                w *= wlen;
            }
        }
    }

    if (invert) {
        for (cd & x : a)
            x /= n;
    }
}
vector<int> multiply(vector<int> const& a, vector<int> const& b) {
    vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
    int n = 1;
    while (n < a.size() + b.size())
        n <<= 1;
    fa.resize(n);
    fb.resize(n);

    fft(fa, false);
    fft(fb, false);
    for (int i = 0; i < n; i++)
        fa[i] *= fb[i];
    fft(fa, true);

    vector<int> result(n);
    for (int i = 0; i < n; i++)
        result[i] = round(fa[i].real());
    return result;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, x, y;
    cin >> n >> x >> y;
    int arr[n+1];
    int i;
    for(i=0;i<=n;i++)
        cin >> arr[i];
    vector<int> fir(x+1), sec(x+1);
    for(i=0;i<=n;i++)
        fir[arr[i]] = 1, sec[x-arr[i]] = 1;
    vector<int> res = multiply(fir, sec);
    int ans[1000001];
    for(i=0;i<=1000000;i++)
        ans[i] = -1;
    for(i=x+1;i<res.size();i++)
        if(res[i])
        {
            int num = 2*(i-x+y);
            for(int j=num;j<=1000000;j+=num)
                ans[j] = num;
        }
    int q;
    cin >> q;
    while(q--)
    {
        int l;
        cin >> l;
        cout << ans[l] << " ";
    }

}


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

using cd = complex<double>;
const double PI = acos(-1);

int reverse(int num, int lg_n) {
    int res = 0;
    for (int i = 0; i < lg_n; i++) {
        if (num & (1 << i))
            res |= 1 << (lg_n - 1 - i);
    }
    return res;
}
void fft(vector<cd> & a, bool invert) {
    int n = a.size();
    int lg_n = 0;
    while ((1 << lg_n) < n)
        lg_n++;

    for (int i = 0; i < n; i++) {
        if (i < reverse(i, lg_n))
            swap(a[i], a[reverse(i, lg_n)]);
    }

    for (int len = 2; len <= n; len <<= 1) {
        double ang = 2 * PI / len * (invert ? -1 : 1);
        cd wlen(cos(ang), sin(ang));
        for (int i = 0; i < n; i += len) {
            cd w(1);
            for (int j = 0; j < len / 2; j++) {
                cd u = a[i+j], v = a[i+j+len/2] * w;
                a[i+j] = u + v;
                a[i+j+len/2] = u - v;
                w *= wlen;
            }
        }
    }

    if (invert) {
        for (cd & x : a)
            x /= n;
    }
}
vector<int> multiply(vector<int> const& a, vector<int> const& b) {
    vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
    int n = 1;
    while (n < a.size() + b.size())
        n <<= 1;
    fa.resize(n);
    fb.resize(n);

    fft(fa, false);
    fft(fb, false);
    for (int i = 0; i < n; i++)
        fa[i] *= fb[i];
    fft(fa, true);

    vector<int> result(n);
    for (int i = 0; i < n; i++)
        result[i] = round(fa[i].real());
    return result;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, x, y;
    cin >> n >> x >> y;
    int arr[n+1];
    int i;
    for(i=0;i<=n;i++)
        cin >> arr[i];
    vector<int> fir(x+1), sec(x+1);
    for(i=0;i<=n;i++)
        fir[arr[i]] = 1, sec[x-arr[i]] = 1;
    vector<int> res = multiply(fir, sec);

    int q;
    cin >> q;
    while(q--)
    {
        int l;
        cin >> l;
        int ans = -1;
        for(i=1;i*i<=l;i++)
        {
            if(l%i == 0)
            {
                int a = i;
                int b = l/i;
                if(a%2 == 0)
                {
                    a = a/2;
                    a = a + x - y;
                    if(a > x && a < res.size() && res[a])
                        ans = max(ans, i);
                }
                if(b%2 == 0)
                {
                    b = b/2;
                    b = b + x - y;
                    if(b > x && b < res.size() && res[b])
                        ans = max(ans, l/i);
                }
            }
        }
        cout << ans << " ";
    }

}


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

using cd = complex<double>;
const double PI = acos(-1);

int reverse(int num, int lg_n) {
    int res = 0;
    for (int i = 0; i < lg_n; i++) {
        if (num & (1 << i))
            res |= 1 << (lg_n - 1 - i);
    }
    return res;
}
void fft(vector<cd> & a, bool invert) {
    int n = a.size();
    int lg_n = 0;
    while ((1 << lg_n) < n)
        lg_n++;

    for (int i = 0; i < n; i++) {
        if (i < reverse(i, lg_n))
            swap(a[i], a[reverse(i, lg_n)]);
    }

    for (int len = 2; len <= n; len <<= 1) {
        double ang = 2 * PI / len * (invert ? -1 : 1);
        cd wlen(cos(ang), sin(ang));
        for (int i = 0; i < n; i += len) {
            cd w(1);
            for (int j = 0; j < len / 2; j++) {
                cd u = a[i+j], v = a[i+j+len/2] * w;
                a[i+j] = u + v;
                a[i+j+len/2] = u - v;
                w *= wlen;
            }
        }
    }

    if (invert) {
        for (cd & x : a)
            x /= n;
    }
}
vector<int> multiply(vector<int> const& a, vector<int> const& b) {
    vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
    int n = 1;
    while (n < a.size() + b.size())
        n <<= 1;
    fa.resize(n);
    fb.resize(n);

    fft(fa, false);
    fft(fb, false);
    for (int i = 0; i < n; i++)
        fa[i] *= fb[i];
    fft(fa, true);

    vector<int> result(n);
    for (int i = 0; i < n; i++)
        result[i] = round(fa[i].real());
    return result;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, x, y;
    cin >> n >> x >> y;
    int arr[n+1];
    int i;
    for(i=0;i<=n;i++)
        cin >> arr[i];
    vector<int> fir(x+1), sec(x+1);
    for(i=0;i<=n;i++)
        fir[arr[i]] = 1, sec[x-arr[i]] = 1;
    vector<int> res = multiply(fir, sec);

    int q;
    cin >> q;
    while(q--)
    {
        int l;
        cin >> l;
        int ans = -1;
        for(i=1;i*i<=l;i++)
        {
            if(l%i == 0)
            {
                int a = i;
                int b = l/i;
                if(a%2 == 0)
                {
                    a = a/2;
                    a = a + x - y;
                    if(a > x && res[a])
                        ans = max(ans, i);
                }
                if(b%2 == 0)
                {
                    b = b/2;
                    b = b + x - y;
                    if(b > x && res[b])
                        ans = max(ans, l/i);
                }
            }
        }
        cout << ans << " ";
    }

}


#include<bits/stdc++.h>

using namespace std;

#define forn(i, n) for(int i = 0; i < n; i++)
#define sz(a) ((int)(a).size())

const int LOGN = 20;
const int N = (1 << LOGN);
const int K = 200043;
const int M = 1000043;

typedef double ld;
typedef long long li;

const ld PI = acos(-1.0);

struct comp
{
    ld x, y;
    comp(ld x = .0, ld y = .0) : x(x), y(y) {}
    inline comp conj() { return comp(x, -y); }
};

inline comp operator +(const comp &a, const comp &b)
{
    return comp(a.x + b.x, a.y + b.y);
}

inline comp operator -(const comp &a, const comp &b)
{
    return comp(a.x - b.x, a.y - b.y);
}

inline comp operator *(const comp &a, const comp &b)
{
    return comp(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}

inline comp operator /(const comp &a, const ld &b)
{
    return comp(a.x / b, a.y / b);
}

vector<comp> w[LOGN];
vector<int> rv[LOGN];

void precalc()
{
    for(int st = 0; st < LOGN; st++)
    {
        w[st].assign(1 << st, comp());
        for(int k = 0; k < (1 << st); k++)
        {
            double ang = PI / (1 << st) * k;
            w[st][k] = comp(cos(ang), sin(ang));
        }

        rv[st].assign(1 << st, 0);
        if(st == 0)
        {
            rv[st][0] = 0;
            continue;
        }
        int h = (1 << (st - 1));
        for(int k = 0; k < (1 << st); k++)
            rv[st][k] = (rv[st - 1][k & (h - 1)] << 1) | (k >= h);
    }
}

inline void fft(comp a[N], int n, int ln, bool inv)
{
    for(int i = 0; i < n; i++)
    {
        int ni = rv[ln][i];
        if(i < ni)
            swap(a[i], a[ni]);
    }

    for(int st = 0; (1 << st) < n; st++)
    {
        int len = (1 << st);
        for(int k = 0; k < n; k += (len << 1))
        {
            for(int pos = k; pos < k + len; pos++)
            {
                comp l = a[pos];
                comp r = a[pos + len] * (inv ? w[st][pos - k].conj() : w[st][pos - k]);

                a[pos] = l + r;
                a[pos + len] = l - r;
            }
        }
    }

    if(inv) for(int i = 0; i < n; i++)
        a[i] = a[i] / n;
}

comp aa[N];
comp bb[N];
comp cc[N];

inline void multiply(comp a[N], int sza, comp b[N], int szb, comp c[N], int &szc)
{
    int n = 1, ln = 0;
    while(n < (sza + szb))
        n <<= 1, ln++;
    for(int i = 0; i < n; i++)
        aa[i] = (i < sza ? a[i] : comp());
    for(int i = 0; i < n; i++)
        bb[i] = (i < szb ? b[i] : comp());

    fft(aa, n, ln, false);
    fft(bb, n, ln, false);

    for(int i = 0; i < n; i++)
        cc[i] = aa[i] * bb[i];

    fft(cc, n, ln, true);

    szc = n;
    for(int i = 0; i < n; i++)
        c[i] = cc[i];
}

comp a[N];
comp b[N];
comp c[N];
int used[M];
int dp[M];

int main()
{
    precalc();
    int n, x, y;
    for(int i = 0; i < M; i++)
        dp[i] = -1;
    scanf("%d %d %d", &n, &x, &y);
    vector<int> A(n + 1);
    for(int i = 0; i <= n; i++)
        scanf("%d", &A[i]);
    for(int i = 0; i <= n; i++)
    {
        a[A[i]] = comp(1.0, 0.0);
        b[K - A[i]] = comp(1.0, 0.0);
    }
    int s = 0;
    multiply(a, K + 1, b, K + 1, c, s);
    for(int i = K + 1; i < s; i++)
        if(c[i].x > 0.5)
            used[(i - K + y) * 2] = 1;
    for(int i = 1; i < M; i++)
    {
        if(!used[i]) continue;
        for(int j = i; j < M; j += i)
            dp[j] = max(dp[j], i);
    }
    int q;
    scanf("%d", &q);
    for(int i = 0; i < q; i++)
    {
        int l;
        scanf("%d", &l);
        printf("%d ", dp[l]);
    }
}


/*
	author: Ibrahim-Elsayed (Codeforces Handle)
	date: 8/14/2020
*/
#pragma GCC target("popcnt")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

string s;
int counterr = -50, mxIdxS, mxIdxE;
int pmo = 1;

pair<int, int> chk() {
	for(int i = 0; i < (int)s.length(); i++) {
    	if(s[i] == '1') {
    		int j = i+1, cnt = 1;
    		while(j < (int)s.length() && s[j] == '1') {
    			cnt++;
    			j++;
    		}
    		if(cnt > counterr) {
    			mxIdxS = i;
    			mxIdxE = j;
    			counterr = cnt;
    		}
    	}
    }
    return make_pair(mxIdxS, mxIdxE);
}

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

    int t;
    cin >> t;
    while(t--) {
    	cin >> s;
    	ll a = 0;
    	while((int)s.length() != 0) {
    		pair<int, int> fp = chk();
    		if(pmo&1) {
    			a+=counterr;
    		}
    		counterr = -50;
    		s.erase(fp.first, fp.second-fp.first);
    		pmo++;
    	}
    	cout << a << "\n";
    }

    return 0;
}