stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <algorithm>

#include <cstdio>

#include <iostream>

#include <cfloat>

#include <climits>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <queue>

#include <sstream>

#include <stack>

#include <time.h>

#include <vector>

#include <complex>

#include <map>

#include <set>

#include <iomanip>

#include <math.h>

#include <stdlib.h>

#include <list>

#include <utility>

#include <memory>

#include <cstring>

#include <fstream>

#include <numeric>

#include <assert.h>

#include <bits/stdc++.h>

using namespace std;

int n,m,q,mindist[100004],col[100004],s[100004],d[100004],c[100004];

vector <int> g[100004];

void dfs(int x,int dist,int color){

	if (dist<=mindist[x]) return;

	mindist[x]=dist;

	if (col[x]==0) col[x]=color;

	for (int i=0;i<g[x].size();i++)

		dfs(g[x][i],dist-1,color);

}

int main(){

	memset (col,0,sizeof(col));

	memset (mindist,-1,sizeof(mindist));

	cin>>n>>m;

	for (int i=0;i<m;i++){

		int x,y;

		cin>>x>>y;

		g[x].push_back(y);

		g[y].push_back(x);  

	}

	cin>>q;

	for (int i=0;i<q;i++)

		cin>>s[i]>>d[i]>>c[i];

	for (int i=q-1;i>=0;i--)

		dfs(s[i],d[i],c[i]);

	for (int i=1;i<=n;i++)

		cout<<col[i]<<"\n";

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.342885008744685x):
#include <algorithm>

#include <cstdio>

#include <iostream>

#include <cfloat>

#include <climits>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <queue>

#include <sstream>

#include <stack>

#include <time.h>

#include <vector>

#include <complex>

#include <map>

#include <set>

#include <iomanip>

#include <math.h>

#include <stdlib.h>

#include <list>

#include <utility>

#include <memory>

#include <cstring>

#include <fstream>

#include <numeric>

#include <assert.h>

#include <bits/stdc++.h>

using namespace std;

int n,m,cnt,tot,fst[100004],pnt[100004<<1],nxt[100004<<1],ans[100004],dp[100004][12];

void add(int x,int y){

	pnt[++tot]=y;

	nxt[tot]=fst[x];

	fst[x]=tot;

}

int main(){

	cin>>n>>m;

	int i,j,x,y;

	for (i=1;i<=m;i++){

		cin>>x>>y;

		add(x,y);

		add(y,x);

	}

	cin>>cnt;

	for (i=1;i<=cnt;i++){

		cin>>x>>y>>ans[i];

		dp[x][y]=i;

	}

	for (i=9;i>=0;i--)

		for (x=1;x<=n;x++){

			dp[x][i]=max(dp[x][i],dp[x][i+1]);

			for (j=fst[x];j;j=nxt[j])

				dp[x][i]=max(dp[x][i],dp[pnt[j]][i+1]);

			}

	for (i=1;i<=n;i++) cout<<ans[dp[i][0]]<<endl;

	return 0;

}
// Slower C++ solution:
#include <algorithm>

#include <cstdio>

#include <iostream>

#include <cfloat>

#include <climits>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <queue>

#include <sstream>

#include <stack>

#include <time.h>

#include <vector>

#include <complex>

#include <map>

#include <set>

#include <iomanip>

#include <math.h>

#include <stdlib.h>

#include <list>

#include <utility>

#include <memory>

#include <cstring>

#include <fstream>

#include <numeric>

#include <assert.h>

#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> pii;

int N,M,Q;

vector <int> g[100004];

int col[100004],turns[100004];

bool vis[100004];

void bfs(int v,int d,int c){

	memset (vis,0,sizeof(vis));

	queue <pii> q;

	q.push(pii(v,0));

	while (!q.empty()){

		int x=q.front().first,dist=q.front().second;

		q.pop();

		if (d-dist<=turns[x]) continue;

		turns[x]=d-dist; 

		if (dist>d) continue;

		if (vis[x]) continue;

		vis[x]=1;

		if (col[x]==0) col[x]=c;

		for (int i=0;i<g[x].size();i++)

			q.push(pii(g[x][i],dist+1)); 

	}

}

int v[100004],d[100004],c[100004];

