#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y, long long int p) {
  long long int r = 1;
  x = x % p;
  while (y) {
    if (y & 1) r = r * x % p;
    y = y >> 1;
    x = x * x % p;
  }
  return r;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int csrand(int l = 0, int r = 1e9) {
  uniform_int_distribution<int> uid(l, r);
  return uid(rng);
}
const int N = 5e3 + 99;
vector<pair<int, int> > g[N];
int par[N], ht[N], ed[N], ans[N];
void dfs(int v, int p) {
  par[v] = p;
  for (auto x : g[v]) {
    if (x.first != p) {
      ht[x.first] = ht[v] + 1;
      ed[x.first] = x.second;
      dfs(x.first, v);
    }
  }
}
struct anjn {
  int u, v, wt;
} ARR[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) ans[i] = 1;
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back({y, i});
    g[y].push_back({x, i});
  }
  dfs(1, 1);
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int u, v, wt;
    cin >> u >> v >> wt;
    ARR[i].u = u;
    ARR[i].v = v;
    ARR[i].wt = wt;
    while (ht[u] > ht[v]) {
      ans[ed[u]] = max(ans[ed[u]], wt);
      u = par[u];
    }
    while (ht[u] < ht[v]) {
      ans[ed[v]] = max(ans[ed[v]], wt);
      v = par[v];
    }
    while (u != v) {
      ans[ed[u]] = max(ans[ed[u]], wt);
      u = par[u];
      ans[ed[v]] = max(ans[ed[v]], wt);
      v = par[v];
    }
  }
  for (int i = 0; i < m; i++) {
    int u, v, wt;
    int ans1 = 1e7;
    u = ARR[i].u;
    v = ARR[i].v;
    wt = ARR[i].wt;
    while (ht[u] > ht[v]) {
      ans1 = min(ans[ed[u]], ans1);
      u = par[u];
    }
    while (ht[u] < ht[v]) {
      ans1 = min(ans[ed[v]], ans1);
      v = par[v];
    }
    while (u != v) {
      ans1 = min(ans[ed[u]], ans1);
      u = par[u];
      ans1 = min(ans[ed[v]], ans1);
      v = par[v];
    }
    if (ans1 != wt) {
      cout << -1 << '\n';
      return 0;
    }
  }
  for (int i = 0; i < n - 1; i++) cout << ans[i] << " ";
  cout << '\n';
}
