#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > v1;
vector<vector<int> > cnt;
vector<vector<int> > vis;
int dfs(int n1, int n2, int par) {
  if (n1 == n2) {
    return 1;
  }
  for (int i = 0; i < v1[n1].size(); i++) {
    int child = v1[n1][i];
    if (child == par) {
      continue;
    }
    if (dfs(child, n2, n1)) {
      cnt[n1][child]++;
      cnt[child][n1]++;
      return 1;
    }
  }
  return 0;
}
int fun(int n1, int n2, int par) {
  if (n1 == n2) {
    return 1;
  }
  for (int i = 0; i < v1[n1].size(); i++) {
    int child = v1[n1][i];
    if (child == par) {
      continue;
    }
    if (fun(child, n2, n1)) {
      cnt[n1][child]--;
      cnt[child][n1]--;
      return 1;
    }
  }
  return 0;
}
int check(int n1, int n2, int &flag, int prev, int par) {
  if (n1 == n2) {
    return 1;
  }
  for (int i = 0; i < v1[n1].size(); i++) {
    int child = v1[n1][i];
    if (child == par) {
      continue;
    }
    if (check(child, n2, flag, prev, n1)) {
      if (cnt[n1][child] == 0 ||
          (cnt[n1][child] == prev && vis[n1][child] == 1)) {
        cnt[n1][child] = prev;
        cnt[child][n1] = prev;
        vis[child][n1] = 1;
        vis[n1][child] = 1;
        flag = 1;
      }
      return 1;
    }
  }
  return 0;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  v1 = vector<vector<int> >(n + 1);
  vector<pair<int, int> > section;
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    v1[x].push_back(y);
    v1[y].push_back(x);
    pair<int, int> p1 = {x, y};
    section.push_back(p1);
  }
  cnt = vector<vector<int> >(n + 1, vector<int>(n + 1, 0));
  vis = vector<vector<int> >(n + 1, vector<int>(n + 1, 0));
  int m;
  cin >> m;
  vector<pair<int, pair<int, int> > > val_array;
  for (int i = 0; i < m; i++) {
    int x, y, c;
    cin >> x >> y >> c;
    pair<int, int> p1 = {x, y};
    pair<int, pair<int, int> > pr1 = {c, p1};
    val_array.push_back(pr1);
  }
  sort(val_array.begin(), val_array.end());
  for (int i = 0; i < m; i++) {
    dfs(val_array[i].second.first, val_array[i].second.second, -1);
  }
  int prev = -1;
  int mx = 0;
  for (int i = 0; i < m; i++) {
    int w1 = val_array[i].first;
    int x1 = val_array[i].second.first;
    int y1 = val_array[i].second.second;
    mx = max(mx, w1);
    if (prev != w1) {
      prev = w1;
      int i1 = i;
      while (i1 < m && prev == val_array[i1].first) {
        fun(val_array[i1].second.first, val_array[i1].second.second, -1);
        i1++;
      }
    }
    int flag = 0;
    check(x1, y1, flag, prev, -1);
    if (flag == 0) {
      cout << "-1\n";
      return 0;
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int x1 = section[i].first;
    int y1 = section[i].second;
    if (cnt[x1][y1] == 0) {
      cnt[x1][y1] = mx;
    }
    cout << cnt[x1][y1] << " ";
  }
}
