#include <bits/stdc++.h>
using namespace std;
int n, m, head[5010], tot, val[5010], vis[5010], d, flag, u[5010], v[5010],
    w[5010], check;
struct node {
  int to, next, seq;
} e[5010 * 2];
void add_edge(int u, int v, int seq) {
  tot++, e[tot].to = v, e[tot].seq = seq, e[tot].next = head[u], head[u] = tot;
}
int max(int a, int b) { return a > b ? a : b; }
void dfs(int x, int w, int now) {
  int v, b;
  vis[x] = now;
  if (x == d) {
    flag = 1;
    return;
  }
  for (b = head[x]; b; b = e[b].next) {
    v = e[b].to;
    if (!flag && vis[v] != now) {
      dfs(v, w, now);
      if (flag) {
        int pos = e[b].seq;
        val[pos] = max(val[pos], w);
      }
    }
  }
}
void judge(int x, int w, int now) {
  int v, b;
  vis[x] = now;
  if (x == d) {
    flag = 1;
    return;
  }
  for (b = head[x]; b; b = e[b].next) {
    v = e[b].to;
    if (!flag && vis[v] != now) {
      judge(v, w, now);
      if (flag) {
        int pos = e[b].seq;
        if (val[pos] == w) check = 1;
      }
    }
  }
}
int main() {
  int i, a, b, l, r;
  scanf("%d", &n);
  for (i = 1; i <= n - 1; i++) {
    scanf("%d%d", &a, &b);
    add_edge(a, b, i), add_edge(b, a, i);
  }
  scanf("%d", &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d%d", &u[i], &v[i], &w[i]);
    flag = 0, d = v[i], dfs(u[i], w[i], i);
  }
  for (i = 1; i <= m; i++) {
    flag = 0, check = 0, d = v[i], judge(u[i], w[i], i + m);
    if (check == 0) break;
  }
  if (check == 0)
    printf("-1\n");
  else {
    for (i = 1; i <= n - 1; i++)
      if (val[i] == 0) val[i] = 1;
    printf("%d", val[1]);
    for (i = 2; i <= n - 1; i++) printf(" %d", val[i]);
    printf("\n");
  }
  return 0;
}
