#include "happy.h"
using namespace std;
#include <bits/stdc++.h>
#define ll long long
vector<pair<int,int>> edges[300000];
bool bol[300000];
bool boll[300000];
ll sum=0;
void dfs(int a,int cxor,int orig)
{
    if(bol[a]&&orig<a)
    sum+=cxor;
    boll[a]=true;
    for(auto x:edges[a])
    {
        if(boll[x.first])
        {
            continue;
        }
        dfs(x.first,cxor^x.second,orig);
    }
}
std::vector <long long int> happy (int N, int Q, std::vector <int> u, std::vector <int> v, std::vector <int> h, std::vector <int> t, std::vector <int> l, std::vector <int> r)
{
    int n=N;
    int q=Q;
    for(int i=0;i<n-1;i++)
    {
        edges[u[i]].push_back({v[i],h[i]});
        edges[v[i]].push_back({u[i],h[i]});
    }
    vector<ll>ans(q);
    for(int nesh=0;nesh<q;nesh++)
    {
        sum=0;
        if(t[nesh]==1)
        {
            for(int i=l[nesh];i<=r[nesh];i++)
            {
                bol[i]=true;
            }
        }
        else
        {
            for(int i=l[nesh];i<=r[nesh];i++)
            {
                bol[i]=false;
            }
        }
        for(int i=0;i<n;i++)
        {
            if(!bol[i])
            {
                continue;
            }
            for(int j=0;j<n;j++)
            {
                boll[j]=false;
            }

            dfs(i,0,i);
        }
        ans[nesh]=sum;
    }
    return ans;
}
