#include <bits/stdc++.h>
using namespace std;
const int N = 5005, MAX = 1e6;
int n, m, id[N][N], f[N], p[N];
vector<int> adj[N], A[N];
pair<int, pair<int, int> > q[N];
void dfs(int v) {
  for (int w : adj[v]) {
    if (w == p[v]) continue;
    p[w] = v;
    dfs(w);
  }
}
bool check() {
  for (int i = 0; i < m; i++) {
    int j = 0, g = q[i].first, u = q[i].second.first, v = q[i].second.second;
    for (; j < min(A[u].size(), A[v].size()); j++) {
      if (A[u][j] == A[v][j]) continue;
      break;
    }
    int s = MAX;
    for (int k = j; k < A[u].size(); k++) s = min(s, f[A[u][k]]);
    for (int k = j; k < A[v].size(); k++) s = min(s, f[A[v][k]]);
    if (s != g) return 0;
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    id[u][v] = id[v][u] = i;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1);
  for (int v = 1; v <= n; v++) {
    int w = v;
    for (; p[w]; w = p[w]) A[v].push_back(id[w][p[w]]);
    reverse(A[v].begin(), A[v].end());
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    int u, v, g;
    cin >> u >> v >> g;
    q[i] = {g, {u, v}};
  }
  sort(q, q + m);
  for (int i = 1; i < n; i++) f[i] = MAX;
  for (int i = 0; i < m; i++) {
    int j = 0, g = q[i].first, u = q[i].second.first, v = q[i].second.second;
    for (; j < min(A[u].size(), A[v].size()); j++) {
      if (A[u][j] == A[v][j]) continue;
      break;
    }
    for (int k = j; k < A[u].size(); k++) f[A[u][k]] = g;
    for (int k = j; k < A[v].size(); k++) f[A[v][k]] = g;
  }
  if (check()) {
    for (int i = 1; i < n; i++) cout << f[i] << " ";
  } else
    cout << -1;
}
