#include <bits/stdc++.h>
using namespace std;
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class T>
ostream &operator<<(ostream &os, vector<T> V) {
  os << "[";
  for (auto vv : V) os << vv << ",";
  return os << "]";
}
template <class TH>
void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << '\n';
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',') cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}
const int N = 5e3 + 3;
int n, m, d[N][N], p[N];
bool vis[N];
vector<int> g[N];
vector<pair<int, int> > edges;
vector<pair<int, pair<int, int> > > q;
bool valid;
void fil(int st, int en, int col) {
  memset(p, -1, sizeof p);
  queue<int> q;
  q.push(st);
  p[st] = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    if (v == en) break;
    for (auto nb : g[v])
      if (p[nb] == -1) {
        p[nb] = v;
        q.push(nb);
      }
  }
  (st, en);
  bool done = false;
  for (int cur = en; cur != st; cur = p[cur]) {
    if (d[cur][p[cur]] == 0) {
      (cur, p[cur], col);
      d[cur][p[cur]] = d[p[cur]][cur] = col, done = true;
    } else {
      assert(d[cur][p[cur]] >= col);
      if (d[cur][p[cur]] == col) done = true;
    }
  }
  if (!done) valid = false;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
    edges.push_back(make_pair(a, b));
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    q.push_back(make_pair(c, make_pair(a, b)));
  }
  sort((q).begin(), (q).end());
  reverse((q).begin(), (q).end());
  valid = true;
  for (auto e : q) {
    int st = e.second.first, en = e.second.second, col = e.first;
    (e);
    if (st == en) {
      cout << -1 << '\n';
      return 0;
    }
    fil(st, en, col);
    if (!valid) {
      cout << -1 << '\n';
      return 0;
    }
  }
  for (auto e : edges)
    cout << (d[e.first][e.second] != 0 ? d[e.first][e.second] : 1000000) << " ";
  return 0;
}
