#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + (ch ^ 48);
    ch = getchar();
  }
  return x *= f, void();
}
const int N = 5000 + 5, INF = 1e9;
int n, m, U[N], V[N], W[N], fa[N], pre[N], dep[N], qu[N], qv[N], qw[N];
vector<int> g[N];
void dfs(int u) {
  for (int e : g[u]) {
    int v = (u == U[e] ? V[e] : U[e]);
    if (v == fa[u]) continue;
    fa[v] = u, dep[v] = dep[u] + 1, pre[v] = e, dfs(v);
  }
}
inline void ckmax(int u, int v, int w) {
  if (dep[u] < dep[v]) swap(u, v);
  while (dep[u] > dep[v]) {
    W[pre[u]] = max(W[pre[u]], w);
    u = fa[u];
  }
  while (u != v) {
    W[pre[u]] = max(W[pre[u]], w);
    u = fa[u];
    W[pre[v]] = max(W[pre[v]], w);
    v = fa[v];
  }
}
inline int Min(int u, int v) {
  int ans = INF;
  if (dep[u] < dep[v]) swap(u, v);
  while (dep[u] > dep[v]) {
    ans = min(ans, W[pre[u]]);
    u = fa[u];
  }
  while (u != v) {
    ans = min(ans, W[pre[u]]);
    u = fa[u];
    ans = min(ans, W[pre[v]]);
    v = fa[v];
  }
  return ans;
}
int main() {
  read(n);
  for (int i = 1; i < n; i++) {
    read(U[i]), read(V[i]);
    g[U[i]].push_back(i);
    g[V[i]].push_back(i);
    W[i] = 1;
  }
  dfs(1);
  read(m);
  for (int i = 1; i <= m; i++) {
    read(qu[i]), read(qv[i]), read(qw[i]);
    ckmax(qu[i], qv[i], qw[i]);
  }
  for (int i = 1; i <= m; i++) {
    if (Min(qu[i], qv[i]) != qw[i]) {
      puts("-1");
      exit(0);
    }
  }
  for (int i = 1; i < n; i++) printf("%d\n", W[i]);
  return 0;
}
