#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <map>
#include <random>
#include <unordered_map>
typedef long long ll;
typedef unsigned long long ull;
typedef long double LD;
using namespace std;
const int INF = 2147483647;
const int INF2 = 0x3f3f3f3f;
const ll INF64 = 1e18;
const double INFD = 1e30;
const double EPS = 1e-6;
const double PI = 3.14159265;
const ll MOD = 20101009;
const int MAXN = 5005;
int n, m, k;

int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1};
int CASE = 1;

struct Query {
    int a, b, s;
};
vector<int> G[MAXN];
vector<Query> Qs;
vector<Query> edges;
int parent[MAXN], dep[MAXN], val[MAXN];
int son[MAXN], sz[MAXN], dfn[MAXN], top[MAXN], DFN;

#define ls nodes[p << 1]
#define rs nodes[p << 1 | 1]
#define self nodes[p]
struct SegTree {
    struct Node {
        int tag, minn;
    };
    Node nodes[MAXN << 2];
    void modify(int l, int r, int p, int t) {
        self.tag = max(self.tag, t);
        self.minn = max(self.minn, t);
    }
    void push_down(int l, int r, int p) {
        if (self.tag) {
            int mid = (l + r) / 2;
            modify(l, mid, p << 1, self.tag),
                modify(mid + 1, r, p << 1 | 1, self.tag);
            self.tag = 0;
        }
    }
    void push_up(int p) { self.minn = min(ls.minn, rs.minn); }
    void update(int x, int y, int l, int r, int p, int v) {
        if (x <= l && y >= r) {
            self.tag = max(self.tag, v);
            self.minn = max(self.minn, v);
            return;
        }
        push_down(l, r, p);
        int mid = (l + r) / 2;
        if (x <= mid) update(x, y, l, mid, p << 1, v);
        if (y > mid) update(x, y, mid + 1, r, p << 1 | 1, v);
        push_up(p);
    }
    int query(int x, int y, int l, int r, int p) {
        if (x <= l && y >= r) return self.minn;
        int mid = (l + r) / 2, res = INF2;
        push_down(l, r, p);
        if (x <= mid) res = min(res, query(x, y, l, mid, p << 1));
        if (y > mid) res = min(res, query(x, y, mid + 1, r, p << 1 | 1));
        return max(res, 1);
    }
};

void dfs1(int u, int p) {
    sz[u] = 1, parent[u] = p;
    int maxx = 0;
    for (auto v : G[u]) {
        if (v == p) continue;
        dep[v] = dep[u] + 1;
        dfs1(v, u);
        sz[u] += sz[v];
        if (sz[v] > maxx) maxx = sz[v], son[u] = v;
    }
}

void dfs2(int u, int p) {
    dfn[u] = ++DFN, top[u] = p;
    if (!son[u]) return;
    dfs2(son[u], p);
    for (auto v : G[u]) {
        if (v == parent[u] || v == son[u]) continue;
        dfs2(v, v);
    }
}

SegTree tree;
void apply(int u, int v, int s) {
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) swap(u, v);
        tree.update(dfn[top[u]], dfn[u], 1, n, 1, s);
        u = parent[top[u]];
    }
    if (dep[u] > dep[v]) swap(u, v);
    tree.update(dfn[u] + 1, dfn[v], 1, n, 1, s);
}

int query(int u, int v) {
    int minn = INF2;
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) swap(u, v);
        minn = min(minn, tree.query(dfn[top[u]], dfn[u], 1, n, 1));
        u = parent[top[u]];
    }
    if (dep[u] > dep[v]) swap(u, v);
    minn = min(minn, tree.query(dfn[u] + 1, dfn[v], 1, n, 1));
    return minn;
}
int main() {
#ifdef LOCALLL
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
#endif
    scanf("%d", &n);
    for (int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v), G[v].push_back(u);
        edges.push_back({u, v});
    }
    dfs1(1, 1), dfs2(1, 1);
    scanf("%d", &m);
    while (m--) {
        int a, b, s;
        scanf("%d%d%d", &a, &b, &s);
        apply(a, b, s), Qs.push_back({a, b, s});
    }
    for (auto& q : Qs) {
        if (query(q.a, q.b) != q.s) {
            printf("-1");
            return 0;
        }
    }
    for (auto& e : edges) {
        int v = dep[e.a] > dep[e.b] ? e.a : e.b;
        printf("%d ", tree.query(dfn[v], dfn[v], 1, n, 1));
    }
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <map>
#include <random>
#include <unordered_map>
typedef long long ll;
typedef unsigned long long ull;
typedef long double LD;
using namespace std;
const int INF = 2147483647;
const int INF2 = 0x3f3f3f3f;
const ll INF64 = 1e18;
const double INFD = 1e30;
const double EPS = 1e-6;
const double PI = 3.14159265;
const ll MOD = 20101009;
const int MAXN = 5005;
int n, m, k;

int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1};
int CASE = 1;

struct Query {
    int a, b, s;
};
vector<int> G[MAXN];
vector<Query> Qs;
vector<Query> edges;
int parent[MAXN], dep[MAXN], val[MAXN];
int son[MAXN], sz[MAXN], dfn[MAXN], top[MAXN], DFN;

#define ls nodes[p << 1]
#define rs nodes[p << 1 | 1]
#define self nodes[p]
struct SegTree {
    struct Node {
        int tag, minn;
    };
    Node nodes[MAXN << 2];
    void modify(int l, int r, int p, int t) {
        self.tag = max(self.tag, t);
        self.minn = max(self.minn, t);
    }
    void push_down(int l, int r, int p) {
        if (self.tag) {
            int mid = (l + r) / 2;
            modify(l, mid, p << 1, self.tag),
                modify(mid + 1, r, p << 1 | 1, self.tag);
            self.tag = 0;
        }
    }
    void push_up(int p) { self.minn = min(ls.minn, rs.minn); }
    void update(int x, int y, int l, int r, int p, int v) {
        if (x <= l && y >= r) {
            self.tag = max(self.tag, v);
            self.minn = max(self.minn, v);
            return;
        }
        push_down(l, r, p);
        int mid = (l + r) / 2;
        if (x <= mid) update(x, y, l, mid, p << 1, v);
        if (y > mid) update(x, y, mid + 1, r, p << 1 | 1, v);
        push_up(p);
    }
    int query(int x, int y, int l, int r, int p) {
        if (x <= l && y >= r) return self.minn;
        int mid = (l + r) / 2, res = INF2;
        push_down(l, r, p);
        if (x <= mid) res = min(res, query(x, y, l, mid, p << 1));
        if (y > mid) res = min(res, query(x, y, mid + 1, r, p << 1 | 1));
        return max(res, 1);
    }
};

void dfs1(int u, int p) {
    sz[u] = 1, parent[u] = p;
    int maxx = 0;
    for (auto v : G[u]) {
        if (v == p) continue;
        dep[v] = dep[u] + 1;
        dfs1(v, u);
        sz[u] += sz[v];
        if (sz[v] > maxx) maxx = sz[v], son[u] = v;
    }
}

void dfs2(int u, int p) {
    dfn[u] = ++DFN, top[u] = p;
    if (!son[u]) return;
    dfs2(son[u], p);
    for (auto v : G[u]) {
        if (v == parent[u] || v == son[u]) continue;
        dfs2(v, v);
    }
}

SegTree tree;
void apply(int u, int v, int s) {
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) swap(u, v);
        tree.update(dfn[top[u]], dfn[u], 1, n, 1, s);
        u = parent[top[u]];
    }
    if (dep[u] > dep[v]) swap(u, v);
    tree.update(dfn[u] + 1, dfn[v], 1, n, 1, s);
}

int query(int u, int v) {
    int minn = INF2;
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) swap(u, v);
        minn = min(minn, tree.query(dfn[top[u]], dfn[u], 1, n, 1));
        u = parent[top[u]];
    }
    if (dep[u] > dep[v]) swap(u, v);
    minn = min(minn, tree.query(dfn[u] + 1, dfn[v], 1, n, 1));
    return minn;
}
int main() {
#ifdef LOCALLL
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
#endif
    scanf("%d", &n);
    for (int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v), G[v].push_back(u);
        edges.push_back({u, v});
    }
    dfs1(1, 1), dfs2(1, 1);
    scanf("%d", &m);
    while (m--) {
        int a, b, s;
        scanf("%d%d%d", &a, &b, &s);
        apply(a, b, s), Qs.push_back({a, b, s});
    }
    for (auto& q : Qs) {
        if (query(q.a, q.b) != q.s) {
            printf("-1");
            return 0;
        }
    }
    for (auto& e : edges) {
        int v = dep[e.a] > dep[e.b] ? e.a : e.b;
        printf("%d ", tree.query(v, v, 1, n, 1));
    }
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <map>
#include <random>
#include <unordered_map>
typedef long long ll;
typedef unsigned long long ull;
typedef long double LD;
using namespace std;
const int INF = 2147483647;
const int INF2 = 0x3f3f3f3f;
const ll INF64 = 1e18;
const double INFD = 1e30;
const double EPS = 1e-6;
const double PI = 3.14159265;
const ll MOD = 20101009;
const int MAXN = 5005;
int n, m, k;

int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1};
int CASE = 1;

struct Query {
    int a, b, s;
};
vector<int> G[MAXN];
vector<Query> Qs;
vector<Query> edges;
int parent[MAXN], dep[MAXN], val[MAXN];

void dfs(int u, int p) {
    val[u] = 1, parent[u] = p;
    for (auto v : G[u]) {
        if (v == p) continue;
        dep[v] = dep[u] + 1;
        dfs(v, u);
    }
}

void apply(int u, int v, int s) {
    if (dep[u] < dep[v]) swap(u, v);
    while (dep[u] > dep[v]) val[u] = max(val[u], s), u = parent[u];
    while (u != v) {
        val[u] = max(val[u], s), val[v] = max(val[v], s);
        u = parent[u], v = parent[v];
    }
}

int query(int u, int v) {
    if (dep[u] < dep[v]) swap(u, v);
    int minn = INF2;
    while (dep[u] > dep[v]) minn = min(minn, val[u]), u = parent[u];
    while (u != v) {
        minn = min(minn, val[u]), minn = min(minn, val[v]);
        u = parent[u], v = parent[v];
    }
    return minn;
}
int main() {
#ifdef LOCALLL
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
#endif
    scanf("%d", &n);
    for (int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v), G[v].push_back(u);
        edges.push_back({u, v});
    }
    dfs(1, 1);
    scanf("%d", &m);
    while (m--) {
        int a, b, s;
        scanf("%d%d%d", &a, &b, &s);
        apply(a, b, s), Qs.push_back({a, b, s});
    }
    for (auto& q : Qs) {
        if (query(q.a, q.b) != q.s) {
            printf("-1");
            return 0;
        }
    }
    for (auto& e : edges) {
        printf("%d ", val[dep[e.a] > dep[e.b] ? e.a : e.b]);
    }
    return 0;
}


//https://www.cnblogs.com/shuitiangong/
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 5e3+10;
struct EDGE {
    int u, v, w;
    EDGE (int a=0, int b=0, int c=0) : u(a), v(b), w(c) {}
} lmt[maxn];
vector<P> e[maxn];
int f[maxn]; bool ok;
bool dfs(int p, int u, int goal, int w) {
    if (u == goal) return true;
    for (auto v : e[u])
        if (v.first != p && dfs(u, v.first, goal, w)) {
            if (f[v.second]==w) ok = true;
            f[v.second] = max(f[v.second], w);
            return true;
        }
    return false;
}
int main(void) {
    int n; cin >> n;
    for (int i = 0, u, v; i<n-1; ++i) {
        cin >> u >> v;
        e[u].emplace_back(v, i);
        e[v].emplace_back(u, i);
        f[i] = 1;
    }
    int m; cin >> m;
    for (int i = 0, u, v, w; i<m; ++i) {
        cin >> u >> v >> w;
        lmt[i] = EDGE(u, v, w);
        dfs(0, u, v, w);
    }
    ok = false;
    for (int i = 0; i<m; ++i) {
        dfs(0, lmt[i].u, lmt[i].v, lmt[i].w);
        if (!ok) {
            printf("-1\n");
            return 0;
        }
    }
    for (int i = 0; i<n-1; ++i) printf(i==n-2?"%d\n":"%d ", f[i]);
    return 0;
}


