#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 10;
pair<int, int> a[N];
vector<int> g[N];
int d[N][N], pre[N];
bool vis[N];
bool bfs(int s, int e, int r) {
  memset(vis, 0, sizeof(vis));
  queue<int> q;
  q.push(s);
  vis[s] = 1, pre[s] = s;
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (auto tt : g[t]) {
      if (!vis[tt]) q.push(tt), pre[tt] = t, vis[tt] = 1;
      if (tt == e) {
        int mi = 1e9;
        int i = pre[tt], j = e;
        while (pre[j] != j) {
          if (d[i][j])
            mi = min(mi, d[i][j]);
          else
            d[i][j] = d[j][i] = r, mi = min(mi, d[i][j]);
          j = i, i = pre[i];
        }
        return mi == r ? true : false;
      }
    }
  }
}
int main(void) {
  std::ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    a[i] = {x, y};
    g[x].push_back(y);
    g[y].push_back(x);
  }
  int m;
  cin >> m;
  vector<pair<int, pair<int, int>>> v;
  for (int i = 0; i < m; i++) {
    int a, b, r;
    cin >> a >> b >> r;
    v.push_back({r, {a, b}});
  }
  sort(v.begin(), v.end(), greater<pair<int, pair<int, int>>>());
  bool f = true;
  for (int i = 0;
       i < m && (f = bfs(v[i].second.first, v[i].second.second, v[i].first));
       i++)
    ;
  if (!f)
    cout << -1 << endl;
  else {
    for (int i = 0; i < n - 1; i++)
      cout << (!d[a[i].first][a[i].second] ? 1 : d[a[i].first][a[i].second])
           << ' ';
    cout << endl;
  }
  return 0;
}
