#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, i, j, k, l, ans = 0, flag = 0, t, p[5005], h[5005], q, col[5005];
vector<int> v[5005];
int edges[5005][5005];
void dfs(int node, int height, int parent) {
  p[node] = parent;
  h[node] = height;
  for (auto child : v[node]) {
    if (child != parent) {
      dfs(child, height + 1, node);
    }
  }
}
void update(int first, int second, int w) {
  while (first != second) {
    if (h[first] > h[second]) {
      col[edges[first][p[first]]] = w;
      first = p[first];
    } else if (h[second] > h[first]) {
      col[edges[second][p[second]]] = w;
      second = p[second];
    } else {
      col[edges[first][p[first]]] = w;
      first = p[first];
      col[edges[second][p[second]]] = w;
      second = p[second];
    }
  }
  return;
}
bool check(int first, int second, int w) {
  int cost = 1000000007;
  while (first != second) {
    if (h[first] > h[second]) {
      cost = min(cost, col[edges[first][p[first]]]);
      first = p[first];
    } else if (h[second] > h[first]) {
      cost = min(cost, col[edges[second][p[second]]]);
      second = p[second];
    } else {
      cost = min(cost, col[edges[first][p[first]]]);
      first = p[first];
      cost = min(cost, col[edges[second][p[second]]]);
      second = p[second];
    }
  }
  return cost == w;
}
vector<pair<int, pair<int, int> > > query;
void solve() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> j >> k;
    edges[j][k] = edges[k][j] = i;
    v[j].push_back(k);
    v[k].push_back(j);
    col[i] = 1000000;
  }
  dfs(1, 1, -1);
  cin >> q;
  for (int i = 0; i < q; i++) {
    int first, second, w;
    cin >> first >> second >> w;
    query.push_back({w, {first, second}});
  }
  sort((query).begin(), (query).end());
  for (auto i : query) {
    update(i.second.first, i.second.second, i.first);
  }
  for (auto i : query) {
    if (!check(i.second.first, i.second.second, i.first)) {
      cout << -1 << '\n';
      return;
    }
  }
  for (int i = 0; i < n - 1; i++) {
    cout << col[i] << " ";
  }
  cout << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int test = 1;
  while (test--) {
    solve();
  }
  return 0;
}
