#include <bits/stdc++.h>

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
//#define endl '\n'
#define f first
#define s second
#define pb push_back

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

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

struct dinic {
	struct edge {
		int to;
		char cap;
		int rev;
		edge(int to_, char cap_, int rev_) : to(to_), cap(cap_), rev(rev_) {}
	};

	vector<vector<edge>> g;
	vector<int> lev;
	dinic(int n): g(n){}

	void add(int a, int b, int c) { // de a pra b com cap. c
		g[a].pb(edge(b, c, g[b].size()));
		g[b].pb(edge(a, 0, g[a].size()-1));
	}
	bool bfs(int s, int t) {
		lev = vector<int>(g.size(), -1); lev[s] = 0;
		queue<int> q; q.push(s);
		while (q.size()) {
			int u = q.front(); q.pop();
			for (auto& i : g[u]) {
				if (lev[i.to] != -1 or !i.cap) continue;
				lev[i.to] = lev[u] + 1;
				if (i.to == t) return 1;
				q.push(i.to);
			}
		}
		return 0;
	}
	int dfs(int v, int s, int f = INF){
		if (v == s) return f;
		int tem = f;
		for (auto& i : g[v]) {
			if (lev[i.to] != lev[v] + 1 or !i.cap) continue;
			int foi = dfs(i.to, s, min(tem, (int)i.cap));
			tem -= foi, i.cap -= foi, g[i.to][i.rev].cap += foi;
		}
		if (f == tem) lev[v] = -1;
		return f - tem;
	}
	int max_flow(int s, int t) {
		int f = 0;
		while (bfs(s, t)) f += dfs(s, t);
		return f;
	}
};

const int MAX = 5e3+10;

int val[MAX], lev[MAX], pai[MAX], ans[MAX];
vector<int> g[MAX];

void dfs(int k=0, int p=0, int d=0) {
	pai[k] = p;
	lev[k] = d;
	for (int i : g[k]) if (i != p) dfs(i, k, d+1);
}

int main(){ _
	int n; cin >> n;
	for (int i = 0; i < n-1; i++) val[i] = 1;
	map<pair<int, int>, int > mm;
	for (int i = 0; i < n-1; i++) {
		int a, b; cin >> a >> b; a--, b--;
		g[a].pb(b);
		g[b].pb(a);
		mm[make_pair(a, b)] = i;
		mm[make_pair(b, a)] = i;
	}
	dfs();
	int q; cin >> q;
	vector<pair<ii, int> > qu;
	for (int i = 0; i < q; i++) {
		int a, b, c; cin >> a >> b >> c; a--, b--;
		qu.pb({{a, b}, c});
		while (a != b) {
			if (lev[a] < lev[b]) swap(a, b);
			int& x = val[mm[make_pair(a, pai[a])]];
			x = max(x, c);
			a = pai[a];
		}
	}
//	for (int i = 0; i < n-1; i++) cout << val[i] << " ";
//	cout << endl;
	dinic d(n-1+q+2);
	for (int i = 0; i < n-1; i++) d.add(n-1+q, i, 1);
	for (int i = 0; i < q; i++) d.add(n-1+i, n+q, 1);
	for (int i = 0; i < q; i++) {
		int a = qu[i].f.f, b = qu[i].f.s;
		while (a != b) {
			if (lev[a] < lev[b]) swap(a, b);
			int ind = mm[make_pair(a, pai[a])];
			if (val[ind] <= qu[i].s) d.add(ind, n-1+i, 1);
			a = pai[a];
		}
	}
	d.max_flow(n-1+q, n+q);
	for (int i = 0; i < n-1; i++) {
		ans[i] = 1e6;
		for (auto e : d.g[i]) if (e.to != n-1+q and !e.cap) ans[i] = qu[e.to-(n-1)].s;
	}
//	for (int i = 0; i < n-1; i++) cout << ans[i] << " ";
//	cout << endl;
	for (auto i : qu) {
		int a = i.f.f, b = i.f.s;
		int mi = INF;
		while (a != b) {
			if (lev[a] < lev[b]) swap(a, b);
			mi = min(mi, ans[mm[make_pair(a, pai[a])]]);
			a = pai[a];
		}
		if (mi != i.s) return cout << -1 << endl, 0;
	}
	for (int i = 0; i < n-1; i++) cout << ans[i] << " ";
	cout << endl;
	exit(0);
}


#include <bits/stdc++.h>

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define f first
#define s second
#define pb push_back

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

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

const int MAX = 5e3+10;

vector<vector<int> > g(MAX);
int in[MAX], h[MAX], sz[MAX];
int pai[MAX], t;

void build(int k, int p = -1, int f = 1) {
	in[k] = t++; sz[k] = 1;
	for (int& i : g[k]) if (i != p) {
		pai[i] = k;
		h[i] = (i == g[k][0] ? h[k] : i);
		build(i, k, f); sz[k] += sz[i];

		if (sz[i] > sz[g[k][0]]) swap(i, g[k][0]);
	}
	if (p*f == -1) t = 0, h[k] = k, build(k, -1, 0);
}

int lca(int a, int b) {
	if (in[a] < in[b]) swap(a, b);
	return h[a] == h[b] ? b : lca(pai[h[a]], b);
}

int val[MAX], val2[MAX], ans[MAX];

int dfs(int k, int p=-1) {
	val2[k] = val[k];
	for (int i : g[k]) if (i != p) val2[k] += dfs(i, k);
	return val2[k];
}

int main(){ _
	int n; cin >> n;
	for (int i = 0; i <= n; i++) ans[i] = 1;
	map<pair<int, int>, int> mm;
	for (int i = 0; i < n-1; i++) {
		int a, b; cin >> a >> b; a--, b--;
		mm[make_pair(a, b)] = i;
		mm[make_pair(b, a)] = i;
		g[a].pb(b);
		g[b].pb(a);
	}
	build(0);
	pai[0] = 0;
	int q; cin >> q;
	vector<pair<ii, int> > qu;
	for (int i = 0; i < q; i++) {
		int a, b, m; cin >> a >> b >> m;
		a--, b--;
		qu.pb({{a, b}, m});
	}
	sort(qu.begin(), qu.end(), [](pair<ii, int> a, pair<ii, int> b){ return a.s < b.s; });
	for (int i = 0; i < q; i++) {
		//cout << "---- " << i << endl;
		memset(val, 0, sizeof(val));
		memset(val2, 0, sizeof(val2));
		for (int j = i+1; j < q; j++) if (qu[j].s > qu[i].s) {
			int a, b;
			tie(a, b) = qu[j].f;
			//cout << "colocando no " << a << " " << b << endl;
			val[a]++;
			val[b]++;
			int l = lca(a, b);
			val[l] -= 2;
		}
		dfs(0);
		int a, b;
		tie(a, b) = qu[i].f;
		int l = lca(a, b);
		bool deu = 0;
		while (a != l) {
			if (!val2[a]) {
				ans[a] = qu[i].s;
				deu = 1;
			}
			a = pai[a];
		}
		while (b != l) {
			if (!val2[b]) {
				ans[b] = qu[i].s;
				deu = 1;
			}
			b = pai[b];
		}
		if (!deu) return cout << -1 << endl, 0;
	}
	vector<int> anss(n-1, 1);
	for (int i = 0; i < n; i++) if (pai[i] != i) {
		int x = ans[i];
		anss[mm[make_pair(i, pai[i])]] = x;
	}
	for (int i = 0; i < n-1; i++) cout << anss[i] << " ";
	cout << endl;
	exit(0);
}


#include <bits/stdc++.h>

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
//#define endl '\n'
#define f first
#define s second
#define pb push_back

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

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

struct dinic {
	struct edge {
		short to;
		char cap;
		short rev;
		edge(short to_, char cap_, short rev_) : to(to_), cap(cap_), rev(rev_) {}
	};

	vector<vector<edge>> g;
	vector<int> lev;
	dinic(int n): g(n){}

	void add(int a, int b, int c) { // de a pra b com cap. c
		g[a].pb(edge(b, c, g[b].size()));
		g[b].pb(edge(a, 0, g[a].size()-1));
	}
	bool bfs(int s, int t) {
		lev = vector<int>(g.size(), -1); lev[s] = 0;
		queue<int> q; q.push(s);
		while (q.size()) {
			int u = q.front(); q.pop();
			for (auto& i : g[u]) {
				if (lev[i.to] != -1 or !i.cap) continue;
				lev[i.to] = lev[u] + 1;
				if (i.to == t) return 1;
				q.push(i.to);
			}
		}
		return 0;
	}
	int dfs(int v, int s, int f = INF){
		if (v == s) return f;
		int tem = f;
		for (auto& i : g[v]) {
			if (lev[i.to] != lev[v] + 1 or !i.cap) continue;
			int foi = dfs(i.to, s, min(tem, (int)i.cap));
			tem -= foi, i.cap -= foi, g[i.to][i.rev].cap += foi;
		}
		if (f == tem) lev[v] = -1;
		return f - tem;
	}
	int max_flow(int s, int t) {
		int f = 0;
		while (bfs(s, t)) f += dfs(s, t);
		return f;
	}
};

const int MAX = 5e3+10;

int val[MAX], lev[MAX], pai[MAX], ans[MAX];
vector<int> g[MAX];

void dfs(int k=0, int p=0, int d=0) {
	pai[k] = p;
	lev[k] = d;
	for (int i : g[k]) if (i != p) dfs(i, k, d+1);
}

int main(){ _
	int n; cin >> n;
	for (int i = 0; i < n-1; i++) val[i] = 1;
	map<pair<int, int>, int > mm;
	for (int i = 0; i < n-1; i++) {
		int a, b; cin >> a >> b; a--, b--;
		g[a].pb(b);
		g[b].pb(a);
		mm[make_pair(a, b)] = i;
		mm[make_pair(b, a)] = i;
	}
	dfs();
	int q; cin >> q;
	vector<pair<ii, int> > qu;
	for (int i = 0; i < q; i++) {
		int a, b, c; cin >> a >> b >> c; a--, b--;
		qu.pb({{a, b}, c});
		while (a != b) {
			if (lev[a] < lev[b]) swap(a, b);
			int& x = val[mm[make_pair(a, pai[a])]];
			x = max(x, c);
			a = pai[a];
		}
	}
//	for (int i = 0; i < n-1; i++) cout << val[i] << " ";
//	cout << endl;
	dinic d(n-1+q+2);
	for (int i = 0; i < n-1; i++) d.add(n-1+q, i, 1);
	for (int i = 0; i < q; i++) d.add(n-1+i, n+q, 1);
	for (int i = 0; i < q; i++) {
		int a = qu[i].f.f, b = qu[i].f.s;
		while (a != b) {
			if (lev[a] < lev[b]) swap(a, b);
			int ind = mm[make_pair(a, pai[a])];
			if (val[ind] <= qu[i].s) d.add(ind, n-1+i, 1);
			a = pai[a];
		}
	}
	d.max_flow(n-1+q, n+q);
	for (int i = 0; i < n-1; i++) {
		ans[i] = 1e6;
		for (auto e : d.g[i]) if (e.to != n-1+q and !e.cap) ans[i] = qu[e.to-(n-1)].s;
	}
//	for (int i = 0; i < n-1; i++) cout << ans[i] << " ";
//	cout << endl;
	for (auto i : qu) {
		int a = i.f.f, b = i.f.s;
		int mi = INF;
		while (a != b) {
			if (lev[a] < lev[b]) swap(a, b);
			mi = min(mi, ans[mm[make_pair(a, pai[a])]]);
			a = pai[a];
		}
		if (mi != i.s) return cout << -1 << endl, 0;
	}
	for (int i = 0; i < n-1; i++) cout << ans[i] << " ";
	cout << endl;
	exit(0);
}


#include <bits/stdc++.h>

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
//#define endl '\n'
#define f first
#define s second
#define pb push_back

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

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

const int MAX = 5e3+10;

namespace dinic {
	struct edge {
		unsigned short to, rev;
		edge(short to_, char cap_, short rev_) : to(to_), rev(rev_) {
			if (cap_) to |= (1<<15);
		}
	};

	vector<edge> g[MAX];
	vector<int> lev;
	int n;

	void build(int n_) {
		n = n_;
	}

	void add(int a, int b) { // de a pra b com cap. c
		g[a].pb(edge(b, 1, g[b].size()));
		g[b].pb(edge(a, 0, g[a].size()-1));
	}
	bool bfs(int s, int t) {
		lev = vector<int>(n, -1); lev[s] = 0;
		queue<int> q; q.push(s);
		while (q.size()) {
			int u = q.front(); q.pop();
			for (auto& i : g[u]) {
				if (lev[i.to&32767] != -1 or !(i.to>>15)) continue;
				lev[i.to&32767] = lev[u] + 1;
				if ((i.to&32767) == t) return 1;
				q.push(i.to&32767);
			}
		}
		return 0;
	}
	int dfs(int v, int s, int f = INF){
		if (v == s) return f;
		int tem = f;
		for (auto& i : g[v]) {
			if (lev[i.to&32767] != lev[v] + 1 or !(i.to>>15)) continue;
			int foi = dfs(i.to&32767, s, min(tem, i.to>>15));
			if (foi) {
				tem -= foi;
				i.to ^= (1<<15);
				g[i.to&32767][i.rev].to ^= (1<<15);
			}
		}
		if (f == tem) lev[v] = -1;
		return f - tem;
	}
	int max_flow(int s, int t) {
		int f = 0;
		while (bfs(s, t)) f += dfs(s, t);
		return f;
	}
}

int val[MAX], lev[MAX], pai[MAX], ans[MAX];
vector<int> g[MAX];

void dfs(int k=0, int p=0, int d=0) {
	pai[k] = p;
	lev[k] = d;
	for (int i : g[k]) if (i != p) dfs(i, k, d+1);
}

