#include <bits/stdc++.h>
using namespace std;
const int MAX_V = 5002;
vector<int> G[MAX_V];
int Gm[MAX_V][MAX_V];
int mini = 1e9 + 7;
bool fill_dfs(int v, int t, int w, int prev = -1) {
  if (v == t) return true;
  for (auto e : G[v]) {
    if (e == prev) continue;
    if (fill_dfs(e, t, w, v)) {
      Gm[e][v] = w, Gm[v][e] = w;
      return true;
    }
  }
  return false;
}
bool query_dfs(int v, int t, int prev = -1) {
  if (v == t) return true;
  for (auto e : G[v]) {
    if (e == prev) continue;
    if (query_dfs(e, t, v)) {
      mini = min(Gm[e][v], mini);
      return true;
    }
  }
  return false;
}
int main() {
  int n;
  cin >> n;
  n--;
  vector<pair<int, int>> input(n);
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    input[i] = {a, b};
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int m;
  cin >> m;
  vector<pair<int, pair<int, int>>> g;
  for (int i = 0; i < m; i++) {
    int a, b, v;
    cin >> a >> b >> v;
    g.push_back({v, {a, b}});
  }
  sort(g.begin(), g.end());
  for (auto e : g) fill_dfs(e.second.first, e.second.second, e.first);
  bool ans = true;
  for (auto e : g) {
    mini = 1e9 + 7;
    query_dfs(e.second.first, e.second.second);
    ans &= (e.first == mini);
  }
  if (ans) {
    for (int i = 0; i < (int)input.size(); i++) {
      cout << max(Gm[input[i].first][input[i].second], 1) << " ";
    }
    cout << endl;
  } else
    cout << -1 << endl;
  return 0;
}
