#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<int> V;
#define F(N,S,E) for(int N=(int)(S);N<=(int)(E);N++)
#define R(N,S,E) for(int N=(int)(S);N>=(int)(E);N--)
#define D(N,E) for(int N=0;N<(int)(E);N++)
LL read(){LL x;scanf("%lld",&x);return x;}

const int N=2.1e5;
int a[N],b[N];

struct BIT{
	int a[N];
	void add(int x,int v){
		for(x++;x<N;x+=x&-x)a[x]+=v;
	}
	int query(int x)const{
		int ans=0;
		for(x++;x;x&=x-1)ans+=a[x];
		return ans;
	}
}tr;

map<int,V> qwq;
int p[N];
int main(){
	int n=read();
	D(i,n)a[i]=read()+i;	
	D(i,n)b[i]=read()+i;
	map<int,int> cnta,cntb;
	D(i,n)cnta[a[i]]++;
	D(i,n)cntb[b[i]]++;
	if(cnta!=cntb){
		puts("-1");
		exit(0);
	}
	D(i,n){
		qwq[b[i]].push_back(i);
	}
	R(i,n-1,0){
		V &v=qwq[a[i]];
		p[i]=v.back();
		v.pop_back();
	}
	LL ans=0;
	R(i,n-1,0){
		ans+=tr.query(p[i]);
		tr.add(p[i],1);
	}
	printf("%lld\n",ans);
	return 0;
}
#include<bits/stdc++.h>
#define ll  long long int
#define ull unsigned long long 
#define ff first
#define ss second
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pb push_back 
#define mp make_pair
#define inf 1000000000000000
#define mod 1000000007
#define ld long double
#define pll pair<ll,ll>
using namespace std;
ll n,timer;
vector<ll> adj[2000009];
vector<ll> euler,tin,tout;

void dfs(ll node,ll par)
{
    tin[node]=++timer;
    euler.pb(node);

    for(ll j:adj[node])
        if(j!=par)
            dfs(j,node);

    tout[node]=++timer;
    euler.pb(node);
}

void build(ll root)
{
    tin.resize(n),tout.resize(n);
    timer=-1;
    dfs(root,-1);
}

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

vector<ll> ans(2*n+1,0);
vector<ll> a(n-1),b(n-1);

for(ll i=0;i<n-1;i++)
{
	cin>>a[i]>>b[i];
	a[i]--;
	b[i]--;

	adj[a[i]].pb(b[i]);
	adj[b[i]].pb(a[i]);
}

build(0);

ll q;
cin>>q;

while(q--)
{
	ll t,x,y,e,inc;
	cin>>t>>e>>inc;
	x=a[e-1],y=b[e-1];

	if(t==2)
		swap(x,y);

	if(tin[y]>tin[x])
	{
		ans[0]+=inc;
		ans[tin[y]]-=inc;
		ans[tout[y]+1]+=inc;
	}

	else
	{
		ans[tin[x]]+=inc;
		ans[tout[x]+1]-=inc;
	}
}


ll w=0;
vector<ll> res(n+10,0);
for(ll i=0;i<2*n;i++)
{
	w+=ans[i];
	res[euler[i]]+=w;
}

for(ll i=0;i<n;i++)
cout<<res[i]/2<<" "<<endl;

}
