#include <bits/stdc++.h>
#include "happy.h"
using namespace std;
int open[2005];
int dist[2005][2005];
vector<int> grf[2005];
int viz[2005];
void dfs(int nod, int sum, int nodorg)
{
    //cout<<nod;
    viz[nod]=1;
    dist[nodorg][nod]=dist[nod][nodorg]=sum;
    for(auto it:grf[nod])
    {
        if(!viz[it])
            dfs(it,sum^dist[nod][it],nodorg);
        //cout<<it<<" ";
    }
}
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)
{
    for(int i=0;i<n-1;i++)
    {
        grf[u[i]].push_back(v[i]);
        grf[v[i]].push_back(u[i]);
        dist[u[i]][v[i]]=dist[v[i]][u[i]]=h[i];
    }
    //cout<<"!";
    for(int i=0;i<n;i++)
    {
        for(int i=0;i<n;i++)
            viz[i]=0;
        dfs(i,0,i);
    }
    vector<long long int> poftim;
    for(int det=0;det<q;det++)
    {
        if(t[det]==1)
        {
            for(int i=l[det];i<=r[det];i++)
                open[i]=1;
        }
        else
        {
            for(int i=l[det];i<=r[det];i++)
                open[i]=0;
        }
        long long sum=0;
        for(int i=0;i<n;i++)
        {
            if(open[i]==1)
            {
                for(int j=0;j<=i;j++)
                {
                    if(open[j]==1)
                        sum+=dist[i][j];
                }
            }
        }
        sum/=2;
        poftim.push_back(sum);
    }
    return poftim;
}