#include <bits/stdc++.h>
using namespace std;
vector<long long> happy(int n, int q, vector<int> u, vector<int> v, vector<int> h, vector<int> t, vector<int> l, vector<int> r) {
    vector<vector<pair<int, long long>>> graph(n + 1);
    for (int i = 0; i < n - 1; i++) {
        graph[u[i] + 1].push_back({v[i] + 1, h[i]});
        graph[v[i] + 1].push_back({u[i] + 1, h[i]});
    }
    vector<long long> path(n + 1, -1);
    queue<long long> qu;
    qu.push(1);
    path[1] = 0;
    while (!qu.empty()) {
        int u = qu.front();
        qu.pop();
        for (auto [v, w2] : graph[u]) {
            if (path[v] == -1) {
                path[v] = path[u] ^ w2;
                qu.push(v);
            }
        }
    }
    vector<long long> ans;
    vector<bool> stat(n + 1, false);
    long long sum = 0;
    for (int i = 0; i < q; i++) {
        l[i]++; r[i]++;
        if (t[i] == 1) {
            if (!stat[l[i]]) {
                stat[l[i]] = true;
                for (int k = 1; k < l[i]; k++) {
                    if (stat[k]) {
                        sum += path[k] ^ path[l[i]];
                    }
                }
                for (int k = l[i] + 1; k <= n; k++) {
                    if (stat[k]) {
                        sum += path[k] ^ path[l[i]];
                    }
                }
            }
        } else {
            if (stat[l[i]]) {
                stat[l[i]] = false;
                for (int k = 1; k < l[i]; k++) {
                    if (stat[k]) {
                        sum -= path[k] ^ path[l[i]];
                    }
                }
                for (int k = l[i] + 1; k <= n; k++) {
                    if (stat[k]) {
                        sum -= path[k] ^ path[l[i]];
                    }
                }
            }
        }
        ans.push_back(sum);
    }
    return ans;
}