int main(){ _
	int n; cin >> n;
	for (int i = 0; i < n-1; i++) val[i] = 1;
	map<pair<int, int>, int > mm;
	for (int i = 0; i < n-1; i++) {
		int a, b; cin >> a >> b; a--, b--;
		g[a].pb(b);
		g[b].pb(a);
		mm[make_pair(a, b)] = i;
		mm[make_pair(b, a)] = i;
	}
	dfs();
	int q; cin >> q;
	vector<pair<ii, int> > qu;
	for (int i = 0; i < q; i++) {
		int a, b, c; cin >> a >> b >> c; a--, b--;
		qu.pb({{a, b}, c});
		while (a != b) {
			if (lev[a] < lev[b]) swap(a, b);
			int& x = val[mm[make_pair(a, pai[a])]];
			x = max(x, c);
			a = pai[a];
		}
	}
//	for (int i = 0; i < n-1; i++) cout << val[i] << " ";
//	cout << endl;
	dinic::build(n-1+q+2);
	for (int i = 0; i < n-1; i++) dinic::add(n-1+q, i);
	for (int i = 0; i < q; i++) dinic::add(n-1+i, n+q);
	for (int i = 0; i < q; i++) {
		int a = qu[i].f.f, b = qu[i].f.s;
		while (a != b) {
			if (lev[a] < lev[b]) swap(a, b);
			int ind = mm[make_pair(a, pai[a])];
			if (val[ind] <= qu[i].s) dinic::add(ind, n-1+i);
			a = pai[a];
		}
	}
	dinic::max_flow(n-1+q, n+q);
	for (int i = 0; i < n-1; i++) {
		ans[i] = 1e6;
		for (auto e : dinic::g[i]) if ((e.to&32767) != n-1+q and !(e.to>>15))
			ans[i] = qu[e.to-(n-1)].s;
	}
//	for (int i = 0; i < n-1; i++) cout << ans[i] << " ";
//	cout << endl;
	for (auto i : qu) {
		int a = i.f.f, b = i.f.s;
		int mi = INF;
		while (a != b) {
			if (lev[a] < lev[b]) swap(a, b);
			mi = min(mi, ans[mm[make_pair(a, pai[a])]]);
			a = pai[a];
		}
		if (mi != i.s) return cout << -1 << endl, 0;
	}
	for (int i = 0; i < n-1; i++) cout << ans[i] << " ";
	cout << endl;
	exit(0);
}


#include <bits/stdc++.h>

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define f first
#define s second
#define pb push_back

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

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

const int MAX = 5e3+10;

namespace beats {
	struct node {
		int tam;
		int sum, lazy; // lazy pra soma
		int mi1, mi2, mi; // mi = #mi1
		int ma1, ma2, ma; // ma = #ma1

		node(int x = 0) {
			sum = mi1 = ma1 = x;
			mi2 = INF, ma2 = -INF;
			mi = ma = tam = 1;
			lazy = 0;
		}
		node(const node& l, const node& r) {
			sum = l.sum + r.sum, tam = l.tam + r.tam;
			lazy = 0;
			if (l.mi1 > r.mi1) {
				mi1 = r.mi1, mi = r.mi;
				mi2 = min(l.mi1, r.mi2);
			} else if (l.mi1 < r.mi1) {
				mi1 = l.mi1, mi = l.mi;
				mi2 = min(r.mi1, l.mi2);
			} else {
				mi1 = l.mi1, mi = l.mi+r.mi;
				mi2 = min(l.mi2, r.mi2);
			}
			if (l.ma1 < r.ma1) {
				ma1 = r.ma1, ma = r.ma;
				ma2 = max(l.ma1, r.ma2);
			} else if (l.ma1 > r.ma1) {
				ma1 = l.ma1, ma = l.ma;
				ma2 = max(r.ma1, l.ma2);
			} else {
				ma1 = l.ma1, ma = l.ma+r.ma;
				ma2 = max(l.ma2, r.ma2);
			}
		}
		void setmin(int x) {
			if (x >= ma1) return;
			sum += (x - ma1)*ma;
			if (mi1 == ma1) mi1 = x;
			if (mi2 == ma1) mi2 = x;
			ma1 = x;
		}
		void setmax(int x) {
			if (x <= mi1) return;
			sum += (x - mi1)*mi;
			if (ma1 == mi1) ma1 = x;
			if (ma2 == mi1) ma2 = x;
			mi1 = x;
		}
		void setsum(int x) {
			mi1 += x, mi2 += x, ma1 += x, ma2 += x;
			sum += x*tam;
			lazy += x;
		}
	};

	node seg[4*MAX];
	int n, *v;

	node build(int p=1, int l=0, int r=n-1) {
		if (l == r) return seg[p] = {v[l]};
		int m = (l+r)/2;
		return seg[p] = {build(2*p, l, m), build(2*p+1, m+1, r)};
	}
	void build(int n2, int* v2) {
		n = n2, v = v2;
		build();
	}
	void prop(int p, int l, int r) {
		if (l == r) return;
		for (int k = 0; k < 2; k++) {
			if (seg[p].lazy) seg[2*p+k].setsum(seg[p].lazy);
			seg[2*p+k].setmin(seg[p].ma1);
			seg[2*p+k].setmax(seg[p].mi1);
		}
		seg[p].lazy = 0;
	}
	pair<pair<int, int>, int> query(int a, int b, int p=1, int l=0, int r=n-1) {
		if (b < l or r < a) return {{INF, -INF}, 0};
		if (a <= l and r <= b) return {{seg[p].mi1, seg[p].ma1}, seg[p].sum};
		prop(p, l, r);
		int m = (l+r)/2;
		auto L = query(a, b, 2*p, l, m), R = query(a, b, 2*p+1, m+1, r);
		return {{min(L.f.f, R.f.f), max(L.f.s, R.f.s)}, L.s+R.s};
	}
	node updatemax(int a, int b, int x, int p=1, int l=0, int r=n-1) {
		if (b < l or r < a or seg[p].mi1 >= x) return seg[p];
		if (a <= l and r <= b and seg[p].mi2 > x) {
			seg[p].setmax(x);
			return seg[p];
		}
		prop(p, l, r);
		int m = (l+r)/2;
		return seg[p] = {updatemax(a, b, x, 2*p, l, m),
						updatemax(a, b, x, 2*p+1, m+1, r)};
	}
};

namespace hld {
	vector<pair<int, int> > g[MAX];
	int in[MAX], out[MAX], sz[MAX];
	int sobe[MAX], pai[MAX];
	int h[MAX], v[MAX], t;

	void build_hld(int k, int p = -1, int f = 1) {
		v[in[k] = t++] = sobe[k]; sz[k] = 1;
		for (auto& i : g[k]) if (i.f != p) {
			sobe[i.f] = i.s; pai[i.f] = k;
			h[i.f] = (i == g[k][0] ? h[k] : i.f);
			build_hld(i.f, k, f); sz[k] += sz[i.f];

			if (sz[i.f] > sz[g[k][0].f]) swap(i, g[k][0]);
		}
		out[k] = t;
		if (p*f == -1) build_hld(h[k] = k, -1, t = 0);
	}
	void build(int root = 0) {
		t = 0;
		build_hld(root);
		beats::build(t, v);
	}
	int query(int a, int b) {
		if (a == b) return INF;
		if (in[a] < in[b]) swap(a, b);

		if (h[a] == h[b]) return beats::query(in[b]+1, in[a]).f.f;
		return min(beats::query(in[h[a]], in[a]).f.f, query(pai[h[a]], b));
	}
	void update(int a, int b, int x) {
		if (a == b) return;
		if (in[a] < in[b]) swap(a, b);

		if (h[a] == h[b]) return (void)beats::updatemax(in[b]+1, in[a], x);
		beats::updatemax(in[h[a]], in[a], x); update(pai[h[a]], b, x);
	}
};

int main(){ _
	int n; cin >> n;
	vector<ii> ar;
	for (int i = 0; i < n-1; i++) {
		int a, b; cin >> a >> b; a--, b--;
		ar.pb({a, b});
		hld::g[a].pb({b, 1});
		hld::g[b].pb({a, 1});
	}
	hld::build();
	int m; cin >> m;
	vector<pair<ii, int> > q;
	while (m--) {
		int a, b, c; cin >> a >> b >> c; a--, b--;
		q.pb({{a, b}, c});
		hld::update(a, b, c);
	}
	for (auto i : q) if (hld::query(i.f.f, i.f.s) != i.s) return cout << -1 << endl, 0;
	for (auto i : ar) cout << hld::query(i.f, i.s) << " ";
	cout << endl;
	exit(0);
}


#include <bits/stdc++.h>

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
//#define endl '\n'
#define f first
#define s second
#define pb push_back

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

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

const int MAX = 5e3+10;

namespace lct {
	struct node {
		int p, path_p, ch[2];
		ll val, sub;
		bool rev;
		int sz, ar;
		ll lazy;
		node() {}
		node(int v, int ar_) :
		p(-1), path_p(-1), val(v), sub(v), rev(0), sz(ar_), ar(ar_), lazy(INF) {
			ch[0] = ch[1] = -1;
		}
	};

	node t[2*MAX];
	map<ii, int> mapp;

	void prop(int x) {
		if (t[x].lazy != INF) {
			t[x].val = t[x].lazy, t[x].sub = t[x].lazy;
			if (t[x].ch[0]+1) t[t[x].ch[0]].lazy = t[x].lazy;
			if (t[x].ch[1]+1) t[t[x].ch[1]].lazy = t[x].lazy;
		}
		if (t[x].rev) {
			swap(t[x].ch[0], t[x].ch[1]);
			if (t[x].ch[0]+1) t[t[x].ch[0]].rev ^= 1;
			if (t[x].ch[1]+1) t[t[x].ch[1]].rev ^= 1;
		}
		t[x].lazy = INF, t[x].rev = 0;
	}
	void update(int x) {
		t[x].sz = t[x].ar, t[x].sub = t[x].val;
		for (int i = 0; i < 2; i++) if (t[x].ch[i]+1) {
			prop(t[x].ch[i]);
			t[x].sz += t[t[x].ch[i]].sz;
			t[x].sub = min(t[x].sub, t[t[x].ch[i]].sub);
		}
	}
	void rotate(int x) {
		int p = t[x].p, pp = t[p].p;
		if (pp+1) t[pp].ch[t[pp].ch[1] == p] = x;
		bool d = t[p].ch[0] == x;
		t[p].ch[!d] = t[x].ch[d], t[x].ch[d] = p;
		if (t[p].ch[!d]+1) t[t[p].ch[!d]].p = p;
		t[x].p = pp, t[p].p = x;
		t[x].path_p = t[p].path_p, t[p].path_p = -1;
		update(p), update(x);
	}
	int splay(int x) {
		int r = x;
		vector<int> st = {r};
		while (t[r].p+1) r = t[r].p, st.push_back(r);
		while (st.size()) {
			int u = st.back(); st.pop_back();
			prop(u);
		}
		while (t[x].p+1) {
			int p = t[x].p, pp = t[p].p;
			if (pp == -1) return rotate(x), x;
			if ((t[pp].ch[0] == p)^(t[p].ch[0] == x))
				rotate(x), rotate(x);
			else rotate(p), rotate(x);
		}
		return x;
	}
	int access(int v) {
		splay(v);
		if (t[v].ch[1]+1) {
			t[t[v].ch[1]].path_p = v;
			t[v].ch[1] = t[t[v].ch[1]].p = -1;
			update(v);
		}
		int last = v;
		for (int w; (w=t[v].path_p)+1; update(w), splay(v)) {
			splay(last = w);
			if (t[w].ch[1]+1) {
				t[t[w].ch[1]].path_p = w;
				t[t[w].ch[1]].p = -1;
			}
			t[w].ch[1] = v, t[v].p = w;
		}
		return last;
	}
	void make_tree(int v, int w, int ar=0) { t[v] = node(w, ar); }
	int find_root(int v) {
		access(v);
		while (t[v].ch[0]+1) v = t[v].ch[0];
		return splay(v);
	}
	void rootify(int v) {
		access(v);
		t[v].rev ^= 1;
	}
	ll query(int v, int w) {
		rootify(w), access(v);
		return t[v].sub;
	}
	void update(int v, int w, int x) {
		rootify(w), access(v);
		t[v].lazy = x;
	}
	void link2(int v, int w) {
		rootify(v), access(w);
		t[w].ch[1] = v, t[v].p = w;
		update(w);
	}
	void link(int v, int w, int x) { // v--w com peso x
		int id = MAX+mapp.size();
		mapp[make_pair(v, w)] = id;
		make_tree(id, x, 1);
		link2(v, id), link2(id, w);
	}
	void cut2(int v, int w) {
		rootify(w), access(v);
		t[v].ch[0] = t[t[v].ch[0]].p = -1;
		update(v);
	}
	void cut(int v, int w) {
		int id = mapp[make_pair(v, w)];
		cut2(v, id), cut2(id, w);
	}
	int lca(int v, int w) {
		access(v);
		return access(w);
	}
}

int main(){ _
	int n; cin >> n;
	for (int i = 0; i < n; i++) lct::make_tree(i, INF);
	vector<ii> ar;
	for (int i = 0; i < n-1; i++) {
		int a, b; cin >> a >> b; a--, b--;
		ar.pb({a, b});
		lct::link(a, b, 1);
	}
	int m; cin >> m;
	vector<pair<ii, int> > q;
	while (m--) {
		int a, b, c; cin >> a >> b >> c; a--, b--;
		q.pb({{a, b}, c});
	}
	sort(q.begin(), q.end(), [](pair<ii, int> a, pair<ii, int> b) {
		return a.s < b.s;
	});
	for (auto i : q) lct::update(i.f.f, i.f.s, i.s);
	for (auto i : q) if (lct::query(i.f.f, i.f.s) != i.s) return cout << -1 << endl, 0;
	for (auto i : ar) cout << lct::query(i.f, i.s) << " ";
	cout << endl;
	exit(0);
}


#include <bits/stdc++.h>

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define f first
#define s second
#define pb push_back

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

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

const int MAX = 5e3+10;

