#include <iostream>
#include <vector>
using namespace std;
int val[300005], on[300005], f[20];
vector <long long> ras;
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++ ){
        val[i + 1] = ( val[i] ^ h[i] );
        ///cout << val[i] << ' ';
    }
    ///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++;
        }
        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] );
                }
            }
        }
        ras.push_back( sum );
    }
    return ras;
}