#include <bits/stdc++.h>
using namespace std;
vector<int> adj[5050];
int f[5050][5050] = {};
bool dfs(int a, int parent, int c, vector<pair<int, int>>& edges) {
  if (a == c) return true;
  bool res = false;
  for (int b : adj[a]) {
    if (b == parent) continue;
    bool aux = dfs(b, a, c, edges);
    if (aux) edges.push_back({a, b});
    res |= aux;
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<pair<int, int>> edges;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
    edges.push_back({a, b});
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      f[i][j] = 1;
    }
  }
  int m;
  cin >> m;
  vector<tuple<int, int, int>> query;
  for (int i = 0; i < m; i++) {
    int a, b, g;
    cin >> a >> b >> g;
    query.push_back(make_tuple(g, a, b));
  }
  sort(query.begin(), query.end());
  for (auto q : query) {
    int a = get<1>(q);
    int b = get<2>(q);
    int g = get<0>(q);
    vector<pair<int, int>> edges;
    dfs(a, 0, b, edges);
    for (auto e : edges) {
      int u = e.first;
      int v = e.second;
      f[u][v] = f[v][u] = max(f[u][v], g);
    }
  }
  bool ans = true;
  for (auto q : query) {
    int a = get<1>(q);
    int b = get<2>(q);
    int g = get<0>(q);
    vector<pair<int, int>> edges;
    dfs(a, 0, b, edges);
    int aux = (1 << 29);
    for (auto e : edges) {
      int u = e.first;
      int v = e.second;
      aux = min(aux, f[u][v]);
    }
    if (aux != g) ans = false;
  }
  if (!ans)
    cout << -1 << '\n';
  else {
    for (auto e : edges) {
      int a = e.first;
      int b = e.second;
      cout << f[a][b] << " ";
    }
  }
  return 0;
}