int main(){

	memset (col,0,sizeof(col));

	memset (turns,-1,sizeof(turns));

	cin>>N>>M;

	for (int i=0;i<M;i++){

		int x,y;

		cin>>x>>y;

		g[x].push_back(y);

		g[y].push_back(x);

	}

	cin>>Q;

	for (int i=0;i<Q;i++){

		cin>>v[i]>>d[i]>>c[i];

	}

	for (int i=Q-1;i>=0;i--)

		bfs(v[i],d[i],c[i]);

	for (int i=1;i<=N;i++)

		cout<<col[i]<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1329568614101377x):
#include <algorithm>

#include <cstdio>

#include <iostream>

#include <cfloat>

#include <climits>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <queue>

#include <sstream>

#include <stack>

#include <time.h>

#include <vector>

#include <complex>

#include <map>

#include <set>

#include <iomanip>

#include <math.h>

#include <stdlib.h>

#include <list>

#include <utility>

#include <memory>

#include <cstring>

#include <fstream>

#include <numeric>

#include <assert.h>

#include <bits/stdc++.h>

using namespace std;

int cl[120000];

int lst[120000];

vector <int> eds[120000];

int n,m,q;

void dfs(int v,int d,int c){

	if (!cl[v]) cl[v]=c;

	if (lst[v]>=d) return;

	lst[v]=d;

	if (d==0) return;

	for (int u: eds[v])

		dfs(u,d-1,c);

}

vector <tuple<int,int,int> > vv;

int main(){

	cin>>n>>m;

	for (int i=0;i<m;i++){

		int a,b;

		cin>>a>>b;

		--a,--b;

		eds[a].push_back(b);

		eds[b].push_back(a);  

	}

	cin>>q;

	for (int i=0;i<q;i++){

		int v,d,c;

		cin>>v>>d>>c;

		--v;

		vv.push_back(make_tuple(v,d,c)); 

	}

	reverse (vv.begin(),vv.end());

	for (int i=0;i<q;i++){

		int v,d,c;

		tie(v,d,c)=vv[i];

		dfs(v,d,c);

	}

	for (int i=0;i<n;i++) cout<<cl[i]<<endl;

	return 0;

}
// Slower C++ solution:
//************************

//*  Date: 2018.10.22    *

//*  Problem:            *

//*  Author: xuziyuan    * 

//************************



#include <bits/stdc++.h>



#define rt0 return 0

#define rep(i,n) for(int i=0;i<n;i++)

#define repn(i,n) for(int i=1;i<=n;i++)

#define replet(c) for(char c='a';c<='z';c++)

#define LL long long

#define pii pair<int,int>

#define pb push_back

#define fi first

#define se second

#define mpr make_pair

#define sqr(a) ((a)*(a))



using namespace std;



const LL MOD=1e9+7;



int n,m,q;

vector <int> g[100010];

int colo[100010],most[100010];

vector <pair<pii,int> > pa;

bool vis[100010];



int main()

{

	memset(most,-1,sizeof(most));

	cin>>n>>m;

	rep(i,m)

	{

		int x,y;

		scanf("%d%d",&x,&y);

		g[x].pb(y);

		g[y].pb(x);

	}

	cin>>q;

	rep(i,q)

	{

		int x,y,z;

		scanf("%d%d%d",&x,&y,&z);

		pa.pb({{x,y},z});

	}

	for(int i=pa.size()-1;i>=0;i--)

	{

		int x=pa[i].fi.fi,y=pa[i].fi.se,z=pa[i].se;

		queue <pii> q;

		q.push({x,0});

		memset(vis,0,sizeof(vis));

		while(!q.empty())

		{

			int pos=q.front().fi,all=q.front().se;

			q.pop();

			if(y-all<=most[pos]) continue;

			most[pos]=y-all;

			if(all>y) continue;

			if(vis[pos]) continue;

			//if(x==1) cout<<pos<<endl;

			vis[pos]=true;

			if(colo[pos]==0) colo[pos]=z;

			rep(i,g[pos].size()) q.push({g[pos][i],all+1});

		}

	}

	repn(i,n) printf("%d\n",colo[i]);

	rt0;

}

// Faster C++ solution for the same problem (measured speedup: 1.16135145189881x):
/***********************

*  Date: 2018.11.28    *

*  Problem:            *

*  Author: xuziyuan    * 

***********************/



#include <bits/stdc++.h>



#define rt0 return 0

#define rep(i,n) for(int i=0;i<n;i++)

