#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int>> adjlist;
vector<int> trace;
const int nax = 5005;
const int INF = 1e9;
int ans[nax];
struct P {
  int a, b, f;
  bool operator<(const P& other) const { return f < other.f; }
};
P passenger[nax];
int edge[nax][nax];
void dfs(int v, int p = -1) {
  trace[v] = p;
  for (int& c : adjlist[v]) {
    if (c == p) continue;
    dfs(c, v);
  }
}
void Destroy(vector<int>& v) {
  v.clear();
  v.shrink_to_fit();
}
void bye() {
  cout << -1;
  exit(0);
}
void solve() {
  cin >> n;
  adjlist.resize(n + 1);
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    adjlist[u].push_back(v);
    adjlist[v].push_back(u);
    edge[u][v] = i;
    edge[v][u] = i;
  }
  cin >> m;
  for (int i = 1; i < n; ++i) ans[i] = 1;
  for (int i = 1; i <= m; ++i) {
    cin >> passenger[i].a >> passenger[i].b >> passenger[i].f;
  }
  sort(passenger + 1, passenger + m + 1);
  for (int i = 1; i <= m; ++i) {
    int start = passenger[i].a, finish = passenger[i].b;
    int mini = passenger[i].f;
    Destroy(trace);
    trace.resize(nax);
    dfs(start);
    vector<int> path;
    for (int j = finish; j != start; j = trace[j]) {
      path.push_back(j);
    }
    path.push_back(start);
    for (int id = 0; id < ((int)path.size()) - 1; ++id) {
      int get_id = edge[path[id]][path[id + 1]];
      ans[get_id] = max(ans[get_id], mini);
    }
  }
  for (int i = 1; i <= m; ++i) {
    int start = passenger[i].a, finish = passenger[i].b;
    int mini = passenger[i].f;
    Destroy(trace);
    trace.resize(nax);
    dfs(start);
    vector<int> path;
    for (int j = finish; j != start; j = trace[j]) {
      path.push_back(j);
    }
    path.push_back(start);
    int compare = INF;
    for (int id = 0; id < ((int)path.size()) - 1; ++id) {
      int get_id = edge[path[id]][path[id + 1]];
      compare = min(compare, ans[get_id]);
    }
    if (compare != mini) {
      bye();
    }
  }
  for (int i = 1; i < n; ++i) cout << ans[i] << ' ';
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
}