vector<vector<int> > g(MAX);
int in[MAX], h[MAX], sz[MAX];
int pai[MAX], t;

void build(int k, int p = -1, int f = 1) {
	in[k] = t++; sz[k] = 1;
	for (int& i : g[k]) if (i != p) {
		pai[i] = k;
		h[i] = (i == g[k][0] ? h[k] : i);
		build(i, k, f); sz[k] += sz[i];

		if (sz[i] > sz[g[k][0]]) swap(i, g[k][0]);
	}
	if (p*f == -1) t = 0, h[k] = k, build(k, -1, 0);
}

int lca(int a, int b) {
	if (in[a] < in[b]) swap(a, b);
	return h[a] == h[b] ? b : lca(pai[h[a]], b);
}

int val[MAX], val2[MAX], ans[MAX];

int dfs(int k, int p=-1) {
	val2[k] = val[k];
	for (int i : g[k]) if (i != p) val2[k] += dfs(i, k);
	return val2[k];
}

int main(){ _
	int n; cin >> n;
	map<pair<int, int>, int> mm;
	for (int i = 0; i < n-1; i++) {
		int a, b; cin >> a >> b; a--, b--;
		mm[make_pair(a, b)] = i;
		mm[make_pair(b, a)] = i;
		g[a].pb(b);
		g[b].pb(a);
	}
	build(0);
	pai[0] = 0;
	int q; cin >> q;
	vector<pair<ii, int> > qu;
	for (int i = 0; i < q; i++) {
		int a, b, m; cin >> a >> b >> m;
		a--, b--;
		qu.pb({{a, b}, m});
	}
	sort(qu.begin(), qu.end(), [](pair<ii, int> a, pair<ii, int> b){ return a.s < b.s; });
	for (int i = 0; i < q; i++) {
		//cout << "---- " << i << endl;
		memset(val, 0, sizeof(val));
		memset(val2, 0, sizeof(val2));
		for (int j = i+1; j < q; j++) if (qu[j].s > qu[i].s) {
			int a, b;
			tie(a, b) = qu[j].f;
			//cout << "colocando no " << a << " " << b << endl;
			val[a]++;
			val[b]++;
			int l = lca(a, b);
			if (pai[l] != l) val[pai[l]] -= 2;
		}
		dfs(0);
		int a, b;
		tie(a, b) = qu[i].f;
		int l = lca(a, b);
		bool deu = 0;
		while (a != l) {
			if (!val2[a]) {
				ans[a] = qu[i].s;
				deu = 1;
			}
			a = pai[a];
		}
		while (b != l) {
			if (!val2[b]) {
				ans[b] = qu[i].s;
				deu = 1;
			}
			b = pai[b];
		}
		if (!deu) return cout << -1 << endl, 0;
	}
	vector<int> anss(n-1, 0);
	for (int i = 0; i < n; i++) if (pai[i] != i) {
		int x = ans[i];
		anss[mm[make_pair(i, pai[i])]] = x;
	}
	for (int i = 0; i < n-1; i++) cout << anss[i] << " ";
	cout << endl;
	exit(0);
}


#include <bits/stdc++.h>

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define f first
#define s second
#define pb push_back

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

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

const int MAX = 5e3+10;

vector<vector<int> > g(MAX);
int in[MAX], h[MAX], sz[MAX];
int pai[MAX], t;

void build(int k, int p = -1, int f = 1) {
	in[k] = t++; sz[k] = 1;
	for (int& i : g[k]) if (i != p) {
		pai[i] = k;
		h[i] = (i == g[k][0] ? h[k] : i);
		build(i, k, f); sz[k] += sz[i];

		if (sz[i] > sz[g[k][0]]) swap(i, g[k][0]);
	}
	if (p*f == -1) t = 0, h[k] = k, build(k, -1, 0);
}

int lca(int a, int b) {
	if (in[a] < in[b]) swap(a, b);
	return h[a] == h[b] ? b : lca(pai[h[a]], b);
}

int val[MAX], val2[MAX], ans[MAX];

int dfs(int k, int p=-1) {
	val2[k] = val[k];
	for (int i : g[k]) if (i != p) val2[k] += dfs(i, k);
	return val2[k];
}

int main(){ _
	int n; cin >> n;
	for (int i = 0; i <= n; i++) ans[i] = 1;
	map<pair<int, int>, int> mm;
	for (int i = 0; i < n-1; i++) {
		int a, b; cin >> a >> b; a--, b--;
		mm[make_pair(a, b)] = i;
		mm[make_pair(b, a)] = i;
		g[a].pb(b);
		g[b].pb(a);
	}
	build(0);
	pai[0] = 0;
	int q; cin >> q;
	vector<pair<ii, int> > qu;
	for (int i = 0; i < q; i++) {
		int a, b, m; cin >> a >> b >> m;
		a--, b--;
		qu.pb({{a, b}, m});
	}
	sort(qu.begin(), qu.end(), [](pair<ii, int> a, pair<ii, int> b){ return a.s < b.s; });
	for (int i = 0; i < q; i++) {
		//cout << "---- " << i << endl;
		memset(val, 0, sizeof(val));
		memset(val2, 0, sizeof(val2));
		for (int j = i+1; j < q; j++) if (qu[j].s > qu[i].s) {
			int a, b;
			tie(a, b) = qu[j].f;
			//cout << "colocando no " << a << " " << b << endl;
			val[a]++;
			val[b]++;
			int l = lca(a, b);
			val[l]--;
			if (pai[l] != l) val[pai[l]]--;
		}
		dfs(0);
		int a, b;
		tie(a, b) = qu[i].f;
		int l = lca(a, b);
		bool deu = 0;
		while (a != l) {
			if (!val2[a]) {
				ans[a] = qu[i].s;
				deu = 1;
			}
			a = pai[a];
		}
		while (b != l) {
			if (!val2[b]) {
				ans[b] = qu[i].s;
				deu = 1;
			}
			b = pai[b];
		}
		if (!deu) return cout << -1 << endl, 0;
	}
	vector<int> anss(n-1, 1);
	for (int i = 0; i < n; i++) if (pai[i] != i) {
		int x = ans[i];
		anss[mm[make_pair(i, pai[i])]] = x;
	}
	for (int i = 0; i < n-1; i++) cout << anss[i] << " ";
	cout << endl;
	exit(0);
}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll,null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<ll,ll> pi;
#define FOR(i,vv,n) for(ll i=vv;i<n;i++)
#define FORR(i,n,vv) for(ll i=n-1;i>=vv;i--)
#define ve vector
#define maxe(v) *max_element(v.begin(),v.end())
#define mine(v) *min_element(v.begin(),v.end())
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mp make_pair
#define M 1000000007
#define PRECISE cout.precision(18);
#define F first
#define S second

struct nikhil{
ll s,e, val;
};

ll n=0;
ve <pair<ll,ll> > v[5000];

void dfs(ll ini,ll ver,ve <pair<ll,ll>> &p){
ll c=0;
for(auto j:v[ver]){

	if(j.F!=ini){
		c++;
		dfs(ver,j.F,p);
		p[j.F]={ver,j.S};
	}
}
if(c==0){
	p[ver]={ini,v[ver][0].S};
}

}

ve <ll> fun(ll s,ll e){

	ve <pair<ll,ll>> p(n);
	dfs(-1,s,p);
	ve <ll> ans;
	while(e!=s){
		ans.pb(p[e].S);
		e=p[e].F;
	}

	return ans;

}
bool comp(nikhil a,nikhil b){
return a.val>b.val;}