//https://www.cnblogs.com/shuitiangong/
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 5e3+10;
struct EDGE {
    int u, v, w;
    EDGE (int a=0, int b=0, int c=0) : u(a), v(b), w(c) {}
} lmt[maxn];
vector<P> e[maxn];
int f[maxn]; bool ok;
bool dfs(int p, int u, int goal, int w) {
    if (u == goal) return true;
    for (auto v : e[u])
        if (v.first != p && dfs(u, v.first, goal, w)) {
            f[v.second] = max(f[v.second], w);
            if (f[v.second]==w) ok = true;
            return true;
        }
    return false;
}
int main(void) {
    int n; cin >> n;
    for (int i = 0, u, v; i<n-1; ++i) {
        cin >> u >> v;
        e[u].emplace_back(v, i);
        e[v].emplace_back(u, i);
        f[i] = 1;
    }
    int m; cin >> m;
    for (int i = 0, u, v, w; i<m; ++i) {
        cin >> u >> v >> w;
        lmt[i] = EDGE(u, v, w);
        dfs(0, u, v, w);
    }
    ok = false;
    for (int i = 0; i<m; ++i) {
        dfs(0, lmt[i].u, lmt[i].v, lmt[i].w);
        if (!ok) {
            printf("-1\n");
            return 0;
        }
    }
    for (int i = 0; i<n-1; ++i) printf(i==n-2?"%d\n":"%d ", f[i]);
    return 0;
}


//https://www.cnblogs.com/shuitiangong/
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 5e3+10;
struct EDGE {
    int u, v, w;
    EDGE (int a=0, int b=0, int c=0) : u(a), v(b), w(c) {}
} lmt[maxn];
vector<P> e[maxn];
int f[maxn]; bool ok;
bool dfs(int p, int u, int goal, int w) {
    if (u == goal) return true;
    for (auto v : e[u])
        if (v.first != p && dfs(u, v.first, goal, w)) {
            if (f[v.second]!=w) ok = false;
            f[v.second] = max(f[v.second], w);
            return true;
        }
    return false;
}
int main(void) {
    int n; cin >> n;
    for (int i = 0, u, v; i<n-1; ++i) {
        cin >> u >> v;
        e[u].emplace_back(v, i);
        e[v].emplace_back(u, i);
        f[i] = 1;
    }
    int m; cin >> m;
    for (int i = 0, u, v, w; i<m; ++i) {
        cin >> u >> v >> w;
        lmt[i] = EDGE(u, v, w);
        dfs(0, u, v, w);
    }
    ok = true;
    for (int i = 0; i<m; ++i) {
        dfs(0, lmt[i].u, lmt[i].v, lmt[i].w);
        if (!ok) {
            printf("-1\n");
            return 0;
        }
    }
    for (int i = 0; i<n-1; ++i) printf(i==n-2?"%d\n":"%d ", f[i]);
    return 0;
}


//https://www.cnblogs.com/shuitiangong/
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 5e3+10;
vector<int> e[maxn]; int d[maxn], minn; P edge[maxn];
struct LMT {
    int u, v, w;
    bool operator < (const LMT &a) const {
        return w < a.w;
    }
    LMT (int a=0, int b=0, int c=0) : u(a), v(b), w(c) {}
} lmt[maxn];
bool dfs(int p, int u, int goal, int w) {
    if (u==goal) return true;
    for (auto v : e[u])
        if (v!=p && dfs(u, v, goal, w)) {
            d[v] = w;
            return true;
        }
    return false;
}
bool checker(int p, int u, int goal, int w) {
    if (u==goal) return true;
    for (auto v : e[u])
        if (v!=p && checker(u, v, goal, w)) {
            minn = min(minn, d[v]);
            return true;
        }
    return false;
}
int main(void) {
    int n; scanf("%d", &n);
    for (int i = 0, u, v; i<n-1; ++i) {
        scanf("%d%d", &u, &v);
        e[u].push_back(v);
        e[v].push_back(u);
        edge[i] = P(u,v);
    }
    int m; scanf("%d", &m);
    minn = INF;
    for (int i = 0, u, v, w; i<m; ++i) {
        scanf("%d%d%d", &u, &v, &w);
        if (u>v) swap(u,v);
        lmt[i] = LMT(u,v,w);
        minn = min(minn, w);
    }
    fill(d, d+n+1, minn);
    sort(lmt, lmt+m);
    for (int i = 0; i<m; ++i) dfs(0, lmt[i].u, lmt[i].v, lmt[i].w);
    for (int i = 0; i<m; ++i) {
        minn = INF;
        checker(0, lmt[i].u, lmt[i].v, lmt[i].w);
        if (minn != lmt[i].w) {
            printf("-1\n");
            return 0;
        }
    }
    for (int i = 0; i<n-1; ++i) printf(i==n ? "%d\n":"%d ", d[edge[i].second]);
    return 0;
}


//https://www.cnblogs.com/shuitiangong/
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 5e3+10;
vector<int> e[maxn]; int d[maxn], minn;
struct LMT {
    int u, v, w;
    bool operator < (const LMT &a) const {
        return w < a.w;
    }
    LMT (int a=0, int b=0, int c=0) : u(a), v(b), w(c) {}
} lmt[maxn];
bool dfs(int p, int u, int goal, int w) {
    if (u==goal) return true;
    for (auto v : e[u])
        if (v!=p && dfs(u, v, goal, w)) {
            d[v] = w;
            return true;
        }
    return false;
}
bool checker(int p, int u, int goal, int w) {
    if (u==goal) return true;
    for (auto v : e[u])
        if (v!=p && checker(u, v, goal, w)) {
            minn = min(minn, d[v]);
            return true;
        }
    return false;
}
int main(void) {
    int n; scanf("%d", &n);
    for (int i = 0, u, v; i<n-1; ++i) {
        scanf("%d%d", &u, &v);
        e[u].push_back(v);
        e[v].push_back(u);
    }
    int m; scanf("%d", &m);
    minn = INF;
    for (int i = 0, u, v, w; i<m; ++i) {
        scanf("%d%d%d", &u, &v, &w);
        lmt[i] = LMT(u,v,w);
        minn = min(minn, w);
    }
    fill(d, d+n+1, minn);
    sort(lmt, lmt+m);
    for (int i = 0; i<m; ++i)
        dfs(0, lmt[i].u, lmt[i].v, lmt[i].w);
    for (int i = 0; i<m; ++i) {
        minn = INF;
        checker(0, lmt[i].u, lmt[i].v, lmt[i].w);
        if (minn != lmt[i].w) {
            printf("-1\n");
            return 0;
        }
    }
    for (int i = 2; i<=n; ++i) printf(i==n ? "%d\n":"%d ", d[i]);
    return 0;
}


//https://www.cnblogs.com/shuitiangong/
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 5e3+10;
struct EDGE {
    int u, v, w;
    EDGE (int a=0, int b=0, int c=0) : u(a), v(b), w(c) {}
} lmt[maxn];
vector<P> e[maxn];
int f[maxn]; bool ok;
bool dfs(int p, int u, int goal, int w) {
    if (u == goal) return true;
    for (auto v : e[u])
        if (v.first != p && dfs(u, v.first, goal, w)) {
            f[v.second] = max(f[v.second], w);
            if (f[v.second]==w) ok = true;
            return true;
        }
    return false;
}
int main(void) {
    int n; scanf("%d", &n);
    for (int i = 0, u, v; i<n-1; ++i) {
        scanf("%d%d", &u,&v);
        e[u].emplace_back(v, i);
        e[v].emplace_back(u, i);
        f[i] = 1;
    }
    int m; scanf("%d", &m);
    for (int i = 0, u, v, w; i<m; ++i) {
        scanf("%d%d%d", &u, &v, &w);
        lmt[i] = EDGE(u, v, w);
        dfs(0, u, v, w);
    }
    for (int i = 0; i<m; ++i) {
        ok = false;
        dfs(0, lmt[i].u, lmt[i].v, lmt[i].w);
        if (!ok) {
            printf("-1\n");
            return 0;
        }
    }
    for (int i = 0; i<n-1; ++i) printf(i==n-2?"%d\n":"%d ", f[i]);
    return 0;
}


//https://www.cnblogs.com/shuitiangong/
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 5e3+10;
struct EDGE {
    int u, v, w;
    EDGE (int a=0, int b=0, int c=0) : u(a), v(b), w(c) {}
} lmt[maxn];
vector<P> e[maxn];
int f[maxn]; bool ok;
bool dfs(int p, int u, int goal, int w) {
    if (u == goal) return true;
    for (auto v : e[u])
        if (v.first != p && dfs(u, v.first, goal, w)) {
            f[v.second] = max(f[v.second], w);
            if (f[v.second]==w) ok = true;
            return true;
        }
    return false;
}
int main(void) {
    int n; cin >> n;
    for (int i = 0, u, v; i<n-1; ++i) {
        cin >> u >> v;
        e[u].emplace_back(v, i);
        e[v].emplace_back(u, i);
        f[i] = 1;
    }
    int m; cin >> m;
    for (int i = 0, u, v, w; i<m; ++i) {
        cin >> u >> v >> w;
        lmt[i] = EDGE(u, v, w);
        dfs(0, u, v, w);
    }
    for (int i = 0; i<m; ++i) {
        ok = false;
        dfs(0, lmt[i].u, lmt[i].v, lmt[i].w);
        if (!ok) {
            printf("-1\n");
            return 0;
        }
    }
    for (int i = 0; i<n-1; ++i) printf(i==n-2?"%d\n":"%d ", f[i]);
    return 0;
}


//https://www.cnblogs.com/shuitiangong/
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 5e3+10;
vector<int> e[maxn]; int d[maxn], minn;
struct LMT {
    int u, v, w;
    bool operator < (const LMT &a) const {
        return w < a.w;
    }
    LMT (int a=0, int b=0, int c=0) : u(a), v(b), w(c) {}
} lmt[maxn];
bool dfs(int p, int u, int goal, int w) {
    if (u==goal) return true;
    for (auto v : e[u])
        if (v!=p && dfs(u, v, goal, w)) {
            d[v] = w;
            return true;
        }
    return false;
}
bool checker(int p, int u, int goal, int w) {
    if (u==goal) return true;
    for (auto v : e[u])
        if (v!=p && checker(u, v, goal, w)) {
            minn = min(minn, d[v]);
            return true;
        }
    return false;
}
int main(void) {
    int n; scanf("%d", &n);
    for (int i = 0, u, v; i<n-1; ++i) {
        scanf("%d%d", &u, &v);
        e[u].push_back(v);
        e[v].push_back(u);
    }
    int m; scanf("%d", &m);
    minn = INF;
    for (int i = 0, u, v, w; i<m; ++i) {
        scanf("%d%d%d", &u, &v, &w);
        if (u>v) swap(u,v);
        lmt[i] = LMT(u,v,w);
        minn = min(minn, w);
    }
    fill(d, d+n+1, minn);
    sort(lmt, lmt+m);
    for (int i = 0; i<m; ++i) dfs(0, lmt[i].u, lmt[i].v, lmt[i].w);
    for (int i = 0; i<m; ++i) {
        minn = INF;
        checker(0, lmt[i].u, lmt[i].v, lmt[i].w);
        if (minn != lmt[i].w) {
            printf("-1\n");
            return 0;
        }
    }
    for (int i = 2; i<=n; ++i) printf(i==n ? "%d\n":"%d ", d[i]);
    return 0;
}


//https://www.cnblogs.com/shuitiangong/
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 5e3+10;
vector<int> e[maxn]; int d[maxn], minn; P edge[maxn];
struct LMT {
    int u, v, w;
    bool operator < (const LMT &a) const {
        return w < a.w;
    }
    LMT (int a=0, int b=0, int c=0) : u(a), v(b), w(c) {}
} lmt[maxn];
bool dfs(int p, int u, int goal, int w) {
    if (u==goal) return true;
    for (auto v : e[u])
        if (v!=p && dfs(u, v, goal, w)) {
            d[v] = w;
            return true;
        }
    return false;
}
bool checker(int p, int u, int goal, int w) {
    if (u==goal) return true;
    for (auto v : e[u])
        if (v!=p && checker(u, v, goal, w)) {
            minn = min(minn, d[v]);
            return true;
        }
    return false;
}
int main(void) {
    int n; scanf("%d", &n);
    for (int i = 0, u, v; i<n-1; ++i) {
        scanf("%d%d", &u, &v);
        if (u>v) swap(u, v);
        e[u].push_back(v);
        e[v].push_back(u);
        edge[i] = P(u,v);
    }
    int m; scanf("%d", &m);
    minn = INF;
    for (int i = 0, u, v, w; i<m; ++i) {
        scanf("%d%d%d", &u, &v, &w);
        if (u>v) swap(u,v);
        lmt[i] = LMT(u,v,w);
        minn = min(minn, w);
    }
    fill(d, d+n+1, minn);
    sort(lmt, lmt+m);
    for (int i = 0; i<m; ++i) dfs(0, lmt[i].u, lmt[i].v, lmt[i].w);
    for (int i = 0; i<m; ++i) {
        minn = INF;
        checker(0, lmt[i].u, lmt[i].v, lmt[i].w);
        if (minn != lmt[i].w) {
            printf("-1\n");
            return 0;
        }
    }
    for (int i = 0; i<n-1; ++i) printf(i==n ? "%d\n":"%d ", d[edge[i].second]);
    return 0;
}


