#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
vector<int> g[N];
pair<int, int> edge[N];
int val[N], dep[N], par[N];
void dfs(int v, int p, int __dep = 0) {
  dep[v] = __dep;
  par[v] = p;
  for (auto x : g[v]) {
    if (x == p) continue;
    if (edge[x].first == v)
      dfs(edge[x].second, x, __dep + 1);
    else
      dfs(edge[x].first, x, __dep + 1);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << setprecision(32);
  int n;
  cin >> n;
  int u, v;
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    edge[i] = {u, v};
    g[u].push_back(i);
    g[v].push_back(i);
    val[i] = 1;
  }
  dfs(1, 0);
  int m, a, b, g;
  cin >> m;
  vector<pair<pair<int, int>, int> > vec;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> g;
    vec.push_back({{a, b}, g});
    if (dep[a] < dep[b]) swap(a, b);
    while (dep[a] > dep[b]) {
      int x = par[a];
      val[x] = max(val[x], g);
      a = edge[x].first + edge[x].second - a;
    }
    while (a != b) {
      int x = par[a];
      val[x] = max(val[x], g);
      a = edge[x].first + edge[x].second - a;
      x = par[b];
      val[x] = max(val[x], g);
      b = edge[x].first + edge[x].second - b;
    }
  }
  for (int i = 0; i < m; i++) {
    a = vec[i].first.first;
    b = vec[i].first.second;
    g = vec[i].second;
    int mini = 1000000;
    if (dep[a] < dep[b]) swap(a, b);
    while (dep[a] > dep[b]) {
      int x = par[a];
      mini = min(val[x], mini);
      a = edge[x].first + edge[x].second - a;
    }
    while (a != b) {
      int x = par[a];
      mini = min(val[x], mini);
      a = edge[x].first + edge[x].second - a;
      x = par[b];
      mini = min(val[x], mini);
      b = edge[x].first + edge[x].second - b;
    }
    if (mini != g) {
      cout << -1 << endl;
      exit(0);
    }
  }
  for (int i = 1; i < n; i++) {
    cout << val[i] << " ";
  }
  return 0;
}