int main(){

	#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
	 #endif
    FAST
	//PRECISE

cin>>n;
FOR(i,0,n-1){
	ll x,y;
	cin>>x>>y;
	x--;y--;
	v[x].pb({y,i});
	v[y].pb({x,i});
}

ll m;
cin>>m;
nikhil a[m];
FOR(i,0,m){
	ll x,y,z;
	cin>>x>>y>>z;
	x--;y--;
	a[i]={x,y,z};

}
sort(a,a+m,comp);
ve <ll> c(n-1,0);
FOR(i,0,m){
	ve <ll> ok=fun(a[i].s,a[i].e);

	FOR(j,0,ok.size()){
		if(c[ok[j]]==0)
		c[ok[j]]=a[i].val;
	}
}

FOR(i,0,m){
	ve <ll> ok=fun(a[i].s,a[i].e);
	ll mi=M;
	FOR(i,0,ok.size()){
		mi=min(mi,c[ok[i]]);
	}
	if(mi!=a[i].val){
		cout<<-1;
		return 0;
	}
}
FOR(i,0,n-1){
	if(c[i]==0)
		c[i]=1;
cout<<c[i]<<" ";}

return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll,null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<ll,ll> pi;
#define FOR(i,vv,n) for(ll i=vv;i<n;i++)
#define FORR(i,n,vv) for(ll i=n-1;i>=vv;i--)
#define ve vector
#define maxe(v) *max_element(v.begin(),v.end())
#define mine(v) *min_element(v.begin(),v.end())
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mp make_pair
#define M 1000000007
#define PRECISE cout.precision(18);
#define F first
#define S second

struct nikhil{
ll s,e, val;
};

ll n=0;
ve <ll> v[5000];
ve <ll> ans(5000,-1);

void dfs(ll ini,ll ver,ve <ll> &p){

for(auto j:v[ver]){
	if(j!=ini){
		dfs(ver,j,p);
		p[j]=ver;
	}
}

}

ve <ll> fun(ll s,ll e){
	ve <ll> p(n,0);
	dfs(-1,s,p);
	ve <ll> ans;
	while(e!=s){
		ans.pb(e);
		e=p[e];
	}
	ans.pb(s);
	return ans;

}
bool comp(nikhil a,nikhil b){
return a.val>b.val;}

int main(){

	#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
	 #endif
    FAST
	//PRECISE

cin>>n;
FOR(i,0,n-1){
	ll x,y;
	cin>>x>>y;
	x--;y--;
	v[x].pb(y);
	v[y].pb(x);
}

ll m;
cin>>m;
nikhil a[m];
FOR(i,0,m){
	ll x,y,z;
	cin>>x>>y>>z;
	x--;y--;
	a[i]={x,y,z};

}
sort(a,a+m,comp);
ve <ll> c(n,0);
FOR(i,0,m){
	ve <ll> ok=fun(a[i].s,a[i].e);
	FOR(j,0,ok.size()){
		if(c[ok[j]]==0)
		c[ok[j]]=a[i].val;
	}
}

FOR(i,0,m){
	ve <ll> ok=fun(a[i].s,a[i].e);
	ll mi=M;
	FOR(i,0,ok.size()){
		mi=min(mi,c[ok[i]]);
	}
	if(mi!=a[i].val){
		cout<<-1;
		return 0;
	}
}
FOR(i,0,n)
cout<<c[i]<<" ";

return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll,null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<ll,ll> pi;
#define FOR(i,vv,n) for(ll i=vv;i<n;i++)
#define FORR(i,n,vv) for(ll i=n-1;i>=vv;i--)
#define ve vector
#define maxe(v) *max_element(v.begin(),v.end())
#define mine(v) *min_element(v.begin(),v.end())
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mp make_pair
#define M 1000000007
#define PRECISE cout.precision(18);
#define F first
#define S second

struct nikhil{
ll s,e, val;
};

ll n=0;
ve <pair<ll,ll> > v[5000];

void dfs(ll ini,ll ver,ve <pair<ll,ll>> &p){
ll c=0;
for(auto j:v[ver]){
	if(j.F!=ini){
		c++;
		dfs(ver,j.F,p);
		p[j.F]={ver,j.S};
	}
}
if(c==0){
	p[ver]={ini,v[ini][0].S};
}

}

ve <ll> fun(ll s,ll e){
	ve <pair<ll,ll>> p(n);
	dfs(-1,s,p);
	ve <ll> ans;
	while(e!=s){
		ans.pb(p[e].S);
		e=p[e].F;
	}
	ans.pb(s);
	return ans;

}
bool comp(nikhil a,nikhil b){
return a.val>b.val;}

int main(){

	#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
	 #endif
    FAST
	//PRECISE

cin>>n;
FOR(i,0,n-1){
	ll x,y;
	cin>>x>>y;
	x--;y--;
	v[x].pb({y,i});
	v[y].pb({x,i});
}

ll m;
cin>>m;
nikhil a[m];
FOR(i,0,m){
	ll x,y,z;
	cin>>x>>y>>z;
	x--;y--;
	a[i]={x,y,z};

}
sort(a,a+m,comp);
ve <ll> c(n-1,0);
FOR(i,0,m){
	ve <ll> ok=fun(a[i].s,a[i].e);

	FOR(j,0,ok.size()){
		if(c[ok[j]]==0)
		c[ok[j]]=a[i].val;
	}
}

FOR(i,0,m){
	ve <ll> ok=fun(a[i].s,a[i].e);
	ll mi=M;
	FOR(i,0,ok.size()){
		mi=min(mi,c[ok[i]]);
	}
	if(mi!=a[i].val){
		cout<<-1;
		return 0;
	}
}
FOR(i,0,n-1){
	if(c[i]==0)
		c[i]=M;
cout<<c[i]<<" ";}

return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll,null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<ll,ll> pi;
#define FOR(i,vv,n) for(ll i=vv;i<n;i++)
#define FORR(i,n,vv) for(ll i=n-1;i>=vv;i--)
#define ve vector
#define maxe(v) *max_element(v.begin(),v.end())
#define mine(v) *min_element(v.begin(),v.end())
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mp make_pair
#define M 1000000007
#define PRECISE cout.precision(18);
#define F first
#define S second

struct nikhil{
ll s,e, val;
};

ll n=0;
ve <pair<ll,ll> > v[5000];

void dfs(ll ini,ll ver,ve <pair<ll,ll>> &p){
ll c=0;
for(auto j:v[ver]){
	if(j.F!=ini){
		c++;
		dfs(ver,j.F,p);
		p[j.F]={ver,j.S};
	}
}
if(c==0){
	p[ver]={ini,v[ini][0].S};
}

}

ve <ll> fun(ll s,ll e){
	ve <pair<ll,ll>> p(n);
	dfs(-1,s,p);
	ve <ll> ans;
	while(e!=s){
		ans.pb(p[e].S);
		e=p[e].F;
	}
	ans.pb(s);
	return ans;

}
bool comp(nikhil a,nikhil b){
return a.val>b.val;}

int main(){

	#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
	 #endif
    FAST
	//PRECISE

cin>>n;
FOR(i,0,n-1){
	ll x,y;
	cin>>x>>y;
	x--;y--;
	v[x].pb({y,i});
	v[y].pb({x,i});
}

ll m;
cin>>m;
nikhil a[m];
FOR(i,0,m){
	ll x,y,z;
	cin>>x>>y>>z;
	x--;y--;
	a[i]={x,y,z};

}
sort(a,a+m,comp);
ve <ll> c(n-1,0);
FOR(i,0,m){
	ve <ll> ok=fun(a[i].s,a[i].e);

	FOR(j,0,ok.size()){
		if(c[ok[j]]==0)
		c[ok[j]]=a[i].val;
	}
}

FOR(i,0,m){
	ve <ll> ok=fun(a[i].s,a[i].e);
	ll mi=M;
	FOR(i,0,ok.size()){
		mi=min(mi,c[ok[i]]);
	}
	if(mi!=a[i].val){
		cout<<-1;
		return 0;
	}
}
FOR(i,0,n-1)
cout<<c[i]<<" ";

return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll,null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<ll,ll> pi;
#define FOR(i,vv,n) for(ll i=vv;i<n;i++)
#define FORR(i,n,vv) for(ll i=n-1;i>=vv;i--)
#define ve vector
#define maxe(v) *max_element(v.begin(),v.end())
#define mine(v) *min_element(v.begin(),v.end())
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mp make_pair
#define M 1000000007
#define PRECISE cout.precision(18);
#define F first
#define S second

struct nikhil{
ll s,e, val;
};

ll n=0;
ve <pair<ll,ll> > v[5000];

void dfs(ll ini,ll ver,ve <pair<ll,ll>> &p){
ll c=0;
for(auto j:v[ver]){
	if(j.F!=ini){
		c++;
		dfs(ver,j.F,p);
		p[j.F]={ver,j.S};
	}
}
if(c==0){
	p[ver]={ini,v[ini][0].S};
}

}

ve <ll> fun(ll s,ll e){
	ve <pair<ll,ll>> p(n);
	dfs(-1,s,p);
	ve <ll> ans;
	while(e!=s){
		ans.pb(p[e].S);
		e=p[e].F;
	}
	ans.pb(s);
	return ans;

}
bool comp(nikhil a,nikhil b){
return a.val>b.val;}

int main(){

	#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
	 #endif
    FAST
	//PRECISE

cin>>n;
FOR(i,0,n-1){
	ll x,y;
	cin>>x>>y;
	x--;y--;
	v[x].pb({y,i});
	v[y].pb({x,i});
}

ll m;
cin>>m;
nikhil a[m];
FOR(i,0,m){
	ll x,y,z;
	cin>>x>>y>>z;
	x--;y--;
	a[i]={x,y,z};

}
sort(a,a+m,comp);
ve <ll> c(n-1,0);
FOR(i,0,m){
	ve <ll> ok=fun(a[i].s,a[i].e);

	FOR(j,0,ok.size()){
		if(c[ok[j]]==0)
		c[ok[j]]=a[i].val;
	}
}

FOR(i,0,m){
	ve <ll> ok=fun(a[i].s,a[i].e);
	ll mi=M;
	FOR(i,0,ok.size()){
		mi=min(mi,c[ok[i]]);
	}
	if(mi!=a[i].val){
		cout<<-1;
		return 0;
	}
}
FOR(i,0,n-1){
	if(c[i]==0)
		c[i]=1;
cout<<c[i]<<" ";}

return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll,null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<ll,ll> pi;
#define FOR(i,vv,n) for(ll i=vv;i<n;i++)
#define FORR(i,n,vv) for(ll i=n-1;i>=vv;i--)
#define ve vector
#define maxe(v) *max_element(v.begin(),v.end())
#define mine(v) *min_element(v.begin(),v.end())
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mp make_pair
#define M 1000000007
#define PRECISE cout.precision(18);
#define F first
#define S second

struct nikhil{
ll s,e, val;
};

ll n=0;
ve <pair<ll,ll> > v[5000];

void dfs(ll ini,ll ver,ve <pair<ll,ll>> &p){
ll c=0;
for(auto j:v[ver]){

	if(j.F!=ini){
		c++;
		dfs(ver,j.F,p);
		p[j.F]={ver,j.S};
	}
}
if(c==0){
	p[ver]={ini,v[ver][0].S};
}
}

ve <ll> fun(ll s,ll e){

	ve <pair<ll,ll>> p(n);
	dfs(-1,s,p);
	ve <ll> ans;
	while(e!=s){
		ans.pb(p[e].S);
		e=p[e].F;
	}
	ans.pb(s);
	return ans;

}
bool comp(nikhil a,nikhil b){
return a.val>b.val;}

int main(){

	#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
	 #endif
    FAST
	//PRECISE

cin>>n;
FOR(i,0,n-1){
	ll x,y;
	cin>>x>>y;
	x--;y--;
	v[x].pb({y,i});
	v[y].pb({x,i});
}

ll m;
cin>>m;
nikhil a[m];
FOR(i,0,m){
	ll x,y,z;
	cin>>x>>y>>z;
	x--;y--;
	a[i]={x,y,z};

}
sort(a,a+m,comp);
ve <ll> c(n-1,0);
FOR(i,0,m){
	ve <ll> ok=fun(a[i].s,a[i].e);
	FOR(j,0,ok.size()){
		if(c[ok[j]]==0)
		c[ok[j]]=a[i].val;
	}
}

FOR(i,0,m){
	ve <ll> ok=fun(a[i].s,a[i].e);
	ll mi=M;
	FOR(i,0,ok.size()){
		mi=min(mi,c[ok[i]]);
	}
	if(mi!=a[i].val){
		cout<<-1;
		return 0;
	}
}
FOR(i,0,n-1){
	if(c[i]==0)
		c[i]=1;
cout<<c[i]<<" ";}

return 0;
}


#include<bits/stdc++.h>
//#define ddd printf("ddd\n")
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
using namespace std;

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

const int maxn = 5005;
const int inf = 0x3f3f3f3f;
const int modd = 1e9+7;

inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
template <class T> inline void sc(T &x){char c;x=0;while((c=getchar())<'0');while(c>='0'&&c<='9')x=x*10+(c-48),c=getchar();}
inline ll gcd(ll a,ll b){ return b==0? a: gcd(b,a%b); }
inline ll exgcd(ll a,ll b,ll &x,ll &y){ll d; (b==0? (x=1,y=0,d=a): (d=exgcd(b,a%b,y,x),y-=a/b*x)); return d;}
inline ll qpow(ll a,ll n){ll sum=1;while(n){if(n&1)sum=sum*a%modd;a=a*a%modd;n>>=1;}return sum;}
inline ll qmul(ll a,ll n){ll sum=0;while(n){if(n&1)sum=(sum+a)%modd;a=(a+a)%modd;n>>=1;}return sum;}
inline ll inv(ll a) {return qpow(a,modd-2);}

struct Coke{
	int x,y;
}p[maxn];
//int du[maxn];
set<int>st[maxn];  //点相邻的点
set<pr> SS;        //点与点的度数
int mp[maxn][maxn];  //点与点之间的最小值
set<int> bian[maxn]; //要求最小值的点
int ru[maxn];
int ans[maxn][maxn];
bool ddd[maxn][maxn];

int main()
{
	int n;scanf("%d",&n);
	for(int i=1;i<=n-1;i++)
	{
		scanf("%d%d",&p[i].x,&p[i].y);
		int x=p[i].x,y=p[i].y;
		ddd[x][y]=ddd[y][x]=1;
		st[x].insert(y);
		st[y].insert(x);
		ru[x]++;ru[y]++;
	}
	for(int i=1;i<=n;i++) SS.insert({st[i].size(),i});

	int flag=1;
	int m;scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		int x,y,k;scanf("%d%d%d",&x,&y,&k);
		bian[x].insert(y);
		bian[y].insert(x);
		if(!mp[x][y]||mp[x][y]==k) mp[x][y]=mp[y][x]=k;
		else flag=0;
	}

	//for(int i=1;i<=n-1;i++)
	while(SS.size())
	{
		//cout<<flag<<endl;
		if(!flag) break;
		auto it=SS.begin();
		int u=(*it).second;
		int v=*(st[u].begin());
		st[v].erase(u);
		//cout<<u<<" "<<v<<endl;
		SS.erase(it);
		auto ttt=SS.lower_bound({ru[v],v});

		SS.erase(ttt);
		ru[u]--;ru[v]--;

		if(ru[v]>0) SS.insert({ru[v],v});
		int mx=0;
		for(auto dk:bian[u])
		{
			//if(u==2) cout<<dk<<endl;
			//if(u==3) cout<<dk<<endl;
			//cout<<"???"<<dk<<" "<<mp[u][dk]<<endl;

			mx=max(mx,mp[u][dk]);
			//if(u==3) cout<<dk<<endl;

			if(v==dk) continue;

			//if(v==2&&dk==3||v==3&&dk==2) cout<<ddd[v][dk]<<endl;
			//if(!mp[v][dk]&&(st[v].find(dk)!=st[v].end()))flag=0;
			if(mp[v][dk]&&mp[v][dk]!=mp[u][dk]&&ddd[v][dk]) flag=0;
			if(!mp[v][dk]) mp[v][dk]=mp[dk][v]=mp[u][dk];
			else if(mp[u][dk]<=mp[v][dk]) ;
			else flag=0;

			if(!flag) break;
			//cout<<u<<" "<<dk<<"??"<<endl;
			bian[v].insert(dk);
			st[dk].erase(u);
			bian[dk].erase(u);
			//if(u==3) cout<<mx<<endl;
		}
		if(mx==0) mx=1e6;
		bian[v].erase(u);
		ans[u][v]=ans[v][u]=mx;
		if(!flag) break;
		//cout<<mx<<endl;
	}
	if(flag)
	for(int i=1;i<=n-1;i++)
	{
		int x=p[i].x,y=p[i].y;
		//cout<<x<<" "<<y<<" ";
		printf("%d%c",ans[x][y]," \n"[i==n-1]);
	}
	else printf("-1\n");
}


#include<bits/stdc++.h>
//#define ddd printf("ddd\n")
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
using namespace std;

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

const int maxn = 5005;
const int inf = 0x3f3f3f3f;
const int modd = 1e9+7;

inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
template <class T> inline void sc(T &x){char c;x=0;while((c=getchar())<'0');while(c>='0'&&c<='9')x=x*10+(c-48),c=getchar();}
inline ll gcd(ll a,ll b){ return b==0? a: gcd(b,a%b); }
inline ll exgcd(ll a,ll b,ll &x,ll &y){ll d; (b==0? (x=1,y=0,d=a): (d=exgcd(b,a%b,y,x),y-=a/b*x)); return d;}
inline ll qpow(ll a,ll n){ll sum=1;while(n){if(n&1)sum=sum*a%modd;a=a*a%modd;n>>=1;}return sum;}
inline ll qmul(ll a,ll n){ll sum=0;while(n){if(n&1)sum=(sum+a)%modd;a=(a+a)%modd;n>>=1;}return sum;}
inline ll inv(ll a) {return qpow(a,modd-2);}

struct Coke{
	int x,y;
}p[maxn];
//int du[maxn];
set<int>st[maxn];  //点相邻的点
set<pr> SS;        //点与点的度数
int mp[maxn][maxn];  //点与点之间的最小值
set<int> bian[maxn]; //要求最小值的点
int ru[maxn];
int ans[maxn][maxn];
bool ddd[maxn][maxn];

int main()
{
	int n;scanf("%d",&n);
	for(int i=1;i<=n-1;i++)
	{
		scanf("%d%d",&p[i].x,&p[i].y);
		int x=p[i].x,y=p[i].y;
		ddd[x][y]=ddd[y][x]=1;
		st[x].insert(y);
		st[y].insert(x);
		ru[x]++;ru[y]++;
	}
	for(int i=1;i<=n;i++) SS.insert({st[i].size(),i});

	int flag=1;
	int m;scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		int x,y,k;scanf("%d%d%d",&x,&y,&k);
		bian[x].insert(y);
		bian[y].insert(x);
		if(!mp[x][y]||mp[x][y]==k) mp[x][y]=mp[y][x]=k;
		else flag=0;
	}

	//for(int i=1;i<=n-1;i++)
	while(SS.size())
	{
		//cout<<flag<<endl;
		if(!flag) break;
		auto it=SS.begin();
		int u=(*it).second;
		int v=*(st[u].begin());
		st[v].erase(u);
		//cout<<u<<" "<<v<<endl;
		SS.erase(it);
		auto ttt=SS.lower_bound({ru[v],v});

		SS.erase(ttt);
		ru[u]--;ru[v]--;

		if(ru[v]>0) SS.insert({ru[v],v});
		int mx=0;
		for(auto dk:bian[u])
		{
			//if(u==2) cout<<dk<<endl;
			//if(u==3) cout<<dk<<endl;
			//cout<<"???"<<dk<<" "<<mp[u][dk]<<endl;

			mx=max(mx,mp[u][dk]);
			//if(u==3) cout<<dk<<endl;

			if(v==dk) continue;

			//if(v==2&&dk==3||v==3&&dk==2) cout<<ddd[v][dk]<<endl;
			//if(!mp[v][dk]&&(st[v].find(dk)!=st[v].end()))flag=0;
			if(mp[v][dk]!=mp[u][dk]&&ddd[v][dk]) flag=0;
			if(!mp[v][dk]) mp[v][dk]=mp[dk][v]=mp[u][dk];
			else if(mp[u][dk]<=mp[v][dk]) ;
			else flag=0;

			if(!flag) break;
			//cout<<u<<" "<<dk<<"??"<<endl;
			bian[v].insert(dk);
			st[dk].erase(u);
			bian[dk].erase(u);
			//if(u==3) cout<<mx<<endl;
		}
		if(mx==0) mx=1e6;
		bian[v].erase(u);
		ans[u][v]=ans[v][u]=mx;
		if(!flag) break;
		//cout<<mx<<endl;
	}
	if(flag)
	for(int i=1;i<=n-1;i++)
	{
		int x=p[i].x,y=p[i].y;
		//cout<<x<<" "<<y<<" ";
		printf("%d%c",ans[x][y]," \n"[i==n-1]);
	}
	else printf("-1\n");
}


#include<bits/stdc++.h>
//#define ddd printf("ddd\n")
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
using namespace std;

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

const int maxn = 5005;
const int inf = 0x3f3f3f3f;
const int modd = 1e9+7;

inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
template <class T> inline void sc(T &x){char c;x=0;while((c=getchar())<'0');while(c>='0'&&c<='9')x=x*10+(c-48),c=getchar();}
inline ll gcd(ll a,ll b){ return b==0? a: gcd(b,a%b); }
inline ll exgcd(ll a,ll b,ll &x,ll &y){ll d; (b==0? (x=1,y=0,d=a): (d=exgcd(b,a%b,y,x),y-=a/b*x)); return d;}
inline ll qpow(ll a,ll n){ll sum=1;while(n){if(n&1)sum=sum*a%modd;a=a*a%modd;n>>=1;}return sum;}
inline ll qmul(ll a,ll n){ll sum=0;while(n){if(n&1)sum=(sum+a)%modd;a=(a+a)%modd;n>>=1;}return sum;}
inline ll inv(ll a) {return qpow(a,modd-2);}

struct Coke{
	int x,y;
}p[maxn];
//int du[maxn];
set<int>st[maxn];  //点相邻的点
set<pr> SS;        //点与点的度数
int mp[maxn][maxn];  //点与点之间的最小值
set<int> bian[maxn]; //要求最小值的点
int ru[maxn];
int ans[maxn][maxn];
int ddd[maxn][maxn];

int main()
{
	int n;scanf("%d",&n);
	for(int i=1;i<=n-1;i++)
	{
		scanf("%d%d",&p[i].x,&p[i].y);
		int x=p[i].x,y=p[i].y;
		ddd[x][y]=ddd[y][x]=1;
		st[x].insert(y);
		st[y].insert(x);
		ru[x]++;ru[y]++;
	}
	for(int i=1;i<=n;i++) SS.insert({st[i].size(),i});

	int flag=1;
	int m;scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		int x,y,k;scanf("%d%d%d",&x,&y,&k);
		bian[x].insert(y);
		bian[y].insert(x);
		if(!mp[x][y]||mp[x][y]==k) mp[x][y]=mp[y][x]=k;
		else flag=0;
	}

	//for(int i=1;i<=n-1;i++)
	while(SS.size())
	{
		//cout<<flag<<endl;
		if(!flag) break;
		auto it=SS.begin();
		int u=(*it).second;
		int v=*(st[u].begin());
		st[v].erase(u);
		//cout<<u<<" "<<v<<endl;
		SS.erase(it);
		auto ttt=SS.lower_bound({ru[v],v});

		SS.erase(ttt);
		ru[u]--;ru[v]--;

		if(ru[v]>0) SS.insert({ru[v],v});
		int mx=0;
		for(auto dk:bian[u])
		{
			//if(u==2) cout<<dk<<endl;
			//if(u==3) cout<<dk<<endl;
			//cout<<"???"<<dk<<" "<<mp[u][dk]<<endl;

			mx=max(mx,mp[u][dk]);
			//if(u==3) cout<<dk<<endl;

			if(v==dk) continue;

			//if(v==2&&dk==3||v==3&&dk==2) cout<<ddd[v][dk]<<endl;
			if(mp[v][dk]!=mp[u][dk]&&ddd[v][dk]) flag=0;
			if(!mp[v][dk]) mp[v][dk]=mp[dk][v]=mp[u][dk];
			else if(mp[u][dk]<=mp[v][dk]) ;
			else flag=0;
			//if(!mp[v][dk]&&(st[v].find(dk)!=st[v].end()))flag=0;

			if(!flag) break;
			//cout<<u<<" "<<dk<<"??"<<endl;
			bian[v].insert(dk);
			st[dk].erase(u);
			bian[dk].erase(u);
			//if(u==3) cout<<mx<<endl;
		}
		if(mx==0) mx=1e6;
		bian[v].erase(u);
		ans[u][v]=ans[v][u]=mx;
		if(!flag) break;
		//cout<<mx<<endl;
	}
	if(flag)
	for(int i=1;i<=n-1;i++)
	{
		int x=p[i].x,y=p[i].y;
		//cout<<x<<" "<<y<<" ";
		printf("%d%c",ans[x][y]," \n"[i==n-1]);
	}
	else printf("-1\n");
}


#include<bits/stdc++.h>
#define ddd printf("ddd\n")
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
using namespace std;

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

const int maxn = 5005;
const int inf = 0x3f3f3f3f;
const int modd = 1e9+7;

inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
template <class T> inline void sc(T &x){char c;x=0;while((c=getchar())<'0');while(c>='0'&&c<='9')x=x*10+(c-48),c=getchar();}
inline ll gcd(ll a,ll b){ return b==0? a: gcd(b,a%b); }
inline ll exgcd(ll a,ll b,ll &x,ll &y){ll d; (b==0? (x=1,y=0,d=a): (d=exgcd(b,a%b,y,x),y-=a/b*x)); return d;}
inline ll qpow(ll a,ll n){ll sum=1;while(n){if(n&1)sum=sum*a%modd;a=a*a%modd;n>>=1;}return sum;}
inline ll qmul(ll a,ll n){ll sum=0;while(n){if(n&1)sum=(sum+a)%modd;a=(a+a)%modd;n>>=1;}return sum;}
inline ll inv(ll a) {return qpow(a,modd-2);}

struct Coke{
	int x,y;
}p[maxn];
//int du[maxn];
set<int>st[maxn];  //点相邻的点
set<pr> SS;        //点与点的度数
int mp[maxn][maxn];  //点与点之间的最小值
set<int> bian[maxn]; //要求最小值的点
int ru[maxn];
int ans[maxn][maxn];

int main()
{
	int n;scanf("%d",&n);
	for(int i=1;i<=n-1;i++)
	{
		scanf("%d%d",&p[i].x,&p[i].y);
		int x=p[i].x,y=p[i].y;
		st[x].insert(y);
		st[y].insert(x);
		ru[x]++;ru[y]++;
	}
	for(int i=1;i<=n;i++) SS.insert({st[i].size(),i});

	int flag=1;
	int m;scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		int x,y,k;scanf("%d%d%d",&x,&y,&k);
		bian[x].insert(y);
		bian[y].insert(x);
		if(!mp[x][y]||mp[x][y]==k) mp[x][y]=mp[y][x]=k;
		else flag=0;
	}

	//for(int i=1;i<=n-1;i++)
	while(SS.size())
	{
		//cout<<flag<<endl;
		if(!flag) break;
		auto it=SS.begin();
		int u=(*it).second;
		int v=*(st[u].begin());
		st[v].erase(u);
		//cout<<u<<" "<<v<<endl;
		SS.erase(it);
		auto ttt=SS.lower_bound({ru[v],v});

		SS.erase(ttt);
		ru[u]--;ru[v]--;

		if(ru[v]>0) SS.insert({ru[v],v});
		int mx=0;
		for(auto dk:bian[u])
		{
			//if(u==2) cout<<dk<<endl;
			//if(u==3) cout<<dk<<endl;
			//cout<<"???"<<dk<<" "<<mp[u][dk]<<endl;

			mx=max(mx,mp[u][dk]);
			if(u==3) cout<<dk<<endl;
			// cout<<mp[3][4]<<endl;
			//cout<<v<<" "<<dk<<endl;
			if(v==dk) continue;
			if(!mp[v][dk]) mp[v][dk]=mp[dk][v]=mp[u][dk];
			else if(mp[u][dk]<=mp[v][dk]) ;
			else flag=0;
			if(!flag) break;
			//cout<<u<<" "<<dk<<"??"<<endl;
			bian[v].insert(dk);
			st[dk].erase(u);
			bian[dk].erase(u);
			//if(u==3) cout<<mx<<endl;
		}
		if(mx==0) mx=1e6;
		bian[v].erase(u);
		ans[u][v]=ans[v][u]=mx;
		if(!flag) break;
		//cout<<mx<<endl;
	}
	if(flag)
	for(int i=1;i<=n-1;i++)
	{
		int x=p[i].x,y=p[i].y;
		//cout<<x<<" "<<y<<" ";
		printf("%d%c",ans[x][y]," \n"[i==n-1]);
	}
	else printf("-1\n");
}


#include<bits/stdc++.h>
#define ddd printf("ddd\n")
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
using namespace std;

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

const int maxn = 5005;
const int inf = 0x3f3f3f3f;
const int modd = 1e9+7;

inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
template <class T> inline void sc(T &x){char c;x=0;while((c=getchar())<'0');while(c>='0'&&c<='9')x=x*10+(c-48),c=getchar();}
inline ll gcd(ll a,ll b){ return b==0? a: gcd(b,a%b); }
inline ll exgcd(ll a,ll b,ll &x,ll &y){ll d; (b==0? (x=1,y=0,d=a): (d=exgcd(b,a%b,y,x),y-=a/b*x)); return d;}
inline ll qpow(ll a,ll n){ll sum=1;while(n){if(n&1)sum=sum*a%modd;a=a*a%modd;n>>=1;}return sum;}
inline ll qmul(ll a,ll n){ll sum=0;while(n){if(n&1)sum=(sum+a)%modd;a=(a+a)%modd;n>>=1;}return sum;}
inline ll inv(ll a) {return qpow(a,modd-2);}

struct Coke{
	int x,y;
}p[maxn];
//int du[maxn];
set<int>st[maxn];  //点相邻的点
set<pr> SS;        //点与点的度数
int mp[maxn][maxn];  //点与点之间的最小值
set<int> bian[maxn]; //要求最小值的点
int ru[maxn];
int ans[maxn][maxn];

int main()
{
	int n;scanf("%d",&n);
	for(int i=1;i<=n-1;i++)
	{
		scanf("%d%d",&p[i].x,&p[i].y);
		int x=p[i].x,y=p[i].y;
		st[x].insert(y);
		st[y].insert(x);
		ru[x]++;ru[y]++;
	}
	for(int i=1;i<=n;i++) SS.insert({st[i].size(),i});

	int flag=1;
	int m;scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		int x,y,k;scanf("%d%d%d",&x,&y,&k);
		bian[x].insert(y);
		bian[y].insert(x);
		if(!mp[x][y]||mp[x][y]==k) mp[x][y]=mp[y][x]=k;
		else flag=0;
	}

	//for(int i=1;i<=n-1;i++)
	while(SS.size())
	{
		//cout<<flag<<endl;
		if(!flag) break;
		auto it=SS.begin();
		int u=(*it).second;
		int v=*(st[u].begin());
		st[v].erase(u);
		//cout<<u<<" "<<v<<endl;
		SS.erase(it);
		auto ttt=SS.lower_bound({ru[v],v});

		SS.erase(ttt);
		ru[u]--;ru[v]--;

		if(ru[v]>0) SS.insert({ru[v],v});
		int mx=0;
		for(auto dk:bian[u])
		{
			//if(u==2) cout<<dk<<endl;
			//if(u==3) cout<<dk<<endl;
			//cout<<"???"<<dk<<" "<<mp[u][dk]<<endl;

			mx=max(mx,mp[u][dk]);
			//if(u==3) cout<<dk<<endl;

			if(v==dk) continue;
			if(!mp[v][dk]) mp[v][dk]=mp[dk][v]=mp[u][dk];
			else if(mp[u][dk]<=mp[v][dk]) ;
			else flag=0;
			if(!flag) break;
			//cout<<u<<" "<<dk<<"??"<<endl;
			bian[v].insert(dk);
			st[dk].erase(u);
			bian[dk].erase(u);
			//if(u==3) cout<<mx<<endl;
		}
		if(mx==0) mx=1e6;
		bian[v].erase(u);
		ans[u][v]=ans[v][u]=mx;
		if(!flag) break;
		//cout<<mx<<endl;
	}
	if(flag)
	for(int i=1;i<=n-1;i++)
	{
		int x=p[i].x,y=p[i].y;
		//cout<<x<<" "<<y<<" ";
		printf("%d%c",ans[x][y]," \n"[i==n-1]);
	}
	else printf("-1\n");
}


