#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <unordered_map>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <assert.h>
#include <cstring>
#include <random>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'

using namespace std;
using namespace __gnu_pbds;

template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <class T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

#include "happy.h"
vector<pair<int,long long>> v[400000];
bool take[4000000];
bool used[2005];
long long dist[2005];

void dfs(int i, int par) {
    for(auto x : v[i]) {
        if(x.first==par) continue;
        dist[x.first]=dist[i]^x.second;
        dfs(x.first, i);
    }
}
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) {
    for(int i = 0; i < u.size(); i++) {
        ::v[u[i]].push_back({v[i], h[i]});
        ::v[v[i]].push_back({u[i], h[i]});
    }
    vector<long long> ansv;
    for(int i = 0; i < Q; i++) {
        long long ans=0;
        for(int j = l[i]; j <= r[i]; j++) {if(t[i]==1) take[j]=1; else take[j]=0;}
        for(int j = 0; j < N; j++) {
            if(!take[j]) continue;
            memset(used, 0, sizeof(used));
            memset(dist, 0, sizeof(dist));
            dfs(j, -1);
            for(int k = 0; k < N; k++) if(take[k]) ans+=dist[k];
        }

        ansv.push_back(ans/2);
    }
    return ansv;
}
/*
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

6 1
0 1 1
1 2 2
2 3 3
2 4 4
4 5 2
1 1 3
*/
