#include<bits/stdc++.h>
using namespace std;
# define C continue;
# define R return
# define D double
# define I insert
# define ll long long
# define ld long double
# define ull unsigned long long
# define ui unsigned int
# define pb push_back
# define pf push_front
# define vi vector < int >
# define vc vector < char >
# define vs vector < string >
# define vb vector < bool >
# define vd vector < D >
# define vll vector < ll >
# define vull vector < ull >
# define vld vector < ld >
# define PQ priority_queue
# define vvi vector < vector < int > >
# define vvb vector < vector < bool > >
# define vvc vector < vector < char > >
# define vvll vector < vector < ll > >
# define vvd vector < vector < D > >
# define vvld vector < vector < ld > >
# define all(v) (v).begin() , (v).end()
# define allrev(v) (v).rbegin() , (v).rend()
# define allcomp(v) v.begin() , v.end() , comp
# define allrevcomp(v) v.rbegin() , v.rend() , comp
# define pii pair < int , int >
# define pll pair < ll , ll >
# define pld pair < ld , ld >
# define pDD pair < D , D >
# define vpld vector < pld >
# define vpii vector < pii >
# define vpll vector < pll >
# define vpDD vector < pDD >
# define vvpii vector < vector < pii > >
# define F first
# define S second
# define mp make_pair
# define dist(a,b,p,q) sqrt((p-a)*(p-a) + (q-b)*(q-b))
# define pp(n) printf("%.10Lf",n);
# define line cout<<"\n";
# define fast ios_base::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0);
string vow = "aeiou";
int month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};
const int dx[] = { -1 , 0 , 0 , 1 } ;
const int dy[] = { 0 , -1 , 1 , 0 } ;
const ld pie = 3.1415926535897932384626 ;
const ll mod = 1e9 + 7 ;
/// Tip : If a and b are positive integers ; we may say - ceil (a/b) = 1 + floor ( (a-1)/b ) .
const int N = 5e3 + 3 , inf = 1e6 + 2 ;
vpii g[N] ;
int ans[N] ;
bool vis[N] ;
int n ;
void bad()
{
cout << -1 ;
line ; exit ( 0 ) ;
}
void read()
{
cin >> n ;
int m = n - 1 ;
for ( int i=0 ; i < m ; i ++ )
{
int a , b ;
cin >> a >> b ;
a -- ; b -- ;
g[a].pb ( { i , b } ) ;
g[b].pb ( { i , a } ) ;
}
for ( int i=0 ; i < n ; i ++ )
ans[i] = inf ;
}
map < pii , int > h ;
void take_queries()
{
int q ; cin >> q ;
while ( q -- )
{
int a , b , w ;
cin >> a >> b >> w ;
a -- ; b -- ;
if ( a > b ) swap ( a , b ) ;
pii temp ;
temp.F = a ; temp.S = b ;
if ( h.count ( temp ) )
{
if ( h[temp] != w )
bad() ;
}
h[temp] = w ;
}
}
vector < pair < pii , int > > v ;
bool comp ( const pair < pii , int > &a , const pair < pii , int > &b )
{
if ( a.S < b.S )
return true ;
return false ;
}
void sort_queries ()
{
int z = h.size() ;
v.resize ( z ) ;
int indx = 0 ;
for ( auto &temp : h )
v[indx ++] = temp ;
sort ( allcomp ( v ) ) ;
}
int src , des , w ;
bool ok = false ;
void FILL()
{
for ( int i=0 ; i < n ; i ++ )
vis[i] = false ;
}
void dfs1 ( int node )
{
vis[node] = 1 ;
if ( node == des )
{
ok = true ;
return ;
}
for ( auto &temp : g[node] )
{
int i = temp.S ;
int indx = temp.F ;
if ( vis[i] ) C ;
dfs1 ( i ) ;
if ( ok == false ) C ;
ans[indx] = w ;
return ;
}
}
void read_queries()
{
for ( auto &temp : v )
{
src = temp.F.F ;
des = temp.F.S ;
w = temp.S ;
ok = false ; FILL() ;
dfs1 ( src ) ;
}
}
void dfs ( int node , int min_path = inf )
{
vis[node] = 1 ;
if ( node == des )
{
if ( min_path == w )
ok = true ;
else bad() ;
}
for ( auto &temp : g[node] )
{
int i = temp.S ;
int indx = temp.F ;
if ( vis[i] ) C ;
dfs ( i , min ( min_path , ans[indx] ) ) ;
if ( ok ) return ;
}
}
void check_if_tree_ok()
{
for ( auto &temp : v )
{
src = temp.F.F ;
des = temp.F.S ;
w = temp.S ;
FILL() ; ok = false ;
dfs ( src ) ;
if ( ok == false )
bad() ;
}
}
void print_ans ()
{
int maxx = 1e6 ;
for ( int i=0 ; i < n - 1 ; i ++ )
{
cout << min ( ans[i] , maxx ) << " " ;
}
line ;
}
void solve ( int test_case )
{
read() ;
take_queries() ;
sort_queries() ;
read_queries() ;
check_if_tree_ok() ;
print_ans() ;
}
int main()
{fast
int t = 1;
// cin >> t;
for ( int i=0 ; i < t ; i++ ) solve(i);
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiMgZGVmaW5lIEMgY29udGludWU7CiMgZGVmaW5lIFIgcmV0dXJuCgojIGRlZmluZSBEIGRvdWJsZQojIGRlZmluZSBJIGluc2VydAojIGRlZmluZSBsbCBsb25nIGxvbmcKIyBkZWZpbmUgbGQgbG9uZyBkb3VibGUKCiMgZGVmaW5lIHVsbCB1bnNpZ25lZCBsb25nIGxvbmcKIyBkZWZpbmUgdWkgdW5zaWduZWQgaW50CgojIGRlZmluZSBwYiBwdXNoX2JhY2sKIyBkZWZpbmUgcGYgcHVzaF9mcm9udAoKIyBkZWZpbmUgdmkgdmVjdG9yIDwgaW50ID4KIyBkZWZpbmUgdmMgdmVjdG9yIDwgY2hhciA+CiMgZGVmaW5lIHZzIHZlY3RvciA8IHN0cmluZyA+CiMgZGVmaW5lIHZiIHZlY3RvciA8IGJvb2wgPgojIGRlZmluZSB2ZCB2ZWN0b3IgPCBEID4KIyBkZWZpbmUgdmxsIHZlY3RvciA8IGxsID4KIyBkZWZpbmUgdnVsbCB2ZWN0b3IgPCB1bGwgPgojIGRlZmluZSB2bGQgdmVjdG9yIDwgbGQgPgojIGRlZmluZSBQUSBwcmlvcml0eV9xdWV1ZQoKIyBkZWZpbmUgdnZpIHZlY3RvciA8IHZlY3RvciA8IGludCA+ID4KIyBkZWZpbmUgdnZiIHZlY3RvciA8IHZlY3RvciA8IGJvb2wgPiA+CiMgZGVmaW5lIHZ2YyB2ZWN0b3IgPCB2ZWN0b3IgPCBjaGFyID4gPgojIGRlZmluZSB2dmxsIHZlY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