#include<bits/stdc++.h>
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
using namespace std;

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

const int maxn = 5000+20;
const int inf = 0x3f3f3f3f;
const int modd = 1e9+7;

inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
template <class T> inline void sc(T &x){char c;x=0;while((c=getchar())<'0');while(c>='0'&&c<='9')x=x*10+(c-48),c=getchar();}
inline ll gcd(ll a,ll b){ return b==0? a: gcd(b,a%b); }
inline ll exgcd(ll a,ll b,ll &x,ll &y){ll d; (b==0? (x=1,y=0,d=a): (d=exgcd(b,a%b,y,x),y-=a/b*x)); return d;}
inline ll qpow(ll a,ll n){ll sum=1;while(n){if(n&1)sum=sum*a%modd;a=a*a%modd;n>>=1;}return sum;}
inline ll qmul(ll a,ll n){ll sum=0;while(n){if(n&1)sum=(sum+a)%modd;a=(a+a)%modd;n>>=1;}return sum;}
inline ll inv(ll a) {return qpow(a,modd-2);}

vector<int>vec[maxn];
int n,m,v[maxn];
int f[maxn][maxn];

struct Milk{
	int a,b;
}qw[maxn];

struct Coke{
	int top,tail,value;
	bool operator < (Coke cur)
	{
		return value<cur.value;
	}
}p[maxn];

