#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 5005;
vector<pair<int, int>> G[N];
int col[N], id[N], ans[N];
int fa[N], dep[N];
void dfs(int u, int f) {
  dep[u] = dep[f] + 1;
  fa[u] = f;
  for (auto it : G[u]) {
    if (it.first == f) continue;
    id[it.first] = it.second;
    dfs(it.first, u);
  }
}
void update(int x, int y, int z) {
  while (x != y) {
    if (dep[x] < dep[y]) swap(x, y);
    col[x] = max(col[x], z);
    x = fa[x];
  }
}
int query(int x, int y) {
  int mn = 1e7;
  while (x != y) {
    if (dep[x] < dep[y]) swap(x, y);
    mn = min(mn, col[x]);
    x = fa[x];
  }
  return mn;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  for (int i = 1; i <= int(n - 1); ++i) {
    int x, y;
    cin >> x >> y;
    G[x].emplace_back(y, i);
    G[y].emplace_back(x, i);
  }
  dfs(1, 0);
  int m;
  cin >> m;
  vector<tuple<int, int, int>> q(m);
  for (int i = 0; i < int(m); ++i) {
    int x, y, z;
    cin >> x >> y >> z;
    update(x, y, z);
    q[i] = make_tuple(x, y, z);
  }
  for (auto it : q) {
    int x, y, z;
    tie(x, y, z) = it;
    if (query(x, y) != z) {
      cout << -1;
      return 0;
    }
  }
  for (int i = int(2); i <= int(n); ++i) {
    if (col[i])
      ans[id[i]] = col[i];
    else
      ans[id[i]] = 1e6;
  }
  for (int i = 1; i <= int(n - 1); ++i) cout << ans[i] << ' ';
  return 0;
}