//https://www.cnblogs.com/shuitiangong/
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 5e3+10;
struct EDGE {
    int u, v, w;
    EDGE (int a=0, int b=0, int c=0) : u(a), v(b), w(c) {}
} lmt[maxn];
vector<P> e[maxn];
int f[maxn]; bool ok;
bool dfs(int p, int u, int goal, int w) {
    if (u == goal) return true;
    for (auto v : e[u])
        if (v.first != p && dfs(u, v.first, goal, w)) {
            if (f[v.second]<w) ok = false;
            f[v.second] = max(f[v.second], w);
            return true;
        }
    return false;
}
int main(void) {
    int n; cin >> n;
    for (int i = 0, u, v; i<n-1; ++i) {
        cin >> u >> v;
        e[u].emplace_back(v, i);
        e[v].emplace_back(u, i);
        f[i] = 1;
    }
    int m; cin >> m;
    for (int i = 0, u, v, w; i<m; ++i) {
        cin >> u >> v >> w;
        lmt[i] = EDGE(u, v, w);
        dfs(0, u, v, w);
    }
    ok = true;
    for (int i = 0; i<m; ++i) {
        dfs(0, lmt[i].u, lmt[i].v, lmt[i].w);
        if (!ok) {
            printf("-1\n");
            return 0;
        }
    }
    for (int i = 0; i<n-1; ++i) printf(i==n-2?"%d\n":"%d ", f[i]);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX_V = 5002;
vector<int> G[MAX_V];
int Gm[MAX_V][MAX_V];
int mini = 1e9 + 7;

bool fill_dfs (int v, int t, int w, int prev = -1)
{
	//cout << "V = "<< v << " T = " << t << endl;
	if (v == t) return true;

	for (auto e : G[v]) {
		if (e == prev) continue;
		if (fill_dfs (e, t, w, v)) {
			Gm[e][v] = w, Gm[v][e] = w;
			return true;
		}
	}
	return false;
}

bool query_dfs (int v, int t, int prev = -1)
{
	if (v == t) return true;

	for (auto e : G[v]) {
		if (e == prev) continue;
		if (query_dfs (e, t, v)) {
			mini = min (Gm[e][v], mini);
			return true;
		}
	}
	return false;
}

int main ()
{
	int n;
	cin >> n;
	n--;

	vector<pair<int, int>> input (n);
	for (int i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		input[i] = {a, b};
		G[a].push_back (b);
		G[b].push_back (a);
	}

	int m;
	cin >> m;
	vector<pair<int, pair<int, int>>> g;
	for (int i = 0; i < m; i++) {
		int a, b, v;
		cin >> a >> b >> v;
		g.push_back ({v, {a, b}});
	}
	sort (g.begin (), g.end ());

	//cout << "FILL DFS" << endl;
	for (auto e : g)
		fill_dfs (e.second.first, e.second.second, e.first);

	bool ans = true;
	for (auto e : g) {
		mini = 1e9 + 7;
		query_dfs (e.second.first, e.second.second);
		ans &= (e.first == mini);
	}
	if (ans) {
		for (int i = 0; i < (int) input.size (); i++) {
			cout << max (Gm[input[i].first][input[i].second], 1) << " ";
		}
		cout << endl;
	}
	else
		cout << -1 << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX_V = 5002;
vector<int> G[MAX_V];
int Gm[MAX_V][MAX_V];
int mini = 1e9 + 7;

inline bool fill_dfs (int v, int t, int w, int prev = -1)
{
	//cout << "V = "<< v << " T = " << t << endl;
	if (v == t) return true;

	for (auto e : G[v]) {
		if (e == prev) continue;
		if (fill_dfs (e, t, w, v)) {
			Gm[e][v] = w, Gm[v][e] = w;
			return true;
		}
	}
	return false;
}

inline bool query_dfs (int v, int t, int prev = -1)
{
	if (v == t) return true;

	for (auto e : G[v]) {
		if (e == prev) continue;
		if (query_dfs (e, t, v)) {
			mini = min (Gm[e][v], mini);
			return true;
		}
	}
	return false;
}

int main ()
{
	ios::sync_with_stdio (false);
	cin.tie(0);

	int n;
	cin >> n;
	n--;

	vector<pair<int, int>> input (n);
	for (int i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		input[i] = {a, b};
		G[a].push_back (b);
		G[b].push_back (a);
	}

	int m;
	cin >> m;
	vector<pair<int, pair<int, int>>> g;
	for (int i = 0; i < m; i++) {
		int a, b, v;
		cin >> a >> b >> v;
		g.push_back ({v, {a, b}});
	}
	sort (g.begin (), g.end ());

	//cout << "FILL DFS" << endl;
	for (auto e : g)
		fill_dfs (e.second.first, e.second.second, e.first);

	bool ans = true;
	for (auto e : g) {
		mini = 1e9 + 7;
		query_dfs (e.second.first, e.second.second);
		ans &= (e.first == mini);
	}
	if (ans) {
		for (int i = 0; i < (int) input.size (); i++) {
			cout << max (Gm[input[i].first][input[i].second], 1) << " ";
		}
		cout << endl;
	}
	else
		cout << -1 << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX_V = 5002;
vector<int> G[MAX_V];
int Gm[MAX_V][MAX_V];
int mini = 1e9 + 7;

bool fill_dfs (int v, int t, int w, int prev = -1)
{
	//cout << "V = "<< v << " T = " << t << endl;
	if (v == t) return true;

	for (auto e : G[v]) {
		if (e == prev) continue;
		if (fill_dfs (e, t, w, v)) {
			Gm[e][v] = w, Gm[v][e] = w;
			return true;
		}
	}
	return false;
}

bool query_dfs (int v, int t, int prev = -1)
{
	if (v == t) return true;

	for (auto e : G[v]) {
		if (e == prev) continue;
		if (query_dfs (e, t, v)) {
			mini = min (Gm[e][v], mini);
			return true;
		}
	}
	return false;
}

int main ()
{
	ios::sync_with_stdio (false);
	cin.tie(0);

	int n;
	cin >> n;
	n--;

	vector<pair<int, int>> input (n);
	for (int i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		input[i] = {a, b};
		G[a].push_back (b);
		G[b].push_back (a);
	}

	int m;
	cin >> m;
	vector<pair<int, pair<int, int>>> g;
	for (int i = 0; i < m; i++) {
		int a, b, v;
		cin >> a >> b >> v;
		g.push_back ({v, {a, b}});
	}
	sort (g.begin (), g.end ());

	//cout << "FILL DFS" << endl;
	for (auto e : g)
		fill_dfs (e.second.first, e.second.second, e.first);

	bool ans = true;
	for (auto e : g) {
		mini = 1e9 + 7;
		query_dfs (e.second.first, e.second.second);
		ans &= (e.first == mini);
	}
	if (ans) {
		for (int i = 0; i < (int) input.size (); i++) {
			cout << max (Gm[input[i].first][input[i].second], 1) << " ";
		}
		cout << endl;
	}
	else
		cout << -1 << endl;
	return 0;
}


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cmath>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include<sstream>
#include<fstream>
#define ZERO 1e-6
#define pii pair<int,int>
#define p(s) printf("%lld\n",s)
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
const int MAXN=5e3+10;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;

inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
	{
		if(ch=='-')
		{
			f=-1;
		}
		ch=getchar();
	}
    while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
    return x*f;
}

inline void write(int x)
{
	char F[200];
	int tmp=x>0?x:-x ;
	if(x<0)
	{
		putchar('-');
	}
	int cnt=0 ;
	while(tmp>0)
	{
		F[cnt++]=tmp%10+'0';
		tmp/=10;
	}
	while(cnt>0)
	{
		putchar(F[--cnt]);
	}
}

struct Edge
{
	int to;
	int val;
	int next;
}e[MAXN<<1];

int N,M;
int tot;
int head[MAXN];
int a[MAXN];
int b[MAXN];
int g[MAXN];
int pa[MAXN][MAXN];

void addEdge(int u,int v)
{
	e[tot].to=v;
	e[tot].val=1;
	e[tot].next=head[u];
	head[u]=tot++;
}

bool dfs(int pa,int From,int To,int Val)
{
	if(From==To)
	{
		return true;
	}
	for(int i=head[From];i!=-1;i=e[i].next)
	{
		if(e[i].to==pa)
		{
			continue;
		}
		if(dfs(From,e[i].to,To,Val))
		{
			if(e[i].val<Val)
			{
				e[i].val=Val;
				e[i^1].val=Val;
			}
			return true;
		}
	}
	return false;
}

bool check(int pa,int From,int To,int & Val)
{
	if(From==To)
	{
		return true;
	}
	for(int i=head[From];i!=-1;i=e[i].next)
	{
		if(e[i].to==pa)
		{
			continue;
		}
		if(check(From,e[i].to,To,Val))
		{
			Val=min(Val,e[i].val);
			return true;
		}
	}
	return false;
}

int main()
{
	int u,v;
	int temp;
	bool able;

	while(~scanf("%d",&N))
	{
		tot=0;
		memset(head,-1,sizeof(head));
		for(int i=2;i<=N;i++)
		{
			u=read();
			v=read();
			addEdge(u,v);
			addEdge(v,u);
		}
		scanf("%d",&M);
		for(int i=0;i<M;i++)
		{
			a[i]=read();
			b[i]=read();
			g[i]=read();
			pa[i][a[i]]=-1;
			dfs(-1,a[i],b[i],g[i]);
		}
		able=true;
		for(int i=0;i<M&&able;i++)
		{
			temp=INF;
			check(-1,a[i],b[i],temp);
			if(temp!=g[i])
			{
				able=false;
				break;
			}
		}
		if(able)
		{
			for(int i=0;i<tot;i+=2)
			{
				write(e[i].val);
				printf(" ");
			}
			printf("\n");
		}
		else
		{
			printf("-1\n");
		}
	}

	return 0;
}
/*

*/


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cmath>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include<sstream>
#include<fstream>
#define ZERO 1e-6
#define pii pair<int,int>
#define p(s) printf("%lld\n",s)
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
const int MAXN=5e3+10;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;

inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
	{
		if(ch=='-')
		{
			f=-1;
		}
		ch=getchar();
	}
    while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
    return x*f;
}

inline void write(int x)
{
	char F[200];
	int tmp=x>0?x:-x ;
	if(x<0)
	{
		putchar('-');
	}
	int cnt=0 ;
	while(tmp>0)
	{
		F[cnt++]=tmp%10+'0';
		tmp/=10;
	}
	while(cnt>0)
	{
		putchar(F[--cnt]);
	}
}

struct Edge
{
	int From;
	int To;
}e[MAXN];

int N,M;
map<pii,int> mp;
vector<int> E[MAXN];
int a[MAXN];
int b[MAXN];
int g[MAXN];
int pa[MAXN][MAXN];

void bfs(int id,int From,int To,int Val)
{
	queue<int> V;
	int now;

	while(!V.empty())
	{
		V.pop();
	}
	V.push(From);
	pa[id][From]=-1;
	while(!V.empty())
	{
		now=V.front();
		V.pop();
		if(now==To)
		{
			break;
		}
		for(int i=0;i<E[now].size();i++)
		{
			if(E[now][i]!=pa[id][now])
			{
				V.push(E[now][i]);
				pa[id][E[now][i]]=now;
			}
		}
	}
	now=To;
	while(~pa[id][now])
	{
		if(mp[pii(now,pa[id][now])]<Val)
		{
			mp[pii(now,pa[id][now])]=Val;
			mp[pii(pa[id][now],now)]=Val;
		}
		now=pa[id][now];
	}
}

bool check(int id,int From,int To,int Val)
{
	int now;
	int mini;

	now=To;
	mini=INF;
	while(~pa[id][now])
	{
		mini=min(mini,mp[pii(now,pa[id][now])]);
		now=pa[id][now];
	}

	return mini==Val?true:false;
}

int main()
{
	bool able;

	while(~scanf("%d",&N))
	{
		mp.clear();
		for(int i=2;i<=N;i++)
		{
			e[i].From=read();
			e[i].To=read();
			E[e[i].From].push_back(e[i].To);
			E[e[i].To].push_back(e[i].From);
			mp[pii(e[i].From,e[i].To)]=1;
			mp[pii(e[i].To,e[i].From)]=1;
		}
		scanf("%d",&M);
		for(int i=0;i<M;i++)
		{
			a[i]=read();
			b[i]=read();
			g[i]=read();
			bfs(i,a[i],b[i],g[i]);
		}
		able=true;
		for(int i=0;i<M&&able;i++)
		{
			able=check(i,a[i],b[i],g[i]);
		}
		if(able)
		{
			for(int i=2;i<=N;i++)
			{
				write(mp[pii(e[i].From,e[i].To)]);
				putchar(i==N?'\n':' ');
			}
		}
		else
		{
			printf("-1\n");
		}
	}

	return 0;
}
/*

*/


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cmath>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<sstream>
#include<fstream>
#define ZERO 1e-6
#define pii pair<int,int>
#define p(s) printf("%lld\n",s)
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
const int MAXN=5e3+10;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;

struct Edge
{
	int From;
	int To;
}e[MAXN];

int N,M;
map<pii,int> mp;
vector<int> E[MAXN];
int a[MAXN];
int b[MAXN];
int g[MAXN];
int pa[MAXN][MAXN];
bool visited[MAXN];

void bfs(int id,int From,int To,int Val)
{
	queue<int> V;
	int now;

	while(!V.empty())
	{
		V.pop();
	}
	V.push(From);
	pa[id][From]=-1;
	for(int i=1;i<=N;i++)
	{
		visited[i]=false;
	}
	visited[From]=true;
	while(!V.empty())
	{
		now=V.front();
		V.pop();
		if(now==To)
		{
			break;
		}
		for(int i=0;i<E[now].size();i++)
		{
			if(!visited[E[now][i]])
			{
				visited[E[now][i]]=true;
				V.push(E[now][i]);
				pa[id][E[now][i]]=now;
			}
		}
	}
	now=To;
	while(~pa[id][now])
	{
		if(mp[pii(now,pa[id][now])]<Val)
		{
			mp[pii(now,pa[id][now])]=Val;
			mp[pii(pa[id][now],now)]=Val;
		}
		now=pa[id][now];
	}
}

bool check(int id,int From,int To,int Val)
{
	int now;
	int mini;

	now=To;
	mini=INF;
	while(~pa[id][now])
	{
		mini=min(mini,mp[pii(now,pa[id][now])]);
		now=pa[id][now];
	}

	return mini==Val?true:false;
}

int main()
{
	bool able;

	while(~scanf("%d",&N))
	{
		mp.clear();
		for(int i=2;i<=N;i++)
		{
			scanf("%d%d",&e[i].From,&e[i].To);
			E[e[i].From].push_back(e[i].To);
			E[e[i].To].push_back(e[i].From);
			mp[pii(e[i].From,e[i].To)]=1;
			mp[pii(e[i].To,e[i].From)]=1;
		}
		scanf("%d",&M);
		for(int i=0;i<M;i++)
		{
			scanf("%d%d%d",&a[i],&b[i],&g[i]);
			bfs(i,a[i],b[i],g[i]);
		}
		able=true;
		for(int i=0;i<M&&able;i++)
		{
			able=check(i,a[i],b[i],g[i]);
		}
		if(able)
		{
			for(int i=2;i<=N;i++)
			{
				printf("%d%c",mp[pii(e[i].From,e[i].To)],i==N?'\n':' ');
			}
		}
		else
		{
			printf("-1\n");
		}
	}

	return 0;
}
/*

*/


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cmath>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include<sstream>
#include<fstream>
#define ZERO 1e-6
#define pii pair<int,int>
#define p(s) printf("%lld\n",s)
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
const int MAXN=5e3+10;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;

inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
	{
		if(ch=='-')
		{
			f=-1;
		}
		ch=getchar();
	}
    while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
    return x*f;
}