int vis[maxn];
int dfs1(int top,int tail,int value,int i)
{
	vis[top]=i;
	if(top==tail) return 1;
	for(auto it:vec[top])
	{
		if(vis[it]==i) continue;
		if(dfs1(it,tail,value,i)) return f[top][it]=f[it][top]=value,1;
	}
	return 0;
}

int flag;

int dfs2(int top,int tail,int value,int i)
{
	vis[top]=i;
	if(top==tail) return 1;
	for(auto it:vec[top])
	{
		if(vis[it]==i) continue;
		if(dfs2(it,tail,value,i))
		{
			if(f[top][it]==value) flag=1;
			return 1;
		}
	}
	return 0;
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) for(int j=1;j<=n;j++ )f[i][j]=(int)1e6;
	for(int i=1;i<n;i++)
	{
		int x,y;scanf("%d%d",&x,&y);
		qw[i].a=x,qw[i].b=y;
		vec[x].push_back(y);
		vec[y].push_back(x);
	}
	scanf("%d",&m);
	for(int i=1;i<=m;i++) scanf("%d%d%d",&p[i].top,&p[i].tail,&p[i].value);
	sort(p+1,p+1+m);
	for(int i=1;i<=m;i++) dfs1(p[i].top,p[i].tail,p[i].value,i);
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=m;i++)
	{
		flag=0;dfs2(p[i].top,p[i].tail,p[i].value,i);
		if(!flag) return printf("-1\n"),0;
	}

	for(int i=1;i<n;i++) printf("%d%c",f[qw[i].a][qw[i].b]," \n"[i==n]);
}


#include<bits/stdc++.h>
//#define ddd printf("ddd\n")
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
using namespace std;

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

const int maxn = 5005;
const int inf = 0x3f3f3f3f;
const int modd = 1e9+7;

inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
template <class T> inline void sc(T &x){char c;x=0;while((c=getchar())<'0');while(c>='0'&&c<='9')x=x*10+(c-48),c=getchar();}
inline ll gcd(ll a,ll b){ return b==0? a: gcd(b,a%b); }
inline ll exgcd(ll a,ll b,ll &x,ll &y){ll d; (b==0? (x=1,y=0,d=a): (d=exgcd(b,a%b,y,x),y-=a/b*x)); return d;}
inline ll qpow(ll a,ll n){ll sum=1;while(n){if(n&1)sum=sum*a%modd;a=a*a%modd;n>>=1;}return sum;}
inline ll qmul(ll a,ll n){ll sum=0;while(n){if(n&1)sum=(sum+a)%modd;a=(a+a)%modd;n>>=1;}return sum;}
inline ll inv(ll a) {return qpow(a,modd-2);}

struct Coke{
	int x,y;
}p[maxn];
//int du[maxn];
set<int>st[maxn];  //点相邻的点
set<pr> SS;        //点与点的度数
int mp[maxn][maxn];  //点与点之间的最小值
set<int> bian[maxn]; //要求最小值的点
int ru[maxn];
int ans[maxn][maxn];
//bool ddd[maxn][maxn];

int main()
{
	int n;scanf("%d",&n);
	for(int i=1;i<=n-1;i++)
	{
		scanf("%d%d",&p[i].x,&p[i].y);
		int x=p[i].x,y=p[i].y;
		//ddd[x][y]=ddd[y][x]=1;
		st[x].insert(y);
		st[y].insert(x);
		ru[x]++;ru[y]++;
	}
	for(int i=1;i<=n;i++) SS.insert({st[i].size(),i});

	int flag=1;
	int m;scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		int x,y,k;scanf("%d%d%d",&x,&y,&k);
		bian[x].insert(y);
		bian[y].insert(x);
		if(!mp[x][y]||mp[x][y]==k) mp[x][y]=mp[y][x]=k;
		else flag=0;
	}

	//for(int i=1;i<=n-1;i++)
	while(SS.size())
	{
		//cout<<flag<<endl;
		if(!flag) break;
		auto it=SS.begin();
		int u=(*it).second;
		int v=*(st[u].begin());
		st[v].erase(u);
		//cout<<u<<" "<<v<<endl;
		SS.erase(it);
		auto ttt=SS.lower_bound({ru[v],v});

		SS.erase(ttt);
		ru[u]--;ru[v]--;

		if(ru[v]>0) SS.insert({ru[v],v});
		int mx=0;

		for(auto dk:bian[u])
		{
			mx=max(mx,mp[u][dk]);
			st[dk].erase(u);
			bian[dk].erase(u);
		}
		//cout<<u<<" "<<v<<endl;
		for(auto dk:bian[u])
		{
			//mx=max(mx,mp[u][dk]);
			if(!mx) break;
			if(v==dk) continue;
			if(mx==mp[v][dk]) continue;

			if(mp[u][dk]!=mx&&mp[v][dk]&&mp[v][dk]!=mp[u][dk]) flag=0;
			if(mp[v][dk]&&mp[u][dk]>mp[v][dk]) flag=0;

			if(mx!=mp[u][dk])mp[v][dk]=mp[dk][v]=mp[u][dk],bian[v].insert(dk);
			//cout<<mp[1][6]<<endl;
			//if(!flag) cout<<mx<<endl;
			if(!flag) break;

			/*
			if(!mp[v][dk]) mp[v][dk]=mp[dk][v]=mp[u][dk];
			else if(mp[u][dk]<=mp[v][dk]) ;
			else flag=0;
			*/

			//cout<<u<<" "<<dk<<"??"<<endl;
			//if(u==3) cout<<mx<<endl;
		}
		if(mx==0) mx=1e6;
		bian[v].erase(u);
		ans[u][v]=ans[v][u]=mx;
		//cout<<u<<" "<<v<<" "<<ans[u][v]<<endl;
		if(!flag) break;
		//cout<<mx<<endl;
	}
	if(flag)
	for(int i=1;i<=n-1;i++)
	{
		int x=p[i].x,y=p[i].y;
		//cout<<x<<" "<<y<<" ";
		printf("%d%c",ans[x][y]," \n"[i==n-1]);
	}
	else printf("-1\n");
}


#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 = 1e12;
#define PI 3.14159265

int pow1(int a,int b){
    int res=1;
    while(b>0){
        if(b&1){
        	res=res*a;
		}
        a=a*a;
        b>>=1;
    }
    return res;
}

ll n,m;
vector<vector<pair<ll,ll> > > g;
vector<ll> val;

void dfs(ll v, ll pv, ll pe, vector<pair<ll,ll> >&p)
{
    p[v]=make_pair(pv,pe);
    for(auto it : g[v])
    {
        ll to=it.first;
        ll idx=it.second;
        if(to==pv)continue;
        dfs(to,v,idx,p);
    }
}

int main()
{
    cin>>n;
    g=vector<vector<pair<ll,ll> > >(n);
    rep(i,0,n-1)
    {
        ll u,v;
        cin>>u>>v;
        u--,v--;
        g[u].pb(make_pair(v,i));
        g[v].pb(make_pair(u,i));
    }

    vector<vector<pair<ll,ll> > >p(n,vector<pair<ll,ll> >(n));
    for(ll i=0;i<n;i++)
    dfs(i,-1,-1,p[i]);

    val=vector<ll>(n-1,1e6);

    cin>>m;

    vector<pair<pair<ll,ll>,ll> >q(m);
    rep(i,0,m)
    {
        cin>>q[i].first.first>>q[i].first.second>>q[i].second;
        --q[i].first.first;
        --q[i].first.second;
    }

    sort(q.begin(),q.end(),[&](pair<pair<ll,ll>,ll> a, pair<pair<ll,ll>,ll>b){
        return a.second<b.second;
    });

    for(ll i=0;i<m;i++)
    {
        ll u=q[i].first.first;
        ll v=q[i].first.second;
        while(v!=u)
        {
            ll pv=p[u][v].first;
            ll pe=p[u][v].second;
            val[pe]=q[i].second;
            v=pv;
        }
    }

    for(ll i=0;i<m;i++)
    {
        ll u=q[i].first.first;
        ll v=q[i].first.second;
        ll mx=1e6;
        while(v!=u)
        {
            ll pv=p[u][v].first;
            ll pe=p[u][v].second;
            mx=min(mx,val[pe]);
            v=pv;
        }
        if(mx!=q[i].second)
        {
            cout<<"-1";
            return 0;
        }
    }

    rep(i,0,n-1)
    cout<<val[i]<<" ";
    return 0;
}


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

#define ll int
#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 = 1e12;
#define PI 3.14159265

int pow1(int a,int b){
    int res=1;
    while(b>0){
        if(b&1){
        	res=res*a;
		}
        a=a*a;
        b>>=1;
    }
    return res;
}

vector<vector<ll> >adj;

bool dfs(ll a,ll b, ll prv , vector<ll> &v)
{
    if(a==b)
    {
        v.pb(b);
        return true;
    }
    for(auto it : adj[a])
    {
        if(it!=prv)
        {
            if(dfs(it,b,a,v))
            {
                v.pb(a);
                return true;
            }
        }
    }
    return false;
}

int main()
{
    ll t=1;
//     cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        adj=vector<vector<ll> >(n+1);
        map<pair<ll,ll>,ll>ans;
        vector<pair<ll,ll> > v4;
        rep(i,1,n)
        {
            ll u,v;
            cin>>u>>v;
            adj[u].pb(v);
            adj[v].pb(u);
            ans[{u,v}]=0;
            ans[{v,u}]=0;
            v4.pb({u,v});
        }

        // ll ans[n+1];
        // rep(i,0,n+1)
        // ans[i]=0;

        ll m;
        cin>>m;

        vector<pair< pair<ll,ll>,ll> > v3;
        rep(i,0,m)
        {
            ll a,b,g;
            cin>>a>>b>>g;
            v3.pb({{a,b},g});
            vector<ll> v;
            dfs(a,b,0,v);
            rep(i,1,(ll)v.size()){
            ans[{v[i],v[i-1]}]=max(ans[{v[i],v[i-1]}],g);
            ans[{v[i-1],v[i]}]=max(ans[{v[i-1],v[i]}],g);}
        }

        rep(i,0,n-1)
        {
            if(ans[v4[i]]==0)
            ans[v4[i]]=(1e6);
        }

        // rep(i,0,)

        rep(i,0,m)
        {
            ll a=v3[i].first.first;
            ll b=v3[i].first.second;
            ll g=v3[i].second;
            vector<ll> v;
            dfs(a,b,0,v);
            ll mn=1e6;
            rep(i,1,(ll)v.size())
            {
                mn=min(ans[{v[i],v[i-1]}],mn);
            }
            if(mn!=g)
            {
                cout<<"-1";
                return 0;
            }
        }

        rep(i,0,n-1)
        cout<<ans[v4[i]]<<" ";
    }

}


#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 = 1e12;
#define PI 3.14159265

int pow1(int a,int b){
    int res=1;
    while(b>0){
        if(b&1){
        	res=res*a;
		}
        a=a*a;
        b>>=1;
    }
    return res;
}

vector<vector<ll> >adj;

vector<ll> dfs(ll a,ll b, ll prv)
{
    if(a==b)
    {
        vector<ll> v;
        v.pb(b);
        return v;
    }
    for(auto it : adj[a])
    {
        if(it!=prv)
        {
            vector<ll> v2=dfs(it,b,a);
            if(v2.size()!=0)
            {
                v2.pb(a);
                return v2;
            }
        }
    }
    vector<ll> v;
    return v;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t=1;
//     cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        adj=vector<vector<ll> >(n+1);
        map<pair<ll,ll>,ll>ans;
        vector<pair<ll,ll> > v4;
        rep(i,1,n)
        {
            ll u,v;
            cin>>u>>v;
            adj[u].pb(v);
            adj[v].pb(u);
            ll tmp=max(u,v);
            u=min(v,u);
            v=tmp;
            ans[{u,v}]=0;
            v4.pb({u,v});
        }

        // ll ans[n+1];
        // rep(i,0,n+1)
        // ans[i]=0;

        ll m;
        cin>>m;

        vector<pair< pair<ll,ll>,ll> > v3;
        rep(i,0,m)
        {
            ll a,b,g;
            cin>>a>>b>>g;
            v3.pb({{a,b},g});
            vector<ll> v=dfs(a,b,0);
            rep(i,1,(ll)v.size()){
            if(v[i]<v[i-1])
            ans[{v[i],v[i-1]}]=max(ans[{v[i],v[i-1]}],g);
            else
            ans[{v[i-1],v[i]}]=max(ans[{v[i-1],v[i]}],g);}
        }

        rep(i,0,n-1)
        {
            if(ans[v4[i]]==0)
            ans[v4[i]]=(1e6);
        }

        // rep(i,0,)

        rep(i,0,m)
        {
            ll a=v3[i].first.first;
            ll b=v3[i].first.second;
            ll g=v3[i].second;
            vector<ll> v=dfs(a,b,0);
            ll mn=1e6;
            rep(i,1,(ll)v.size())
            {
                if(v[i]<v[i-1])
                mn=min(ans[{v[i],v[i-1]}],mn);
                else
                mn=min(ans[{v[i-1],v[i]}],mn);
            }
            if(mn!=g)
            {
                cout<<"-1";
                return 0;
            }
        }

        rep(i,0,n-1)
        cout<<ans[v4[i]]<<" ";
    }

}


#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 = 1e12;
#define PI 3.14159265

int pow1(int a,int b){
    int res=1;
    while(b>0){
        if(b&1){
        	res=res*a;
		}
        a=a*a;
        b>>=1;
    }
    return res;
}

vector<vector<ll> >adj;