#define repn(i,n) for(int i=1;i<=n;i++)

#define replet(c) for(char c='a';c<='z';c++)

#define LL long long

#define pii pair <int,int>

#define pb push_back

#define fi first

#define se second

#define mpr make_pair

#define sqr(a) ((a)*(a))



using namespace std;



const LL MOD=1e9+7;



int n,m,q,appe[100010][20],coap[100010];//appe:appearance

vector <int> g[100010];



int main()

{

	cin>>n>>m;

	rep(i,m)

	{

		int x,y;

		scanf("%d%d",&x,&y);

		g[x].pb(y);

		g[y].pb(x);

	}

	cin>>q;

	repn(i,q)

	{

		int x,y;

		scanf("%d%d%d",&x,&y,&coap[i]);

		appe[x][y]=i;

	}

	for(int i=9;i>=0;i--)//算能不能覆盖appe[j][i]之前要先算出appe[p][i+1]会不会被覆盖，所以i要倒着循环 

	{

		repn(p,n)

		{

			rep(k,g[p].size())//i在这里代表可以控制的范围

			{

				int j=g[p][k];

				appe[j][i]=max(appe[j][i],appe[p][i+1]);/*远一条边距离就要加1，这里是看后者可不可以覆盖前者，

				可以说是一种dp，一个类似递归的过程，如果再之前的点把后者覆盖了，也会在前者上体现出来*/

			}	

		}	

	} 

	repn(i,n)

	{

		int lst=0;

		rep(j,11) lst=max(lst,appe[i][j]);

		cout<<coap[lst]<<endl;

	}

	rt0;

}
// Slower C++ solution:
/***********************

*  Date: 2018.11.28    *

*  Problem:            *

*  Author: xuziyuan    * 

***********************/



#include <bits/stdc++.h>



#define rt0 return 0

#define rep(i,n) for(int i=0;i<n;i++)

#define repn(i,n) for(int i=1;i<=n;i++)

#define replet(c) for(char c='a';c<='z';c++)

#define LL long long

#define pii pair <int,int>

#define pb push_back

#define fi first

#define se second

#define mpr make_pair

#define sqr(a) ((a)*(a))



using namespace std;



const LL MOD=1e9+7;



int n,m,q,co[100010],most[100010];

vector <int> g[100010];

vector <pair<int,pii> > qs;

bool vis[100010];



int main()

{

	cin>>n>>m;

	memset(most,-1,sizeof(most));

	rep(i,m)

	{

		int x,y;

		scanf("%d%d",&x,&y);

		g[x].pb(y);

		g[y].pb(x);

	}

	cin>>q;

	rep(i,q)

	{

		int x,y,z;

		scanf("%d%d%d",&x,&y,&z);

		qs.pb({x,{y,z}});

	}

	for(int i=qs.size()-1;i>=0;i--)

	{

		//cout<<qs[i].fi<<' '<<qs[i].se.fi<<' '<<qs[i].se.se<<endl;

		memset(vis,0,sizeof(vis));

		int v=qs[i].fi,colo=qs[i].se.se,dist=qs[i].se.fi;

		queue <pii> q;

		q.push({v,0});

		while(!q.empty())

		{

			int pos=q.front().fi,dist1=q.front().se;

			//cout<<pos<<' '<<colo<<' '<<dist<<' '<<dist1<<endl;

			q.pop();

			if(most[pos]>=dist-dist1) continue;

			most[pos]=dist-dist1;

			if(vis[pos]) continue;

			vis[pos]=true;

			if(co[pos]==0) co[pos]=colo;

			if(dist1==dist) continue;

			rep(j,g[pos].size()) q.push({g[pos][j],dist1+1});

		}

	}

	repn(i,n) printf("%d\n",co[i]);

	rt0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1602853261647152x):
/***********************

*  Date: 2018.11.28    *

*  Problem:            *

*  Author: xuziyuan    * 

***********************/



#include <bits/stdc++.h>



#define rt0 return 0

#define rep(i,n) for(int i=0;i<n;i++)

#define repn(i,n) for(int i=1;i<=n;i++)

#define replet(c) for(char c='a';c<='z';c++)

#define LL long long

#define pii pair <int,int>

#define pb push_back

#define fi first

#define se second

#define mpr make_pair

#define sqr(a) ((a)*(a))



