#include <iostream>
#include "happy.h"
#define mp make_pair

using namespace std;

const int NMAX = 3000;
using ll = long long;

ll timer;
ll tin[NMAX + 5],tout[NMAX + 5];
vector<pair<int,ll>> adj[NMAX + 5];
ll height[NMAX + 5];
ll x[NMAX + 5];
bool open[NMAX + 5];

void dfs(int nod,int parent,ll d)
{
    if(nod!=1)
    {
        x[nod] = x[parent] ^ d;
    }
    for(auto it : adj[nod])
    {
        if(it.first==parent)
            continue;

        dfs(it.first,nod,it.second);
    }
}

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)
{
    for(int i=0;i<n-1;i++)
    {
        u[i]++;
        v[i]++;
        adj[u[i]].push_back(mp(v[i],h[i]));
        adj[v[i]].push_back(mp(u[i],h[i]));
    }
    //cout<<1;
    dfs(1,0,0);
    for(int i=1;i<=n;i++)
    {
        //cout<<x[i]<<' ';
    }
    vector<long long> ans;
    for(int i=0;i<q;i++)
    {
        l[i]++;
        r[i]++;
        for(int j=l[i];j<=r[i];j++)
        {
            if(t[i]==1)
                open[j] = true;
            else
                open[j] = false;
        }
        int sol = 0;
        for(int j=1;j<=n;j++)
        {
            for(int k=j+1;k<=n;k++)
            {
                if(open[j]==true and open[k]==true)
                    sol+=x[j]^x[k];
            }
        }
        ans.push_back(sol);
    }
    return ans;
}

