#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define ll long long
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
const int N=2e5+5;
int n,m,fa[N][20],Log[N],pos[N],dep[N];
int dsu[N],ans[N];
int find(int x){return x==dsu[x]?x:dsu[x]=find(dsu[x]);}
struct Edge{
int u,v,w;
bool operator <(const Edge a)const{
return w<a.w;
}
}e[N];
vector <pii> G[N];
void dfs(int u,int f){
dep[u]=dep[f]+1,fa[u][0]=f;
for(auto x:G[u]){
if(x.fi==f)continue;
pos[x.se]=x.fi;
dfs(x.fi,u);
}
}
int lca(int x,int y) {
if(dep[x]<dep[y])swap(x,y);
while(dep[x]>dep[y]){
x=fa[x][Log[dep[x]-dep[y]]];
}
if(x==y)return x;
for(int i=Log[dep[x]];i>=0;i--){
if(fa[x][i]!=fa[y][i]){
x=fa[x][i];
y=fa[y][i];
}
}
return fa[x][0];
}
void init(){
Log[0]=-1;
rep(i,1,n){
Log[i]=Log[i>>1]+1;
ans[i]=-1;
dsu[i]=i;
}
rep(i,1,16){
rep(j,1,n)fa[j][i]=fa[fa[j][i-1]][i-1];
}
sort(e+1,e+m+1);
}
int main(){
//freopen("disrupt.in","r",stdin);
//freopen("disrupt.out","w",stdout);
n=read(),m=read();
rep(i,1,n-1){
int u=read(),v=read();
G[u].pb({v,i}),G[v].pb({u,i});
}
dfs(1,0);
rep(i,1,m){
e[i]={read(),read(),read()};
}
init();
rep(i,1,m){
int u=e[i].u,v=e[i].v,w=e[i].w;
int L=lca(u,v);
for(u=find(u);dep[u]>dep[L];u=find(fa[u][0])){
ans[u]=w,dsu[u]=fa[u][0];
}
for(v=find(v);dep[v]>dep[L];v=find(fa[v][0])){
ans[v]=w,dsu[v]=fa[v][0];
}
}
rep(i,1,n-1)printf("%d\n",ans[pos[i]]);
return 0;
}