#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e3 + 5;
int D[MAXN][MAXN], p[MAXN][MAXN];
int N, M;
int u[MAXN], v[MAXN];
int s[MAXN], t[MAXN], w[MAXN];
vector<int> G[MAXN];
void dfs(int cur, int par, int root) {
  for (auto nxt : G[cur])
    if (nxt != par) {
      p[root][nxt] = cur;
      dfs(nxt, cur, root);
    }
}
void color(int a, int b, int c) {
  while (a != b) {
    int x = a, y = p[b][a];
    if (D[x][y]) {
      if (D[x][y] < c) D[x][y] = D[y][x] = c;
    } else
      D[x][y] = D[y][x] = c;
    a = p[b][a];
  }
}
void dfs2(int cur, int par, int root) {
  for (auto nxt : G[cur])
    if (nxt != par) {
      D[root][nxt] = min(D[root][cur], D[cur][nxt]);
      dfs2(nxt, cur, root);
    }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i < N; i++) {
    cin >> u[i] >> v[i];
    G[u[i]].push_back(v[i]);
    G[v[i]].push_back(u[i]);
  }
  for (int i = 1; i <= N; i++) dfs(i, 0, i);
  cin >> M;
  for (int i = 1; i <= M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    s[i] = a;
    t[i] = b;
    w[i] = c;
    color(a, b, c);
  }
  for (int i = 1; i <= N; i++) D[i][i] = 1e9, dfs2(i, 0, i);
  for (int i = 1; i <= M; i++)
    if (D[s[i]][t[i]] > w[i]) {
      cout << -1;
      return 0;
    }
  for (int i = 1; i < N; i++) {
    if (D[u[i]][v[i]] == 0)
      cout << "1 ";
    else
      cout << D[u[i]][v[i]] << ' ';
  }
}
