#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 7, INF = 1e9 + 7;
int fa[MAXN], mtx[MAXN] = {}, n, q, h[MAXN], ans[MAXN];
int nChain = 1, chainHead[MAXN] = {}, chainId[MAXN], idx[MAXN];
int uu[MAXN], vv[MAXN], cc[MAXN], v2[MAXN], v1[MAXN];
vector<int> adj[MAXN], vex[MAXN], it[MAXN], lz[MAXN], mn[MAXN];
int find_mtx(int u, int p) {
  int mx = -1, chl = 0;
  for (auto v : adj[u])
    if (v != p) {
      fa[v] = u, ++chl, h[v] = h[u] + 1;
      int mtv = find_mtx(v, u);
      if (mx < mtv) mx = mtv, mtx[u] = v;
    }
  return chl;
}
void build_hld(int u, int p) {
  if (chainHead[nChain] == 0) chainHead[nChain] = u;
  chainId[u] = nChain, vex[nChain].push_back(u),
  idx[u] = (int)vex[nChain].size();
  if (mtx[u] == 0) return;
  build_hld(mtx[u], u);
  for (auto v : adj[u])
    if (v != mtx[u] && v != p) ++nChain, build_hld(v, u);
}
void lp(int id, int g) {
  if (lz[id][g]) {
    it[id][g * 2] = max(it[id][g * 2], lz[id][g]);
    it[id][g * 2 + 1] = max(it[id][g * 2 + 1], lz[id][g]);
    lz[id][g * 2] = max(lz[id][g * 2], lz[id][g]);
    lz[id][g * 2 + 1] = max(lz[id][g * 2 + 1], lz[id][g]);
    lz[id][g] = 0;
  }
}
void update(int id, int g, int l, int r, int d, int c, int val) {
  if (l > c || r < d || l > r || d > c) return;
  if (d <= l && r <= c) {
    it[id][g] = max(it[id][g], val), lz[id][g] = max(lz[id][g], val);
    return;
  }
  lp(id, g);
  int mid = (l + r) >> 1;
  update(id, g * 2, l, mid, d, c, val);
  update(id, g * 2 + 1, mid + 1, r, d, c, val);
  it[id][g] = max(it[id][g * 2], it[id][g * 2 + 1]);
}
void upd_min(int id, int g, int l, int r, int pos, int val) {
  if (l == r) return mn[id][g] = val, void();
  int mid = (l + r) >> 1;
  if (pos > mid)
    upd_min(id, g * 2 + 1, mid + 1, r, pos, val);
  else
    upd_min(id, g * 2, l, mid, pos, val);
  mn[id][g] = min(mn[id][g * 2], mn[id][g * 2 + 1]);
}
int get_min(int id, int g, int l, int r, int d, int c) {
  if (l > c || r < d) return INF;
  if (d <= l && r <= c) return mn[id][g];
  int mid = (l + r) >> 1;
  int ret = min(get_min(id, g * 2, l, mid, d, c),
                get_min(id, g * 2 + 1, mid + 1, r, d, c));
  return ret;
}
int query(int id, int g, int l, int r, int d, int c) {
  if (l > c || r < d || l > r || d > c) return 0;
  if (d <= l && r <= c) return it[id][g];
  lp(id, g);
  int mid = (l + r) >> 1;
  return max(query(id, g * 2, l, mid, d, c),
             query(id, g * 2 + 1, mid + 1, r, d, c));
}
void up(int &u, int &id, int c, int &res) {
  if (c > 0)
    update(id, 1, 1, (int)vex[id].size(), 1, idx[u], c);
  else if (c < 0)
    res = min(res, get_min(id, 1, 1, (int)vex[id].size(), 1, idx[u]));
  else
    res = max(res, query(id, 1, 1, (int)vex[id].size(), 1, idx[u]));
  u = fa[chainHead[id]], id = chainId[u];
}
int find_path(int u, int v, int c) {
  int uId = chainId[u], vId = chainId[v], res = (c == 0 ? 0 : INF);
  while (1) {
    if (uId == vId) break;
    int hu = chainHead[uId], hv = chainHead[vId];
    if (h[hu] > h[hv])
      up(u, uId, c, res);
    else if (h[hu] < h[hv])
      up(v, vId, c, res);
    else
      up(u, uId, c, res), up(v, vId, c, res);
  }
  if (idx[u] > idx[v]) swap(u, v);
  if (c > 0)
    update(vId, 1, 1, (int)vex[vId].size(), idx[u] + 1, idx[v], c);
  else if (c < 0) {
    res =
        min(res, get_min(vId, 1, 1, (int)vex[vId].size(), idx[u] + 1, idx[v]));
  } else
    res = max(res, query(vId, 1, 1, (int)vex[vId].size(), idx[u] + 1, idx[v]));
  return res;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i < n; ++i) {
    cin >> v1[i] >> v2[i];
    int u = v1[i], v = v2[i];
    adj[u].push_back(v), adj[v].push_back(u);
  }
  h[1] = 1, find_mtx(1, 0);
  build_hld(1, 0);
  for (int i = 1; i <= nChain; ++i) {
    it[i].resize(4 * (int)vex[i].size() + 7, 0);
    lz[i].resize(4 * (int)vex[i].size() + 7, 0);
    mn[i].resize(4 * (int)vex[i].size() + 7, 0);
  }
  cin >> q;
  for (int i = 1; i <= q; ++i) {
    cin >> uu[i] >> vv[i] >> cc[i];
    int u = uu[i], v = vv[i], c = cc[i];
    find_path(u, v, c);
  }
  for (int i = 1; i < n; ++i) {
    int u = v1[i], v = v2[i];
    if (u == fa[v]) swap(u, v);
    int id = chainId[u];
    ans[i] = query(id, 1, 1, (int)vex[id].size(), idx[u], idx[u]);
    upd_min(chainId[u], 1, 1, (int)vex[id].size(), idx[u], ans[i]);
  }
  for (int i = 1; i <= q; ++i)
    if (find_path(uu[i], vv[i], -1) != cc[i]) return cout << "-1", 0;
  for (int i = 1; i < n; ++i) cout << (ans[i] == 0 ? 1 : ans[i]) << ' ';
  return 0;
}
