#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> adj[5000];
int p[5000] = {0}, d[5000] = {0};
int f[5000][5000] = {{0}};
vector<pair<int, int>> e;
vector<tuple<int, int, int>> ask;
bool v[5000] = {false};
pair<int, int> a;
void fk(int x, int g) {
  int y = p[x];
  if (x > y) swap(x, y);
  if (f[x][y])
    f[x][y] = max(f[x][y], g);
  else
    f[x][y] = g;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
    if (a > b) swap(a, b);
    e.push_back({a, b});
  }
  stack<int> stk;
  v[0] = true;
  stk.push(0);
  while (stk.size()) {
    int c = stk.top();
    stk.pop();
    for (int ch : adj[c]) {
      if (v[ch]) continue;
      v[ch] = true;
      p[ch] = c;
      d[ch] = d[c] + 1;
      stk.push(ch);
    }
  }
  cin >> m;
  while (m--) {
    int a, b, g;
    cin >> a >> b >> g;
    a--, b--;
    ask.push_back({a, b, g});
    if (d[a] < d[b]) swap(a, b);
    while (d[a] > d[b]) {
      fk(a, g);
      a = p[a];
    }
    while (a != b) {
      fk(a, g);
      fk(b, g);
      a = p[a];
      b = p[b];
    }
  }
  pair<int, int> y;
  for (tuple<int, int, int> abg : ask) {
    int a, b, g;
    tie(a, b, g) = abg;
    if (d[a] < d[b]) swap(a, b);
    while (d[a] > d[b]) {
      y.first = a, y.second = p[a];
      if (a > p[a]) swap(y.first, y.second);
      if (f[y.first][y.second] == g) goto nxt;
      ;
      a = p[a];
    }
    while (a != b) {
      y.first = a, y.second = p[a];
      if (a > p[a]) swap(y.first, y.second);
      if (f[y.first][y.second] == g) goto nxt;
      ;
      y.first = b, y.second = p[b];
      if (b > p[b]) swap(y.first, y.second);
      if (f[y.first][y.second] == g) goto nxt;
      ;
      a = p[a];
      b = p[b];
    }
    cout << "-1\n";
    return 0;
  nxt:;
  }
  for (pair<int, int> ed : e) {
    if (f[ed.first][ed.second])
      cout << f[ed.first][ed.second];
    else
      cout << 1;
    if (ed == *--e.end())
      cout << '\n';
    else
      cout << ' ';
  }
}