inline void write(int x)
{
	char F[200];
	int tmp=x>0?x:-x ;
	if(x<0)
	{
		putchar('-');
	}
	int cnt=0 ;
	while(tmp>0)
	{
		F[cnt++]=tmp%10+'0';
		tmp/=10;
	}
	while(cnt>0)
	{
		putchar(F[--cnt]);
	}
}

struct Edge
{
	int From;
	int To;
}e[MAXN];

int N,M;
map<pii,int> mp;
vector<int> E[MAXN];
int a[MAXN];
int b[MAXN];
int g[MAXN];
int pa[MAXN][MAXN];
bool visited[MAXN];

void bfs(int id,int From,int To,int Val)
{
	queue<int> V;
	int now;

	while(!V.empty())
	{
		V.pop();
	}
	V.push(From);
	pa[id][From]=-1;
	for(int i=1;i<=N;i++)
	{
		visited[i]=false;
	}
	visited[From]=true;
	while(!V.empty())
	{
		now=V.front();
		V.pop();
		if(now==To)
		{
			break;
		}
		for(int i=0;i<E[now].size();i++)
		{
			if(!visited[E[now][i]])
			{
				visited[E[now][i]]=true;
				V.push(E[now][i]);
				pa[id][E[now][i]]=now;
			}
		}
	}
	now=To;
	while(~pa[id][now])
	{
		if(mp[pii(now,pa[id][now])]<Val)
		{
			mp[pii(now,pa[id][now])]=Val;
			mp[pii(pa[id][now],now)]=Val;
		}
		now=pa[id][now];
	}
}

bool check(int id,int From,int To,int Val)
{
	int now;
	int mini;

	now=To;
	mini=INF;
	while(~pa[id][now])
	{
		mini=min(mini,mp[pii(now,pa[id][now])]);
		now=pa[id][now];
	}

	return mini==Val?true:false;
}

int main()
{
	bool able;

	while(~scanf("%d",&N))
	{
		mp.clear();
		for(int i=2;i<=N;i++)
		{
			e[i].From=read();
			e[i].To=read();
			E[e[i].From].push_back(e[i].To);
			E[e[i].To].push_back(e[i].From);
			mp[pii(e[i].From,e[i].To)]=1;
			mp[pii(e[i].To,e[i].From)]=1;
		}
		scanf("%d",&M);
		for(int i=0;i<M;i++)
		{
			a[i]=read();
			b[i]=read();
			g[i]=read();
			bfs(i,a[i],b[i],g[i]);
		}
		able=true;
		for(int i=0;i<M&&able;i++)
		{
			able=check(i,a[i],b[i],g[i]);
		}
		if(able)
		{
			for(int i=2;i<=N;i++)
			{
				write(mp[pii(e[i].From,e[i].To)]);
				putchar(i==N?'\n':' ');
			}
		}
		else
		{
			printf("-1\n");
		}
	}

	return 0;
}
/*

*/


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cmath>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include<sstream>
#include<fstream>
#define ZERO 1e-6
#define pii pair<int,int>
#define p(s) printf("%lld\n",s)
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
const int MAXN=5e3+10;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;

inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
	{
		if(ch=='-')
		{
			f=-1;
		}
		ch=getchar();
	}
    while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
    return x*f;
}

inline void write(int x)
{
	char F[200];
	int tmp=x>0?x:-x ;
	if(x<0)
	{
		putchar('-');
	}
	int cnt=0 ;
	while(tmp>0)
	{
		F[cnt++]=tmp%10+'0';
		tmp/=10;
	}
	while(cnt>0)
	{
		putchar(F[--cnt]);
	}
}

struct Edge
{
	int From;
	int To;
}e[MAXN];

int N,M;
map<pii,int> mp;
vector<int> E[MAXN];
int a[MAXN];
int b[MAXN];
int g[MAXN];
int pa[MAXN][MAXN];

bool dfs(int id,int From,int To,int Val)
{
	if(From==To)
	{
		return true;
	}
	for(int i=0;i<E[From].size();i++)
	{
		if(E[From][i]!=pa[id][From])
		{
			pa[id][E[From][i]]=From;
			if(dfs(id,E[From][i],To,Val))
			{
				if(mp[pii(From,E[From][i])]<Val)
				{
					mp[pii(From,E[From][i])]=Val;
					mp[pii(E[From][i],From)]=Val;
				}
				return true;
			}
		}
	}
	return false;
}

bool check(int id,int From,int To,int Val)
{
	int now;
	int mini;

	now=To;
	mini=INF;
	while(~pa[id][now])
	{
		mini=min(mini,mp[pii(now,pa[id][now])]);
		now=pa[id][now];
		if(mini<Val)
		{
			return false;
		}
	}

	return mini==Val?true:false;
}

int main()
{
	bool able;

	while(~scanf("%d",&N))
	{
		mp.clear();
		for(int i=2;i<=N;i++)
		{
			e[i].From=read();
			e[i].To=read();
			E[e[i].From].push_back(e[i].To);
			E[e[i].To].push_back(e[i].From);
			mp[pii(e[i].From,e[i].To)]=1;
			mp[pii(e[i].To,e[i].From)]=1;
		}
		scanf("%d",&M);
		for(int i=0;i<M;i++)
		{
			a[i]=read();
			b[i]=read();
			g[i]=read();
			pa[i][a[i]]=-1;
			dfs(i,a[i],b[i],g[i]);
		}
		able=true;
		for(int i=0;i<M&&able;i++)
		{
			able=check(i,a[i],b[i],g[i]);
		}
		if(able)
		{
			for(int i=2;i<=N;i++)
			{
				write(mp[pii(e[i].From,e[i].To)]);
				putchar(i==N?'\n':' ');
			}
		}
		else
		{
			printf("-1\n");
		}
	}

	return 0;
}
/*

*/


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cmath>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include<sstream>
#include<fstream>
#define ZERO 1e-6
#define pii pair<int,int>
#define p(s) printf("%lld\n",s)
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
const int MAXN=5e3+10;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;

inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
	{
		if(ch=='-')
		{
			f=-1;
		}
		ch=getchar();
	}
    while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
    return x*f;
}

inline void write(int x)
{
	char F[200];
	int tmp=x>0?x:-x ;
	if(x<0)
	{
		putchar('-');
	}
	int cnt=0 ;
	while(tmp>0)
	{
		F[cnt++]=tmp%10+'0';
		tmp/=10;
	}
	while(cnt>0)
	{
		putchar(F[--cnt]);
	}
}

struct Edge
{
	int From;
	int To;
}e[MAXN];

int N,M;
map<pii,int> mp;
vector<int> E[MAXN];
int a[MAXN];
int b[MAXN];
int g[MAXN];
int pa[MAXN][MAXN];

void bfs(int id,int From,int To,int Val)
{
	queue<int> V;
	int now;

	while(!V.empty())
	{
		V.pop();
	}
	V.push(From);
	pa[id][From]=-1;
	while(!V.empty())
	{
		now=V.front();
		V.pop();
		if(now==To)
		{
			break;
		}
		for(int i=0;i<E[now].size();i++)
		{
			if(E[now][i]!=pa[id][now])
			{
				V.push(E[now][i]);
				pa[id][E[now][i]]=now;
			}
		}
	}
	now=To;
	while(~pa[id][now])
	{
		if(mp[pii(now,pa[id][now])]<Val)
		{
			mp[pii(now,pa[id][now])]=Val;
			mp[pii(pa[id][now],now)]=Val;
		}
		now=pa[id][now];
	}
}

bool check(int id,int From,int To,int Val)
{
	int now;
	int mini;

	now=To;
	mini=INF;
	while(~pa[id][now])
	{
		mini=min(mini,mp[pii(now,pa[id][now])]);
		now=pa[id][now];
		if(mini<Val)
		{
			return false;
		}
	}

	return mini==Val?true:false;
}

int main()
{
	bool able;

	while(~scanf("%d",&N))
	{
		mp.clear();
		for(int i=2;i<=N;i++)
		{
			e[i].From=read();
			e[i].To=read();
			E[e[i].From].push_back(e[i].To);
			E[e[i].To].push_back(e[i].From);
			mp[pii(e[i].From,e[i].To)]=1;
			mp[pii(e[i].To,e[i].From)]=1;
		}
		scanf("%d",&M);
		for(int i=0;i<M;i++)
		{
			a[i]=read();
			b[i]=read();
			g[i]=read();
			bfs(i,a[i],b[i],g[i]);
		}
		able=true;
		for(int i=0;i<M&&able;i++)
		{
			able=check(i,a[i],b[i],g[i]);
		}
		if(able)
		{
			for(int i=2;i<=N;i++)
			{
				write(mp[pii(e[i].From,e[i].To)]);
				putchar(i==N?'\n':' ');
			}
		}
		else
		{
			printf("-1\n");
		}
	}

	return 0;
}
/*

*/


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cmath>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<sstream>
#include<fstream>
#define ZERO 1e-6
#define pii pair<int,int>
#define p(s) printf("%lld\n",s)
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
const int MAXN=1e4+10;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;

