#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair <ll, ll>
#define ret return
#define pb push_back
#define all(x) x.begin(), x.end()
#define fr first
#define sc second
vector <long long> happy (int N, int Q, vector<int> u, vector<int> v, vector<int> h, vector<int> t, vector<int> l, vector<int> r) {
    vector <long long> open(N, 0), pref(N + 8, 0), suf(N + 8, 0), ans;
    long long d = 0;
    for(int i = 0; i < N; ++i) pref[v[i]] = pref[u[i]] + h[i];
    for(int i = 0; i < Q; ++i) {
        long long c = max((long long)0, d);
        if(t[i] == 1 && open[l[i]] == 0) {
            for(int i = 0; i < N; ++i) if(open[i] == 1) c += pref[max(i, l[i])] - pref[min(i, l[i])];
            ans.pb(c);
            open[l[i]] = 1;
        } else if(t[i] == 2 && open[l[i]] == 1) {
            open[l[i]] = 0;
            for(int i = 0; i < N; ++i) if(open[i] == 1) c -= pref[max(i, l[i])] - pref[min(i, l[i])];
            ans.pb(c);
        } else ans.pb(c);
        d = c;
    }
    return ans;
}
