#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <ll, ll> pii;
typedef pair <ll, ll> pll;
void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

const ll maxn = 3e3 + 7;
ll a[maxn];
ll x[maxn];
vector <ll> v[maxn];
ll hh[maxn];
ll used[maxn];
ll par[maxn];
void dfs(ll beg)
{
    used[beg] = 1;
    for(auto w:v[beg])
    {
        if(!used[w])
        {
            par[w] = beg;
            dfs(w);
        }
    }
}
ll sum = 0;
ll ans = 0;
void dfs1(ll beg)
{
    used[beg] = 1;
    for(auto w:v[beg])
    {
        if(w == par[beg])continue;
        if(!used[w])
        {
            x[w] = (x[beg] ^ hh[w]);
            if(a[w] == 1)sum += x[w];
            dfs1(w);
        }
    }
}
std::vector<ll> happy (ll N, ll Q,
std::vector<ll> u, std::vector<ll> V, std::vector<ll> h,
std::vector<ll> t, std::vector<ll> l, std::vector<ll> r)
{
    vector <ll> maxa;
    memset(used, 0, sizeof(used));
    memset(par, 0, sizeof(par));
    for(ll i = 0;i <= N;i++)
    {
        v[i].clear();
    }
    for(ll i = 0;i < V.size();i++)
    {
        hh[i] = h[i];
        v[u[i]].push_back(V[i]);
        v[V[i]].push_back(u[i]);
    }
    dfs(0);
    memset(used, 0, sizeof(used));
    for(ll i = 0;i < N;i++)
    {
        a[i] = 1;
    }
    for(ll i = 0; i < l.size();i++)
    {
        for(ll j = l[i]; j <= r[i];j++)
        {
            a[i] = t[i];
        }
        for(ll i = 0;i < N;i++)
        {
            if(a[i] == 2)continue;
            x[i] = h[i];
            sum = x[i];
            memset(used, 0, sizeof(used));
            dfs(i);
            maxa.push_back(sum);
        }
    }
    return maxa;
}