struct Edge
{
	int From;
	int To;
}e[MAXN];

int N,M;
map<pii,int> mp;
vector<int> E[MAXN];
int a[MAXN];
int b[MAXN];
int g[MAXN];
int pa[MAXN];
bool visited[MAXN];

void bfs(int From,int To,int Val)
{
	queue<int> V;
	int now;

	while(!V.empty())
	{
		V.pop();
	}
	V.push(From);
	pa[From]=-1;
	for(int i=1;i<=N;i++)
	{
		visited[i]=false;
	}
	visited[From]=true;
	while(!V.empty())
	{
		now=V.front();
		V.pop();
		if(now==To)
		{
			break;
		}
		for(int i=0;i<E[now].size();i++)
		{
			if(!visited[E[now][i]])
			{
				visited[E[now][i]]=true;
				V.push(E[now][i]);
				pa[E[now][i]]=now;
			}
		}
	}
	now=To;
	while(~pa[now])
	{
		if(mp[pii(now,pa[now])]<Val)
		{
			mp[pii(now,pa[now])]=Val;
			mp[pii(pa[now],now)]=Val;
		}
		now=pa[now];
	}
}

bool check(int From,int To,int Val)
{
	queue<int> V;
	int now;
	int mini;

	while(!V.empty())
	{
		V.pop();
	}
	V.push(From);
	pa[From]=-1;
	for(int i=1;i<=N;i++)
	{
		visited[i]=false;
	}
	visited[From]=true;
	while(!V.empty())
	{
		now=V.front();
		V.pop();
		if(now==To)
		{
			break;
		}
		for(int i=0;i<E[now].size();i++)
		{
			if(!visited[E[now][i]])
			{
				visited[E[now][i]]=true;
				V.push(E[now][i]);
				pa[E[now][i]]=now;
			}
		}
	}
	now=To;
	mini=INF;
	while(~pa[now])
	{
		mini=min(mini,mp[pii(now,pa[now])]);
		now=pa[now];
	}

	return mini==Val?true:false;
}

int main()
{
	bool able;

	while(~scanf("%d",&N))
	{
		mp.clear();
		for(int i=2;i<=N;i++)
		{
			scanf("%d%d",&e[i].From,&e[i].To);
			E[e[i].From].push_back(e[i].To);
			E[e[i].To].push_back(e[i].From);
			mp[pii(e[i].From,e[i].To)]=1;
			mp[pii(e[i].To,e[i].From)]=1;
		}
		scanf("%d",&M);
		for(int i=0;i<M;i++)
		{
			scanf("%d%d%d",&a[i],&b[i],&g[i]);
			bfs(a[i],b[i],g[i]);
		}
		able=true;
		for(int i=0;i<M&&able;i++)
		{
			able=check(a[i],b[i],g[i]);
		}
		if(able)
		{
			for(int i=2;i<=N;i++)
			{
				printf("%d%c",mp[pii(e[i].From,e[i].To)],i==N?'\n':' ');
			}
		}
		else
		{
			printf("-1\n");
		}
	}

	return 0;
}
/*

*/


#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cmath>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<sstream>
#include<fstream>
#define ZERO 1e-6
#define pii pair<int,int>
#define p(s) printf("%lld\n",s)
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
const int MAXN=5e3+10;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;

ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
	{
		if(ch=='-')
		{
			f=-1;
		}
		ch=getchar();
	}
    while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
    return x*f;
}

struct Edge
{
	int From;
	int To;
}e[MAXN];

int N,M;
map<pii,int> mp;
vector<int> E[MAXN];
int a[MAXN];
int b[MAXN];
int g[MAXN];
int pa[MAXN][MAXN];
bool visited[MAXN];

void bfs(int id,int From,int To,int Val)
{
	queue<int> V;
	int now;

	while(!V.empty())
	{
		V.pop();
	}
	V.push(From);
	pa[id][From]=-1;
	for(int i=1;i<=N;i++)
	{
		visited[i]=false;
	}
	visited[From]=true;
	while(!V.empty())
	{
		now=V.front();
		V.pop();
		if(now==To)
		{
			break;
		}
		for(int i=0;i<E[now].size();i++)
		{
			if(!visited[E[now][i]])
			{
				visited[E[now][i]]=true;
				V.push(E[now][i]);
				pa[id][E[now][i]]=now;
			}
		}
	}
	now=To;
	while(~pa[id][now])
	{
		if(mp[pii(now,pa[id][now])]<Val)
		{
			mp[pii(now,pa[id][now])]=Val;
			mp[pii(pa[id][now],now)]=Val;
		}
		now=pa[id][now];
	}
}

bool check(int id,int From,int To,int Val)
{
	int now;
	int mini;

	now=To;
	mini=INF;
	while(~pa[id][now])
	{
		mini=min(mini,mp[pii(now,pa[id][now])]);
		now=pa[id][now];
	}

	return mini==Val?true:false;
}

int main()
{
	bool able;

	while(~scanf("%d",&N))
	{
		mp.clear();
		for(int i=2;i<=N;i++)
		{
			e[i].From=read();
			e[i].To=read();
			E[e[i].From].push_back(e[i].To);
			E[e[i].To].push_back(e[i].From);
			mp[pii(e[i].From,e[i].To)]=1;
			mp[pii(e[i].To,e[i].From)]=1;
		}
		scanf("%d",&M);
		for(int i=0;i<M;i++)
		{
			a[i]=read();
			b[i]=read();
			g[i]=read();
			bfs(i,a[i],b[i],g[i]);
		}
		able=true;
		for(int i=0;i<M&&able;i++)
		{
			able=check(i,a[i],b[i],g[i]);
		}
		if(able)
		{
			for(int i=2;i<=N;i++)
			{
				printf("%d%c",mp[pii(e[i].From,e[i].To)],i==N?'\n':' ');
			}
		}
		else
		{
			printf("-1\n");
		}
	}

	return 0;
}
/*

*/


