#include "happy.h"
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1000;

bool used[MAXN + 5], iz[MAXN + 5]; long long int ans;
vector<pair<int,int>>graph[MAXN + 5];

void dfs(int node, int parent, long long int suma)
{
    if(!used[node] && !iz[node])ans += suma;
    for(auto i: graph[node])
    {
        if(i.first == parent)continue;
        int nsuma = suma ^ i.second;
        dfs(i.first, node, nsuma);
    }
}

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 < u.size(); i++)
    {
        int x = u[i], y = v[i], w = h[i];
        graph[x].push_back({y, w});
        graph[y].push_back({x, w});
    }

    vector<long long int>res;
    for(int i = 0; i < n; i++)
    {
        used[i] = true;
    }

    for(int i = 0; i < l.size(); i++)
    {
        memset(iz, false, sizeof(iz));
        if(t[i] == 2)
        {
            for(int j = l[i]; j <= r[i]; j++)
                used[j] = true;
        }
        else
        {
            for(int j = l[i]; j <= r[i]; j++)
                used[j] = false;
        }

        ans = 0;
        for(int j = 0; j < n; j++)
        {
            if(!used[j])
            {
                dfs(j, 0, 0);
                iz[j] = true;
            }
        }
        res.push_back(ans);
    }

    return res;
}
/*
6 5
0 1 1
1 2 2
2 3 3
2 4 4
4 5 2
1 1 2
1 2 3
2 2 2
2 4 5
1 0 5
*/
