#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int,int>>>ed;
int open[300001];

vector<long long int> happy (int N, int Q,
                             vector<int> u, vector<int> v, vector<int> h,
                             vector<int> t, vector<int> l, vector<int> r)
{
    ed.resize(N);
    for(int i=0; i<N-1; i++)
    {
        ed[u[i]].push_back({v[i],h[i]});
        ed[v[i]].push_back({u[i],h[i]});
    }
    vector<long long int>ans;
    for(int i=0; i<Q; i++)
    {
        if(t[i]==1)
        {
            open[l[i]]=0;
        }
        else open[l[i]]=1;
        long long int sum=0;
        for(int i=0;i<N;i++)
        {
            if(open[i]==0)continue;
            queue<pair<int,int>>q;
            int used[N+1];
            q.push({i,0});
            while(q.empty())
            {
                int st=q.front().first;
                int r=q.front().second;
                q.pop();
                if(open[st]==1)continue;
                for(auto [b,w]:ed[st])
                {
                    if(used[b]==1 || open[b]==1)continue;
                    sum+=w+r;
                    q.push({b,sum});
                }
            }
        }
        ans.push_back(sum/2);
    }
    return ans;


}
