#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
const long long MAXN = 5005;
vector<long long> edg[MAXN];
long long tin[MAXN];
long long tout[MAXN];
long long d = 0;
long long pred[MAXN];
long long wei[MAXN][MAXN];
long long INF = 1e6;
void dfs(long long v, long long p) {
  tin[v] = d++;
  pred[v] = p;
  for (auto u : edg[v]) {
    if (u == p) continue;
    dfs(u, v);
  }
  tout[v] = d++;
}
bool upper(long long u, long long v) {
  return tin[u] <= tin[v] && tout[u] >= tout[v];
}
bool cmp(tuple<long long, long long, long long> a,
         tuple<long long, long long, long long> b) {
  return get<2>(a) < get<2>(b);
}
void pol(long long a, long long b, long long w) {
  long long tmp = a;
  while (!upper(tmp, b)) {
    wei[tmp][pred[tmp]] = w;
    wei[pred[tmp]][tmp] = w;
    tmp = pred[tmp];
  }
  tmp = b;
  while (!upper(tmp, a)) {
    wei[tmp][pred[tmp]] = w;
    wei[pred[tmp]][tmp] = w;
    tmp = pred[tmp];
  }
}
long long check(long long a, long long b, long long w) {
  long long ans = INF, tmp = a;
  while (!upper(tmp, b)) {
    ans = min(ans, wei[tmp][pred[tmp]]);
    tmp = pred[tmp];
  }
  tmp = b;
  while (!upper(tmp, a)) {
    ans = min(ans, wei[tmp][pred[tmp]]);
    tmp = pred[tmp];
  }
  return ans == w;
}
void fast() {
  ios::sync_with_stdio(0);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
signed main() {
  fast();
  long long n, i, a, b, m, w, j;
  cin >> n;
  vector<pair<long long, long long> > reb(n);
  for (i = 0; i < n - 1; i++) {
    cin >> a >> b;
    edg[a].push_back(b);
    edg[b].push_back(a);
    reb[i] = {a, b};
  }
  dfs(1, 0);
  cin >> m;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) wei[i][j] = INF;
  vector<tuple<long long, long long, long long> > zap(m);
  for (i = 0; i < m; i++) {
    cin >> a >> b >> w;
    zap[i] = make_tuple(a, b, w);
  }
  sort(zap.begin(), zap.end(), cmp);
  for (i = 0; i < m; i++) {
    tie(a, b, w) = zap[i];
    pol(a, b, w);
  }
  for (i = 0; i < m; i++) {
    tie(a, b, w) = zap[i];
    if (!check(a, b, w)) {
      cout << -1 << '\n';
      return 0;
    }
  }
  for (i = 0; i < n - 1; i++) {
    tie(a, b) = reb[i];
    cout << wei[a][b] << " ";
  }
}