using namespace std;



const LL MOD=1e9+7;



int n,m,q,appe[100010][20],coap[100010];//appe:appearance

vector <int> g[100010];



int main()

{

	cin>>n>>m;

	rep(i,m)

	{

		int x,y;

		scanf("%d%d",&x,&y);

		g[x].pb(y);

		g[y].pb(x);

	}

	cin>>q;

	repn(i,q)

	{

		int x,y;

		scanf("%d%d%d",&x,&y,&coap[i]);

		appe[x][y]=i;

	}

	for(int i=9;i>=0;i--)//算能不能覆盖appe[j][i]之前要先算出appe[p][i+1]会不会被覆盖，所以i要倒着循环 

	{

		repn(p,n)

		{

			rep(k,g[p].size())//i在这里代表可以控制的范围

			{

				int j=g[p][k];

				appe[j][i]=max(appe[j][i],appe[p][i+1]);/*远一条边距离就要加1，这里是看后者可不可以覆盖前者，

				可以说是一种dp，一个类似递归的过程，如果再之前的点把后者覆盖了，也会在前者上体现出来*/

			}	

		}	

	} 

	repn(i,n)

	{

		int lst=0;

		rep(j,11) lst=max(lst,appe[i][j]);

		cout<<coap[lst]<<endl;

	}

	rt0;

}
// Slower C++ solution:
/***********************

*  Date: 2018.11.28    *

*  Problem:            *

*  Author: xuziyuan    * 

***********************/



#include <bits/stdc++.h>



#define rt0 return 0

#define rep(i,n) for(int i=0;i<n;i++)

#define repn(i,n) for(int i=1;i<=n;i++)

#define replet(c) for(char c='a';c<='z';c++)

#define LL long long

#define pii pair <int,int>

#define pb push_back

#define fi first

#define se second

#define mpr make_pair

#define sqr(a) ((a)*(a))



using namespace std;



const LL MOD=1e9+7;



int n,m,q,co[100010],most[100010];

vector <int> g[100010];

vector <pair<int,pii> > qs;

bool vis[100010];



int main()

{

	cin>>n>>m;

	memset(most,-1,sizeof(most));

	rep(i,m)

	{

		int x,y;

		scanf("%d%d",&x,&y);

		g[x].pb(y);

		g[y].pb(x);

	}

	cin>>q;

	rep(i,q)

	{

		int x,y,z;

		scanf("%d%d%d",&x,&y,&z);

		qs.pb({x,{y,z}});

	}

	for(int i=qs.size()-1;i>=0;i--)

	{

		//cout<<qs[i].fi<<' '<<qs[i].se.fi<<' '<<qs[i].se.se<<endl;

		memset(vis,0,sizeof(vis));

		int v=qs[i].fi,colo=qs[i].se.se,dist=qs[i].se.fi;

		queue <pii> q;

		q.push({v,0});

		while(!q.empty())

		{

			int pos=q.front().fi,dist1=q.front().se;

			//cout<<pos<<' '<<colo<<' '<<dist<<' '<<dist1<<endl;

			q.pop();

			if(vis[pos]) continue;

			if(most[pos]>=dist-dist1) continue;

			vis[pos]=true;

			most[pos]=dist-dist1;

			if(co[pos]==0) co[pos]=colo;

			if(dist1==dist) continue;

			rep(j,g[pos].size()) q.push({g[pos][j],dist1+1});

		}

	}

	repn(i,n) printf("%d\n",co[i]);

	rt0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1603096609554369x):
/***********************

*  Date: 2018.11.28    *

*  Problem:            *

*  Author: xuziyuan    * 

***********************/



#include <bits/stdc++.h>



#define rt0 return 0

#define rep(i,n) for(int i=0;i<n;i++)

#define repn(i,n) for(int i=1;i<=n;i++)

#define replet(c) for(char c='a';c<='z';c++)

#define LL long long

#define pii pair <int,int>

#define pb push_back

#define fi first

#define se second

#define mpr make_pair

#define sqr(a) ((a)*(a))



using namespace std;



const LL MOD=1e9+7;



int n,m,q,appe[100010][20],coap[100010];//appe:appearance

vector <int> g[100010];



int main()

