Problem: There are n railway stations in Berland. They are connected to each other by n-1 railway sections. The railway network is connected, i.e. can be represented as an undirected tree.

You have a map of that network, so for each railway section you know which stations it connects.

Each of the n-1 sections has some integer value of the scenery beauty. However, these values are not marked on the map and you don't know them. All these values are from 1 to 10^6 inclusive.

You asked m passengers some questions: the j-th one told you three values:

  * his departure station a_j; 
  * his arrival station b_j; 
  * minimum scenery beauty along the path from a_j to b_j (the train is moving along the shortest path from a_j to b_j). 



You are planning to update the map and set some value f_i on each railway section — the scenery beauty. The passengers' answers should be consistent with these values.

Print any valid set of values f_1, f_2, ..., f_{n-1}, which the passengers' answer is consistent with or report that it doesn't exist.

Input

The first line contains a single integer n (2 ≤ n ≤ 5000) — the number of railway stations in Berland.

The next n-1 lines contain descriptions of the railway sections: the i-th section description is two integers x_i and y_i (1 ≤ x_i, y_i ≤ n, x_i ≠ y_i), where x_i and y_i are the indices of the stations which are connected by the i-th railway section. All the railway sections are bidirected. Each station can be reached from any other station by the railway.

The next line contains a single integer m (1 ≤ m ≤ 5000) — the number of passengers which were asked questions. Then m lines follow, the j-th line contains three integers a_j, b_j and g_j (1 ≤ a_j, b_j ≤ n; a_j ≠ b_j; 1 ≤ g_j ≤ 10^6) — the departure station, the arrival station and the minimum scenery beauty along his path.

Output

If there is no answer then print a single integer -1.

Otherwise, print n-1 integers f_1, f_2, ..., f_{n-1} (1 ≤ f_i ≤ 10^6), where f_i is some valid scenery beauty along the i-th railway section.

If there are multiple answers, you can print any of them.

Examples

Input


4
1 2
3 2
3 4
2
1 2 5
1 3 3


Output


5 3 5


Input


6
1 2
1 6
3 1
1 5
4 1
4
6 1 3
3 4 1
6 5 2
1 2 5


Output


5 3 1 2 1 


Input


6
1 2
1 6
3 1
1 5
4 1
4
6 1 1
3 4 3
6 5 3
1 2 4


Output


