// tupo - lesni tochki
#include "happy.h"
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e5;
template<class T> using matrix = vector<vector<T>>;
matrix<pair<int, int>>G;
int pref[MAXN + 4];
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) {
    G = matrix<pair<int, int>>G(n);
    for(int i = 0; i < u.size(); i++){
        G[u[i]].push_back({v[i], h[i]});
        G[v[i]].push_back({u[i], h[i]});
    }
    for(int i = 0; i < n; i++){
        queue<int>q;
        vector<bool>used(n);
        vector<int>dist(n + 1);
        q.push(i);
        used[i] = true;
        while(!q.empty()){
            int u = q.front(); q.pop();
            for(auto [v, w] : G[u]){
                if(used[v])continue;
                used[v] = true;
                dist[v] = dist[u] ^ w;
            }
        }
        for(int j = 0; j < n; j++){
            if(j == i)continue;
            pref[i] += dist[j];
        }
    }
    int sum = 0;
    int ans = 0;
    for(int i = 1; i < n; i++){
        sum += pref[i];
        pref[i] += pref[i - 1];
    }
    vector<int>f;
    for(int i = 0; i < q; i++){
        if(t[i] == 1){
            ans += pref[l[i]] - pref[r[i] - 1];
        }
        if(t[i] == 2){
            ans -= pref[l[i]] - pref[r[i] - 1];
        }
        f.push_back(ans);
    }
    return f;
}