vector<ll> dfs(ll a,ll b, ll prv)
{
    if(a==b)
    {
        vector<ll> v;
        v.pb(b);
        return v;
    }
    for(auto it : adj[a])
    {
        if(it!=prv)
        {
            vector<ll> v2=dfs(it,b,a);
            if(v2.size()!=0)
            {
                v2.pb(a);
                return v2;
            }
        }
    }
    vector<ll> v;
    return v;
}

int main()
{
    ll t=1;
//     cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        adj=vector<vector<ll> >(n+1);
        map<pair<ll,ll>,ll>ans;
        vector<pair<ll,ll> > v4;
        rep(i,1,n)
        {
            ll u,v;
            cin>>u>>v;
            adj[u].pb(v);
            adj[v].pb(u);
            ans[{u,v}]=0;
            ans[{v,u}]=0;
            v4.pb({u,v});
        }

        // ll ans[n+1];
        // rep(i,0,n+1)
        // ans[i]=0;

        ll m;
        cin>>m;

        vector<pair< pair<ll,ll>,ll> > v3;
        rep(i,0,m)
        {
            ll a,b,g;
            cin>>a>>b>>g;
            v3.pb({{a,b},g});
            vector<ll> v=dfs(a,b,0);
            rep(i,1,(ll)v.size()){
            ans[{v[i],v[i-1]}]=max(ans[{v[i],v[i-1]}],g);
            ans[{v[i-1],v[i]}]=max(ans[{v[i-1],v[i]}],g);}
        }

        rep(i,0,n-1)
        {
            if(ans[v4[i]]==0)
            ans[v4[i]]=(1e6);
        }

        // rep(i,0,)

        rep(i,0,m)
        {
            ll a=v3[i].first.first;
            ll b=v3[i].first.second;
            ll g=v3[i].second;
            vector<ll> v=dfs(a,b,0);
            ll mn=1e6;
            rep(i,1,(ll)v.size())
            {
                mn=min(ans[{v[i],v[i-1]}],mn);
            }
            if(mn!=g)
            {
                cout<<"-1";
                return 0;
            }
        }

        rep(i,0,n-1)
        cout<<ans[v4[i]]<<" ";
    }

}


#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 = 1e12;
#define PI 3.14159265

int pow1(int a,int b){
    int res=1;
    while(b>0){
        if(b&1){
        	res=res*a;
		}
        a=a*a;
        b>>=1;
    }
    return res;
}

vector<vector<ll> >adj;

vector<ll> dfs(ll a,ll b, ll prv)
{
    if(a==b)
    {
        vector<ll> v;
        v.pb(b);
        return v;
    }
    for(auto it : adj[a])
    {
        if(it!=prv)
        {
            vector<ll> v2=dfs(it,b,a);
            if(v2.size()!=0)
            {
                v2.pb(a);
                return v2;
            }
        }
    }
    vector<ll> v;
    return v;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t=1;
//     cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        adj=vector<vector<ll> >(n+1);
        map<pair<ll,ll>,ll>ans;
        vector<pair<ll,ll> > v4;
        rep(i,1,n)
        {
            ll u,v;
            cin>>u>>v;
            adj[u].pb(v);
            adj[v].pb(u);
            ans[{u,v}]=0;
            ans[{v,u}]=0;
            v4.pb({u,v});
        }

        // ll ans[n+1];
        // rep(i,0,n+1)
        // ans[i]=0;

        ll m;
        cin>>m;

        vector<pair< pair<ll,ll>,ll> > v3;
        rep(i,0,m)
        {
            ll a,b,g;
            cin>>a>>b>>g;
            v3.pb({{a,b},g});
            vector<ll> v=dfs(a,b,0);
            rep(i,1,(ll)v.size()){
            ans[{v[i],v[i-1]}]=max(ans[{v[i],v[i-1]}],g);
            ans[{v[i-1],v[i]}]=max(ans[{v[i-1],v[i]}],g);}
        }

        rep(i,0,n-1)
        {
            if(ans[v4[i]]==0)
            ans[v4[i]]=(1e6);
        }

        // rep(i,0,)

        rep(i,0,m)
        {
            ll a=v3[i].first.first;
            ll b=v3[i].first.second;
            ll g=v3[i].second;
            vector<ll> v=dfs(a,b,0);
            ll mn=1e6;
            rep(i,1,(ll)v.size())
            {
                mn=min(ans[{v[i],v[i-1]}],mn);
            }
            if(mn!=g)
            {
                cout<<"-1";
                return 0;
            }
        }

        rep(i,0,n-1)
        cout<<ans[v4[i]]<<" ";
    }

}


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

#define ll int
#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 = 1e12;
#define PI 3.14159265

int pow1(int a,int b){
    int res=1;
    while(b>0){
        if(b&1){
        	res=res*a;
		}
        a=a*a;
        b>>=1;
    }
    return res;
}

ll n,m;
vector<vector<pair<ll,ll> > > g;
vector<ll> val;

void dfs(ll v, ll pv, ll pe, vector<pair<ll,ll> >&p)
{
    p[v]=make_pair(pv,pe);
    for(auto it : g[v])
    {
        ll to=it.first;
        ll idx=it.second;
        if(to==pv)continue;
        dfs(to,v,idx,p);
    }
}

int main()
{
    cin>>n;
    g=vector<vector<pair<ll,ll> > >(n);
    rep(i,0,n-1)
    {
        ll u,v;
        cin>>u>>v;
        u--,v--;
        g[u].pb(make_pair(v,i));
        g[v].pb(make_pair(u,i));
    }

    vector<vector<pair<ll,ll> > >p(n,vector<pair<ll,ll> >(n));
    for(ll i=0;i<n;i++)
    dfs(i,-1,-1,p[i]);

    val=vector<ll>(n-1,1e6);

    cin>>m;

    vector<pair<pair<ll,ll>,ll> >q(m);
    rep(i,0,m)
    {
        cin>>q[i].first.first>>q[i].first.second>>q[i].second;
        --q[i].first.first;
        --q[i].first.second;
    }

    sort(q.begin(),q.end(),[&](pair<pair<ll,ll>,ll> a, pair<pair<ll,ll>,ll>b){
        return a.second<b.second;
    });

    for(ll i=0;i<m;i++)
    {
        ll u=q[i].first.first;
        ll v=q[i].first.second;
        while(v!=u)
        {
            ll pv=p[u][v].first;
            ll pe=p[u][v].second;
            val[pe]=q[i].second;
            v=pv;
        }
    }

    for(ll i=0;i<m;i++)
    {
        ll u=q[i].first.first;
        ll v=q[i].first.second;
        ll mx=1e6;
        while(v!=u)
        {
            ll pv=p[u][v].first;
            ll pe=p[u][v].second;
            mx=min(mx,val[pe]);
            v=pv;
        }
        if(mx!=q[i].second)
        {
            cout<<"-1";
            return 0;
        }
    }

    rep(i,0,n-1)
    cout<<val[i]<<" ";
    return 0;
}


#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 = 1e12;
#define PI 3.14159265

int pow1(int a,int b){
    int res=1;
    while(b>0){
        if(b&1){
        	res=res*a;
		}
        a=a*a;
        b>>=1;
    }
    return res;
}

vector<vector<ll> >adj;

bool dfs(ll a,ll b, ll prv , vector<ll> &v)
{
    if(a==b)
    {
        v.pb(b);
        return true;
    }
    for(auto it : adj[a])
    {
        if(it!=prv)
        {
            if(dfs(it,b,a,v))
            {
                v.pb(a);
                return true;
            }
        }
    }
    return false;
}

int main()
{
    ll t=1;
//     cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        adj=vector<vector<ll> >(n+1);
        map<pair<ll,ll>,ll>ans;
        vector<pair<ll,ll> > v4;
        rep(i,1,n)
        {
            ll u,v;
            cin>>u>>v;
            adj[u].pb(v);
            adj[v].pb(u);
            ans[{u,v}]=0;
            ans[{v,u}]=0;
            v4.pb({u,v});
        }

        // ll ans[n+1];
        // rep(i,0,n+1)
        // ans[i]=0;

        ll m;
        cin>>m;

        vector<pair< pair<ll,ll>,ll> > v3;
        rep(i,0,m)
        {
            ll a,b,g;
            cin>>a>>b>>g;
            v3.pb({{a,b},g});
            vector<ll> v;
            dfs(a,b,0,v);
            rep(i,1,(ll)v.size()){
            ans[{v[i],v[i-1]}]=max(ans[{v[i],v[i-1]}],g);
            ans[{v[i-1],v[i]}]=max(ans[{v[i-1],v[i]}],g);}
        }

        rep(i,0,n-1)
        {
            if(ans[v4[i]]==0)
            ans[v4[i]]=(1e6);
        }

        // rep(i,0,)

        rep(i,0,m)
        {
            ll a=v3[i].first.first;
            ll b=v3[i].first.second;
            ll g=v3[i].second;
            vector<ll> v;
            dfs(a,b,0,v);
            ll mn=1e6;
            rep(i,1,(ll)v.size())
            {
                mn=min(ans[{v[i],v[i-1]}],mn);
            }
            if(mn!=g)
            {
                cout<<"-1";
                return 0;
            }
        }

        rep(i,0,n-1)
        cout<<ans[v4[i]]<<" ";
    }

}


#include <bits/stdc++.h>
#define hell                         1000000007

#define Maxi                      10000000000000000
#define lcm(a,b)                     (a*b)/__gcd(a,b)
#define ll                            int

#define vll                          vector<ll>
#define vi                           vector<long long>
#define pll                          vector< pair<ll,ll> >
#define pb                           push_back
//#define mp                           make_pair
#define all(v)                       v.begin(),v.end()
#define lbnd                         lower_bound
#define ubnd                         upper_bound
#define bs                           binary_search
#define F                            first
#define S                            second
#define rep(i,a,b)                   for(i=a;i<b;i++)
#define parr(a,n)                    for(i=0;i<n;i++) cout<<a[i]<<" ";cout<<endl;
#define pcont(a)                     for(auto i:a) cout<<i<<" ";cout<<endl;
#define ret(x)                       return cout<<x,0;
#define dbg(x)                       cout << #x << " is " << x << endl;
#define endl                           '\n'

// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

using namespace std;

vector<pair<ll,ll>>graph[5005];
vector<pair<ll,ll>>parent[5005];
ll visited[5005];
void dfs(ll u,ll v,ll mm,ll index)
{
	parent[mm][u]={v,index};

	visited[u]=1;

	for(auto i:graph[u])
	{
		if(i.F!=v)
		{
			dfs(i.F,u,mm,i.S);
		}
	}
}

  int main()

{

ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);

	ll i,j;
	ll n;
	cin>>n;

	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			parent[i].pb({j,0});
		}
	}

	vector<pair<ll,pair<ll,ll>>>queries;
	for(i=0;i<n-1;i++)
	{
		ll u,v;
		cin>>u>>v;
		u--;
		v--;

		graph[u].pb({v,i});
		graph[v].pb({u,i});
	}

	ll q;
	cin>>q;

	for(i=0;i<n;i++)
	{
		dfs(i,i,i,-1);

	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		cin>>u>>v>>w;
		u--;
		v--;
		queries.pb({w,{u,v}});
	}
	sort(all(queries));

	map<pair<ll,ll>,ll>m1;

	ll val[n];
	for(i=0;i<n;i++)
	{
		val[i]=0;
	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		w=queries[i].F;
		u=queries[i].S.F;
		v=queries[i].S.S;

		while(parent[u][v].F!=v)
		{
				val[parent[u][v].S]=w;
				v=parent[u][v].F;
		}
	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		w=queries[i].F;
		u=queries[i].S.F;
		v=queries[i].S.S;

		ll maxi=1000000;

		while(parent[u][v].F!=v)
		{
			maxi=min(val[parent[u][v].S],maxi);
			v=parent[u][v].F;
		}

		if(maxi!=w)
		{
			cout<<"-1"<<endl;
			return 0;
		}

	}

	for(i=0;i<n-1;i++)
	{

		if(val[i]==0)
		{
			cout<<1000<<" ";
		}
		else
		{
			cout<<val[i]<<" ";
		}
	}
	cout<<endl;

}


#include <bits/stdc++.h>
#define hell                         1000000007

#define Maxi                      10000000000000000
#define lcm(a,b)                     (a*b)/__gcd(a,b)
#define ll                            long long

#define vll                          vector<ll>
#define vi                           vector<long long>
#define pll                          vector< pair<ll,ll> >
#define pb                           push_back
//#define mp                           make_pair
#define all(v)                       v.begin(),v.end()
#define lbnd                         lower_bound
#define ubnd                         upper_bound
#define bs                           binary_search
#define F                            first
#define S                            second
#define rep(i,a,b)                   for(i=a;i<b;i++)
#define parr(a,n)                    for(i=0;i<n;i++) cout<<a[i]<<" ";cout<<endl;
#define pcont(a)                     for(auto i:a) cout<<i<<" ";cout<<endl;
#define ret(x)                       return cout<<x,0;
#define dbg(x)                       cout << #x << " is " << x << endl;
#define endl                           '\n'

// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

using namespace std;

vector<ll>graph[200005];
vector<ll>parent[200005];
ll visited[100005];
void dfs(ll u,ll v,ll mm)
{
	parent[mm][u]=v;

	visited[u]=1;

	for(auto i:graph[u])
	{
		if(!visited[i])
		{
			dfs(i,u,mm);
		}
	}
}

  int main()

