#include <bits/stdc++.h>
using namespace std;
const int N = (int)5e3 + 10, INF = (int)1e6;
struct node {
  int to, nxt;
} e[N << 1];
struct Info {
  int u, v, w;
  bool friend operator<(const Info& a, const Info& b) { return a.w > b.w; }
} info[N];
map<pair<int, int>, int> mp;
pair<int, int> pii;
vector<pair<int, int> > vii;
int n, m, u, v, w, tot, tim;
int head[N], dfn[N], fa[N];
inline void add(int u, int v) {
  e[tot].to = v;
  e[tot].nxt = head[u];
  head[u] = tot++;
  e[tot].to = u;
  e[tot].nxt = head[v];
  head[v] = tot++;
}
void dfs_dfn(int now, int pre) {
  dfn[now] = ++tim;
  for (int o = head[now]; ~o; o = e[o].nxt) {
    if (e[o].to == pre) continue;
    dfs_dfn(e[o].to, now);
  }
}
void dfs_mp(int now, int pre) {
  for (int o = head[now]; ~o; o = e[o].nxt) {
    int to = e[o].to;
    if (to == pre) continue;
    fa[dfn[to]] = dfn[now];
    mp[make_pair(dfn[now], dfn[to])] = INF;
    dfs_mp(e[o].to, now);
  }
}
void show() {
  for (int i = 1; i <= n; ++i) printf("u = %d dfn = %d\n", i, dfn[i]);
  for (int i = 1; i <= m; ++i) cout << info[i].w << endl;
  for (int i = 1; i <= n; ++i) printf("u = %d fa = %d\n", i, fa[dfn[i]]);
}
bool lca(int u, int v, int w) {
  int u_dfn = dfn[u], v_dfn = dfn[v], tmp_w;
  bool ok = 0;
  while (u_dfn != v_dfn) {
    while (u_dfn > v_dfn) {
      pii.first = fa[u_dfn];
      pii.second = u_dfn;
      tmp_w = mp[pii];
      if (tmp_w == INF || tmp_w == w) {
        ok = 1;
        mp[pii] = w;
      }
      u_dfn = pii.first;
    }
    while (u_dfn < v_dfn) {
      pii.first = fa[v_dfn];
      pii.second = v_dfn;
      tmp_w = mp[pii];
      if (tmp_w == INF || tmp_w == w) {
        ok = 1;
        mp[pii] = w;
      }
      v_dfn = pii.first;
    }
  }
  return ok;
}
void solve() {
  for (int i = 1; i <= n; ++i) head[i] = -1;
  tot = 0;
  for (int i = 1; i <= n - 1; ++i) {
    scanf("%d%d", &u, &v);
    vii.push_back(make_pair(u, v));
    add(u, v);
  }
  dfs_dfn(1, 0);
  fa[1] = 1;
  dfs_mp(1, 0);
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &info[i].u, &info[i].v, &info[i].w);
  }
  sort(info + 1, info + 1 + m);
  for (int i = 1; i <= m; ++i) {
    if (lca(info[i].u, info[i].v, info[i].w)) continue;
    printf("-1\n");
    return;
  }
  int l = vii.size();
  int dfn1, dfn2;
  for (int i = 0; i < l; ++i) {
    dfn1 = dfn[vii[i].first];
    dfn2 = dfn[vii[i].second];
    if (dfn1 > dfn2) swap(dfn1, dfn2);
    pii.first = dfn1;
    pii.second = dfn2;
    printf("%d ", mp[pii]);
  }
  cout << endl;
}
int main() {
  scanf("%d", &n);
  solve();
  return 0;
}