{

	cin>>n>>m;

	rep(i,m)

	{

		int x,y;

		scanf("%d%d",&x,&y);

		g[x].pb(y);

		g[y].pb(x);

	}

	cin>>q;

	repn(i,q)

	{

		int x,y;

		scanf("%d%d%d",&x,&y,&coap[i]);

		appe[x][y]=i;

	}

	for(int i=9;i>=0;i--)//算能不能覆盖appe[j][i]之前要先算出appe[p][i+1]会不会被覆盖，所以i要倒着循环 

	{

		repn(p,n)

		{

			rep(k,g[p].size())//i在这里代表可以控制的范围

			{

				int j=g[p][k];

				appe[j][i]=max(appe[j][i],appe[p][i+1]);/*远一条边距离就要加1，这里是看后者可不可以覆盖前者，

				可以说是一种dp，一个类似递归的过程，如果再之前的点把后者覆盖了，也会在前者上体现出来*/

			}	

		}	

	} 

	repn(i,n)

	{

		int lst=0;

		rep(j,11) lst=max(lst,appe[i][j]);

		cout<<coap[lst]<<endl;

	}

	rt0;

}
// Slower C++ solution:
/***********************

*  Date: 2018.11.28    *

*  Problem:            *

*  Author: xuziyuan    * 

***********************/



#include <bits/stdc++.h>



#define rt0 return 0

#define rep(i,n) for(int i=0;i<n;i++)

#define repn(i,n) for(int i=1;i<=n;i++)

#define replet(c) for(char c='a';c<='z';c++)

#define LL long long

#define pii pair <int,int>

#define pb push_back

#define fi first

#define se second

#define mpr make_pair

#define sqr(a) ((a)*(a))



using namespace std;



const LL MOD=1e9+7;



int n,m,q,co[100010],most[100010];

vector <int> g[100010];

vector <pair<int,pii> > qs;

bool vis[100010];



void dfs(int pos,int now,int all,int colo)

{

	if(all-now<=most[pos]) return;

	most[pos]=all-now;

	if(co[pos]==0) co[pos]=colo;

	if(now==all) return;

	rep(i,g[pos].size()) dfs(g[pos][i],now+1,all,colo);

}



int main()

{

	cin>>n>>m;

	memset(most,-1,sizeof(most));

	rep(i,m)

	{

		int x,y;

		scanf("%d%d",&x,&y);

		g[x].pb(y);

		g[y].pb(x);

	}

	cin>>q;

	rep(i,q)

	{

		int x,y,z;

		scanf("%d%d%d",&x,&y,&z);

		qs.pb({x,{y,z}});

	}

	reverse(qs.begin(),qs.end());

	rep(i,q)

	{

		dfs(qs[i].fi,0,qs[i].se.fi,qs[i].se.se);

	}

	repn(i,n) printf("%d\n",co[i]);

	rt0;

}

// Faster C++ solution for the same problem (measured speedup: 1.111461224989853x):
/***********************

*  Date: 2018.11.28    *

*  Problem:            *

*  Author: xuziyuan    * 

***********************/



#include <bits/stdc++.h>



#define rt0 return 0

#define rep(i,n) for(int i=0;i<n;i++)

#define repn(i,n) for(int i=1;i<=n;i++)

#define replet(c) for(char c='a';c<='z';c++)

#define LL long long

#define pii pair <int,int>

#define pb push_back

#define fi first

#define se second

#define mpr make_pair

#define sqr(a) ((a)*(a))



using namespace std;



const LL MOD=1e9+7;



int n,m,q,appe[100010][20],coap[100010];//appe:appearance

vector <int> g[100010];



int main()

{

	cin>>n>>m;

	rep(i,m)

	{

		int x,y;

		scanf("%d%d",&x,&y);

		g[x].pb(y);

		g[y].pb(x);

	}

	cin>>q;

	repn(i,q)

	{

		int x,y;

		scanf("%d%d%d",&x,&y,&coap[i]);

		appe[x][y]=i;

	}

	for(int i=9;i>=0;i--)//算能不能覆盖appe[j][i]之前要先算出appe[p][i+1]会不会被覆盖，所以i要倒着循环 

	{

		repn(p,n)

		{

			rep(k,g[p].size())//i在这里代表可以控制的范围

			{

				int j=g[p][k];

				appe[j][i]=max(appe[j][i],appe[p][i+1]);/*远一条边距离就要加1，这里是看后者可不可以覆盖前者，

				可以说是一种dp，一个类似递归的过程，如果再之前的点把后者覆盖了，也会在前者上体现出来*/

			}	

		}	

	} 

	repn(i,n)

	{

		int lst=0;

		rep(j,11) lst=max(lst,appe[i][j]);

		cout<<coap[lst]<<endl;

	}

	rt0;

}
// Slower C++ solution:
//************************

