#include <bits/stdc++.h>
#include "happy.h"
#define endl '\n'

using namespace std;

const int MAXN = 3e5;

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

void dfs(int node, int parent, int suma)
{
    if(used[node])return;
    if(parent != 0)ans += suma;

    for(auto i: graph[node])
    {
        if(i.first == parent)continue;

        int nsuma = suma;
        if(suma == 0)nsuma = i.second;
        else 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});
    }

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

        ans = 0;
        for(int j = 0; j < n; j++)
        {
            dfs(j, 0, 0);
        }

        cout << ans / 2 << endl;
    }

}
/*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
