#include <bits/stdc++.h>
using namespace std;
int gi() {
  int a;
  scanf("%d", &a);
  return a;
}
long long gli() {
  long long a;
  scanf("%I64d", &a);
  return a;
}
vector<pair<int, int> > ne[5000];
int h[5000];
pair<int, int> pr[5000];
int f[5000];
pair<int, pair<int, int> > v[5000];
void dfs(int a, int p, int hh) {
  h[a] = hh;
  for (pair<int, int> &b : ne[a])
    if (b.first != p) {
      dfs(b.first, a, hh + 1);
      pr[b.first] = {a, b.second};
    }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n = gi();
  for (int i = 0; i < n - 1; i++) {
    int a = gi() - 1;
    int b = gi() - 1;
    ne[a].push_back({b, i});
    ne[b].push_back({a, i});
    f[i] = 1;
  }
  int m = gi();
  for (int i = 0; i < m; i++) {
    int a = gi() - 1;
    int b = gi() - 1;
    int x = gi();
    v[i] = {x, {a, b}};
  }
  sort(v, v + m);
  dfs(0, -1, 0);
  for (int i = 0; i < m; i++) {
    int a = v[i].second.first;
    int b = v[i].second.second;
    while (h[a] > h[b]) {
      f[pr[a].second] = v[i].first;
      a = pr[a].first;
    }
    while (h[a] < h[b]) {
      f[pr[b].second] = v[i].first;
      b = pr[b].first;
    }
    while (a != b) {
      f[pr[a].second] = v[i].first;
      a = pr[a].first;
      f[pr[b].second] = v[i].first;
      b = pr[b].first;
    }
  }
  for (int i = 0; i < m; i++) {
    int a = v[i].second.first;
    int b = v[i].second.second;
    int mn = 2000000;
    while (h[a] > h[b]) {
      mn = ((f[pr[a].second]) < (mn) ? (f[pr[a].second]) : (mn));
      a = pr[a].first;
    }
    while (h[a] < h[b]) {
      mn = ((f[pr[b].second]) < (mn) ? (f[pr[b].second]) : (mn));
      b = pr[b].first;
    }
    while (a != b) {
      mn = ((f[pr[a].second]) < (mn) ? (f[pr[a].second]) : (mn));
      a = pr[a].first;
      mn = ((f[pr[b].second]) < (mn) ? (f[pr[b].second]) : (mn));
      b = pr[b].first;
    }
    if (mn != v[i].first) {
      printf("-1\n");
      return 0;
    }
  }
  for (int i = 0; i < n - 1; i++)
    printf("%d%c", f[i], (i == n - 2 ? '\n' : ' '));
  return 0;
}