//*  Date: 2018.10.22    *

//*  Problem:            *

//*  Author: xuziyuan    * 

//************************



#include <bits/stdc++.h>



#define rt0 return 0

#define rep(i,n) for(int i=0;i<n;i++)

#define repn(i,n) for(int i=1;i<=n;i++)

#define replet(c) for(char c='a';c<='z';c++)

#define LL long long

#define pii pair<int,int>

#define pb push_back

#define fi first

#define se second

#define mpr make_pair

#define sqr(a) ((a)*(a))



using namespace std;



const LL MOD=1e9+7;



int n,m,q;

vector <int> g[100010];

int colo[100010],most[100010];

vector <pair<pii,int> > pa;

bool vis[100010];



int main()

{

	memset(most,-1,sizeof(most));

	cin>>n>>m;

	rep(i,m)

	{

		int x,y;

		scanf("%d%d",&x,&y);

		g[x].pb(y);

		g[y].pb(x);

	}

	cin>>q;

	rep(i,q)

	{

		int x,y,z;

		scanf("%d%d%d",&x,&y,&z);

		pa.pb({{x,y},z});

	}

	for(int i=pa.size()-1;i>=0;i--)

	{

		int x=pa[i].fi.fi,y=pa[i].fi.se,z=pa[i].se;

		queue <pii> q;

		q.push({x,0});

		memset(vis,0,sizeof(vis));

		while(!q.empty())

		{

			int pos=q.front().fi,all=q.front().se;

			q.pop();

			if(y-all<=most[pos]) continue;

			most[pos]=y-all;

			if(all>y) continue;

			if(vis[pos]) continue;

			//if(x==1) cout<<pos<<endl;

			vis[pos]=true;

			if(colo[pos]==0) colo[pos]=z;

			rep(i,g[pos].size()) q.push({g[pos][i],all+1});

		}

	}

	repn(i,n) printf("%d\n",colo[i]);

	rt0;

}

// Faster C++ solution for the same problem (measured speedup: 4.077464380027919x):
/***********************

*  Date: 2018.11.28    *

*  Problem:            *

*  Author: xuziyuan    * 

***********************/



#include <bits/stdc++.h>



#define rt0 return 0

#define rep(i,n) for(int i=0;i<n;i++)

#define repn(i,n) for(int i=1;i<=n;i++)

#define replet(c) for(char c='a';c<='z';c++)

#define LL long long

#define pii pair <int,int>

#define pb push_back

#define fi first

#define se second

#define mpr make_pair

#define sqr(a) ((a)*(a))



using namespace std;



const LL MOD=1e9+7;



int n,m,q,appe[100010][20],coap[100010],len=0;//appe:appearance

int list_top[200010],val[200010],nxt[200010];



void add(int x,int y)

{

	val[++len]=y;

	nxt[len]=list_top[x];

	list_top[x]=len;

}



int main()

{

	cin>>n>>m;

	rep(i,m)

	{

		int x,y;

		scanf("%d%d",&x,&y);

		add(x,y);

		add(y,x);

	}

	cin>>q;

	repn(i,q)

	{

		int x,y;

		scanf("%d%d%d",&x,&y,&coap[i]);

		appe[x][y]=i;

	}

	for(int i=9;i>=0;i--)//算能不能覆盖appe[j][i]之前要先算出appe[p][i+1]会不会被覆盖，所以i要倒着循环 

	{

		repn(p,n)

		{

			for(int k=list_top[p];k!=0;k=nxt[k])//i在这里代表可以控制的范围

			{

				int j=val[k];

				appe[j][i]=max(appe[j][i],appe[p][i+1]);/*远一条边距离就要加1，这里是看后者可不可以覆盖前者，

				可以说是一种dp，一个类似递归的过程，如果再之前的点把后者覆盖了，也会在前者上体现出来*/

			}	

		}	

	} 

	repn(i,n)

	{

		int lst=0;

		rep(j,11) lst=max(lst,appe[i][j]);

		cout<<coap[lst]<<endl;

	}

	rt0;

}
// Slower C++ solution:
/***********************

*  Date: 2018.11.28    *

*  Problem:            *

*  Author: xuziyuan    * 

***********************/