#include<bits/stdc++.h>
using namespace std;
int a, b, x[5050], y[5050], arr[5050][5050], ans[5050][5050];
vector<int> V[5050];
bool visit[5050];
void dfs(int fat, int now)
{
	for(int x : V[now])
	{
		if(visit[x]) continue;
		visit[x] = true;
		arr[fat][x] = now;
		dfs(fat, x);
	}
}
void add(int from, int to, int x)
{
	while(to != arr[from][to])
	{
		ans[arr[from][to]][to] = max(ans[arr[from][to]][to], x);
		ans[to][arr[from][to]] = ans[arr[from][to]][to];
		to = arr[from][to];
	}
}
int check(int from, int to)
{
	int res = 99999999;
	while(to != arr[from][to])
	{
		res = min(res, ans[arr[from][to]][to]);
		to = arr[from][to];
	}
	return res;
}
int main()
{
	cin >> a;
	for(int i=0; i<a - 1; i++)
	{
		cin >> x[i] >> y[i];
		V[x[i]].push_back(y[i]);
		V[y[i]].push_back(x[i]);
	}
	for(int i=1; i<=a; i++)
	{
		memset(visit, 0, sizeof(visit));
		visit[i] = true;
		dfs(i, i);
	}
//	for(int i=1; i<=a; i++)
//	{
//		for(int j=1; j<=a; j++)
//			cout << arr[i][j] << ' ';
//		cout << '\n';
//	}
	cin >> b;
	int one[b + 50], two[b + 50], three[b + 50];
	for(int i=0; i<b; i++)
	{
		cin >> one[i] >> two[i] >> three[i];
		add(one[i], two[i], three[i]);
	}
	for(int i=0; i<b; i++)
	{
		int tmp = check(one[i], two[i]);
		if(tmp != three[i])
		{
			cout << -1;
			return 0;
		}
	}
	for(int i=0; i < a - 1; i++)
	{
		if(!ans[x[i]][y[i]]) ans[x[i]][y[i]] = 1;
		cout << ans[x[i]][y[i]] << ' ';
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a, b, x[5050], y[5050], arr[5050][5050], ans[5050][5050];
vector<int> V[5050];
bool visit[5050];
void dfs(int fat, int now)
{
	for(int x : V[now])
	{
		if(visit[x]) continue;
		visit[x] = true;
		arr[fat][x] = now;
		dfs(fat, x);
	}
}
void add(int from, int to, int x)
{
	while(to != arr[from][to])
	{
		ans[arr[from][to]][to] = max(ans[arr[from][to]][to], x);
		ans[to][arr[from][to]] = ans[arr[from][to]][to];
		to = arr[from][to];
	}
}
int check(int from, int to)
{
	int res = 99999999;
	while(to != arr[from][to])
	{
		res = min(res, ans[arr[from][to]][to]);
		to = arr[from][to];
	}
	return res;
}
int main()
{
	cin >> a;
	for(int i=0; i<a - 1; i++)
	{
		cin >> x[i] >> y[i];
		V[x[i]].push_back(y[i]);
		V[y[i]].push_back(x[i]);
	}
	for(int i=1; i<=a; i++)
	{
		memset(visit, 0, sizeof(visit));
		visit[i] = true;
		dfs(i, i);
	}
//	for(int i=1; i<=a; i++)
//	{
//		for(int j=1; j<=a; j++)
//			cout << arr[i][j] << ' ';
//		cout << '\n';
//	}
	cin >> b;
	int one[b + 50], two[b + 50], three[b + 50];
	for(int i=0; i<b; i++)
	{
		cin >> one[i] >> two[i] >> three[i];
		add(one[i], two[i], three[i]);
	}
	for(int i=0; i<b; i++)
	{
		int tmp = check(one[i], two[i]);
		if(tmp != three[i])
		{
			cout << -1;
			return 0;
		}
	}
	cout << "YES\n";
	for(int i=0; i < a - 1; i++)
		cout << ans[x[i]][y[i]] << ' ';
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a, b, x[5050], y[5050], arr[5050][5050], ans[5050][5050];
vector<int> V[5050];
bool visit[5050];
void dfs(int fat, int now)
{
	for(int x : V[now])
	{
		if(visit[x]) continue;
		visit[x] = true;
		arr[fat][x] = now;
		dfs(fat, x);
	}
}
void add(int from, int to, int x)
{
	while(to != arr[from][to])
	{
		ans[arr[from][to]][to] = max(ans[arr[from][to]][to], x);
		ans[to][arr[from][to]] = ans[arr[from][to]][to];
		to = arr[from][to];
	}
}
int check(int from, int to)
{
	int res = 99999999;
	while(to != arr[from][to])
	{
		res = min(res, ans[arr[from][to]][to]);
		to = arr[from][to];
	}
	return res;
}
int main()
{
	cin >> a;
	for(int i=0; i<a - 1; i++)
	{
		cin >> x[i] >> y[i];
		V[x[i]].push_back(y[i]);
		V[y[i]].push_back(x[i]);
	}
	for(int i=1; i<=a; i++)
	{
		memset(visit, 0, sizeof(visit));
		visit[i] = true;
		dfs(i, i);
	}
//	for(int i=1; i<=a; i++)
//	{
//		for(int j=1; j<=a; j++)
//			cout << arr[i][j] << ' ';
//		cout << '\n';
//	}
	cin >> b;
	int one[b + 50], two[b + 50], three[b + 50];
	for(int i=0; i<b; i++)
	{
		cin >> one[i] >> two[i] >> three[i];
		add(one[i], two[i], three[i]);
	}
	for(int i=0; i<b; i++)
	{
		int tmp = check(one[i], two[i]);
		if(tmp != three[i])
		{
			cout << -1;
			return 0;
		}
	}
	for(int i=0; i < a - 1; i++)
		cout << ans[x[i]][y[i]] << ' ';
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a, b, x[5050], y[5050], arr[5050][5050], ans[5050][5050];
vector<int> V[5050];
bool visit[5050];
void dfs(int fat, int now)
{
	for(int x : V[now])
	{
		if(visit[x]) continue;
		visit[x] = true;
		arr[fat][x] = now;
		dfs(fat, x);
	}
}
void add(int from, int to, int x)
{
	while(to != arr[from][to])
	{
		ans[arr[from][to]][to] = max(ans[arr[from][to]][to], x);
		ans[to][arr[from][to]] = ans[arr[from][to]][to];
		to = arr[from][to];
	}
}
int check(int from, int to)
{
	int res = 99999999;
	while(to != arr[from][to])
	{
		res = min(res, ans[arr[from][to]][to]);
		to = arr[from][to];
	}
	return res;
}
int main()
{
	cin >> a;
	for(int i=0; i<a - 1; i++)
	{
		cin >> x[i] >> y[i];
		V[x[i]].push_back(y[i]);
		V[y[i]].push_back(x[i]);
	}
	for(int i=1; i<=a; i++)
	{
		memset(visit, 0, sizeof(visit));
		visit[i] = true;
		arr[i][i] = i;
		dfs(i, i);
	}
	cin >> b;
	int one[b + 50], two[b + 50], three[b + 50];
	for(int i=0; i<b; i++)
	{
		cin >> one[i] >> two[i] >> three[i];
		add(one[i], two[i], three[i]);
	}
	for(int i=0; i<b; i++)
	{
		int tmp = check(one[i], two[i]);
		if(tmp != three[i])
		{
			cout << -1;
			return 0;
		}
	}
	for(int i=0; i < a - 1; i++)
	{
		if(!ans[x[i]][y[i]]) ans[x[i]][y[i]] = 1;
		cout << ans[x[i]][y[i]] << ' ';
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) cin>>a
#define pii pair<int,int>
//#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 5110;
const int M = 1e9+7;
int n,m,k,t = 15;

int head[maxn],to[maxn*2],Next[maxn*2],cnt = 1;

void add(int u,int v)
{
    to[cnt] = v;Next[cnt] = head[u];head[u] = cnt;cnt++;
}

int ans[maxn][maxn];
bool flag = true;

int d[maxn];
int fa[maxn][16];

void dfs(int u,int pre)
{
    d[u] = d[pre]+1;
    fa[u][0] = pre;
    for(int i = 1; (i<<1) <= d[u]; i++)
    {
        fa[u][i] = fa[fa[u][i-1]][i-1];
    }
    for(int i = head[u]; i ; i = Next[i])
    {
        int v = to[i];
        if(v == pre) continue;
        dfs(v,u);
    }
}

int lca(int x,int y)
{
    if(d[x] > d[y]) swap(x,y);
    for(int i = t; i >= 0; i--)
    {
        if(d[fa[y][i]] >= d[x]) y = fa[y][i];
    }
    if(y == x) return x;
    for(int i = t; i >= 0; i--)
    {
        if(fa[x][i] != fa[y][i])
        {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

void solve(int u,int z)
{
    bool tmp = false;
    if(u == k) tmp = true;
    while(u != k)
    {
        int v = fa[u][0];

        if(ans[u][v] == 0)
        {
            ans[u][v] = ans[v][u] = z;
            tmp = 1;
        }
        else
        {
            if(ans[u][v] < z)
            {
                flag = false;
            }
            tmp = 1;
        }

        u = v;
    }
    if(flag && !tmp) flag = 0;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    cin>>n;
    for(int i = 1,x,y; i < n; i++)
    {
        cin>>x>>y;
        add(x,y);add(y,x);
    }
    dfs(1,0);
    cin>>m;
    for(int i = 1,x,y,z; i <= m; i++)
    {
        cin>>x>>y>>z;
        k = lca(x,y);
        solve(x,z);
        solve(y,z);
        //cout<<flag<<endl;
    }
    if(flag)
    {
        for(int i = 1; i < n; i++)
        {
            int u = to[i*2];
            int v = to[i*2-1];
            if(ans[u][v] == 0) ans[u][v] = 1;
            cout<<ans[u][v]<<' ';
        }
    }
    else puts("-1");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) cin>>a
#define pii pair<int,int>
//#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 5110;
const int M = 1e9+7;
int n,m,k,t = 19;

int head[maxn],to[maxn*2],Next[maxn*2],cnt = 1;

void add(int u,int v)
{
    to[cnt] = v;Next[cnt] = head[u];head[u] = cnt;cnt++;
}

int ans[maxn][maxn];
bool flag = true;

int d[maxn];
int fa[maxn][20];

void dfs(int u,int pre)
{
    d[u] = d[pre]+1;
    fa[u][0] = pre;
    for(int i = 1; (i<<1) <= d[u] && i <= t; i++)
    {
        fa[u][i] = fa[fa[u][i-1]][i-1];
    }
    for(int i = head[u]; i ; i = Next[i])
    {
        int v = to[i];
        if(v == pre) continue;
        dfs(v,u);
    }
}

int lca(int x,int y)
{
    if(d[x] > d[y]) swap(x,y);
    for(int i = t; i >= 0; i--)
    {
        if(d[fa[y][i]] >= d[x]) y = fa[y][i];
    }
    if(y == x) return x;
    for(int i = t; i >= 0; i--)
    {
        if(fa[x][i] != fa[y][i])
        {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

struct node
{
    int x,y,z;
}q[maxn];

bool cmp(node a,node b)
{
    return a.z < b.z;
}

void solve(int u,int z)
{
    while(u != k)
    {
        int v = fa[u][0];
        ans[u][v] = max(ans[u][v],z);
        ans[v][u] = max(ans[v][u],z);
        //cout<<u<<' '<<v<<' '<<ans[u][v]<<' '<<ans[v][u]<<endl;
        u = v;
    }
}

int query(int u)
{
    int res = inf;
    while(u != k)
    {
        int v = fa[u][0];
        res = min(res,ans[u][v]);
        u = v;
    }
    return res;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    mem(d,0);
    ios::sync_with_stdio(false);
    cin.tie(0);
    vector<pii> v;
    cin>>n;
    for(int i = 1,x,y; i < n; i++)
    {
        cin>>x>>y;
        add(x,y);add(y,x);
        v.push_back({x,y});
    }
    dfs(1,0);
    cin>>m;
    for(int i = 0; i < m; i++)
    {
        cin>>q[i].x>>q[i].y>>q[i].z;
    }
    sort(q,q+m,cmp);
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        solve(q[i].x,q[i].z);solve(q[i].y,q[i].z);
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(ans[i][j] == 0) ans[i][j] = 1000000;
        }
    }
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        int tmp = query(q[i].x);
        tmp = min(tmp,query(q[i].y));
        if(tmp != q[i].z)
        {
            puts("-1");
            return 0;
        }
    }
    for(auto i : v)
    {
        cout<<ans[i.first][i.second]<<' ';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) cin>>a
#define pii pair<int,int>
//#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 5110;
const int M = 1e9+7;
int n,m,k,t = 19;

int head[maxn],to[maxn*2],Next[maxn*2],cnt = 1;

void add(int u,int v)
{
    to[cnt] = v;Next[cnt] = head[u];head[u] = cnt;cnt++;
}

int ans[maxn][maxn];
bool flag = true;

int d[maxn];
int fa[maxn][20];

bool vis[maxn];

void dfs(int u,int pre)
{
    if(vis[u]) return;
    vis[u] = 1;
    d[u] = d[pre]+1;
    fa[u][0] = pre;
    for(int i = 1; (i<<1) <= d[u] && i <= t; i++)
    {
        fa[u][i] = fa[fa[u][i-1]][i-1];
    }
    for(int i = head[u]; i ; i = Next[i])
    {
        int v = to[i];
        if(v == pre) continue;
        dfs(v,u);
    }
}

int lca(int x,int y)
{
    if(d[x] > d[y]) swap(x,y);
    for(int i = t; i >= 0; i--)
    {
        if(d[fa[y][i]] >= d[x]) y = fa[y][i];
    }
    if(y == x) return x;
    for(int i = t; i >= 0; i--)
    {
        if(fa[x][i] != fa[y][i])
        {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

struct node
{
    int x,y,z;
}q[maxn];

bool cmp(node a,node b)
{
    return a.z < b.z;
}

void solve(int u,int z)
{
    while(u != k)
    {
        int v = fa[u][0];
        ans[u][v] = max(ans[u][v],z);
        ans[v][u] = max(ans[v][u],z);
        //cout<<u<<' '<<v<<' '<<ans[u][v]<<' '<<ans[v][u]<<endl;
        u = v;
    }
}

int query(int u)
{
    int res = inf;
    while(u != k)
    {
        int v = fa[u][0];
        res = min(res,ans[u][v]);
        u = v;
    }
    return res;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    mem(d,0);
    ios::sync_with_stdio(false);
    cin.tie(0);
    vector<pii> v;
    cin>>n;
    for(int i = 1,x,y; i < n; i++)
    {
        cin>>x>>y;
        add(x,y);add(y,x);
        v.push_back({x,y});
    }
    dfs(1,0);
    cin>>m;
    for(int i = 0; i < m; i++)
    {
        cin>>q[i].x>>q[i].y>>q[i].z;
    }
    sort(q,q+m,cmp);
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        solve(q[i].x,q[i].z);solve(q[i].y,q[i].z);
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(ans[i][j] == 0) ans[i][j] = 1000000;
        }
    }
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        int tmp = query(q[i].x);
        tmp = min(tmp,query(q[i].y));
        if(tmp != q[i].z)
        {
            puts("-1");
            return 0;
        }
    }
    for(auto i : v)
    {
        cout<<ans[i.first][i.second]<<' ';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) cin>>a
#define pii pair<int,int>
//#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 5110;
const int M = 1e9+7;
int n,m,k,t = 29;

int head[maxn],to[maxn*2],Next[maxn*2],cnt = 1;

void add(int u,int v)
{
    to[cnt] = v;Next[cnt] = head[u];head[u] = cnt;cnt++;
}

int ans[maxn][maxn];
bool flag = true;

int d[maxn];
int fa[maxn][30];

void dfs(int u,int pre)
{
    d[u] = d[pre]+1;
    fa[u][0] = pre;
    for(int i = 1; (i<<1) <= d[u]; i++)
    {
        fa[u][i] = fa[fa[u][i-1]][i-1];
    }
    for(int i = head[u]; i ; i = Next[i])
    {
        int v = to[i];
        if(v == pre) continue;
        dfs(v,u);
    }
}

int lca(int x,int y)
{
    if(d[x] > d[y]) swap(x,y);
    for(int i = t; i >= 0; i--)
    {
        if(d[fa[y][i]] >= d[x]) y = fa[y][i];
    }
    if(y == x) return x;
    for(int i = t; i >= 0; i--)
    {
        if(fa[x][i] != fa[y][i])
        {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

struct node
{
    int x,y,z;
}q[maxn];

bool cmp(node a,node b)
{
    return a.z < b.z;
}

void solve(int u,int z)
{
    while(u != k)
    {
        int v = fa[u][0];
        ans[u][v] = max(ans[u][v],z);
        ans[v][u] = max(ans[v][u],z);
        //cout<<u<<' '<<v<<' '<<ans[u][v]<<' '<<ans[v][u]<<endl;
        u = v;
    }
}

int query(int u)
{
    int res = inf;
    while(u != k)
    {
        int v = fa[u][0];
        res = min(res,ans[u][v]);
        u = v;
    }
    return res;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    vector<pii> v;
    cin>>n;
    for(int i = 1,x,y; i < n; i++)
    {
        cin>>x>>y;
        add(x,y);add(y,x);
        v.push_back({x,y});
    }
    dfs(1,0);
    cin>>m;
    for(int i = 0; i < m; i++)
    {
        cin>>q[i].x>>q[i].y>>q[i].z;
    }
    sort(q,q+m,cmp);
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        solve(q[i].x,q[i].z);solve(q[i].y,q[i].z);
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(ans[i][j] == 0) ans[i][j] = 1000000;
        }
    }
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        int tmp = query(q[i].x);
        tmp = min(tmp,query(q[i].y));
        if(tmp != q[i].z)
        {
            puts("-1");
            return 0;
        }
    }
    for(auto i : v)
    {
        cout<<ans[i.first][i.second]<<' ';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) cin>>a
#define pii pair<int,int>
//#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 5110;
const int M = 1e9+7;
int n,m,k,t = 15;

int head[maxn],to[maxn*2],Next[maxn*2],cnt = 1;

void add(int u,int v)
{
    to[cnt] = v;Next[cnt] = head[u];head[u] = cnt;cnt++;
}

int ans[maxn][maxn];
bool flag = true;

int d[maxn];
int fa[maxn][16];

void dfs(int u,int pre)
{
    d[u] = d[pre]+1;
    fa[u][0] = pre;
    for(int i = 1; (i<<1) <= d[u]; i++)
    {
        fa[u][i] = fa[fa[u][i-1]][i-1];
    }
    for(int i = head[u]; i ; i = Next[i])
    {
        int v = to[i];
        if(v == pre) continue;
        dfs(v,u);
    }
}

int lca(int x,int y)
{
    if(d[x] > d[y]) swap(x,y);
    for(int i = t; i >= 0; i--)
    {
        if(d[fa[y][i]] >= d[x]) y = fa[y][i];
    }
    if(y == x) return x;
    for(int i = t; i >= 0; i--)
    {
        if(fa[x][i] != fa[y][i])
        {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

struct node
{
    int x,y,z;
}q[maxn];

bool cmp(node a,node b)
{
    return a.z < b.z;
}

void solve(int u,int z)
{
    while(u != k)
    {
        int v = fa[u][0];
        ans[u][v] = max(ans[u][v],z);
        ans[v][u] = max(ans[v][u],z);
        //cout<<u<<' '<<v<<' '<<ans[u][v]<<' '<<ans[v][u]<<endl;
        u = v;
    }
}

int query(int u)
{
    int res = inf;
    while(u != k)
    {
        int v = fa[u][0];
        if(ans[u][v]) res = min(res,ans[u][v]);
        else ans[u][v] = 1000000;
        u = v;
    }
    return res;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    vector<pii> v;
    cin>>n;
    for(int i = 1,x,y; i < n; i++)
    {
        cin>>x>>y;
        add(x,y);add(y,x);
        v.push_back({x,y});
    }
    dfs(1,0);
    cin>>m;
    for(int i = 0; i < m; i++)
    {
        cin>>q[i].x>>q[i].y>>q[i].z;
    }
    sort(q,q+m,cmp);
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        solve(q[i].x,q[i].z);solve(q[i].y,q[i].z);
    }
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        int tmp = inf;
        tmp = query(q[i].x);
        tmp = min(tmp,query(q[i].y));
        if(tmp < q[i].z)
        {
            puts("-1");
            return 0;
        }
    }
    for(auto i : v)
    {
        cout<<ans[i.first][i.second]<<' ';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) cin>>a
#define pii pair<int,int>
//#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 5110;
const int M = 1e9+7;
int n,m,k,t = 16;

int head[maxn],to[maxn*2],Next[maxn*2],cnt = 1;

void add(int u,int v)
{
    to[cnt] = v;Next[cnt] = head[u];head[u] = cnt;cnt++;
}

int ans[maxn][maxn];
bool flag = true;

int d[maxn];
int fa[maxn][18];

void dfs(int u,int pre)
{
    d[u] = d[pre]+1;
    fa[u][0] = pre;
    for(int i = 1; (i<<1) <= d[u]; i++)
    {
        fa[u][i] = fa[fa[u][i-1]][i-1];
    }
    for(int i = head[u]; i ; i = Next[i])
    {
        int v = to[i];
        if(v == pre) continue;
        dfs(v,u);
    }
}

int lca(int x,int y)
{
    if(d[x] > d[y]) swap(x,y);
    for(int i = t; i >= 0; i--)
    {
        if(d[fa[y][i]] >= d[x]) y = fa[y][i];
    }
    if(y == x) return x;
    for(int i = t; i >= 0; i--)
    {
        if(fa[x][i] != fa[y][i])
        {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

struct node
{
    int x,y,z;
}q[maxn];

bool cmp(node a,node b)
{
    return a.z < b.z;
}

void solve(int u,int z)
{
    while(u != k)
    {
        int v = fa[u][0];
        ans[u][v] = max(ans[u][v],z);
        ans[v][u] = max(ans[v][u],z);
        //cout<<u<<' '<<v<<' '<<ans[u][v]<<' '<<ans[v][u]<<endl;
        u = v;
    }
}

int query(int u)
{
    int res = inf;
    while(u != k)
    {
        int v = fa[u][0];
        res = min(res,ans[u][v]);
        u = v;
    }
    return res;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    vector<pii> v;
    cin>>n;
    for(int i = 1,x,y; i < n; i++)
    {
        cin>>x>>y;
        add(x,y);add(y,x);
        v.push_back({x,y});
    }
    dfs(1,0);
    cin>>m;
    for(int i = 0; i < m; i++)
    {
        cin>>q[i].x>>q[i].y>>q[i].z;
    }
    sort(q,q+m,cmp);
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        solve(q[i].x,q[i].z);solve(q[i].y,q[i].z);
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(ans[i][j] == 0) ans[i][j] = 1000000;
        }
    }
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        int tmp = query(q[i].x);
        tmp = min(tmp,query(q[i].y));
        if(tmp != q[i].z)
        {
            puts("-1");
            return 0;
        }
    }
    for(auto i : v)
    {
        cout<<ans[i.first][i.second]<<' ';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) cin>>a
#define pii pair<int,int>
//#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 5110;
const int M = 1e9+7;
int n,m,k,t = 15;

int head[maxn],to[maxn*2],Next[maxn*2],cnt = 1;

void add(int u,int v)
{
    to[cnt] = v;Next[cnt] = head[u];head[u] = cnt;cnt++;
}

int ans[maxn][maxn];
bool flag = true;

int d[maxn];
int fa[maxn][16];

void dfs(int u,int pre)
{
    d[u] = d[pre]+1;
    fa[u][0] = pre;
    for(int i = 1; (i<<1) <= d[u]; i++)
    {
        fa[u][i] = fa[fa[u][i-1]][i-1];
    }
    for(int i = head[u]; i ; i = Next[i])
    {
        int v = to[i];
        if(v == pre) continue;
        dfs(v,u);
    }
}

int lca(int x,int y)
{
    if(d[x] > d[y]) swap(x,y);
    for(int i = t; i >= 0; i--)
    {
        if(d[fa[y][i]] >= d[x]) y = fa[y][i];
    }
    if(y == x) return x;
    for(int i = t; i >= 0; i--)
    {
        if(fa[x][i] != fa[y][i])
        {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

struct node
{
    int x,y,z;
}q[maxn];

bool cmp(node a,node b)
{
    return a.z < b.z;
}

void solve(int u,int z)
{
    while(u != k)
    {
        int v = fa[u][0];
        ans[u][v] = max(ans[u][v],z);
        ans[v][u] = max(ans[v][u],z);
        //cout<<u<<' '<<v<<' '<<ans[u][v]<<' '<<ans[v][u]<<endl;
        u = v;
    }
}

int query(int u)
{
    int res = inf;
    while(u != k)
    {
        int v = fa[u][0];
        res = min(res,ans[u][v]);
        u = v;
    }
    return res;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    vector<pii> v;
    cin>>n;
    for(int i = 1,x,y; i < n; i++)
    {
        cin>>x>>y;
        add(x,y);add(y,x);
        v.push_back({x,y});
    }
    dfs(1,0);
    cin>>m;
    for(int i = 0; i < m; i++)
    {
        cin>>q[i].x>>q[i].y>>q[i].z;
    }
    sort(q,q+m,cmp);
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        solve(q[i].x,q[i].z);solve(q[i].y,q[i].z);
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(ans[i][j] == 0) ans[i][j] = 1000000;
        }
    }
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        int tmp = inf;
        tmp = query(q[i].x);
        tmp = min(tmp,query(q[i].y));
        if(tmp < q[i].z)
        {
            puts("-1");
            return 0;
        }
    }
    for(auto i : v)
    {
        cout<<ans[i.first][i.second]<<' ';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) cin>>a
#define pii pair<int,int>
//#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 5110;
const int M = 1e9+7;
int n,m,k,t = 15;

int head[maxn],to[maxn*2],Next[maxn*2],cnt = 1;

void add(int u,int v)
{
    to[cnt] = v;Next[cnt] = head[u];head[u] = cnt;cnt++;
}

int ans[maxn][maxn];
bool flag = true;

int d[maxn];
int fa[maxn][16];

void dfs(int u,int pre)
{
    d[u] = d[pre]+1;
    fa[u][0] = pre;
    for(int i = 1; (i<<1) <= d[u]; i++)
    {
        fa[u][i] = fa[fa[u][i-1]][i-1];
    }
    for(int i = head[u]; i ; i = Next[i])
    {
        int v = to[i];
        if(v == pre) continue;
        dfs(v,u);
    }
}

int lca(int x,int y)
{
    if(d[x] > d[y]) swap(x,y);
    for(int i = t; i >= 0; i--)
    {
        if(d[fa[y][i]] >= d[x]) y = fa[y][i];
    }
    if(y == x) return x;
    for(int i = t; i >= 0; i--)
    {
        if(fa[x][i] != fa[y][i])
        {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

struct node
{
    int x,y,z;
}q[maxn];

bool cmp(node a,node b)
{
    return a.z < b.z;
}

void solve(int u,int z)
{
    while(u != k)
    {
        int v = fa[u][0];
        ans[u][v] = max(ans[u][v],z);
        ans[v][u] = max(ans[v][u],z);
        //cout<<u<<' '<<v<<' '<<ans[u][v]<<' '<<ans[v][u]<<endl;
        u = v;
    }
}

int query(int u)
{
    int res = inf;
    while(u != k)
    {
        int v = fa[u][0];
        res = min(res,ans[u][v]);
        u = v;
    }
    return res;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    vector<pii> v;
    cin>>n;
    for(int i = 1,x,y; i < n; i++)
    {
        cin>>x>>y;
        add(x,y);add(y,x);
        v.push_back({x,y});
    }
    dfs(1,0);
    cin>>m;
    for(int i = 0; i < m; i++)
    {
        cin>>q[i].x>>q[i].y>>q[i].z;
    }
    sort(q,q+m,cmp);
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        solve(q[i].x,q[i].z);solve(q[i].y,q[i].z);
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(ans[i][j] == 0) ans[i][j] = 1000000;
        }
    }
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        int tmp = query(q[i].x);
        tmp = min(tmp,query(q[i].y));
        if(tmp != q[i].z)
        {
            puts("-1");
            return 0;
        }
    }
    for(auto i : v)
    {
        cout<<ans[i.first][i.second]<<' ';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) cin>>a
#define pii pair<int,int>
//#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 5110;
const int M = 1e9+7;
int n,m,k,t = 19;

int head[maxn],to[maxn*2],Next[maxn*2],cnt = 1;

void add(int u,int v)
{
    to[cnt] = v;Next[cnt] = head[u];head[u] = cnt;cnt++;
}

int ans[maxn][maxn];
bool flag = true;

int d[maxn];
int fa[maxn][20];

void dfs(int u,int pre)
{
    d[u] = d[pre]+1;
    fa[u][0] = pre;
    for(int i = 1; (i<<1) <= d[u]; i++)
    {
        fa[u][i] = fa[fa[u][i-1]][i-1];
    }
    for(int i = head[u]; i ; i = Next[i])
    {
        int v = to[i];
        if(v == pre) continue;
        dfs(v,u);
    }
}

int lca(int x,int y)
{
    if(d[x] > d[y]) swap(x,y);
    for(int i = t; i >= 0; i--)
    {
        if(d[fa[y][i]] >= d[x]) y = fa[y][i];
    }
    if(y == x) return x;
    for(int i = t; i >= 0; i--)
    {
        if(fa[x][i] != fa[y][i])
        {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

struct node
{
    int x,y,z;
}q[maxn];

bool cmp(node a,node b)
{
    return a.z < b.z;
}

void solve(int u,int z)
{
    while(u != k)
    {
        int v = fa[u][0];
        ans[u][v] = max(ans[u][v],z);
        ans[v][u] = max(ans[v][u],z);
        //cout<<u<<' '<<v<<' '<<ans[u][v]<<' '<<ans[v][u]<<endl;
        u = v;
    }
}

int query(int u)
{
    int res = inf;
    while(u != k)
    {
        int v = fa[u][0];
        res = min(res,ans[u][v]);
        u = v;
    }
    return res;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    mem(d,0);
    ios::sync_with_stdio(false);
    cin.tie(0);
    vector<pii> v;
    cin>>n;
    for(int i = 1,x,y; i < n; i++)
    {
        cin>>x>>y;
        add(x,y);add(y,x);
        v.push_back({x,y});
    }
    dfs(1,0);
    cin>>m;
    for(int i = 0; i < m; i++)
    {
        cin>>q[i].x>>q[i].y>>q[i].z;
    }
    sort(q,q+m,cmp);
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        solve(q[i].x,q[i].z);solve(q[i].y,q[i].z);
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(ans[i][j] == 0) ans[i][j] = 1000000;
        }
    }
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        int tmp = query(q[i].x);
        tmp = min(tmp,query(q[i].y));
        if(tmp != q[i].z)
        {
            puts("-1");
            return 0;
        }
    }
    for(auto i : v)
    {
        cout<<ans[i.first][i.second]<<' ';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) cin>>a
#define pii pair<int,int>
//#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 5110;
const int M = 1e9+7;
int n,m,k,t = 15;

int head[maxn],to[maxn*2],Next[maxn*2],cnt = 1;

void add(int u,int v)
{
    to[cnt] = v;Next[cnt] = head[u];head[u] = cnt;cnt++;
}

int ans[maxn][maxn];
bool flag = true;

int d[maxn];
int fa[maxn][16];

void dfs(int u,int pre)
{
    d[u] = d[pre]+1;
    fa[u][0] = pre;
    for(int i = 1; (i<<1) <= d[u]; i++)
    {
        fa[u][i] = fa[fa[u][i-1]][i-1];
    }
    for(int i = head[u]; i ; i = Next[i])
    {
        int v = to[i];
        if(v == pre) continue;
        dfs(v,u);
    }
}

int lca(int x,int y)
{
    if(d[x] > d[y]) swap(x,y);
    for(int i = t; i >= 0; i--)
    {
        if(d[fa[y][i]] >= d[x]) y = fa[y][i];
    }
    if(y == x) return x;
    for(int i = t; i >= 0; i--)
    {
        if(fa[x][i] != fa[y][i])
        {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

void solve(int x,int y,int z)
{
    bool tmp = false;
    int u = x;
    while(u != k)
    {
        int v = fa[u][0];

        if(ans[u][v] == 0)
        {
            ans[u][v] = ans[v][u] = z;
            tmp = 1;
        }
        else
        {
            if(ans[u][v] < z)
            {
                flag = false;
            }
            if(ans[u][v] == z) tmp = 1;
        }

        u = v;
    }
    u = y;
    while(u != k)
    {
        int v = fa[u][0];

        if(ans[u][v] == 0)
        {
            ans[u][v] = ans[v][u] = z;
            tmp = 1;
        }
        else
        {
            if(ans[u][v] < z)
            {
                flag = false;
            }
            if(ans[u][v] == z) tmp = 1;
        }

        u = v;
    }
    if(flag && !tmp) flag = 0;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    cin>>n;
    for(int i = 1,x,y; i < n; i++)
    {
        cin>>x>>y;
        add(x,y);add(y,x);
    }
    dfs(1,0);
    cin>>m;
    for(int i = 1,x,y,z; i <= m; i++)
    {
        cin>>x>>y>>z;
        k = lca(x,y);
        solve(x,y,z);
    }
    if(flag)
    {
        for(int i = 1; i < n; i++)
        {
            int u = to[i*2];
            int v = to[i*2-1];
            if(ans[u][v] == 0) ans[u][v] = 1;
            cout<<ans[u][v]<<' ';
        }
    }
    else puts("-1");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) cin>>a
#define pii pair<int,int>
//#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 5110;
const int M = 1e9+7;
int n,m,k,t = 15;

int head[maxn],cost[maxn],to[maxn],Next[maxn],cnt = 1;

void add(int u,int v)
{
    to[cnt] = v;Next[cnt] = head[u];head[u] = cnt;cnt++;
}

int ans[maxn][maxn];
bool flag = true;

int d[maxn];
int fa[maxn][16];

void dfs(int u,int pre)
{
    d[u] = d[pre]+1;
    fa[u][0] = pre;
    for(int i = 1; (i<<1) <= d[u]; i++)
    {
        fa[u][i] = fa[fa[u][i-1]][i-1];
    }
    for(int i = head[u]; i ; i = Next[i])
    {
        int v = to[i];
        if(v == pre) continue;
        dfs(v,u);
    }
}

int lca(int x,int y)
{
    if(d[x] > d[y]) swap(x,y);
    for(int i = t; i >= 0; i--)
    {
        if(d[fa[y][i]] >= d[x]) y = fa[y][i];
    }
    if(y == x) return x;
    for(int i = t; i >= 0; i--)
    {
        if(fa[x][i] != fa[y][i])
        {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

void solve(int u,int z)
{
    while(u != k)
    {
        int v = fa[u][0];

        if(ans[u][v] == 0)
        {
            ans[u][v] = ans[v][u] = z;
        }
        else
        {
            if(ans[u][v] < z)
            {
                flag = false;
            }
        }

        u = v;
    }
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    cin>>n;
    for(int i = 1,x,y; i < n; i++)
    {
        cin>>x>>y;
        add(x,y);add(y,x);
    }
    dfs(1,0);
    cin>>m;
    for(int i = 1,x,y,z; i <= m; i++)
    {
        cin>>x>>y>>z;
        k = lca(x,y);
        solve(x,z);solve(y,z);
    }
    if(flag)
    {
        for(int i = 1; i < n; i++)
        {
            int u = to[i*2];
            int v = to[i*2-1];
            cout<<ans[u][v]<<' ';
        }
    }
    else puts("-1");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) cin>>a
#define pii pair<int,int>
//#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 5110;
const int M = 1e9+7;
int n,m,k,t = 15;

int head[maxn],to[maxn*2],Next[maxn*2],cnt = 1;

void add(int u,int v)
{
    to[cnt] = v;Next[cnt] = head[u];head[u] = cnt;cnt++;
}

int ans[maxn][maxn];
bool flag = true;

int d[maxn];
int fa[maxn][16];

void dfs(int u,int pre)
{
    d[u] = d[pre]+1;
    fa[u][0] = pre;
    for(int i = 1; (i<<1) <= d[u]; i++)
    {
        fa[u][i] = fa[fa[u][i-1]][i-1];
    }
    for(int i = head[u]; i ; i = Next[i])
    {
        int v = to[i];
        if(v == pre) continue;
        dfs(v,u);
    }
}

int lca(int x,int y)
{
    if(d[x] > d[y]) swap(x,y);
    for(int i = t; i >= 0; i--)
    {
        if(d[fa[y][i]] >= d[x]) y = fa[y][i];
    }
    if(y == x) return x;
    for(int i = t; i >= 0; i--)
    {
        if(fa[x][i] != fa[y][i])
        {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

void solve(int u,int z)
{
    bool tmp = false;
    if(u == k) tmp = true;
    while(u != k)
    {
        int v = fa[u][0];

        if(ans[u][v] == 0)
        {
            ans[u][v] = ans[v][u] = z;
            tmp = 1;
        }
        else
        {
            if(ans[u][v] < z)
            {
                flag = false;
            }
            if(ans[u][v] == z)
            {
                tmp = 1;
            }
        }

        u = v;
    }
    if(flag && !tmp) flag = 0;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    cin>>n;
    for(int i = 1,x,y; i < n; i++)
    {
        cin>>x>>y;
        add(x,y);add(y,x);
    }
    dfs(1,0);
    cin>>m;
    for(int i = 1,x,y,z; i <= m; i++)
    {
        cin>>x>>y>>z;
        k = lca(x,y);
        solve(x,z);
        solve(y,z);
    }
    if(flag)
    {
        for(int i = 1; i < n; i++)
        {
            int u = to[i*2];
            int v = to[i*2-1];
            if(ans[u][v] == 0) ans[u][v] = 1;
            cout<<ans[u][v]<<' ';
        }
    }
    else puts("-1");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) cin>>a
#define pii pair<int,int>
//#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 5110;
const int M = 1e9+7;
int n,m,k,t = 15;

int head[maxn],to[maxn*2],Next[maxn*2],cnt = 1;

void add(int u,int v)
{
    to[cnt] = v;Next[cnt] = head[u];head[u] = cnt;cnt++;
}

int ans[maxn][maxn];
bool flag = true;

int d[maxn];
int fa[maxn][16];

void dfs(int u,int pre)
{
    d[u] = d[pre]+1;
    fa[u][0] = pre;
    for(int i = 1; (i<<1) <= d[u]; i++)
    {
        fa[u][i] = fa[fa[u][i-1]][i-1];
    }
    for(int i = head[u]; i ; i = Next[i])
    {
        int v = to[i];
        if(v == pre) continue;
        dfs(v,u);
    }
}

int lca(int x,int y)
{
    if(d[x] > d[y]) swap(x,y);
    for(int i = t; i >= 0; i--)
    {
        if(d[fa[y][i]] >= d[x]) y = fa[y][i];
    }
    if(y == x) return x;
    for(int i = t; i >= 0; i--)
    {
        if(fa[x][i] != fa[y][i])
        {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

struct node
{
    int x,y,z;
}q[maxn];

bool cmp(node a,node b)
{
    return a.z < b.z;
}

void solve(int u,int z)
{
    while(u != k)
    {
        int v = fa[u][0];
        ans[u][v] = max(ans[u][v],z);
        ans[v][u] = max(ans[v][u],z);
        //cout<<u<<' '<<v<<' '<<ans[u][v]<<' '<<ans[v][u]<<endl;
        u = v;
    }
}

int query(int u)
{
    int res = inf;
    while(u != k)
    {
        int v = fa[u][0];
        res = min(res,ans[u][v]);
        u = v;
    }
    return res;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    mem(d,0);
    ios::sync_with_stdio(false);
    cin.tie(0);
    vector<pii> v;
    cin>>n;
    for(int i = 1,x,y; i < n; i++)
    {
        cin>>x>>y;
        add(x,y);add(y,x);
        v.push_back({x,y});
    }
    dfs(1,0);
    cin>>m;
    for(int i = 0; i < m; i++)
    {
        cin>>q[i].x>>q[i].y>>q[i].z;
    }
    sort(q,q+m,cmp);
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        solve(q[i].x,q[i].z);solve(q[i].y,q[i].z);
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(ans[i][j] == 0) ans[i][j] = 1000000;
        }
    }
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        int tmp = query(q[i].x);
        tmp = min(tmp,query(q[i].y));
        if(tmp != q[i].z)
        {
            puts("-1");
            return 0;
        }
    }
    for(auto i : v)
    {
        cout<<ans[i.first][i.second]<<' ';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) cin>>a
#define pii pair<int,int>
//#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 5110;
const int M = 1e9+7;
int n,m,k,t = 19;

int head[maxn],to[maxn*2],Next[maxn*2],cnt = 1;

void add(int u,int v)
{
    to[cnt] = v;Next[cnt] = head[u];head[u] = cnt;cnt++;
}

int ans[maxn][maxn];
bool flag = true;

int d[maxn];
int fa[maxn][20];

void dfs(int u,int pre)
{
    d[u] = d[pre]+1;
    fa[u][0] = pre;
    for(int i = 1; (i<<1) <= d[u]; i++)
    {
        fa[u][i] = fa[fa[u][i-1]][i-1];
    }
    for(int i = head[u]; i ; i = Next[i])
    {
        int v = to[i];
        if(v == pre) continue;
        dfs(v,u);
    }
}

int lca(int x,int y)
{
    if(d[x] > d[y]) swap(x,y);
    for(int i = t; i >= 0; i--)
    {
        if(d[fa[y][i]] >= d[x]) y = fa[y][i];
    }
    if(y == x) return x;
    for(int i = t; i >= 0; i--)
    {
        if(fa[x][i] != fa[y][i])
        {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

struct node
{
    int x,y,z;
}q[maxn];

bool cmp(node a,node b)
{
    return a.z < b.z;
}

void solve(int u,int z)
{
    while(u != k)
    {
        int v = fa[u][0];
        ans[u][v] = max(ans[u][v],z);
        ans[v][u] = max(ans[v][u],z);
        //cout<<u<<' '<<v<<' '<<ans[u][v]<<' '<<ans[v][u]<<endl;
        u = v;
    }
}

int query(int u)
{
    int res = inf;
    while(u != k)
    {
        int v = fa[u][0];
        res = min(res,ans[u][v]);
        u = v;
    }
    return res;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    vector<pii> v;
    cin>>n;
    for(int i = 1,x,y; i < n; i++)
    {
        cin>>x>>y;
        add(x,y);add(y,x);
        v.push_back({x,y});
    }
    dfs(1,0);
    cin>>m;
    for(int i = 0; i < m; i++)
    {
        cin>>q[i].x>>q[i].y>>q[i].z;
    }
    sort(q,q+m,cmp);
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        solve(q[i].x,q[i].z);solve(q[i].y,q[i].z);
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(ans[i][j] == 0) ans[i][j] = 1000000;
        }
    }
    for(int i = 0; i < m; i++)
    {
        k = lca(q[i].x,q[i].y);
        int tmp = query(q[i].x);
        tmp = min(tmp,query(q[i].y));
        if(tmp != q[i].z)
        {
            puts("-1");
            return 0;
        }
    }
    for(auto i : v)
    {
        cout<<ans[i.first][i.second]<<' ';
    }
    return 0;
}