-1

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 7, INF = 1e9 + 7;
int fa[MAXN], mtx[MAXN] = {}, n, q, h[MAXN], ans[MAXN];
int nChain = 1, chainHead[MAXN] = {}, chainId[MAXN], idx[MAXN];
int uu[MAXN], vv[MAXN], cc[MAXN], v2[MAXN], v1[MAXN];
vector<int> adj[MAXN], vex[MAXN], it[MAXN], lz[MAXN], mn[MAXN];
int find_mtx(int u, int p) {
  int mx = -1, chl = 0;
  for (auto v : adj[u])
    if (v != p) {
      fa[v] = u, ++chl, h[v] = h[u] + 1;
      int mtv = find_mtx(v, u);
      if (mx < mtv) mx = mtv, mtx[u] = v;
    }
  return chl;
}
void build_hld(int u, int p) {
  if (chainHead[nChain] == 0) chainHead[nChain] = u;
  chainId[u] = nChain, vex[nChain].push_back(u),
  idx[u] = (int)vex[nChain].size();
  if (mtx[u] == 0) return;
  build_hld(mtx[u], u);
  for (auto v : adj[u])
    if (v != mtx[u] && v != p) ++nChain, build_hld(v, u);
}
void lp(int id, int g) {
  if (lz[id][g]) {
    it[id][g * 2] = max(it[id][g * 2], lz[id][g]);
    it[id][g * 2 + 1] = max(it[id][g * 2 + 1], lz[id][g]);
    lz[id][g * 2] = max(lz[id][g * 2], lz[id][g]);
    lz[id][g * 2 + 1] = max(lz[id][g * 2 + 1], lz[id][g]);
    lz[id][g] = 0;
  }
}
void update(int id, int g, int l, int r, int d, int c, int val) {
  if (l > c || r < d || l > r || d > c) return;
  if (d <= l && r <= c) {
    it[id][g] = max(it[id][g], val), lz[id][g] = max(lz[id][g], val);
    return;
  }
  lp(id, g);
  int mid = (l + r) >> 1;
  update(id, g * 2, l, mid, d, c, val);
  update(id, g * 2 + 1, mid + 1, r, d, c, val);
  it[id][g] = max(it[id][g * 2], it[id][g * 2 + 1]);
}
void upd_min(int id, int g, int l, int r, int pos, int val) {
  if (l == r) return mn[id][g] = val, void();
  int mid = (l + r) >> 1;
  if (pos > mid)
    upd_min(id, g * 2 + 1, mid + 1, r, pos, val);
  else
    upd_min(id, g * 2, l, mid, pos, val);
  mn[id][g] = min(mn[id][g * 2], mn[id][g * 2 + 1]);
}
int get_min(int id, int g, int l, int r, int d, int c) {
  if (l > c || r < d) return INF;
  if (d <= l && r <= c) return mn[id][g];
  int mid = (l + r) >> 1;
  int ret = min(get_min(id, g * 2, l, mid, d, c),
                get_min(id, g * 2 + 1, mid + 1, r, d, c));
  return ret;
}
int query(int id, int g, int l, int r, int d, int c) {
  if (l > c || r < d || l > r || d > c) return 0;
  if (d <= l && r <= c) return it[id][g];
  lp(id, g);
  int mid = (l + r) >> 1;
  return max(query(id, g * 2, l, mid, d, c),
             query(id, g * 2 + 1, mid + 1, r, d, c));
}
void up(int &u, int &id, int c, int &res) {
  if (c > 0)
    update(id, 1, 1, (int)vex[id].size(), 1, idx[u], c);
  else if (c < 0)
    res = min(res, get_min(id, 1, 1, (int)vex[id].size(), 1, idx[u]));
  else
    res = max(res, query(id, 1, 1, (int)vex[id].size(), 1, idx[u]));
  u = fa[chainHead[id]], id = chainId[u];
}
int find_path(int u, int v, int c) {
  int uId = chainId[u], vId = chainId[v], res = (c == 0 ? 0 : INF);
  while (1) {
    if (uId == vId) break;
    int hu = chainHead[uId], hv = chainHead[vId];
    if (h[hu] > h[hv])
      up(u, uId, c, res);
    else if (h[hu] < h[hv])
      up(v, vId, c, res);
    else
      up(u, uId, c, res), up(v, vId, c, res);
  }
  if (idx[u] > idx[v]) swap(u, v);
  if (c > 0)
    update(vId, 1, 1, (int)vex[vId].size(), idx[u] + 1, idx[v], c);
  else if (c < 0) {
    res =
        min(res, get_min(vId, 1, 1, (int)vex[vId].size(), idx[u] + 1, idx[v]));
  } else
    res = max(res, query(vId, 1, 1, (int)vex[vId].size(), idx[u] + 1, idx[v]));
  return res;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i < n; ++i) {
    cin >> v1[i] >> v2[i];
    int u = v1[i], v = v2[i];
    adj[u].push_back(v), adj[v].push_back(u);
  }
  h[1] = 1, find_mtx(1, 0);
  build_hld(1, 0);
  for (int i = 1; i <= nChain; ++i) {
    it[i].resize(4 * (int)vex[i].size() + 7, 0);
    lz[i].resize(4 * (int)vex[i].size() + 7, 0);
    mn[i].resize(4 * (int)vex[i].size() + 7, 0);
  }
  cin >> q;
  for (int i = 1; i <= q; ++i) {
    cin >> uu[i] >> vv[i] >> cc[i];
    int u = uu[i], v = vv[i], c = cc[i];
    find_path(u, v, c);
  }
  for (int i = 1; i < n; ++i) {
    int u = v1[i], v = v2[i];
    if (u == fa[v]) swap(u, v);
    int id = chainId[u];
    ans[i] = query(id, 1, 1, (int)vex[id].size(), idx[u], idx[u]);
    upd_min(chainId[u], 1, 1, (int)vex[id].size(), idx[u], ans[i]);
  }
  for (int i = 1; i <= q; ++i)
    if (find_path(uu[i], vv[i], -1) != cc[i]) return cout << "-1", 0;
  for (int i = 1; i < n; ++i) cout << (ans[i] == 0 ? 1 : ans[i]) << ' ';
  return 0;
}