#include <vector>
#include <iostream>

using namespace std;

vector <pair<int, int>> idk[300001];
bool fv2[300001];

long long dfs ( int node, long long auxi, long long sum ) {
    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 ( it.first, auxi, sum+auxi );
        }
    }

    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 = {};

    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 ( i, -1, 0 );
        }
        ans.push_back(sum);
    }


    return ans;
}