#include <iostream>
#include <vector>
using namespace std;
struct muchie{
    int y, c;
};
int val[300005], on[300005], 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, tot, x;
    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 );
    ///cout << val[i] << '\n';
    sum = tot = 0;
    for( i = 0; i < q; i++ ){
        x = l[i];
        if( t[i] == 1 && on[x] == 0 ){
            for( j = 0; j < 20; j++ ){
                if( ( val[x] & ( 1 << j ) ) == 0 ){
                    sum += 1ll * ( 1 << j ) * f[j];
                }
                else{
                    sum += 1ll * ( 1 << j ) * ( tot - f[j] );
                    f[j]++;
                }
            }
            on[x] = 1;
            tot++;
            ///cout << "ADD " << x << ' ' << sum << '\n';
        }
        else if( t[i] == 2 && on[x] == 1 ){
            on[x] = 0;
            tot--;
            for( j = 0; j < 20; j++ ){
                if( ( val[x] & ( 1 << j ) ) == 0 ){
                    sum -= 1ll * ( 1 << j ) * f[j];
                }
                else{
                    f[j]--;
                    sum -= 1ll * ( 1 << j ) * ( tot - f[j] );
                }
            }
            ///cout << "DEL " << x << ' ' << sum << '\n';
        }
        ras.push_back( sum );
    }
    return ras;
}