#include <bits/stdc++.h>   using namespace std;   const int N = 5005;   int n, m, ans[N];   pair<int, int> p[N];   vector<pair<int, int> > g[N];   void dfs(int v, int pr = -1) {     for (auto it : g[v]) {       int to = it.first;       if (to != pr) {         p[to] = make_pair(v, it.second);         dfs(to, v);       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 1; i < n; i++) {       int x, y;       cin >> x >> y;       g[x].push_back(make_pair(y, i));       g[y].push_back(make_pair(x, i));     }     cin >> m;     vector<pair<int, pair<int, int> > > e;     for (int i = 1; i <= m; i++) {       int x, y, z;       cin >> x >> y >> z;       e.push_back(make_pair(z, make_pair(x, y)));     }     sort(e.begin(), e.end());     for (auto it : e) {       int x = it.second.first, y = it.second.second, val = it.first;       dfs(x);       while (y != x) {         ans[p[y].second] = it.first;         y = p[y].first;       }     }     for (auto it : e) {       int x = it.second.first, y = it.second.second, val = it.first;       dfs(x);       int mn = 1e9;       while (y != x) {         mn = min(mn, ans[p[y].second]);         y = p[y].first;       }       if (mn != val) {         return cout <<  -1 , 0;       }     }     for (int i = 1; i < n; i++) {       cout << (ans[i] == 0 ? (int)1e6 : ans[i]) <<    ;     }   }   