#include <iostream>
#include <vector>
using namespace std;
struct muchie{
    int y, c;
};
int val[300005], biti[300005][20], tot[20], f[20];
vector <muchie> muchii[300005];
vector <long long> ras;
void dfs( int x, int t ){
    int i, y, c;
    for( i = 0; i < muchii[x].size(); i++ ){
        y = muchii[x][i].y;
        c = muchii[x][i].c;
        if( y == t ){
            continue;
        }
        val[y] = ( val[x] ^ c );
        dfs( y, x );
    }
}
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 ){
    int i, j, elem, x, y;
    long long sum;
    for( i = 0; i < n - 1; i++ ){
        muchii[u[i]].push_back( { v[i], h[i] } );
        muchii[v[i]].push_back( { u[i], h[i] } );
    }
    dfs( 0, -1 );
    for( i = 0; i < n; i++ ){
        for( j = 0; j < 20; j++ ){
            if( ( val[i] & ( 1 << j ) ) != 0 ){
                biti[i][j] = 1;
            }
            if( i > 0 ){
                biti[i][j] += biti[i - 1][j];
            }
        }
    }
    sum = 0;
    for( i = 0; i < q; i++ ){
        x = l[i];
        y = r[i];
        if( t[i] == 1 ){
            for( j = 0; j < 20; j++ ){
                elem = biti[y][j];
                if( x > 0 ){
                    elem -= biti[x - 1][j];
                }
                sum += 1ll * ( 1 << j ) * f[j] * ( y - x + 1 - elem );
                tot[j] += y - x + 1 - elem;
                ///cout << x << ' ' << y << ' ' << tot[j] - f[j] << ' ' << elem << '\n';
                sum += 1ll * ( 1 << j ) * ( tot[j] - f[j] ) * elem;
                tot[j] += elem;
                f[j] += elem;
            }
        }
        else{
            for( j = 0; j < 20; j++ ){
                elem = biti[y][j];
                if( x > 0 ){
                    elem -= biti[x - 1][j];
                }
                sum -= 1ll * ( 1 << j ) * f[j] * ( y - x + 1 - elem );
                tot[j] -= y - x + 1 - elem;
                sum -= 1ll * ( 1 << j ) * ( tot[j] - f[j] ) * elem;
                tot[j] -= elem;
                f[j] -= elem;
            }
        }
        ras.push_back( sum );
    }
    return ras;
}