#include <iostream>
#include <vector>
using namespace std;
struct muchie{
    int y, c;
};
int on[300005], tot[300005], f[300005][25];
vector <muchie> muchii[300005];
vector <long long> ras;
long long sum;
void dfs( int x, int t ){
    int i, j, k, y, a, b, c;
    for( i = 0; i < muchii[x].size(); i++ ){
        y = muchii[x][i].y;
        c = muchii[x][i].c;
        if( y == t ){
            continue;
        }
        dfs( y, x );
        for( j = 0; j < 25; j++ ){
            if( ( c & ( 1 << j ) ) != 0 ){
                f[y][j] = tot[y] - f[y][j];
            }
        }
    }
    for( j = 0; j < 25; j++ ){
        f[x][j] = 0;
    }
    tot[x] = 0;
    for( i = 0; i < muchii[x].size(); i++ ){
        a = muchii[x][i].y;
        if( a == t ){
            continue;
        }
        for( j = i + 1; j < muchii[x].size(); j++ ){
            b = muchii[x][j].y;
            if( b == t ){
                continue;
            }
            for( k = 0; k < 25; k++ ){
                sum += 1ll * ( 1 << k ) * ( 1ll * f[a][k] * ( tot[b] - f[b][k] ) + 1ll * ( tot[a] - f[a][k] ) * f[b][k] );
            }
        }
        for( j = 0; j < 25; j++ ){
            if( on[x] == 1 ){
                sum += 1ll * ( 1 << j ) * f[a][j];
            }
            f[x][j] += f[a][j];
        }
        tot[x] += tot[a];
    }
    if( on[x] == 1 ){
        tot[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;
    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] } );
    }
    for( i = 0; i < q; i++ ){
        for( j = l[i]; j <= r[i]; j++ ){
            if( t[i] == 1 ){
                on[j] = 1;
            }
            else{
                on[j] = 0;
            }
        }
        sum = 0;
        dfs( 0, -1 );
        ras.push_back( sum );
    }
    return ras;
}