#include <bits/stdc++.h>

using namespace std;

long long dfs ( vector <pair<int, int>> idk[], int node, long long auxi, long long sum, bool fv2[] ) {
    for ( auto it: idk[node] ) {
        if ( fv2[it.first] == 0 ) {
            fv2[it.first] = 1;
            if ( auxi == -1 ) {
                auxi = it.second;
            }
            else {
                auxi = ( auxi ^ it.second );
            }
            return dfs ( idk, it.first, auxi, sum+auxi, fv2 );
        }
    }

    return sum;
}
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 )
{
    vector <long long int> ans = {};
    vector <pair<int, int>> idk[300001];

    bool fv[300001];

    for ( int i = 0; i < 300001; i++ ) {
        fv[i] = 0;
    }
    for ( int i = 0; i < N; i++ ) {
        pair <int, int> abc;
        abc.first = v[i];
        abc.second = h[i];
        idk[u[i]].push_back(abc);
        abc.first = u[i];
        idk[v[i]].push_back(abc);
    }

    long long sum = 0;
    bool fv2[300001];

    for ( int ct = 0; ct < Q; ct++ ) {
        t[ct] %= 2;
        for ( int i = l[ct]; i <= r[ct]; i++ ) {
            fv[i] = t[ct];
        }

        long long sum = 0;

        for ( int i = 0; i < N; i++ ) {
            for ( int j = 0; j < 300001; j++ ) {
                fv2[j] = fv[j];
            }
            sum += dfs ( idk, i, -1, 0, fv2 );
        }
        ans.push_back(sum);
    }

    for ( auto it: ans ) {
       // cout << it << ' ';
    }

    return ans;
}