#include <bits/stdc++.h>



#define rt0 return 0

#define rep(i,n) for(int i=0;i<n;i++)

#define repn(i,n) for(int i=1;i<=n;i++)

#define replet(c) for(char c='a';c<='z';c++)

#define LL long long

#define pii pair <int,int>

#define pb push_back

#define fi first

#define se second

#define mpr make_pair

#define sqr(a) ((a)*(a))



using namespace std;



const LL MOD=1e9+7;



int n,m,q,co[100010],most[100010];

vector <int> g[100010];

vector <pair<int,pii> > qs;

bool vis[100010];



int main()

{

	cin>>n>>m;

	memset(most,-1,sizeof(most));

	rep(i,m)

	{

		int x,y;

		scanf("%d%d",&x,&y);

		g[x].pb(y);

		g[y].pb(x);

	}

	cin>>q;

	rep(i,q)

	{

		int x,y,z;

		scanf("%d%d%d",&x,&y,&z);

		qs.pb({x,{y,z}});

	}

	for(int i=qs.size()-1;i>=0;i--)

	{

		//cout<<qs[i].fi<<' '<<qs[i].se.fi<<' '<<qs[i].se.se<<endl;

		memset(vis,0,sizeof(vis));

		int v=qs[i].fi,colo=qs[i].se.se,dist=qs[i].se.fi;

		queue <pii> q;

		q.push({v,0});

		while(!q.empty())

		{

			int pos=q.front().fi,dist1=q.front().se;

			//cout<<pos<<' '<<colo<<' '<<dist<<' '<<dist1<<endl;

			q.pop();

			if(most[pos]>=dist-dist1) continue;

			most[pos]=dist-dist1;

			if(vis[pos]) continue;

			vis[pos]=true;

			if(co[pos]==0) co[pos]=colo;

			if(dist1==dist) continue;

			rep(j,g[pos].size()) q.push({g[pos][j],dist1+1});

		}

	}

	repn(i,n) printf("%d\n",co[i]);

	rt0;

}

// Faster C++ solution for the same problem (measured speedup: 4.073721249816738x):
/***********************

*  Date: 2018.11.28    *

*  Problem:            *

*  Author: xuziyuan    * 

***********************/



#include <bits/stdc++.h>



#define rt0 return 0

#define rep(i,n) for(int i=0;i<n;i++)

#define repn(i,n) for(int i=1;i<=n;i++)

#define replet(c) for(char c='a';c<='z';c++)

#define LL long long

#define pii pair <int,int>

#define pb push_back

#define fi first

#define se second

#define mpr make_pair

#define sqr(a) ((a)*(a))



using namespace std;



const LL MOD=1e9+7;



int n,m,q,appe[100010][20],coap[100010],len=0;//appe:appearance

int list_top[200010],val[200010],nxt[200010];



void add(int x,int y)

{

	val[++len]=y;

	nxt[len]=list_top[x];

	list_top[x]=len;

}



int main()

{

	cin>>n>>m;

	rep(i,m)

	{

		int x,y;

		scanf("%d%d",&x,&y);

		add(x,y);

		add(y,x);

	}

	cin>>q;

	repn(i,q)

	{

		int x,y;

		scanf("%d%d%d",&x,&y,&coap[i]);

		appe[x][y]=i;

	}

	for(int i=9;i>=0;i--)//算能不能覆盖appe[j][i]之前要先算出appe[p][i+1]会不会被覆盖，所以i要倒着循环 

	{

		repn(p,n)

		{

			for(int k=list_top[p];k!=0;k=nxt[k])//i在这里代表可以控制的范围

			{

				int j=val[k];

				appe[j][i]=max(appe[j][i],appe[p][i+1]);/*远一条边距离就要加1，这里是看后者可不可以覆盖前者，

				可以说是一种dp，一个类似递归的过程，如果再之前的点把后者覆盖了，也会在前者上体现出来*/

			}	

		}	

	} 

	repn(i,n)

	{

		int lst=0;

		rep(j,11) lst=max(lst,appe[i][j]);

		cout<<coap[lst]<<endl;

	}

	rt0;

}