#include <bits/stdc++.h>
using namespace std;
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define loop(i, a) for (int i = 0; i < (a); ++i)
#define cont(i, a) for (int i = 1; i <= (a); ++i)
#define circ(i, a, b) for (int i = (a); i <= (b); ++i)
#define range(i, a, b, c) for (int i = (a); (c) > 0 ? i <= (b) : i >= (b); i += (c))
#define parse(it, x) for (auto &it : (x))
#define pub push_back
#define pob pop_back
#define emb emplace_back
#define mak make_pair
#define mkt make_tuple
typedef long long ll;
typedef long double lf;
const int Inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;

int n;
int a[200005], b[200005], p[200005];
int dy[200005];
map<int, int> lss;
vector<int> pos[200005];

struct Fenwick {
	ll dt[1 << 18];
	void inline add(int a, ll x) { while (a <= n) dt[a] += x, a += a & -a; }
	ll inline sum(int a) { ll res = 0; while (a) res += dt[a], a -= a & -a; return res; }
} bit;

int main() {
    scanf("%d", &n);
    cont(i, n) scanf("%d", a + i), a[i] += i, lss[a[i]];
    cont(i, n) scanf("%d", b + i), b[i] += i;
    int lsc = 0;
    parse(it, lss) it.second = ++lsc;
    cont(i, n) {
        a[i] = lss[a[i]];
        if (!lss.count(b[i])) return puts("-1"), 0;
        b[i] = lss[b[i]];
        pos[b[i]].pub(i);
    }
    cont(i, lsc) reverse(all(pos[i]));
    cont(i, n) {
        if (!SZ(pos[a[i]])) return puts("-1"), 0;
        p[i] = pos[a[i]].back();
        pos[a[i]].pob();
    }
    ll ans = 0;
    range(i, n, 1, -1) {
        ans += bit.sum(p[i]);
        bit.add(p[i], 1);
    }
    printf("%lld\n", ans);
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define int long long
using vec_int = vector<int>;
using P = pair<int,int>;
// using T = tuple<int,int,int>;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)


int charToInt(char c){
    char zero_num = '0';
    return (int)c - (int)zero_num;
}

bool isPrime(int N){
    for(int i=2;i*i<=N;i++){
        if(N%i==0){
            return false;
        }
    }
    return true;
}


signed main(){
    int N; cin>>N; 
    vec_int a(N-1), b(N-1); rep(i,N-1)cin>>a.at(i)>>b.at(i);
    int Q; cin>>Q;
    vec_int t(Q), e(Q), x(Q);
    rep(i,Q)cin>>t.at(i)>>e.at(i)>>x.at(i);

    vector<vec_int> G(N+1);
    rep(i,N-1){
        G.at(a.at(i)).push_back(b.at(i));
        G.at(b.at(i)).push_back(a.at(i));
    }

    vector<int> node_depth(N+1,-1);

    queue<P>  que;
    que.emplace(1,0); 

    while(!que.empty()){
        int node, depth; tie(node, depth) =  que.front(); que.pop();
        node_depth.at(node) = depth;
        for(auto next_node: G.at(node)){
            if(node_depth.at(next_node)>=0)continue;
            que.emplace(next_node, depth+1);
        }
    }



    vec_int score(N+1, 0);
    rep(q, Q){
        int from_node = a.at(e.at(q)-1);
        int to_node = b.at(e.at(q)-1);
        if(t.at(q)==2){
            int temp = from_node;
            from_node = to_node;
            to_node = temp;
        }
        if(node_depth.at(to_node)>node_depth.at(from_node)){
            score.at(1) += x.at(q);
            score.at(to_node) -= x.at(q);
        }else{
            score.at(from_node) += x.at(q);
        }
    }

    vec_int ans(N+1, -1);
    queue<P>  que2;
    que2.emplace(1, 0);
    while(!que2.empty()){
        int node, parent_score; tie(node, parent_score)= que2.front(); que2.pop();
        // cout<<node<<" parent_score:"<<parent_score<<endl;
        int temp = parent_score+score.at(node);
        ans.at(node)  = temp;

        for(auto next_node: G.at(node)){
            if(ans.at(next_node)>=0)continue;
            que2.emplace(next_node, temp);
        }
    }

    rep(i,N){
        cout<<ans.at(i+1)<<endl;
    }

     









    return 0;
}