{

ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);

	ll i,j;
	ll n;
	cin>>n;

	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			parent[i].pb(j);
		}
	}

	vector<pair<ll,pair<ll,ll>>>edges,queries;
	for(i=0;i<n-1;i++)
	{
		ll u,v;
		cin>>u>>v;
		u--;
		v--;
		edges.pb({i,{u,v}});
		graph[u].pb(v);
		graph[v].pb(u);
	}

	ll q;
	cin>>q;

	for(i=0;i<n;i++)
	{
		dfs(i,i,i);

		for(j=0;j<n;j++)
		{
			visited[j]=0;
		}
	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		cin>>u>>v>>w;
		u--;
		v--;
		queries.pb({w,{u,v}});
	}
	sort(all(queries));

	ll mat[n][n];

	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			mat[i][j]=0;
		}
	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		w=queries[i].F;
		u=queries[i].S.F;
		v=queries[i].S.S;

		while(parent[u][v]!=v)
		{
			mat[parent[u][v]][v]=max(mat[parent[u][v]][v],w);
			mat[v][parent[u][v]]=mat[parent[u][v]][v];
			v=parent[u][v];
		}
	}

	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			if(mat[i][j]==0)
			{
				mat[i][j]=1000000;
			}
		}
	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		w=queries[i].F;
		u=queries[i].S.F;
		v=queries[i].S.S;

		ll maxi=1000000;

		while(parent[u][v]!=v)
		{
			maxi=min(mat[parent[u][v]][v],maxi);
			v=parent[u][v];
		}

		if(maxi!=w)
		{
			cout<<"-1"<<endl;
			return 0;
		}

	}

	for(i=0;i<n-1;i++)
	{
		ll u,v;
		u=edges[i].S.F;
		v=edges[i].S.S;

		cout<<mat[u][v]<<" ";
	}
	cout<<endl;

}


#include <bits/stdc++.h>
#define hell                         1000000007

#define Maxi                      10000000000000000
#define lcm(a,b)                     (a*b)/__gcd(a,b)
#define ll                            int

#define vll                          vector<ll>
#define vi                           vector<long long>
#define pll                          vector< pair<ll,ll> >
#define pb                           push_back
//#define mp                           make_pair
#define all(v)                       v.begin(),v.end()
#define lbnd                         lower_bound
#define ubnd                         upper_bound
#define bs                           binary_search
#define F                            first
#define S                            second
#define rep(i,a,b)                   for(i=a;i<b;i++)
#define parr(a,n)                    for(i=0;i<n;i++) cout<<a[i]<<" ";cout<<endl;
#define pcont(a)                     for(auto i:a) cout<<i<<" ";cout<<endl;
#define ret(x)                       return cout<<x,0;
#define dbg(x)                       cout << #x << " is " << x << endl;
#define endl                           '\n'

// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

using namespace std;

vector<ll>graph[200005];
vector<ll>parent[200005];
ll visited[100005];
void dfs(ll u,ll v,ll mm)
{
	parent[mm][u]=v;

	visited[u]=1;

	for(auto i:graph[u])
	{
		if(!visited[i])
		{
			dfs(i,u,mm);
		}
	}
}

  int main()

{

ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);

	ll i,j;
	ll n;
	cin>>n;

	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			parent[i].pb(j);
		}
	}

	vector<pair<ll,pair<ll,ll>>>edges,queries;
	for(i=0;i<n-1;i++)
	{
		ll u,v;
		cin>>u>>v;
		u--;
		v--;
		edges.pb({i,{u,v}});
		graph[u].pb(v);
		graph[v].pb(u);
	}

	ll q;
	cin>>q;

	for(i=0;i<n;i++)
	{
		dfs(i,i,i);

		for(j=0;j<n;j++)
		{
			visited[j]=0;
		}
	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		cin>>u>>v>>w;
		u--;
		v--;
		queries.pb({w,{u,v}});
	}
	sort(all(queries));

	ll mat[n][n];

	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			mat[i][j]=0;
		}
	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		w=queries[i].F;
		u=queries[i].S.F;
		v=queries[i].S.S;

		while(parent[u][v]!=v)
		{
			mat[parent[u][v]][v]=max(mat[parent[u][v]][v],w);
			mat[v][parent[u][v]]=mat[parent[u][v]][v];
			v=parent[u][v];
		}
	}

	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			if(mat[i][j]==0)
			{
				mat[i][j]=1000000;
			}
		}
	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		w=queries[i].F;
		u=queries[i].S.F;
		v=queries[i].S.S;

		ll maxi=1000000;

		while(parent[u][v]!=v)
		{
			maxi=min(mat[parent[u][v]][v],maxi);
			v=parent[u][v];
		}

		if(maxi!=w)
		{
			cout<<"-1"<<endl;
			return 0;
		}

	}

	for(i=0;i<n-1;i++)
	{
		ll u,v;
		u=edges[i].S.F;
		v=edges[i].S.S;

		cout<<mat[u][v]<<" ";
	}
	cout<<endl;

}


#include <bits/stdc++.h>
#define hell                         1000000007

#define Maxi                      10000000000000000
#define lcm(a,b)                     (a*b)/__gcd(a,b)
#define ll                            int

#define vll                          vector<ll>
#define vi                           vector<long long>
#define pll                          vector< pair<ll,ll> >
#define pb                           push_back
//#define mp                           make_pair
#define all(v)                       v.begin(),v.end()
#define lbnd                         lower_bound
#define ubnd                         upper_bound
#define bs                           binary_search
#define F                            first
#define S                            second
#define rep(i,a,b)                   for(i=a;i<b;i++)
#define parr(a,n)                    for(i=0;i<n;i++) cout<<a[i]<<" ";cout<<endl;
#define pcont(a)                     for(auto i:a) cout<<i<<" ";cout<<endl;
#define ret(x)                       return cout<<x,0;
#define dbg(x)                       cout << #x << " is " << x << endl;
#define endl                           '\n'

// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

using namespace std;

vector<pair<ll,ll>>graph[5005];
vector<pair<ll,ll>>parent[5005];
ll visited[5005];
void dfs(ll u,ll v,ll mm,ll index)
{
	parent[mm][u]={v,index};

	visited[u]=1;

	for(auto i:graph[u])
	{
		if(!visited[i.F])
		{
			dfs(i.F,u,mm,i.S);
		}
	}
}

  int main()

{

ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);

	ll i,j;
	ll n;
	cin>>n;

	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			parent[i].pb({j,0});
		}
	}

	vector<pair<ll,pair<ll,ll>>>queries;
	for(i=0;i<n-1;i++)
	{
		ll u,v;
		cin>>u>>v;
		u--;
		v--;

		graph[u].pb({v,i});
		graph[v].pb({u,i});
	}

	ll q;
	cin>>q;

	for(i=0;i<n;i++)
	{
		dfs(i,i,i,-1);

		for(j=0;j<n;j++)
		{
			visited[j]=0;
		}
	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		cin>>u>>v>>w;
		u--;
		v--;
		queries.pb({w,{u,v}});
	}
	sort(all(queries));

	map<pair<ll,ll>,ll>m1;

	ll val[n];
	for(i=0;i<n;i++)
	{
		val[i]=0;
	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		w=queries[i].F;
		u=queries[i].S.F;
		v=queries[i].S.S;

		if(u==v)
		{
			cout<<-1<<endl;
			return 0;
		}

		while(parent[u][v].F!=v)
		{
				val[parent[u][v].S]=w;
				v=parent[u][v].F;
		}
	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		w=queries[i].F;
		u=queries[i].S.F;
		v=queries[i].S.S;

		ll maxi=1000000;

		while(parent[u][v].F!=v)
		{
			maxi=min(val[parent[u][v].S],maxi);
			v=parent[u][v].F;
		}

		if(maxi!=w)
		{
			cout<<"-1"<<endl;
			return 0;
		}

	}

	for(i=0;i<n-1;i++)
	{

		if(val[i]==0)
		{
			cout<<1000<<" ";
		}
		else
		{
			cout<<val[i]<<" ";
		}
	}
	cout<<endl;

}


#include <bits/stdc++.h>
#define hell                         1000000007

#define Maxi                      10000000000000000
#define lcm(a,b)                     (a*b)/__gcd(a,b)
#define ll                            int

#define vll                          vector<ll>
#define vi                           vector<long long>
#define pll                          vector< pair<ll,ll> >
#define pb                           push_back
//#define mp                           make_pair
#define all(v)                       v.begin(),v.end()
#define lbnd                         lower_bound
#define ubnd                         upper_bound
#define bs                           binary_search
#define F                            first
#define S                            second
#define rep(i,a,b)                   for(i=a;i<b;i++)
#define parr(a,n)                    for(i=0;i<n;i++) cout<<a[i]<<" ";cout<<endl;
#define pcont(a)                     for(auto i:a) cout<<i<<" ";cout<<endl;
#define ret(x)                       return cout<<x,0;
#define dbg(x)                       cout << #x << " is " << x << endl;
#define endl                           '\n'

// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

using namespace std;

vector<ll>graph[200005];
vector<ll>parent[200005];
ll visited[100005];
void dfs(ll u,ll v,ll mm)
{
	parent[mm][u]=v;

	visited[u]=1;

	for(auto i:graph[u])
	{
		if(!visited[i])
		{
			dfs(i,u,mm);
		}
	}
}

  int main()

{

ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);

	ll i,j;
	ll n;
	cin>>n;

	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			parent[i].pb(j);
		}
	}

	vector<pair<ll,pair<ll,ll>>>edges,queries;
	for(i=0;i<n-1;i++)
	{
		ll u,v;
		cin>>u>>v;
		u--;
		v--;
		edges.pb({i,{u,v}});
		graph[u].pb(v);
		graph[v].pb(u);
	}

	ll q;
	cin>>q;

	for(i=0;i<n;i++)
	{
		dfs(i,i,i);

		for(j=0;j<n;j++)
		{
			visited[j]=0;
		}
	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		cin>>u>>v>>w;
		u--;
		v--;
		queries.pb({w,{u,v}});
	}
	sort(all(queries));

	map<pair<ll,ll>,ll>m1;

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		w=queries[i].F;
		u=queries[i].S.F;
		v=queries[i].S.S;

		while(parent[u][v]!=v)
		{
			if(m1.find({v,parent[u][v]})==m1.end())
			{
				m1[{v,parent[u][v]}]=w;
				m1[{parent[u][v],v}]=w;
				v=parent[u][v];
				continue;
			}
			m1[{parent[u][v],v}]=max(m1[{parent[u][v],v}],w);
			m1[{v,parent[u][v]}]=max(m1[{parent[u][v],v}],w);
			v=parent[u][v];

		}
	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		w=queries[i].F;
		u=queries[i].S.F;
		v=queries[i].S.S;

		ll maxi=1000000;

		while(parent[u][v]!=v)
		{
			maxi=min(m1[{parent[u][v],v}],maxi);
			v=parent[u][v];
		}

		if(maxi!=w)
		{
			cout<<"-1"<<endl;
			return 0;
		}

	}

	for(i=0;i<n-1;i++)
	{
		ll u,v;
		u=edges[i].S.F;
		v=edges[i].S.S;

		if(m1.find({u,v})==m1.end())
		{
			cout<<1000<<" ";
		}
		else
		{
			cout<<m1[{u,v}]<<" ";
		}
	}
	cout<<endl;

}


#include <bits/stdc++.h>
#define hell                         1000000007

#define Maxi                      10000000000000000
#define lcm(a,b)                     (a*b)/__gcd(a,b)
#define ll                            int

#define vll                          vector<ll>
#define vi                           vector<long long>
#define pll                          vector< pair<ll,ll> >
#define pb                           push_back
//#define mp                           make_pair
#define all(v)                       v.begin(),v.end()
#define lbnd                         lower_bound
#define ubnd                         upper_bound
#define bs                           binary_search
#define F                            first
#define S                            second
#define rep(i,a,b)                   for(i=a;i<b;i++)
#define parr(a,n)                    for(i=0;i<n;i++) cout<<a[i]<<" ";cout<<endl;
#define pcont(a)                     for(auto i:a) cout<<i<<" ";cout<<endl;
#define ret(x)                       return cout<<x,0;
#define dbg(x)                       cout << #x << " is " << x << endl;
#define endl                           '\n'

// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

using namespace std;

vector<pair<ll,ll>>graph[200005];
vector<pair<ll,ll>>parent[200005];
ll visited[100005];
void dfs(ll u,ll v,ll mm,ll index)
{
	parent[mm][u]={v,index};

	visited[u]=1;

	for(auto i:graph[u])
	{
		if(!visited[i.F])
		{
			dfs(i.F,u,mm,i.S);
		}
	}
}

  int main()

{

ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);

	ll i,j;
	ll n;
	cin>>n;

	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			parent[i].pb({j,0});
		}
	}

	vector<pair<ll,pair<ll,ll>>>edges,queries;
	for(i=0;i<n-1;i++)
	{
		ll u,v;
		cin>>u>>v;
		u--;
		v--;
		edges.pb({i,{u,v}});
		graph[u].pb({v,i});
		graph[v].pb({u,i});
	}

	ll q;
	cin>>q;

	for(i=0;i<n;i++)
	{
		dfs(i,i,i,-1);

		for(j=0;j<n;j++)
		{
			visited[j]=0;
		}
	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		cin>>u>>v>>w;
		u--;
		v--;
		queries.pb({w,{u,v}});
	}
	sort(all(queries));

	map<pair<ll,ll>,ll>m1;

	ll val[n];
	for(i=0;i<n;i++)
	{
		val[i]=0;
	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		w=queries[i].F;
		u=queries[i].S.F;
		v=queries[i].S.S;

		if(u==v)
		{
			cout<<-1<<endl;
			return 0;
		}

		while(parent[u][v].F!=v)
		{
				val[parent[u][v].S]=w;
				v=parent[u][v].F;
		}
	}

	for(i=0;i<q;i++)
	{
		ll u,v,w;
		w=queries[i].F;
		u=queries[i].S.F;
		v=queries[i].S.S;

		ll maxi=1000000;

		while(parent[u][v].F!=v)
		{
			maxi=min(val[parent[u][v].S],maxi);
			v=parent[u][v].F;
		}

		if(maxi!=w)
		{
			cout<<"-1"<<endl;
			return 0;
		}

	}

	for(i=0;i<n-1;i++)
	{
		ll u,v;
		u=edges[i].S.F;
		v=edges[i].S.S;

		if(val[edges[i].F]==0)
		{
			cout<<1000<<" ";
		}
		else
		{
			cout<<val[edges[i].F]<<" ";
		}
	}
	cout<<endl;

}